📄 q3canvas.cpp
字号:
\i Canvas items are usually much faster to manipulate and redraw than child widgets, with the speed advantage becoming especially great when there are \e many canvas items and non-rectangular items. In most situations canvas items are also a lot more memory efficient than child widgets. \i It's easy to detect overlapping items (collision detection). \i The canvas can be larger than a widget. A million-by-million canvas is perfectly possible. At such a size a widget might be very inefficient, and some window systems might not support it at all, whereas Q3Canvas scales well. Even with a billion pixels and a million items, finding a particular canvas item, detecting collisions, etc., is still fast (though the memory consumption may be prohibitive at such extremes). \i Two or more Q3CanvasView objects can view the same canvas. \i An arbitrary transformation matrix can be set on each Q3CanvasView which makes it easy to zoom, rotate or shear the viewed canvas. \i Widgets provide a lot more functionality, such as input (QKeyEvent, QMouseEvent etc.) and layout management (QGridLayout etc.). \endlist 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 using setBackgroundColor(), or to a repeated pixmap using setBackgroundPixmap() or to a mosaic of smaller pixmaps using setTiles(). Individual tiles can be set with setTile(). There are corresponding get functions, e.g. backgroundColor() and backgroundPixmap(). Note that Q3Canvas does not inherit from QWidget, even though it has some functions which provide the same functionality as those in QWidget. One of these is setBackgroundPixmap(); some others are resize(), size(), width() and height(). \l 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 using Q3CanvasItem::setCanvas(). Canvas items are movable (and in the case of Q3CanvasSprites, animated) objects that inherit Q3CanvasItem. 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 example Q3CanvasItem::x() is greater than width()). 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 with Q3CanvasItem::move(). The advance() function moves all Q3CanvasItem::animated() canvas items and setAdvancePeriod() makes Q3Canvas move them automatically on a periodic basis. In the context of the Q3Canvas classes, to `animate' a canvas item is to set it in motion, i.e. using Q3CanvasItem::setVelocity(). Animation of a canvas item itself, i.e. items which change over time, is enabled by calling Q3CanvasSprite::setFrameAnimation(), or more generally by subclassing and reimplementing Q3CanvasItem::advance(). To detect collisions use one of the Q3CanvasItem::collisions() functions. The changed parts of the canvas are redrawn (if they are visible in a canvas view) whenever update() is called. You can either call update() manually after having changed the contents of the canvas, or force periodic updates using setUpdatePeriod(). If you have moving objects on the canvas, you must call advance() every time the objects should move one step further. Periodic calls to advance() can be forced using setAdvancePeriod(). The advance() function will call Q3CanvasItem::advance() on every item that is \link Q3CanvasItem::animated() animated\endlink 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 into \e chunks; 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 to Q3Canvas'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. The Q3Canvas constructor tries to pick a suitable size, but you can call retune() to change it at any time. The chunkSize() 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 reimplement drawForeground(), you can draw things in front of all the canvas items. Areas can be set as changed with setChanged() and set unchanged with setUnchanged(). The entire canvas can be set as changed with setAllChanged(). A list of all the items on the canvas is returned by allItems(). An area can be copied (painted) to a QPainter with drawArea(). If the canvas is resized it emits the resized() signal. The examples/canvas application and the 2D graphics page of the examples/demo application demonstrate many of Q3Canvas's facilities. \sa Q3CanvasView Q3CanvasItem, QtCanvas, {Porting to Graphics View}*/void Q3Canvas::init(int w, int h, int chunksze, int mxclusters){ d = new Q3CanvasData; awidth=w; aheight=h; chunksize=chunksze; maxclusters=mxclusters; chwidth=(w+chunksize-1)/chunksize; chheight=(h+chunksize-1)/chunksize; chunks=new Q3CanvasChunk[chwidth*chheight]; update_timer = 0; bgcolor = white; grid = 0; htiles = 0; vtiles = 0; dblbuf = false; debug_redraw_areas = false;}/*! Create a Q3Canvas with no size. \a parent and \a name are passed to the QObject superclass. \warning You \e must call resize() at some time after creation to be able to use the canvas.*/Q3Canvas::Q3Canvas(QObject* parent, const char* name) : QObject(parent, name){ init(0,0);}/*! Constructs a Q3Canvas that is \a w pixels wide and \a h pixels high.*/Q3Canvas::Q3Canvas(int w, int h){ init(w,h);}/*! Constructs a Q3Canvas which will be composed of \a h tiles horizontally and \a v tiles vertically. Each tile will be an image \a tilewidth by \a tileheight pixels taken from pixmap \a p. The pixmap \a p 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. \table \row \i 0 \i 1 \i 2 \i 3 \row \i 4 \i 5 \i 6 \i 7 \endtable The Q3Canvas 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. \sa setTiles()*/Q3Canvas::Q3Canvas(QPixmap p, int h, int v, int tilewidth, int tileheight){ init(h*tilewidth, v*tileheight, scm(tilewidth,tileheight)); setTiles(p, h, v, tilewidth, tileheight);}void qt_unview(Q3Canvas* c){ for (Q3CanvasView* view=c->d->viewList.first(); view != 0; view=c->d->viewList.next()) { view->viewing = 0; }}/*! Destroys the canvas and all the canvas's canvas items.*/Q3Canvas::~Q3Canvas(){ qt_unview(this); Q3CanvasItemList all = allItems(); for (Q3CanvasItemList::Iterator it=all.begin(); it!=all.end(); ++it) delete *it; delete [] chunks; delete [] grid; delete d;}/*!\internalReturns the chunk at a chunk position \a i, \a j.*/Q3CanvasChunk& Q3Canvas::chunk(int i, int j) const{ return chunks[i+chwidth*j];}/*!\internalReturns the chunk at a pixel position \a x, \a y.*/Q3CanvasChunk& Q3Canvas::chunkContaining(int x, int y) const{ return chunk(x/chunksize,y/chunksize);}/*! Returns a list of all the items in the canvas.*/Q3CanvasItemList Q3Canvas::allItems(){ Q3CanvasItemList list; for (Q3PtrDictIterator<void> it=d->itemDict; it.currentKey(); ++it) { list.prepend((Q3CanvasItem*)it.currentKey()); } return list;}/*! Changes the size of the canvas to have a width of \a w and a height of \a h. This is a slow operation.*/void Q3Canvas::resize(int w, int h){ if (awidth==w && aheight==h) return; Q3CanvasItem* item; Q3PtrList<Q3CanvasItem> hidden; for (Q3PtrDictIterator<void> it=d->itemDict; it.currentKey(); ++it) { if (((Q3CanvasItem*)it.currentKey())->isVisible()) { ((Q3CanvasItem*)it.currentKey())->hide(); hidden.append(((Q3CanvasItem*)it.currentKey())); } } int nchwidth=(w+chunksize-1)/chunksize; int nchheight=(h+chunksize-1)/chunksize; Q3CanvasChunk* newchunks = new Q3CanvasChunk[nchwidth*nchheight]; // Commit the new values. // awidth=w; aheight=h; chwidth=nchwidth; chheight=nchheight; delete [] chunks; chunks=newchunks; for (item=hidden.first(); item != 0; item=hidden.next()) { item->show(); } setAllChanged(); emit resized();}/*! \fn void Q3Canvas::resized() This signal is emitted whenever the canvas is resized. Each Q3CanvasView connects to this signal to keep the scrollview's size correct.*//*! Change the efficiency tuning parameters to \a mxclusters clusters, each of size \a chunksze. This is a slow operation if there are many objects on the canvas. The canvas is divided into chunks which are rectangular areas \a chunksze wide by \a chunksze 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 by setTiles(). 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. The \a mxclusters 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.*/void Q3Canvas::retune(int chunksze, int mxclusters){ maxclusters=mxclusters; if (chunksize!=chunksze) { Q3PtrList<Q3CanvasItem> hidden; for (Q3PtrDictIterator<void> it=d->itemDict; it.currentKey(); ++it) { if (((Q3CanvasItem*)it.currentKey())->isVisible()) { ((Q3CanvasItem*)it.currentKey())->hide(); hidden.append(((Q3CanvasItem*)it.currentKey())); } } chunksize=chunksze; int nchwidth=(awidth+chunksize-1)/chunksize; int nchheight=(aheight+chunksize-1)/chunksize; Q3CanvasChunk* newchunks = new Q3CanvasChunk[nchwidth*nchheight]; // Commit the new values. // chwidth=nchwidth; chheight=nchheight; delete [] chunks; chunks=newchunks; for (Q3CanvasItem* item=hidden.first(); item != 0; item=hidden.next()) { item->show(); } }}/*! \fn int Q3Canvas::width() const Returns the width of the canvas, in pixels.*//*! \fn int Q3Canvas::height() const Returns the height of the canvas, in pixels.*//*! \fn QSize Q3Canvas::size() const Returns the size of the canvas, in pixels.*//*! \fn QRect Q3Canvas::rect() const Returns a rectangle the size of the canvas.*//*! \fn bool Q3Canvas::onCanvas(int x, int y) const Returns true if the pixel position (\a x, \a y) is on the canvas; otherwise returns false. \sa validChunk()*//*! \fn bool Q3Canvas::onCanvas(const QPoint& p) const \overload Returns true if the pixel position \a p is on the canvas; otherwise returns false. \sa validChunk()*//*! \fn bool Q3Canvas::validChunk(int x, int y) const Returns true if the chunk position (\a x, \a y) is on the canvas; otherwise returns false. \sa onCanvas()*//*! \fn bool Q3Canvas::validChunk(const QPoint& p) const \overload Returns true if the chunk position \a p is on the canvas; otherwise returns false. \sa onCanvas()*//*! \fn int Q3Canvas::chunkSize() const Returns the chunk size of the canvas. \sa retune()*//*!\fn bool Q3Canvas::sameChunk(int x1, int y1, int x2, int y2) const\internalTells if the points (\a x1, \a y1) and (\a x2, \a y2) are within the same chunk.*//*!\internalThis method adds an the item \a item to the list of Q3CanvasItem objectsin the Q3Canvas. The Q3CanvasItem class calls this.*/void Q3Canvas::addItem(Q3CanvasItem* item){ d->itemDict.insert((void*)item,(void*)1);}/*!\internalThis method adds the item \a item to the list of Q3CanvasItem objectsto be moved. The Q3CanvasItem class calls this.*/void Q3Canvas::addAnimation(Q3CanvasItem* item){ d->animDict.insert((void*)item,(void*)1);}/*!\internalThis method adds the item \a item to the list of Q3CanvasItem objectswhich are no longer to be moved. The Q3CanvasItem class calls this.*/void Q3Canvas::removeAnimation(Q3CanvasItem* item){ d->animDict.remove((void*)item);}/*!\internalThis method removes the item \a item from the list of Q3CanvasItem objectsin this Q3Canvas. The Q3CanvasItem class calls this.*/void Q3Canvas::removeItem(Q3CanvasItem* item){ d->itemDict.remove((void*)item);}/*!\internalThis method adds the view \a view to the list of Q3CanvasView objectsviewing this Q3Canvas. The Q3CanvasView class calls this.*/void Q3Canvas::addView(Q3CanvasView* view){ d->viewList.append(view); if (htiles>1 || vtiles>1 || pm.isNull()) view->viewport()->setBackgroundColor(backgroundColor());}/*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -