Qt 4.8
Macros | Functions
qgraphicsscene.cpp File Reference
#include "qgraphicsscene.h"
#include "qgraphicsitem.h"
#include "qgraphicsitem_p.h"
#include "qgraphicslayout.h"
#include "qgraphicsscene_p.h"
#include "qgraphicssceneevent.h"
#include "qgraphicsview.h"
#include "qgraphicsview_p.h"
#include "qgraphicswidget.h"
#include "qgraphicswidget_p.h"
#include "qgraphicssceneindex_p.h"
#include "qgraphicsscenebsptreeindex_p.h"
#include "qgraphicsscenelinearindex_p.h"
#include <QtCore/qdebug.h>
#include <QtCore/qlist.h>
#include <QtCore/qmath.h>
#include <QtCore/qrect.h>
#include <QtCore/qset.h>
#include <QtCore/qstack.h>
#include <QtCore/qtimer.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/QMetaMethod>
#include <QtGui/qapplication.h>
#include <QtGui/qdesktopwidget.h>
#include <QtGui/qevent.h>
#include <QtGui/qgraphicslayout.h>
#include <QtGui/qgraphicsproxywidget.h>
#include <QtGui/qgraphicswidget.h>
#include <QtGui/qmatrix.h>
#include <QtGui/qpaintengine.h>
#include <QtGui/qpainter.h>
#include <QtGui/qpixmapcache.h>
#include <QtGui/qpolygon.h>
#include <QtGui/qstyleoption.h>
#include <QtGui/qtooltip.h>
#include <QtGui/qtransform.h>
#include <QtGui/qinputcontext.h>
#include <QtGui/qgraphicseffect.h>
#include <QtGui/qaccessible.h>
#include <private/qapplication_p.h>
#include <private/qobject_p.h>
#include <private/qt_x11_p.h>
#include <private/qgraphicseffect_p.h>
#include <private/qgesturemanager_p.h>
#include <private/qpathclipper_p.h>
#include "moc_qgraphicsscene.cpp"

Go to the source code of this file.

Macros

#define ENSURE_TRANSFORM_PTR
 
#define G_DEBUG   if (0) qDebug
 

Functions

static void _q_hoverFromMouseEvent (QGraphicsSceneHoverEvent *hover, const QGraphicsSceneMouseEvent *mouseEvent)
 
static void _q_paintIntoCache (QPixmap *pix, QGraphicsItem *item, const QRegion &pixmapExposed, const QTransform &itemToPixmap, QPainter::RenderHints renderHints, const QStyleOptionGraphicsItem *option, bool painterStateProtection)
 
static void _q_paintItem (QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget, bool useWindowOpacity, bool painterStateProtection)
 
bool qt_sendSpontaneousEvent (QObject *receiver, QEvent *event)
 
static void setClip (QPainter *painter, QGraphicsItem *item)
 
static void setWorldTransform (QPainter *painter, const QTransform *const transformPtr, const QTransform *effectTransform)
 
static bool transformIsSimple (const QTransform &transform)
 
static bool updateHelper (QGraphicsViewPrivate *view, QGraphicsItemPrivate *item, const QRectF &rect, bool itemIsUntransformable)
 

Macro Definition Documentation

◆ ENSURE_TRANSFORM_PTR

#define ENSURE_TRANSFORM_PTR
Value:
if (!transformPtr) { \
Q_ASSERT(!itemIsUntransformable); \
if (viewTransform) { \
transform = item->d_ptr->sceneTransform; \
transform *= *viewTransform; \
transformPtr = &transform; \
} else { \
transformPtr = &item->d_ptr->sceneTransform; \
translateOnlyTransform = item->d_ptr->sceneTransformTranslateOnly; \
} \
}

Referenced by QGraphicsScenePrivate::drawSubtreeRecursive().

◆ G_DEBUG

#define G_DEBUG   if (0) qDebug

Function Documentation

◆ _q_hoverFromMouseEvent()

static void _q_hoverFromMouseEvent ( QGraphicsSceneHoverEvent hover,
const QGraphicsSceneMouseEvent mouseEvent 
)
static

