Q3CanvasView Class Reference

#include <q3canvas.h>

Inheritance diagram for Q3CanvasView:

Inheritance graph
[legend]
Collaboration diagram for Q3CanvasView:

Collaboration graph
[legend]
List of all members.

Detailed Description

The Q3CanvasView class provides an on-screen view of a Q3Canvas.

A Q3CanvasView is widget which provides a view of a Q3Canvas.

If you want users to be able to interact with a canvas view, subclass Q3CanvasView. You might then reimplement Q3ScrollView::contentsMousePressEvent(). For example:

    void MyCanvasView::contentsMousePressEvent(QMouseEvent* e)
    {
        Q3CanvasItemList l = canvas()->collisions(e->pos());
        for (Q3CanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
            if ((*it)->rtti() == Q3CanvasRectangle::RTTI)
                qDebug("A Q3CanvasRectangle lies somewhere at this point");
        }
    }

The canvas view shows canvas canvas(); this can be changed using setCanvas().

A transformation matrix can be used to transform the view of the canvas in various ways, for example, zooming in or out or rotating. For example:

    QMatrix wm;
    wm.scale(2, 2);   // Zooms in by 2 times
    wm.rotate(90);    // Rotates 90 degrees counter clockwise
                      // around the origin.
    wm.translate(0, -canvas->height());
                      // moves the canvas down so what was visible
                      // before is still visible.
    myCanvasView->setWorldMatrix(wm);

Use setWorldMatrix() to set the canvas view's world matrix: you must ensure that the world matrix is invertible. The current world matrix is retrievable with worldMatrix(), and its inversion is retrievable with inverseWorldMatrix().

Example:

The following code finds the part of the canvas that is visible in this view, i.e. the bounding rectangle of the view in canvas coordinates.

    QRect rc = QRect(myCanvasView->contentsX(), myCanvasView->contentsY(),
                     myCanvasView->visibleWidth(), myCanvasView->visibleHeight());
    QRect canvasRect = myCanvasView->inverseWorldMatrix().mapRect(rc);

See also:
QMatrix QPainter::setWorldMatrix(), QtCanvas, {Porting to Graphics View}

Definition at line 326 of file q3canvas.h.

Public Member Functions

 Q3CanvasView (QWidget *parent=0, const char *name=0, Qt::WindowFlags f=0)
 Q3CanvasView (Q3Canvas *viewing, QWidget *parent=0, const char *name=0, Qt::WindowFlags f=0)
 ~Q3CanvasView ()
Q3Canvascanvas () const
void setCanvas (Q3Canvas *v)
const QMatrixworldMatrix () const
const QMatrixinverseWorldMatrix () const
bool setWorldMatrix (const QMatrix &)

Protected Member Functions

void drawContents (QPainter *p, int cx, int cy, int cw, int ch)
QSize sizeHint () const

Private Slots

void updateContentsSize ()

Private Member Functions

void drawContents (QPainter *)

Private Attributes

Q3Canvasviewing
Q3CanvasViewDatad

Friends

class Q3Canvas
void qt_unview (Q3Canvas *c)


Constructor & Destructor Documentation

Q3CanvasView::Q3CanvasView ( QWidget parent = 0,
const char *  name = 0,
Qt::WindowFlags  f = 0 
)

Constructs a Q3CanvasView with parent parent, and name name, using the widget flags f. The canvas view is not associated with a canvas, so you must to call setCanvas() to view a canvas.

Definition at line 3356 of file q3canvas.cpp.

References d, setCanvas(), and viewing.

03357     : Q3ScrollView(parent,name,f|WResizeNoErase|WStaticContents)
03358 {
03359     d = new Q3CanvasViewData;
03360     viewing = 0;
03361     setCanvas(0);
03362 }

Here is the call graph for this function:

