
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQ3Canvas class provides a 2D area that can containQ3CanvasItem objects.More...
| Header: | #include <Q3Canvas> |
| Inherits: | QObject |
| Q3Canvas(QObject * parent = 0, const char * name = 0) | |
| Q3Canvas(int w, int h) | |
| Q3Canvas(QPixmap p, int h, int v, int tilewidth, int tileheight) | |
| virtual | ~Q3Canvas() |
| Q3CanvasItemList | allItems() |
| QColor | backgroundColor() const |
| QPixmap | backgroundPixmap() const |
| int | chunkSize() const |
| Q3CanvasItemList | collisions(const QPoint & p) const |
| Q3CanvasItemList | collisions(const QRect & r) const |
| Q3CanvasItemList | collisions(const Q3PointArray & chunklist, const Q3CanvasItem * item, bool exact) const |
| void | drawArea(const QRect & clip, QPainter * painter, bool dbuf = false) |
| int | height() const |
| bool | onCanvas(int x, int y) const |
| bool | onCanvas(const QPoint & p) const |
| QRect | rect() const |
| virtual void | resize(int w, int h) |
| virtual void | retune(int chunksze, int mxclusters = 100) |
| virtual void | setAdvancePeriod(int ms) |
| virtual void | setAllChanged() |
| virtual void | setBackgroundColor(const QColor & c) |
| virtual void | setBackgroundPixmap(const QPixmap & p) |
| virtual void | setChanged(const QRect & area) |
| virtual void | setDoubleBuffering(bool y) |
| virtual void | setTile(int x, int y, int tilenum) |
| virtual void | setTiles(QPixmap p, int h, int v, int tilewidth, int tileheight) |
| virtual void | setUnchanged(const QRect & area) |
| virtual void | setUpdatePeriod(int ms) |
| QSize | size() const |
| int | tile(int x, int y) const |
| int | tileHeight() const |
| int | tileWidth() const |
| int | tilesHorizontally() const |
| int | tilesVertically() const |
| bool | validChunk(int x, int y) const |
| bool | validChunk(const QPoint & p) const |
| int | width() const |
| void | resized() |
| virtual void | drawBackground(QPainter & painter, const QRect & clip) |
| virtual void | drawForeground(QPainter & painter, const QRect & clip) |
TheQ3Canvas class provides a 2D area that can containQ3CanvasItem objects.
TheQ3Canvas class manages its 2D graphic area and all the canvas items the area contains. The canvas has no visual appearance of its own. Instead, it is displayed on screen using aQ3CanvasView. MultipleQ3CanvasView widgets may be associated with a canvas to provide multiple views of the same canvas.
The canvas is optimized for large numbers of items, particularly where only a small percentage of the items change at any one time. If the entire display changes very frequently, you should consider using your own customQ3ScrollView subclass.
Qt provides a rich set of canvas item classes, e.g.Q3CanvasEllipse,Q3CanvasLine,Q3CanvasPolygon,Q3CanvasPolygonalItem,Q3CanvasRectangle,Q3CanvasSpline,Q3CanvasSprite andQ3CanvasText. You can subclass to create your own canvas items;Q3CanvasPolygonalItem is the most common base class used for this purpose.
Items appear on the canvas after theirshow() function has been called (orsetVisible(true)), andafterupdate() has been called. The canvas only shows items that arevisible, and then only ifupdate() is called. (By default the canvas is white and so are canvas items, so if nothing appears try changing colors.)
If you created the canvas without passing a width and height to the constructor you must also callresize().
Although a canvas may appear to be similar to a widget with child widgets, there are several notable differences:
A canvas consists of a background, a number of canvas items organized by x, y and z coordinates, and a foreground. A canvas item's z coordinate can be treated as a layer number -- canvas items with a higher z coordinate appear in front of canvas items with a lower z coordinate.
The background is white by default, but can be set to a different color usingsetBackgroundColor(), or to a repeated pixmap usingsetBackgroundPixmap() or to a mosaic of smaller pixmaps usingsetTiles(). Individual tiles can be set withsetTile(). There are corresponding get functions, e.g.backgroundColor() andbackgroundPixmap().
Note thatQ3Canvas does not inherit fromQWidget, even though it has some functions which provide the same functionality as those inQWidget. One of these issetBackgroundPixmap(); some others areresize(),size(),width() andheight().Q3CanvasView is the widget used to display a canvas on the screen.
Canvas items are added to a canvas by constructing them and passing the canvas to the canvas item's constructor. An item can be moved to a different canvas usingQ3CanvasItem::setCanvas().
Canvas items are movable (and in the case of Q3CanvasSprites, animated) objects that inheritQ3CanvasItem. Each canvas item has a position on the canvas (x, y coordinates) and a height (z coordinate), all of which are held as floating-point numbers. Moving canvas items also have x and y velocities. It's possible for a canvas item to be outside the canvas (for exampleQ3CanvasItem::x() is greater thanwidth()). When a canvas item is off the canvas,onCanvas() returns false and the canvas disregards the item. (Canvas items off the canvas do not slow down any of the common operations on the canvas.)
Canvas items can be moved withQ3CanvasItem::move(). Theadvance() function moves allQ3CanvasItem::animated() canvas items andsetAdvancePeriod() makesQ3Canvas move them automatically on a periodic basis. In the context of theQ3Canvas classes, to `animate' a canvas item is to set it in motion, i.e. usingQ3CanvasItem::setVelocity(). Animation of a canvas item itself, i.e. items which change over time, is enabled by callingQ3CanvasSprite::setFrameAnimation(), or more generally by subclassing and reimplementingQ3CanvasItem::advance(). To detect collisions use one of theQ3CanvasItem::collisions() functions.
The changed parts of the canvas are redrawn (if they are visible in a canvas view) wheneverupdate() is called. You can either callupdate() manually after having changed the contents of the canvas, or force periodic updates usingsetUpdatePeriod(). If you have moving objects on the canvas, you must calladvance() every time the objects should move one step further. Periodic calls toadvance() can be forced usingsetAdvancePeriod(). Theadvance() function will callQ3CanvasItem::advance() on every item that isanimated and trigger an update of the affected areas afterwards. (A canvas item that is `animated' is simply a canvas item that is in motion.)
Q3Canvas organizes its canvas items intochunks; these are areas on the canvas that are used to speed up most operations. Many operations start by eliminating most chunks (i.e. those which haven't changed) and then process only the canvas items that are in the few interesting (i.e. changed) chunks. A valid chunk,validChunk(), is one which is on the canvas.
The chunk size is a key factor toQ3Canvas's speed: if there are too many chunks, the speed benefit of grouping canvas items into chunks is reduced. If the chunks are too large, it takes too long to process each one. TheQ3Canvas constructor tries to pick a suitable size, but you can callretune() to change it at any time. ThechunkSize() function returns the current chunk size. The canvas items always make sure they're in the right chunks; all you need to make sure of is that the canvas uses the right chunk size. A good rule of thumb is that the size should be a bit smaller than the average canvas item size. If you have moving objects, the chunk size should be a bit smaller than the average size of the moving items.
The foreground is normally nothing, but if you reimplementdrawForeground(), you can draw things in front of all the canvas items.
Areas can be set as changed withsetChanged() and set unchanged withsetUnchanged(). The entire canvas can be set as changed withsetAllChanged(). A list of all the items on the canvas is returned byallItems().
An area can be copied (painted) to aQPainter withdrawArea().
If the canvas is resized it emits theresized() signal.
The examples/canvas application and the 2D graphics page of the examples/demo application demonstrate many ofQ3Canvas's facilities.
See alsoQ3CanvasView,Q3CanvasItem,QtCanvas, andPorting to Graphics View.
Create aQ3Canvas with no size.parent andname are passed to theQObject superclass.
Warning: Youmust callresize() at some time after creation to be able to use the canvas.
Constructs aQ3Canvas that isw pixels wide andh pixels high.
Constructs aQ3Canvas which will be composed ofh tiles horizontally andv tiles vertically. Each tile will be an imagetilewidth bytileheight pixels taken from pixmapp.
The pixmapp is a list of tiles, arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.
| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
TheQ3Canvas is initially sized to show exactly the given number of tiles horizontally and vertically. If it is resized to be larger, the entire matrix of tiles will be repeated as often as necessary to cover the area. If it is smaller, tiles to the right and bottom will not be visible.
See alsosetTiles().
[virtual]Q3Canvas::~Q3Canvas()Destroys the canvas and all the canvas's canvas items.
[virtual slot]void Q3Canvas::advance()Moves allQ3CanvasItem::animated() canvas items on the canvas and refreshes all changes to all views of the canvas. (An `animated' item is an item that is in motion; see setVelocity().)
The advance takes place in two phases. In phase 0, theQ3CanvasItem::advance() function of eachQ3CanvasItem::animated() canvas item is called with parameter 0. Then all these canvas items are called again, with parameter 1. In phase 0, the canvas items should not change position, merely examine other items on the canvas for which special processing is required, such as collisions between items. In phase 1, all canvas items should change positions, ignoring any other items on the canvas. This two-phase approach allows for considerations of "fairness", although noQ3CanvasItem subclasses supplied with Qt do anything interesting in phase 0.
The canvas can be configured to call this function periodically withsetAdvancePeriod().
See alsoupdate().
Returns a list of all the items in the canvas.
Returns the color set bysetBackgroundColor(). By default, this is white.
This function is not a reimplementation of QWidget::backgroundColor() (Q3Canvas is not a subclass ofQWidget), but all Q3CanvasViews that are viewing the canvas will set their backgrounds to this color.
See alsosetBackgroundColor() andbackgroundPixmap().
Returns the pixmap set bysetBackgroundPixmap(). By default, this is a null pixmap.
See alsosetBackgroundPixmap() andbackgroundColor().
Returns the chunk size of the canvas.
See alsoretune().
Returns a list of canvas items that collide with the pointp. The list is ordered by z coordinates, from highest z coordinate (front-most item) to lowest z coordinate (rear-most item).
This is an overloaded function.
Returns a list of items which collide with the rectangler. The list is ordered by z coordinates, from highest z coordinate (front-most item) to lowest z coordinate (rear-most item).
This is an overloaded function.
Returns a list of canvas items which intersect with the chunks listed inchunklist, excludingitem. Ifexact is true, only those which actuallycollide withitem are returned; otherwise canvas items are included just for being in the chunks.
This is a utility function mainly used to implement the simplerQ3CanvasItem::collisions() function.
Paints all canvas items that are in the areaclip topainter, using double-buffering ifdbuf is true.
e.g. to print the canvas to a printer:
[virtual protected]void Q3Canvas::drawBackground(QPainter & painter, constQRect & clip)This virtual function is called for all updates of the canvas. It renders any background graphics using the painterpainter, in the areaclip. If the canvas has a background pixmap or a tiled background, that graphic is used, otherwise the canvas is cleared using the background color.
If the graphics for an area change, you must explicitly callsetChanged(constQRect&) for the result to be visible whenupdate() is next called.
See alsosetBackgroundColor(),setBackgroundPixmap(), andsetTiles().
[virtual protected]void Q3Canvas::drawForeground(QPainter & painter, constQRect & clip)This virtual function is called for all updates of the canvas. It renders any foreground graphics using the painterpainter, in the areaclip.
If the graphics for an area change, you must explicitly callsetChanged(constQRect&) for the result to be visible whenupdate() is next called.
The default is to draw nothing.
Returns the height of the canvas, in pixels.
Returns true if the pixel position (x,y) is on the canvas; otherwise returns false.
See alsovalidChunk().
This is an overloaded function.
Returns true if the pixel positionp is on the canvas; otherwise returns false.
See alsovalidChunk().
Returns a rectangle the size of the canvas.
[virtual]void Q3Canvas::resize(int w,int h)Changes the size of the canvas to have a width ofw and a height ofh. This is a slow operation.
[signal]void Q3Canvas::resized()This signal is emitted whenever the canvas is resized. EachQ3CanvasView connects to this signal to keep the scrollview's size correct.
[virtual]void Q3Canvas::retune(int chunksze,int mxclusters = 100)Change the efficiency tuning parameters tomxclusters clusters, each of sizechunksze. This is a slow operation if there are many objects on the canvas.
The canvas is divided into chunks which are rectangular areaschunksze wide bychunksze high. Use a chunk size which is about the average size of the canvas items. If you choose a chunk size which is too small it will increase the amount of calculation required when drawing since each change will affect many chunks. If you choose a chunk size which is too large the amount of drawing required will increase because for each change, a lot of drawing will be required since there will be many (unchanged) canvas items which are in the same chunk as the changed canvas items.
Internally, a canvas uses a low-resolution "chunk matrix" to keep track of all the items in the canvas. A 64x64 chunk matrix is the default for a 1024x1024 pixel canvas, where each chunk collects canvas items in a 16x16 pixel square. This default is also affected bysetTiles(). You can tune this default using this function. For example if you have a very large canvas and want to trade off speed for memory then you might set the chunk size to 32 or 64.
Themxclusters argument is the number of rectangular groups of chunks that will be separately drawn. If the canvas has a large number of small, dispersed items, this should be about that number. Our testing suggests that a large number of clusters is almost always best.
[virtual]void Q3Canvas::setAdvancePeriod(int ms)Sets the canvas to calladvance() everyms milliseconds. Any previous setting by setAdvancePeriod() orsetUpdatePeriod() is overridden.
Ifms is less than 0 advancing will be stopped.
[virtual]void Q3Canvas::setAllChanged()Marks the whole canvas as changed. All views of the canvas will be entirely redrawn whenupdate() is called next.
[virtual]void Q3Canvas::setBackgroundColor(constQColor & c)Sets the solid background to be the colorc.
See alsobackgroundColor(),setBackgroundPixmap(), andsetTiles().
[virtual]void Q3Canvas::setBackgroundPixmap(constQPixmap & p)Sets the solid background to be the pixmapp repeated as necessary to cover the entire canvas.
See alsobackgroundPixmap(),setBackgroundColor(), andsetTiles().
[virtual]void Q3Canvas::setChanged(constQRect & area)Marksarea as changed. Thisarea will be redrawn in all views that are showing it whenupdate() is called next.
[virtual]void Q3Canvas::setDoubleBuffering(bool y)Ify is true (the default) double-buffering is switched on; otherwise double-buffering is switched off.
Turning off double-buffering causes the redrawn areas to flicker a little and also gives a (usually small) performance improvement.
[virtual]void Q3Canvas::setTile(int x,int y,int tilenum)Sets the tile at (x,y) to use tile numbertilenum, which is an index into the tile pixmaps. The canvas will update appropriately whenupdate() is next called.
The images are taken from the pixmap set bysetTiles() and are arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.
| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
[virtual]void Q3Canvas::setTiles(QPixmap p,int h,int v,int tilewidth,int tileheight)Sets theQ3Canvas to be composed ofh tiles horizontally andv tiles vertically. Each tile will be an imagetilewidth bytileheight pixels from pixmapp.
The pixmapp is a list of tiles, arranged left to right, (and in the case of pixmaps that have multiple rows of tiles, top to bottom), with tile 0 in the top-left corner, tile 1 next to the right, and so on, e.g.
| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
If the canvas is larger than the matrix of tiles, the entire matrix is repeated as necessary to cover the whole canvas. If it is smaller, tiles to the right and bottom are not visible.
The width and height ofp must be a multiple oftilewidth andtileheight. If they are not the function will do nothing.
If you want to unset any tiling set, then just pass in a null pixmap and 0 forh,v,tilewidth, andtileheight.
[virtual]void Q3Canvas::setUnchanged(constQRect & area)Marksarea asunchanged. The area willnot be redrawn in the views for the nextupdate(), unless it is marked or changed again before the next call toupdate().
[virtual]void Q3Canvas::setUpdatePeriod(int ms)Sets the canvas to callupdate() everyms milliseconds. Any previous setting bysetAdvancePeriod() or setUpdatePeriod() is overridden.
Ifms is less than 0 automatic updating will be stopped.
Returns the size of the canvas, in pixels.
Returns the tile at position (x,y). Initially, all tiles are 0.
The parameters must be within range, i.e. 0 <x <tilesHorizontally() and 0 <y <tilesVertically().
See alsosetTile().
Returns the height of each tile.
Returns the width of each tile.
Returns the number of tiles horizontally.
Returns the number of tiles vertically.
[virtual slot]void Q3Canvas::update()Repaints changed areas in all views of the canvas.
See alsoadvance().
Returns true if the chunk position (x,y) is on the canvas; otherwise returns false.
See alsoonCanvas().
This is an overloaded function.
Returns true if the chunk positionp is on the canvas; otherwise returns false.
See alsoonCanvas().
Returns the width of the canvas, in pixels.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.