Definition at line 280 of file qgraphicsscene.cpp.

Referenced by QGraphicsScene::mouseMoveEvent(), QGraphicsScene::mousePressEvent(), and QGraphicsScene::mouseReleaseEvent().

281 {
282  hover->setWidget(mouseEvent->widget());
283  hover->setPos(mouseEvent->pos());
284  hover->setScenePos(mouseEvent->scenePos());
285  hover->setScreenPos(mouseEvent->screenPos());
286  hover->setLastPos(mouseEvent->lastPos());
287  hover->setLastScenePos(mouseEvent->lastScenePos());
288  hover->setLastScreenPos(mouseEvent->lastScreenPos());
289  hover->setModifiers(mouseEvent->modifiers());
290  hover->setAccepted(mouseEvent->isAccepted());
291 }
QPoint screenPos() const
Returns the mouse cursor position in screen coordinates.
void setAccepted(bool accepted)
Definition: qcoreevent.h:306
QWidget * widget() const
Returns the widget where the event originated, or 0 if the event originates from another application...
void setWidget(QWidget *widget)
Sets the widget related to this event.
QPointF lastScenePos() const
Returns the last recorded mouse cursor position in scene coordinates.
bool isAccepted() const
Definition: qcoreevent.h:307
void setModifiers(Qt::KeyboardModifiers modifiers)
Sets the modifiers for the current hover event to modifiers.
void setLastScreenPos(const QPoint &pos)
QPoint lastScreenPos() const
Returns the last recorded mouse cursor position in screen coordinates.
QPointF scenePos() const
Returns the mouse cursor position in scene coordinates.
void setLastScenePos(const QPointF &pos)
void setScreenPos(const QPoint &pos)
Sets the position associated with the hover event to the given point in screen coordinates.
void setPos(const QPointF &pos)
Sets the position associated with the hover event to the given point in item coordinates.
QPointF pos() const
Returns the mouse cursor position in item coordinates.
Qt::KeyboardModifiers modifiers() const
Returns the keyboard modifiers in use at the time the event was sent.
QPointF lastPos() const
Returns the last recorded mouse cursor position in item coordinates.
void setLastPos(const QPointF &pos)
void setScenePos(const QPointF &pos)
Sets the position associated with the hover event to the given point in scene coordinates.

◆ _q_paintIntoCache()

static void _q_paintIntoCache ( QPixmap pix,
QGraphicsItem item,
const QRegion pixmapExposed,
const QTransform itemToPixmap,
QPainter::RenderHints  renderHints,
const QStyleOptionGraphicsItem option,
bool  painterStateProtection 
)
static

Definition at line 4466 of file qgraphicsscene.cpp.

Referenced by QGraphicsScenePrivate::drawItemHelper().