Q3CanvasView::Q3CanvasView ( Q3Canvas canvas,
QWidget parent = 0,
const char *  name = 0,
Qt::WindowFlags  f = 0 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Constructs a Q3CanvasView which views canvas canvas, with parent parent, and name name, using the widget flags f.

Definition at line 3370 of file q3canvas.cpp.

References canvas(), d, setCanvas(), and viewing.

03371     : Q3ScrollView(parent,name,f|WResizeNoErase|WStaticContents)
03372 {
03373     d = new Q3CanvasViewData;
03374     viewing = 0;
03375     setCanvas(canvas);
03376 }

Here is the call graph for this function:

Q3CanvasView::~Q3CanvasView (  ) 

Destroys the canvas view. The associated canvas is not deleted.

Definition at line 3381 of file q3canvas.cpp.

References d, and setCanvas().

03382 {
03383     delete d;
03384     d = 0;
03385     setCanvas(0);
03386 }

Here is the call graph for this function:


Member Function Documentation

Q3Canvas * Q3CanvasView::canvas (  )  const [inline]

Returns a pointer to the canvas which the Q3CanvasView is currently showing.

Definition at line 335 of file q3canvas.h.

Referenced by Q3CanvasView(), setCanvas(), and sizeHint().

00336   { return viewing; }

void Q3CanvasView::setCanvas ( Q3Canvas canvas  ) 

Sets the canvas that the Q3CanvasView is showing to the canvas canvas.

Definition at line 3400 of file q3canvas.cpp.

References canvas(), QObject::connect(), d, QObject::disconnect(), SIGNAL, SLOT, QWidget::update(), updateContentsSize(), and viewing.

Referenced by Q3CanvasView(), and ~Q3CanvasView().

03401 {
03402     if (viewing == canvas)
03403         return;
03404 
03405     if (viewing) {
03406   disconnect(viewing);
03407   viewing->removeView(this);
03408     }
03409     viewing=canvas;
03410     if (viewing) {
03411   connect(viewing,SIGNAL(resized()), this, SLOT(updateContentsSize()));
03412   viewing->addView(this);
03413   viewing->setAllChanged();
03414     }
03415     if (d) // called by d'tor
03416         updateContentsSize();
03417     update();
03418 }

Here is the call graph for this function:

const QMatrix & Q3CanvasView::worldMatrix (  )  const

Returns a reference to the canvas view's current transformation matrix.

See also:
setWorldMatrix() inverseWorldMatrix()

Definition at line 3426 of file q3canvas.cpp.

References d, and Q3CanvasViewData::xform.

Referenced by Q3Canvas::drawViewArea(), and Q3Canvas::update().

03427 {
03428     return d->xform;
03429 }

const QMatrix & Q3CanvasView::inverseWorldMatrix (  )  const

Returns a reference to the inverse of the canvas view's current transformation matrix.

See also:
setWorldMatrix() worldMatrix()

Definition at line 3437 of file q3canvas.cpp.

References d, and Q3CanvasViewData::ixform.

Referenced by Q3Canvas::update().

03438 {
03439     return d->ixform;
03440 }

bool Q3CanvasView::setWorldMatrix ( const QMatrix wm  ) 

Sets the transformation matrix of the Q3CanvasView to wm. The matrix must be invertible (i.e. if you create a world matrix that zooms out by 2 times, then the inverse of this matrix is one that will zoom in by 2 times).

When you use this, you should note that the performance of the Q3CanvasView will decrease considerably.

Returns false if wm is not invertable; otherwise returns true.

See also:
worldMatrix() inverseWorldMatrix() QMatrix::isInvertible()

Definition at line 3455 of file q3canvas.cpp.

References d, QMatrix::isInvertible(), Q3CanvasViewData::ixform, QWidget::update(), updateContentsSize(), Q3ScrollView::viewport(), and Q3CanvasViewData::xform.

03456 {
03457     bool ok = wm.isInvertible();
03458     if (ok) {
03459   d->xform = wm;
03460   d->ixform = wm.invert();
03461   updateContentsSize();
03462   viewport()->update();
03463     }
03464     return ok;
03465 }

Here is the call graph for this function:

void Q3CanvasView::drawContents ( QPainter p,
int  cx,
int  cy,
int  cw,
int  ch 
) [protected, virtual]

Repaints part of the Q3Canvas that the canvas view is showing starting at cx by cy, with a width of cw and a height of ch using the painter p.

Reimplemented from Q3ScrollView.

Definition at line 3501 of file q3canvas.cpp.

References d, Q3Canvas::drawViewArea(), Q3CanvasViewData::eraseRegion, i, QRegion::isEmpty(), p, QRegion::rects(), and viewing.

03502 {
03503     QRect r(cx,cy,cw,ch);
03504     if (!d->eraseRegion.isEmpty()) {
03505         const QVector<QRect> rects = d->eraseRegion.rects();
03506         for (int i = 0; i < rects.size(); ++i)
03507             p->eraseRect(rects.at(i));
03508 
03509         d->eraseRegion = QRegion();
03510     }
03511 
03512     if (viewing) {
03513         viewing->drawViewArea(this,p,r,false);
03514     } else {
03515   p->eraseRect(r);
03516     }
03517 }

Here is the call graph for this function:

QSize Q3CanvasView::sizeHint (  )  const [protected, virtual]

Suggests a size sufficient to view the entire canvas.

Reimplemented from Q3ScrollView.

Definition at line 3532 of file q3canvas.cpp.

References canvas(), QApplication::desktop(), Q3Frame::frameWidth(), QWidget::size(), and Q3ScrollView::sizeHint().

03533 {
03534     if (!canvas())
03535   return Q3ScrollView::sizeHint();
03536     // should maybe take transformations into account
03537     return (canvas()->size() + 2 * QSize(frameWidth(), frameWidth()))
03538      .boundedTo(3 * QApplication::desktop()->size() / 4);
03539 }

Here is the call graph for this function:

void Q3CanvasView::drawContents ( QPainter  )  [private, virtual]

Reimplemented from Q3ScrollView.

Definition at line 3525 of file q3canvas.cpp.

03526 {
03527 }

void Q3CanvasView::updateContentsSize (  )  [private, slot]

Definition at line 3468 of file q3canvas.cpp.

References Q3ScrollView::contentsHeight(), Q3ScrollView::contentsToViewport(), Q3ScrollView::contentsWidth(), d, Q3CanvasViewData::eraseRegion, QRect::height(), Q3Canvas::height(), QMatrix::map(), QWidget::rect(), Q3ScrollView::resizeContents(), viewing, QRect::width(), Q3Canvas::width(), and Q3CanvasViewData::xform.

Referenced by setCanvas(), and setWorldMatrix().

03469 {
03470     if (viewing) {
03471   QRect br;
03472 #ifndef QT_NO_TRANSFORMATIONS
03473   br = d->xform.map(QRect(0,0,viewing->width(),viewing->height()));
03474 #else
03475   br = QRect(0,0,viewing->width(),viewing->height());
03476 #endif
03477 
03478   if (br.width() < contentsWidth()) {
03479       QRect r(contentsToViewport(QPoint(br.width(),0)),
03480         QSize(contentsWidth()-br.width(),contentsHeight()));
03481             d->eraseRegion = r;
03482   }
03483   if (br.height() < contentsHeight()) {
03484       QRect r(contentsToViewport(QPoint(0,br.height())),
03485         QSize(contentsWidth(),contentsHeight()-br.height()));
03486             d->eraseRegion |= r;
03487   }
03488 
03489   resizeContents(br.width(),br.height());
03490     } else {
03491         d->eraseRegion = rect();
03492   resizeContents(1,1);
03493     }
03494 }


Friends And Related Function Documentation

friend class Q3Canvas [friend]

Definition at line 348 of file q3canvas.h.

void qt_unview ( Q3Canvas c  )  [friend]

Definition at line 658 of file q3canvas.cpp.

00659 {
00660     for (Q3CanvasView* view=c->d->viewList.first(); view != 0; view=c->d->viewList.next()) {
00661   view->viewing = 0;
00662     }
00663 }


Member Data Documentation

Q3Canvas* Q3CanvasView::viewing [private]

Definition at line 350 of file q3canvas.h.

Referenced by drawContents(), Q3CanvasView(), setCanvas(), and updateContentsSize().

Q3CanvasViewData* Q3CanvasView::d [private]

Reimplemented from Q3ScrollView.

Definition at line 351 of file q3canvas.h.

Referenced by drawContents(), inverseWorldMatrix(), Q3CanvasView(), setCanvas(), setWorldMatrix(), updateContentsSize(), worldMatrix(), and ~Q3CanvasView().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:44:41 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1