4469 {
4470  QPixmap subPix;
4471  QPainter pixmapPainter;
4472  QRect br = pixmapExposed.boundingRect();
4473 
4474  // Don't use subpixmap if we get a full update.
4475  if (pixmapExposed.isEmpty() || (pixmapExposed.rectCount() == 1 && br.contains(pix->rect()))) {
4476  pix->fill(Qt::transparent);
4477  pixmapPainter.begin(pix);
4478  } else {
4479  subPix = QPixmap(br.size());
4480  subPix.fill(Qt::transparent);
4481  pixmapPainter.begin(&subPix);
4482  pixmapPainter.translate(-br.topLeft());
4483  if (!pixmapExposed.isEmpty()) {
4484  // Applied to subPix; paint is adjusted to the coordinate space is
4485  // correct.
4486  pixmapPainter.setClipRegion(pixmapExposed);
4487  }
4488  }
4489 
4490  pixmapPainter.setRenderHints(pixmapPainter.renderHints(), false);
4491  pixmapPainter.setRenderHints(renderHints, true);
4492  pixmapPainter.setWorldTransform(itemToPixmap, true);
4493 
4494  // Render.
4495  _q_paintItem(item, &pixmapPainter, option, 0, false, painterStateProtection);
4496  pixmapPainter.end();
4497 
4498  if (!subPix.isNull()) {
4499  // Blit the subpixmap into the main pixmap.
4500  pixmapPainter.begin(pix);
4502  pixmapPainter.setClipRegion(pixmapExposed);
4503  pixmapPainter.drawPixmap(br.topLeft(), subPix);
4504  pixmapPainter.end();
4505  }
4506 }
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
static void _q_paintItem(QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget, bool useWindowOpacity, bool painterStateProtection)
QRect boundingRect() const
Returns the bounding rectangle of this region.
Definition: qregion.cpp:4363
int rectCount() const
Returns the number of rectangles that will be returned in rects().
Definition: qregion.cpp:4461
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false...
Definition: qpainter.cpp:1723
bool isEmpty() const
Returns true if the region is empty; otherwise returns false.
Definition: qregion.cpp:4098
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition: qpixmap.cpp:1080
void setRenderHints(RenderHints hints, bool on=true)
Sets the given render hints on the painter if on is true; otherwise clears the render hints...
Definition: qpainter.cpp:7649
void setClipRegion(const QRegion &, Qt::ClipOperation op=Qt::ReplaceClip)
Sets the clip region to the given region using the specified clip operation.
Definition: qpainter.cpp:2917
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
Definition: qpainter.cpp:2422
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630
QRect rect() const
Returns the pixmap&#39;s enclosing rectangle.
Definition: qpixmap.cpp:676
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
RenderHints renderHints() const
Returns a flag that specifies the rendering hints that are set for this painter.
Definition: qpainter.cpp:7675
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
bool end()
Ends painting.
Definition: qpainter.cpp:1929
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
Definition: qpainter.cpp:3311

◆ _q_paintItem()

static void _q_paintItem ( QGraphicsItem item,
QPainter painter,
const QStyleOptionGraphicsItem option,
QWidget widget,
bool  useWindowOpacity,
bool  painterStateProtection 
)
static

Definition at line 4422 of file qgraphicsscene.cpp.

Referenced by _q_paintIntoCache(), and QGraphicsScenePrivate::drawItemHelper().

4425 {
4426  if (!item->isWidget()) {
4427  item->paint(painter, option, widget);
4428  return;
4429  }
4430  QGraphicsWidget *widgetItem = static_cast<QGraphicsWidget *>(item);
4431  QGraphicsProxyWidget *proxy = qobject_cast<QGraphicsProxyWidget *>(widgetItem);
4432  const qreal windowOpacity = (proxy && proxy->widget() && useWindowOpacity)
4433  ? proxy->widget()->windowOpacity() : qreal(1.0);
4434  const qreal oldPainterOpacity = painter->opacity();
4435 
4436  if (qFuzzyIsNull(windowOpacity))
4437  return;
4438  // Set new painter opacity.
4439  if (windowOpacity < 1.0)
4440  painter->setOpacity(oldPainterOpacity * windowOpacity);
4441 
4442  // set layoutdirection on the painter
4443  Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection();
4444  painter->setLayoutDirection(widgetItem->layoutDirection());
4445 
4446  if (widgetItem->isWindow() && widgetItem->windowType() != Qt::Popup && widgetItem->windowType() != Qt::ToolTip
4447  && !(widgetItem->windowFlags() & Qt::FramelessWindowHint)) {
4448  if (painterStateProtection)
4449  painter->save();
4450  widgetItem->paintWindowFrame(painter, option, widget);
4451  if (painterStateProtection)
4452  painter->restore();
4453  } else if (widgetItem->autoFillBackground()) {
4454  painter->fillRect(option->exposedRect, widgetItem->palette().window());
4455  }
4456 
4457  widgetItem->paint(painter, option, widget);
4458 
4459  // Restore layoutdirection on the painter.
4460  painter->setLayoutDirection(oldLayoutDirection);
4461  // Restore painter opacity.
4462  if (windowOpacity < 1.0)
4463  painter->setOpacity(oldPainterOpacity);
4464 }
Qt::WindowFlags windowFlags
the widget&#39;s window flags
double windowOpacity
The level of opacity for the window.
Definition: qwidget.h:201
double qreal
Definition: qglobal.h:1193
qreal opacity() const
Returns the opacity of the painter.
Definition: qpainter.cpp:2115
bool isWindow() const
Returns true if the item is a QGraphicsWidget window, otherwise returns false.
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
T * qobject_cast(QObject *object)
Definition: qobject.h:375
Qt::WindowType windowType() const
Returns the widgets window type.
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
virtual void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
This virtual function is called by QGraphicsScene to draw the window frame for windows using painter...
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented Function
LayoutDirection
Definition: qnamespace.h:1580
QPalette palette
the widget&#39;s palette
The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene...
Qt::LayoutDirection layoutDirection() const
Returns the layout direction used by the painter when drawing text.
Definition: qpainter.cpp:8729
bool autoFillBackground
whether the widget background is filled automatically
bool isWidget() const
Returns true if this item is a widget (i.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)=0
This function, which is usually called by QGraphicsView, paints the contents of an item in local coor...
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
Qt::LayoutDirection layoutDirection
the layout direction for this widget.
void setLayoutDirection(Qt::LayoutDirection direction)
Sets the layout direction used by the painter when drawing text, to the specified direction...
Definition: qpainter.cpp:8717
void setOpacity(qreal opacity)
Sets the opacity of the painter to opacity.
Definition: qpainter.cpp:2139
QRectF exposedRect
the exposed rectangle, in item coordinates
Definition: qstyleoption.h:873
const QBrush & window() const
Returns the window (general background) brush of the current color group.
Definition: qpalette.h:135
QWidget * widget() const
Returns a pointer to the embedded widget.
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.

◆ qt_sendSpontaneousEvent()

bool qt_sendSpontaneousEvent ( QObject receiver,
QEvent event 
)

Definition at line 5779 of file qapplication.cpp.

Referenced by QGraphicsScenePrivate::sendEvent().

5780 {
5781  return QCoreApplication::sendSpontaneousEvent(receiver, event);
5782 }
static bool sendSpontaneousEvent(QObject *receiver, QEvent *event)

◆ setClip()

static void setClip ( QPainter painter,
QGraphicsItem item 
)
inlinestatic

Definition at line 4986 of file qgraphicsscene.cpp.

Referenced by QGraphicsScenePrivate::draw().

4987 {
4988  painter->save();
4989  QRectF clipRect;
4990  const QPainterPath clipPath(item->shape());
4991  if (QPathClipper::pathToRect(clipPath, &clipRect))
4992  painter->setClipRect(clipRect, Qt::IntersectClip);
4993  else
4994  painter->setClipPath(clipPath, Qt::IntersectClip);
4995 }
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
virtual QPainterPath shape() const
Returns the shape of this item as a QPainterPath in local coordinates.
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
static bool pathToRect(const QPainterPath &path, QRectF *rect=0)
void setClipPath(const QPainterPath &path, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip path for the painter to the given path, with the clip operation...
Definition: qpainter.cpp:3365

◆ setWorldTransform()

static void setWorldTransform ( QPainter painter,
const QTransform *const  transformPtr,
const QTransform effectTransform 
)
inlinestatic

Definition at line 4997 of file qgraphicsscene.cpp.

Referenced by QGraphicsScenePrivate::draw(), and QPainter::setTransform().

4999 {
5000  Q_ASSERT(transformPtr);
5001  if (effectTransform)
5002  painter->setWorldTransform(*transformPtr * *effectTransform);
5003  else
5004  painter->setWorldTransform(*transformPtr);
5005 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630

◆ transformIsSimple()

static bool transformIsSimple ( const QTransform transform)
inlinestatic

Definition at line 4510 of file qgraphicsscene.cpp.

Referenced by QGraphicsScenePrivate::drawItemHelper().

4511 {
4513  if (type <= QTransform::TxScale) {
4514  return true;
4515  } else if (type == QTransform::TxRotate) {
4516  // Check for 90, and 270 degree rotations.
4517  qreal m11 = transform.m11();
4518  qreal m12 = transform.m12();
4519  qreal m21 = transform.m21();
4520  qreal m22 = transform.m22();
4521  if (m11 == 0.0f && m22 == 0.0f) {
4522  if (m12 == 1.0f && m21 == -1.0f)
4523  return true; // 90 degrees.
4524  else if (m12 == -1.0f && m21 == 1.0f)
4525  return true; // 270 degrees.
4526  else if (m12 == -1.0f && m21 == -1.0f)
4527  return true; // 90 degrees inverted y.
4528  else if (m12 == 1.0f && m21 == 1.0f)
4529  return true; // 270 degrees inverted y.
4530  }
4531  }
4532  return false;
4533 }
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
qreal m21() const
Returns the horizontal shearing factor.
Definition: qtransform.h:249
qreal m22() const
Returns the vertical scaling factor.
Definition: qtransform.h:253
TransformationType type() const
Returns the transformation type of this matrix.
qreal m12() const
Returns the vertical shearing factor.
Definition: qtransform.h:241
TransformationType
Definition: qtransform.h:68
qreal m11() const
Returns the horizontal scaling factor.
Definition: qtransform.h:237

◆ updateHelper()

static bool updateHelper ( QGraphicsViewPrivate view,
QGraphicsItemPrivate item,
const QRectF rect,
bool  itemIsUntransformable 
)
inlinestatic

Definition at line 5228 of file qgraphicsscene.cpp.

Referenced by QGraphicsScenePrivate::processDirtyItemsRecursive().

5230 {
5231  Q_ASSERT(view);
5232  Q_ASSERT(item);
5233 
5234  QGraphicsItem *itemq = static_cast<QGraphicsItem *>(item->q_ptr);
5235  QGraphicsView *viewq = static_cast<QGraphicsView *>(view->q_ptr);
5236 
5237  if (itemIsUntransformable) {
5238  const QTransform xform = itemq->deviceTransform(viewq->viewportTransform());
5239  if (!item->hasBoundingRegionGranularity)
5240  return view->updateRectF(xform.mapRect(rect));
5241  return view->updateRegion(rect, xform);
5242  }
5243 
5244  if (item->sceneTransformTranslateOnly && view->identityMatrix) {
5245  const qreal dx = item->sceneTransform.dx();
5246  const qreal dy = item->sceneTransform.dy();
5247  QRectF r(rect);
5248  r.translate(dx - view->horizontalScroll(), dy - view->verticalScroll());
5249  return view->updateRectF(r);
5250  }
5251 
5252  if (!viewq->isTransformed()) {
5253  if (!item->hasBoundingRegionGranularity)
5254  return view->updateRectF(item->sceneTransform.mapRect(rect));
5255  return view->updateRegion(rect, item->sceneTransform);
5256  }
5257 
5258  QTransform xform = item->sceneTransform;
5259  xform *= viewq->viewportTransform();
5260  if (!item->hasBoundingRegionGranularity)
5261  return view->updateRectF(xform.mapRect(rect));
5262  return view->updateRegion(rect, xform);
5263 }
bool updateRectF(const QRectF &rect)
qreal dy() const
Returns the vertical translation factor.
Definition: qtransform.h:277
double qreal
Definition: qglobal.h:1193
bool isTransformed() const
Returns true if the view is transformed (i.
QObject * q_ptr
Definition: qobject.h:91
QGraphicsItem * q_ptr
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
qint64 horizontalScroll() const
Returns the horizontal scroll value (the X value of the left edge of the viewport).
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QTransform viewportTransform() const
Returns a matrix that maps viewport coordinates to scene coordinates.
quint32 hasBoundingRegionGranularity
QRect mapRect(const QRect &) const
Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate ...
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
quint32 sceneTransformTranslateOnly
QTransform deviceTransform(const QTransform &viewportTransform) const
Returns this item&#39;s device transformation matrix, using viewportTransform to map from scene to device...
bool updateRegion(const QRectF &rect, const QTransform &xform)
qreal dx() const
Returns the horizontal translation factor.
Definition: qtransform.h:273
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:64
qint64 verticalScroll() const
Returns the vertical scroll value (the X value of the top edge of the viewport).
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65