| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | //#define QGRAPHICSVIEW_DEBUG
|
|---|
| 43 |
|
|---|
| 44 | static const int QGRAPHICSVIEW_REGION_RECT_THRESHOLD = 50;
|
|---|
| 45 |
|
|---|
| 46 | static const int QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS = 503; // largest prime < 2^9
|
|---|
| 47 |
|
|---|
| 48 | /*!
|
|---|
| 49 | \class QGraphicsView
|
|---|
| 50 | \brief The QGraphicsView class provides a widget for displaying the
|
|---|
| 51 | contents of a QGraphicsScene.
|
|---|
| 52 | \since 4.2
|
|---|
| 53 | \ingroup multimedia
|
|---|
| 54 | \ingroup graphicsview-api
|
|---|
| 55 | \mainclass
|
|---|
| 56 |
|
|---|
| 57 | QGraphicsView visualizes the contents of a QGraphicsScene in a scrollable
|
|---|
| 58 | viewport. To create a scene with geometrical items, see QGraphicsScene's
|
|---|
| 59 | documentation. QGraphicsView is part of \l{The Graphics View Framework}.
|
|---|
| 60 |
|
|---|
| 61 | To visualize a scene, you start by constructing a QGraphicsView object,
|
|---|
| 62 | passing the address of the scene you want to visualize to QGraphicsView's
|
|---|
| 63 | constructor. Alternatively, you can call setScene() to set the scene at a
|
|---|
| 64 | later point. After you call show(), the view will by default scroll to the
|
|---|
| 65 | center of the scene and display any items that are visible at this
|
|---|
| 66 | point. For example:
|
|---|
| 67 |
|
|---|
| 68 | \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsview.cpp 0
|
|---|
| 69 |
|
|---|
| 70 | You can explicitly scroll to any position on the scene by using the
|
|---|
| 71 | scroll bars, or by calling centerOn(). By passing a point to centerOn(),
|
|---|
| 72 | QGraphicsView will scroll its viewport to ensure that the point is
|
|---|
| 73 | centered in the view. An overload is provided for scrolling to a
|
|---|
| 74 | QGraphicsItem, in which case QGraphicsView will see to that the center of
|
|---|
| 75 | the item is centered in the view. If all you want is to ensure that a
|
|---|
| 76 | certain area is visible, (but not necessarily centered,) you can call
|
|---|
| 77 | ensureVisible() instead.
|
|---|
| 78 |
|
|---|
| 79 | QGraphicsView can be used to visualize a whole scene, or only parts of it.
|
|---|
| 80 | The visualized area is by default detected automatically when the view is
|
|---|
| 81 | displayed for the first time (by calling
|
|---|
| 82 | QGraphicsScene::itemsBoundingRect()). To set the visualized area rectangle
|
|---|
| 83 | yourself, you can call setSceneRect(). This will adjust the scroll bars'
|
|---|
| 84 | ranges appropriately. Note that although the scene supports a virtually
|
|---|
| 85 | unlimited size, the range of the scroll bars will never exceed the range of
|
|---|
| 86 | an integer (INT_MIN, INT_MAX). When the scene is larger than the scroll
|
|---|
| 87 | bars' values, you can choose to use translate() to navigate the scene
|
|---|
| 88 | instead.
|
|---|
| 89 |
|
|---|
| 90 | QGraphicsView visualizes the scene by calling render(). By default, the
|
|---|
| 91 | items are drawn onto the viewport by using a regular QPainter, and using
|
|---|
| 92 | default render hints. To change the default render hints that
|
|---|
| 93 | QGraphicsView passes to QPainter when painting items, you can call
|
|---|
| 94 | setRenderHints().
|
|---|
| 95 |
|
|---|
| 96 | By default, QGraphicsView provides a regular QWidget for the viewport
|
|---|
| 97 | widget. You can access this widget by calling viewport(), or you can
|
|---|
| 98 | replace it by calling setViewport(). To render using OpenGL, simply call
|
|---|
| 99 | setViewport(new QGLWidget). QGraphicsView takes ownership of the viewport
|
|---|
| 100 | widget.
|
|---|
| 101 |
|
|---|
| 102 | QGraphicsView supports affine transformations, using QMatrix. You can
|
|---|
| 103 | either pass a matrix to setMatrix(), or you can call one of the
|
|---|
| 104 | convenience functions rotate(), scale(), translate() or shear(). The most
|
|---|
| 105 | two common transformations are scaling, which is used to implement
|
|---|
| 106 | zooming, and rotation. QGraphicsView keeps the center of the view fixed
|
|---|
| 107 | during a transformation.
|
|---|
| 108 |
|
|---|
| 109 | You can interact with the items on the scene by using the mouse and
|
|---|
| 110 | keyboard. QGraphicsView translates the mouse and key events into \e scene
|
|---|
| 111 | events, (events that inherit QGraphicsSceneEvent,), and forward them to
|
|---|
| 112 | the visualized scene. In the end, it's the individual item that handles
|
|---|
| 113 | the events and reacts to them. For example, if you click on a selectable
|
|---|
| 114 | item, the item will typically let the scene know that it has been
|
|---|
| 115 | selected, and it will also redraw itself to display a selection
|
|---|
| 116 | rectangle. Similiary, if you click and drag the mouse to move a movable
|
|---|
| 117 | item, it's the item that handles the mouse moves and moves itself. Item
|
|---|
| 118 | interaction is enabled by default, and you can toggle it by calling
|
|---|
| 119 | setInteractive().
|
|---|
| 120 |
|
|---|
| 121 | You can also provide your own custom scene interaction, by creating a
|
|---|
| 122 | subclass of QGraphicsView, and reimplementing the mouse and key event
|
|---|
| 123 | handlers. To simplify how you programmatically interact with items in the
|
|---|
| 124 | view, QGraphicsView provides the mapping functions mapToScene() and
|
|---|
| 125 | mapFromScene(), and the item accessors items() and itemAt(). These
|
|---|
| 126 | functions allow you to map points, rectangles, polygons and paths between
|
|---|
| 127 | view coordinates and scene coordinates, and to find items on the scene
|
|---|
| 128 | using view coordinates.
|
|---|
| 129 |
|
|---|
| 130 | \img graphicsview-view.png
|
|---|
| 131 |
|
|---|
| 132 | \sa QGraphicsScene, QGraphicsItem, QGraphicsSceneEvent
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | /*!
|
|---|
| 136 | \enum QGraphicsView::ViewportAnchor
|
|---|
| 137 |
|
|---|
| 138 | This enums describe the possible anchors that QGraphicsView can
|
|---|
| 139 | use when the user resizes the view or when the view is
|
|---|
| 140 | transformed.
|
|---|
| 141 |
|
|---|
| 142 | \value NoAnchor No anchor, i.e. the view leaves the scene's
|
|---|
| 143 | position unchanged.
|
|---|
| 144 | \value AnchorViewCenter The scene point at the center of the view
|
|---|
| 145 | is used as the anchor.
|
|---|
| 146 | \value AnchorUnderMouse The point under the mouse is used as the anchor.
|
|---|
| 147 |
|
|---|
| 148 | \sa resizeAnchor, transformationAnchor
|
|---|
| 149 | */
|
|---|
| 150 |
|
|---|
| 151 | /*!
|
|---|
| 152 | \enum QGraphicsView::ViewportUpdateMode
|
|---|
| 153 |
|
|---|
| 154 | \since 4.3
|
|---|
| 155 |
|
|---|
| 156 | This enum describes how QGraphicsView updates its viewport when the scene
|
|---|
| 157 | contents change or are exposed.
|
|---|
| 158 |
|
|---|
| 159 | \value FullViewportUpdate When any visible part of the scene changes or is
|
|---|
| 160 | reexposed, QGraphicsView will update the entire viewport. This approach is
|
|---|
| 161 | fastest when QGraphicsView spends more time figuring out what to draw than
|
|---|
| 162 | it would spend drawing (e.g., when very many small items are repeatedly
|
|---|
| 163 | updated). This is the preferred update mode for viewports that do not
|
|---|
| 164 | support partial updates, such as QGLWidget, and for viewports that need to
|
|---|
| 165 | disable scroll optimization.
|
|---|
| 166 |
|
|---|
| 167 | \value MinimalViewportUpdate QGraphicsView will determine the minimal
|
|---|
| 168 | viewport region that requires a redraw, minimizing the time spent drawing
|
|---|
| 169 | by avoiding a redraw of areas that have not changed. This is
|
|---|
| 170 | QGraphicsView's default mode. Although this approach provides the best
|
|---|
| 171 | performance in general, if there are many small visible changes on the
|
|---|
| 172 | scene, QGraphicsView might end up spending more time finding the minimal
|
|---|
| 173 | approach than it will spend drawing.
|
|---|
| 174 |
|
|---|
| 175 | \value SmartViewportUpdate QGraphicsView will attempt to find an optimal
|
|---|
| 176 | update mode by analyzing the areas that require a redraw.
|
|---|
| 177 |
|
|---|
| 178 | \value BoundingRectViewportUpdate The bounding rectangle of all changes in
|
|---|
| 179 | the viewport will be redrawn. This mode has the advantage that
|
|---|
| 180 | QGraphicsView searches only one region for changes, minimizing time spent
|
|---|
| 181 | determining what needs redrawing. The disadvantage is that areas that have
|
|---|
| 182 | not changed also need to be redrawn.
|
|---|
| 183 |
|
|---|
| 184 | \value NoViewportUpdate QGraphicsView will never update its viewport when
|
|---|
| 185 | the scene changes; the user is expected to control all updates. This mode
|
|---|
| 186 | disables all (potentially slow) item visibility testing in QGraphicsView,
|
|---|
| 187 | and is suitable for scenes that either require a fixed frame rate, or where
|
|---|
| 188 | the viewport is otherwise updated externally.
|
|---|
| 189 |
|
|---|
| 190 | \sa viewportUpdateMode
|
|---|
| 191 | */
|
|---|
| 192 |
|
|---|
| 193 | /*!
|
|---|
| 194 | \enum QGraphicsView::OptimizationFlag
|
|---|
| 195 |
|
|---|
| 196 | \since 4.3
|
|---|
| 197 |
|
|---|
| 198 | This enum describes flags that you can enable to improve rendering
|
|---|
| 199 | performance in QGraphicsView. By default, none of these flags are set.
|
|---|
| 200 | Note that setting a flag usually imposes a side effect, and this effect
|
|---|
| 201 | can vary between paint devices and platforms.
|
|---|
| 202 |
|
|---|
| 203 | \value DontClipPainter QGraphicsView sometimes clips the painter when
|
|---|
| 204 | rendering the scene contents. This can generally improve performance
|
|---|
| 205 | (e.g., rendering only small parts of a large pixmap), and protects against
|
|---|
| 206 | rendering mistakes (e.g., drawing outside bounding rectangles, or outside
|
|---|
| 207 | the exposed area). In some situations, however, the painter clip can slow
|
|---|
| 208 | down rendering; especially when all painting is restricted to inside
|
|---|
| 209 | exposed areas. By enabling this flag, QGraphicsView will completely
|
|---|
| 210 | disable its implicit clipping. Note that rendering artifacts from using a
|
|---|
| 211 | semi-transparent foreground or background brush can occur if clipping is
|
|---|
| 212 | disabled.
|
|---|
| 213 |
|
|---|
| 214 | \value DontSavePainterState When rendering, QGraphicsView protects the
|
|---|
| 215 | painter state (see QPainter::save()) when rendering the background or
|
|---|
| 216 | foreground, and when rendering each item. This allows you to leave the
|
|---|
| 217 | painter in an altered state (i.e., you can call QPainter::setPen() or
|
|---|
| 218 | QPainter::setBrush() without restoring the state after painting). However,
|
|---|
| 219 | if the items consistently do restore the state, you should enable this
|
|---|
| 220 | flag to prevent QGraphicsView from doing the same.
|
|---|
| 221 |
|
|---|
| 222 | \value DontAdjustForAntialiasing Disables QGraphicsView's antialiasing
|
|---|
| 223 | auto-adjustment of exposed areas. Items that render antialiased lines on
|
|---|
| 224 | the boundaries of their QGraphicsItem::boundingRect() can end up rendering
|
|---|
| 225 | parts of the line outside. To prevent rendering artifacts, QGraphicsView
|
|---|
| 226 | expands all exposed regions by 2 pixels in all directions. If you enable
|
|---|
| 227 | this flag, QGraphicsView will no longer perform these adjustments,
|
|---|
| 228 | minimizing the areas that require redrawing, which improves performance. A
|
|---|
| 229 | common side effect is that items that do draw with antialiasing can leave
|
|---|
| 230 | painting traces behind on the scene as they are moved.
|
|---|
| 231 | */
|
|---|
| 232 |
|
|---|
| 233 | /*!
|
|---|
| 234 | \enum QGraphicsView::CacheModeFlag
|
|---|
| 235 |
|
|---|
| 236 | This enum describes the flags that you can set for a QGraphicsView's cache
|
|---|
| 237 | mode.
|
|---|
| 238 |
|
|---|
| 239 | \value CacheNone All painting is done directly onto the viewport.
|
|---|
| 240 |
|
|---|
| 241 | \value CacheBackground The background is cached. This affects both custom
|
|---|
| 242 | backgrounds, and backgrounds based on the backgroundBrush property. When
|
|---|
| 243 | this flag is enabled, QGraphicsView will allocate one pixmap with the full
|
|---|
| 244 | size of the viewport.
|
|---|
| 245 |
|
|---|
| 246 | \sa cacheMode
|
|---|
| 247 | */
|
|---|
| 248 |
|
|---|
| 249 | /*!
|
|---|
| 250 | \enum QGraphicsView::DragMode
|
|---|
| 251 |
|
|---|
| 252 | This enum describes the default action for the view when pressing and
|
|---|
| 253 | dragging the mouse over the viewport.
|
|---|
| 254 |
|
|---|
| 255 | \value NoDrag Nothing happens; the mouse event is ignored.
|
|---|
| 256 |
|
|---|
| 257 | \value ScrollHandDrag The cursor changes into a pointing hand, and
|
|---|
| 258 | dragging the mouse around will scroll the scrolbars. This mode works both
|
|---|
| 259 | in \l{QGraphicsView::interactive}{interactive} and non-interactive mode.
|
|---|
| 260 |
|
|---|
| 261 | \value RubberBandDrag A rubber band will appear. Dragging the mouse will
|
|---|
| 262 | set the rubber band geometry, and all items covered by the rubber band are
|
|---|
| 263 | selected. This mode is disabled for non-interactive views.
|
|---|
| 264 |
|
|---|
| 265 | \sa dragMode, QGraphicsScene::setSelectionArea()
|
|---|
| 266 | */
|
|---|
| 267 |
|
|---|
| 268 | #include "qgraphicsview.h"
|
|---|
| 269 | #include "qgraphicsview_p.h"
|
|---|
| 270 |
|
|---|
| 271 | #ifndef QT_NO_GRAPHICSVIEW
|
|---|
| 272 |
|
|---|
| 273 | #include "qgraphicsitem.h"
|
|---|
| 274 | #include "qgraphicsitem_p.h"
|
|---|
| 275 | #include "qgraphicsscene.h"
|
|---|
| 276 | #include "qgraphicsscene_p.h"
|
|---|
| 277 | #include "qgraphicssceneevent.h"
|
|---|
| 278 | #include "qgraphicswidget.h"
|
|---|
| 279 |
|
|---|
| 280 | #include <QtCore/qdatetime.h>
|
|---|
| 281 | #include <QtCore/qdebug.h>
|
|---|
| 282 | #include <QtCore/qmath.h>
|
|---|
| 283 | #include <QtGui/qapplication.h>
|
|---|
| 284 | #include <QtGui/qdesktopwidget.h>
|
|---|
| 285 | #include <QtGui/qevent.h>
|
|---|
| 286 | #include <QtGui/qlayout.h>
|
|---|
| 287 | #include <QtGui/qtransform.h>
|
|---|
| 288 | #include <QtGui/qmatrix.h>
|
|---|
| 289 | #include <QtGui/qpainter.h>
|
|---|
| 290 | #include <QtGui/qscrollbar.h>
|
|---|
| 291 | #include <QtGui/qstyleoption.h>
|
|---|
| 292 | #ifdef Q_WS_X11
|
|---|
| 293 | #include <private/qt_x11_p.h>
|
|---|
| 294 | #endif
|
|---|
| 295 |
|
|---|
| 296 | QT_BEGIN_NAMESPACE
|
|---|
| 297 |
|
|---|
| 298 | inline int q_round_bound(qreal d) //### (int)(qreal) INT_MAX != INT_MAX for single precision
|
|---|
| 299 | {
|
|---|
| 300 | if (d <= (qreal) INT_MIN)
|
|---|
| 301 | return INT_MIN;
|
|---|
| 302 | else if (d >= (qreal) INT_MAX)
|
|---|
| 303 | return INT_MAX;
|
|---|
| 304 | return d >= 0.0 ? int(d + 0.5) : int(d - int(d-1) + 0.5) + int(d-1);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /*!
|
|---|
| 308 | \internal
|
|---|
| 309 | */
|
|---|
| 310 | QGraphicsViewPrivate::QGraphicsViewPrivate()
|
|---|
| 311 | : renderHints(QPainter::TextAntialiasing),
|
|---|
| 312 | dragMode(QGraphicsView::NoDrag),
|
|---|
| 313 | sceneInteractionAllowed(true), hasSceneRect(false),
|
|---|
| 314 | connectedToScene(false),
|
|---|
| 315 | mousePressButton(Qt::NoButton),
|
|---|
| 316 | identityMatrix(true),
|
|---|
| 317 | dirtyScroll(true),
|
|---|
| 318 | accelerateScrolling(true),
|
|---|
| 319 | leftIndent(0), topIndent(0),
|
|---|
| 320 | lastMouseEvent(QEvent::None, QPoint(), Qt::NoButton, 0, 0),
|
|---|
| 321 | useLastMouseEvent(false),
|
|---|
| 322 | keepLastCenterPoint(true),
|
|---|
| 323 | alignment(Qt::AlignCenter),
|
|---|
| 324 | transforming(false),
|
|---|
| 325 | transformationAnchor(QGraphicsView::AnchorViewCenter), resizeAnchor(QGraphicsView::NoAnchor),
|
|---|
| 326 | viewportUpdateMode(QGraphicsView::MinimalViewportUpdate),
|
|---|
| 327 | optimizationFlags(0),
|
|---|
| 328 | scene(0),
|
|---|
| 329 | #ifndef QT_NO_RUBBERBAND
|
|---|
| 330 | rubberBanding(false),
|
|---|
| 331 | rubberBandSelectionMode(Qt::IntersectsItemShape),
|
|---|
| 332 | #endif
|
|---|
| 333 | handScrolling(false), handScrollMotions(0), cacheMode(0),
|
|---|
| 334 | mustAllocateStyleOptions(false),
|
|---|
| 335 | mustResizeBackgroundPixmap(true),
|
|---|
| 336 | #ifndef QT_NO_CURSOR
|
|---|
| 337 | hasStoredOriginalCursor(false),
|
|---|
| 338 | #endif
|
|---|
| 339 | lastDragDropEvent(0),
|
|---|
| 340 | fullUpdatePending(true),
|
|---|
| 341 | dirtyRectCount(0),
|
|---|
| 342 | updatingLater(false),
|
|---|
| 343 | updateSceneSlotReimplementedChecked(false)
|
|---|
| 344 | {
|
|---|
| 345 | styleOptions.reserve(QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS);
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | /*!
|
|---|
| 349 | \internal
|
|---|
| 350 | */
|
|---|
| 351 | void QGraphicsViewPrivate::recalculateContentSize()
|
|---|
| 352 | {
|
|---|
| 353 | Q_Q(QGraphicsView);
|
|---|
| 354 |
|
|---|
| 355 | QSize maxSize = q->maximumViewportSize();
|
|---|
| 356 | int width = maxSize.width();
|
|---|
| 357 | int height = maxSize.height();
|
|---|
| 358 | QRectF viewRect = matrix.mapRect(q->sceneRect());
|
|---|
| 359 |
|
|---|
| 360 | bool frameOnlyAround = (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, 0, q));
|
|---|
| 361 | if (frameOnlyAround) {
|
|---|
| 362 | if (hbarpolicy == Qt::ScrollBarAlwaysOn)
|
|---|
| 363 | height -= frameWidth * 2;
|
|---|
| 364 | if (vbarpolicy == Qt::ScrollBarAlwaysOn)
|
|---|
| 365 | width -= frameWidth * 2;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | // Adjust the maximum width and height of the viewport based on the width
|
|---|
| 369 | // of visible scroll bars.
|
|---|
| 370 | int scrollBarExtent = q->style()->pixelMetric(QStyle::PM_ScrollBarExtent, 0, q);
|
|---|
| 371 | if (frameOnlyAround)
|
|---|
| 372 | scrollBarExtent += frameWidth * 2;
|
|---|
| 373 |
|
|---|
| 374 | bool useHorizontalScrollBar = (viewRect.width() > width) && hbarpolicy != Qt::ScrollBarAlwaysOff;
|
|---|
| 375 | bool useVerticalScrollBar = (viewRect.height() > height) && vbarpolicy != Qt::ScrollBarAlwaysOff;
|
|---|
| 376 | if (useHorizontalScrollBar && !useVerticalScrollBar) {
|
|---|
| 377 | if (viewRect.height() > height - scrollBarExtent)
|
|---|
| 378 | useVerticalScrollBar = true;
|
|---|
| 379 | }
|
|---|
| 380 | if (useVerticalScrollBar && !useHorizontalScrollBar) {
|
|---|
| 381 | if (viewRect.width() > width - scrollBarExtent)
|
|---|
| 382 | useHorizontalScrollBar = true;
|
|---|
| 383 | }
|
|---|
| 384 | if (useHorizontalScrollBar && hbarpolicy != Qt::ScrollBarAlwaysOn)
|
|---|
| 385 | height -= scrollBarExtent;
|
|---|
| 386 | if (useVerticalScrollBar && vbarpolicy != Qt::ScrollBarAlwaysOn)
|
|---|
| 387 | width -= scrollBarExtent;
|
|---|
| 388 |
|
|---|
| 389 | // Setting the ranges of these scroll bars can/will cause the values to
|
|---|
| 390 | // change, and scrollContentsBy() will be called correspondingly. This
|
|---|
| 391 | // will reset the last center point.
|
|---|
| 392 | QPointF savedLastCenterPoint = lastCenterPoint;
|
|---|
| 393 |
|
|---|
| 394 | // Remember the former indent settings
|
|---|
| 395 | qreal oldLeftIndent = leftIndent;
|
|---|
| 396 | qreal oldTopIndent = topIndent;
|
|---|
| 397 |
|
|---|
| 398 | // If the whole scene fits horizontally, we center the scene horizontally,
|
|---|
| 399 | // and ignore the horizontal scroll bars.
|
|---|
| 400 | int left = q_round_bound(viewRect.left());
|
|---|
| 401 | int right = q_round_bound(viewRect.right() - width);
|
|---|
| 402 | if (left >= right) {
|
|---|
| 403 | q->horizontalScrollBar()->setRange(0, 0);
|
|---|
| 404 |
|
|---|
| 405 | switch (alignment & Qt::AlignHorizontal_Mask) {
|
|---|
| 406 | case Qt::AlignLeft:
|
|---|
| 407 | leftIndent = -viewRect.left();
|
|---|
| 408 | break;
|
|---|
| 409 | case Qt::AlignRight:
|
|---|
| 410 | leftIndent = width - viewRect.width() - viewRect.left() - 1;
|
|---|
| 411 | break;
|
|---|
| 412 | case Qt::AlignHCenter:
|
|---|
| 413 | default:
|
|---|
| 414 | leftIndent = width / 2 - (viewRect.left() + viewRect.right()) / 2;
|
|---|
| 415 | break;
|
|---|
| 416 | }
|
|---|
| 417 | } else {
|
|---|
| 418 | q->horizontalScrollBar()->setRange(left, right);
|
|---|
| 419 | q->horizontalScrollBar()->setPageStep(width);
|
|---|
| 420 | q->horizontalScrollBar()->setSingleStep(width / 20);
|
|---|
| 421 | leftIndent = 0;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | // If the whole scene fits vertically, we center the scene vertically, and
|
|---|
| 425 | // ignore the vertical scroll bars.
|
|---|
| 426 | int top = q_round_bound(viewRect.top());
|
|---|
| 427 | int bottom = q_round_bound(viewRect.bottom() - height);
|
|---|
| 428 | if (top >= bottom) {
|
|---|
| 429 | q->verticalScrollBar()->setRange(0, 0);
|
|---|
| 430 |
|
|---|
| 431 | switch (alignment & Qt::AlignVertical_Mask) {
|
|---|
| 432 | case Qt::AlignTop:
|
|---|
| 433 | topIndent = -viewRect.top();
|
|---|
| 434 | break;
|
|---|
| 435 | case Qt::AlignBottom:
|
|---|
| 436 | topIndent = height - viewRect.height() - viewRect.top() - 1;
|
|---|
| 437 | break;
|
|---|
| 438 | case Qt::AlignVCenter:
|
|---|
| 439 | default:
|
|---|
| 440 | topIndent = height / 2 - (viewRect.top() + viewRect.bottom()) / 2;
|
|---|
| 441 | break;
|
|---|
| 442 | }
|
|---|
| 443 | } else {
|
|---|
| 444 | q->verticalScrollBar()->setRange(top, bottom);
|
|---|
| 445 | q->verticalScrollBar()->setPageStep(height);
|
|---|
| 446 | q->verticalScrollBar()->setSingleStep(height / 20);
|
|---|
| 447 | topIndent = 0;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | // Restorethe center point from before the ranges changed.
|
|---|
| 451 | lastCenterPoint = savedLastCenterPoint;
|
|---|
| 452 |
|
|---|
| 453 | // Issue a full update if the indents change.
|
|---|
| 454 | // ### If the transform is still the same, we can get away with just a
|
|---|
| 455 | // scroll instead.
|
|---|
| 456 | if (oldLeftIndent != leftIndent || oldTopIndent != topIndent) {
|
|---|
| 457 | dirtyScroll = true;
|
|---|
| 458 | q->viewport()->update();
|
|---|
| 459 | } else if (q->isRightToLeft() && !leftIndent) {
|
|---|
| 460 | // In reverse mode, the horizontal scroll always changes after the content
|
|---|
| 461 | // size has changed, as the scroll is calculated by summing the min and
|
|---|
| 462 | // max values of the range and subtracting the current value. In normal
|
|---|
| 463 | // mode the scroll remains unchanged unless the indent has changed.
|
|---|
| 464 | dirtyScroll = true;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | if (cacheMode & QGraphicsView::CacheBackground) {
|
|---|
| 468 | // Invalidate the background pixmap
|
|---|
| 469 | mustResizeBackgroundPixmap = true;
|
|---|
| 470 | }
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /*!
|
|---|
| 474 | \internal
|
|---|
| 475 | */
|
|---|
| 476 | void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor)
|
|---|
| 477 | {
|
|---|
| 478 | Q_Q(QGraphicsView);
|
|---|
| 479 | switch (anchor) {
|
|---|
| 480 | case QGraphicsView::AnchorUnderMouse: {
|
|---|
| 481 | if (q->underMouse()) {
|
|---|
| 482 | // Last scene pos: lastMouseMoveScenePoint
|
|---|
| 483 | // Current mouse pos:
|
|---|
| 484 | QPointF transformationDiff = q->mapToScene(q->viewport()->rect().center())
|
|---|
| 485 | - q->mapToScene(q->mapFromGlobal(QCursor::pos()));
|
|---|
| 486 | q->centerOn(lastMouseMoveScenePoint + transformationDiff);;
|
|---|
| 487 | } else {
|
|---|
| 488 | q->centerOn(lastCenterPoint);
|
|---|
| 489 | }
|
|---|
| 490 | break;
|
|---|
| 491 | }
|
|---|
| 492 | case QGraphicsView::AnchorViewCenter:
|
|---|
| 493 | q->centerOn(lastCenterPoint);
|
|---|
| 494 | break;
|
|---|
| 495 | case QGraphicsView::NoAnchor:
|
|---|
| 496 | break;
|
|---|
| 497 | }
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | /*!
|
|---|
| 501 | \internal
|
|---|
| 502 | */
|
|---|
| 503 | void QGraphicsViewPrivate::updateLastCenterPoint()
|
|---|
| 504 | {
|
|---|
| 505 | Q_Q(QGraphicsView);
|
|---|
| 506 | lastCenterPoint = q->mapToScene(q->viewport()->rect().center());
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | /*!
|
|---|
| 510 | \internal
|
|---|
| 511 |
|
|---|
| 512 | Returns the horizontal scroll value (the X value of the left edge of the
|
|---|
| 513 | viewport).
|
|---|
| 514 | */
|
|---|
| 515 | qint64 QGraphicsViewPrivate::horizontalScroll() const
|
|---|
| 516 | {
|
|---|
| 517 | if (dirtyScroll)
|
|---|
| 518 | const_cast<QGraphicsViewPrivate *>(this)->updateScroll();
|
|---|
| 519 | return scrollX;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 | /*!
|
|---|
| 523 | \internal
|
|---|
| 524 |
|
|---|
| 525 | Returns the vertical scroll value (the X value of the top edge of the
|
|---|
| 526 | viewport).
|
|---|
| 527 | */
|
|---|
| 528 | qint64 QGraphicsViewPrivate::verticalScroll() const
|
|---|
| 529 | {
|
|---|
| 530 | if (dirtyScroll)
|
|---|
| 531 | const_cast<QGraphicsViewPrivate *>(this)->updateScroll();
|
|---|
| 532 | return scrollY;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | /*!
|
|---|
| 536 | \internal
|
|---|
| 537 | */
|
|---|
| 538 | void QGraphicsViewPrivate::updateScroll()
|
|---|
| 539 | {
|
|---|
| 540 | Q_Q(QGraphicsView);
|
|---|
| 541 | scrollX = qint64(-leftIndent);
|
|---|
| 542 | if (q->isRightToLeft()) {
|
|---|
| 543 | if (!leftIndent) {
|
|---|
| 544 | scrollX += q->horizontalScrollBar()->minimum();
|
|---|
| 545 | scrollX += q->horizontalScrollBar()->maximum();
|
|---|
| 546 | scrollX -= q->horizontalScrollBar()->value();
|
|---|
| 547 | }
|
|---|
| 548 | } else {
|
|---|
| 549 | scrollX += q->horizontalScrollBar()->value();
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | scrollY = qint64(q->verticalScrollBar()->value() - topIndent);
|
|---|
| 553 |
|
|---|
| 554 | dirtyScroll = false;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | /*!
|
|---|
| 558 | \internal
|
|---|
| 559 | */
|
|---|
| 560 | void QGraphicsViewPrivate::replayLastMouseEvent()
|
|---|
| 561 | {
|
|---|
| 562 | if (!useLastMouseEvent || !scene)
|
|---|
| 563 | return;
|
|---|
| 564 | mouseMoveEventHandler(&lastMouseEvent);
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | /*!
|
|---|
| 568 | \internal
|
|---|
| 569 | */
|
|---|
| 570 | void QGraphicsViewPrivate::storeMouseEvent(QMouseEvent *event)
|
|---|
| 571 | {
|
|---|
| 572 | useLastMouseEvent = true;
|
|---|
| 573 | lastMouseEvent = QMouseEvent(QEvent::MouseMove, event->pos(), event->globalPos(),
|
|---|
| 574 | event->button(), event->buttons(), event->modifiers());
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | void QGraphicsViewPrivate::mouseMoveEventHandler(QMouseEvent *event)
|
|---|
| 578 | {
|
|---|
| 579 | Q_Q(QGraphicsView);
|
|---|
| 580 |
|
|---|
| 581 | storeMouseEvent(event);
|
|---|
| 582 | lastMouseEvent.setAccepted(false);
|
|---|
| 583 |
|
|---|
| 584 | if (!sceneInteractionAllowed)
|
|---|
| 585 | return;
|
|---|
| 586 | if (handScrolling)
|
|---|
| 587 | return;
|
|---|
| 588 | if (!scene)
|
|---|
| 589 | return;
|
|---|
| 590 |
|
|---|
| 591 | QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
|
|---|
| 592 | mouseEvent.setWidget(q->viewport());
|
|---|
| 593 | mouseEvent.setButtonDownScenePos(mousePressButton, mousePressScenePoint);
|
|---|
| 594 | mouseEvent.setButtonDownScreenPos(mousePressButton, mousePressScreenPoint);
|
|---|
| 595 | mouseEvent.setScenePos(q->mapToScene(event->pos()));
|
|---|
| 596 | mouseEvent.setScreenPos(event->globalPos());
|
|---|
| 597 | mouseEvent.setLastScenePos(lastMouseMoveScenePoint);
|
|---|
| 598 | mouseEvent.setLastScreenPos(lastMouseMoveScreenPoint);
|
|---|
| 599 | mouseEvent.setButtons(event->buttons());
|
|---|
| 600 | mouseEvent.setButton(event->button());
|
|---|
| 601 | mouseEvent.setModifiers(event->modifiers());
|
|---|
| 602 | lastMouseMoveScenePoint = mouseEvent.scenePos();
|
|---|
| 603 | lastMouseMoveScreenPoint = mouseEvent.screenPos();
|
|---|
| 604 | mouseEvent.setAccepted(false);
|
|---|
| 605 | QApplication::sendEvent(scene, &mouseEvent);
|
|---|
| 606 |
|
|---|
| 607 | // Remember whether the last event was accepted or not.
|
|---|
| 608 | lastMouseEvent.setAccepted(mouseEvent.isAccepted());
|
|---|
| 609 |
|
|---|
| 610 | if (mouseEvent.isAccepted() && mouseEvent.buttons() != 0) {
|
|---|
| 611 | // The event was delivered to a mouse grabber; the press is likely to
|
|---|
| 612 | // have set a cursor, and we must not change it.
|
|---|
| 613 | return;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | #ifndef QT_NO_CURSOR
|
|---|
| 617 | // Find the topmost item under the mouse with a cursor.
|
|---|
| 618 | foreach (QGraphicsItem *item, scene->d_func()->cachedItemsUnderMouse) {
|
|---|
| 619 | if (item->hasCursor()) {
|
|---|
| 620 | _q_setViewportCursor(item->cursor());
|
|---|
| 621 | return;
|
|---|
| 622 | }
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | // No items with cursors found; revert to the view cursor.
|
|---|
| 626 | if (hasStoredOriginalCursor) {
|
|---|
| 627 | // Restore the original viewport cursor.
|
|---|
| 628 | hasStoredOriginalCursor = false;
|
|---|
| 629 | q->viewport()->setCursor(originalCursor);
|
|---|
| 630 | }
|
|---|
| 631 | #endif
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | /*!
|
|---|
| 635 | \internal
|
|---|
| 636 | */
|
|---|
| 637 | #ifndef QT_NO_RUBBERBAND
|
|---|
| 638 | QRegion QGraphicsViewPrivate::rubberBandRegion(const QWidget *widget, const QRect &rect) const
|
|---|
| 639 | {
|
|---|
| 640 | QStyleHintReturnMask mask;
|
|---|
| 641 | QStyleOptionRubberBand option;
|
|---|
| 642 | option.initFrom(widget);
|
|---|
| 643 | option.rect = rect;
|
|---|
| 644 | option.opaque = false;
|
|---|
| 645 | option.shape = QRubberBand::Rectangle;
|
|---|
| 646 |
|
|---|
| 647 | QRegion tmp;
|
|---|
| 648 | tmp += rect;
|
|---|
| 649 | if (widget->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, widget, &mask))
|
|---|
| 650 | tmp &= mask.region;
|
|---|
| 651 | return tmp;
|
|---|
| 652 | }
|
|---|
| 653 | #endif
|
|---|
| 654 |
|
|---|
| 655 | /*!
|
|---|
| 656 | \internal
|
|---|
| 657 | */
|
|---|
| 658 | #ifndef QT_NO_CURSOR
|
|---|
| 659 | void QGraphicsViewPrivate::_q_setViewportCursor(const QCursor &cursor)
|
|---|
| 660 | {
|
|---|
| 661 | Q_Q(QGraphicsView);
|
|---|
| 662 | QWidget *viewport = q->viewport();
|
|---|
| 663 | if (!hasStoredOriginalCursor) {
|
|---|
| 664 | hasStoredOriginalCursor = true;
|
|---|
| 665 | originalCursor = viewport->cursor();
|
|---|
| 666 | }
|
|---|
| 667 | viewport->setCursor(cursor);
|
|---|
| 668 | }
|
|---|
| 669 | #endif
|
|---|
| 670 |
|
|---|
| 671 | /*!
|
|---|
| 672 | \internal
|
|---|
| 673 | */
|
|---|
| 674 | #ifndef QT_NO_CURSOR
|
|---|
| 675 | void QGraphicsViewPrivate::_q_unsetViewportCursor()
|
|---|
| 676 | {
|
|---|
| 677 | Q_Q(QGraphicsView);
|
|---|
| 678 | foreach (QGraphicsItem *item, q->items(lastMouseEvent.pos())) {
|
|---|
| 679 | if (item->hasCursor()) {
|
|---|
| 680 | _q_setViewportCursor(item->cursor());
|
|---|
| 681 | return;
|
|---|
| 682 | }
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | // Restore the original viewport cursor.
|
|---|
| 686 | hasStoredOriginalCursor = false;
|
|---|
| 687 | if (dragMode == QGraphicsView::ScrollHandDrag)
|
|---|
| 688 | q->viewport()->setCursor(Qt::OpenHandCursor);
|
|---|
| 689 | else
|
|---|
| 690 | q->viewport()->setCursor(originalCursor);
|
|---|
| 691 | }
|
|---|
| 692 | #endif
|
|---|
| 693 |
|
|---|
| 694 | /*!
|
|---|
| 695 | \internal
|
|---|
| 696 | */
|
|---|
| 697 | void QGraphicsViewPrivate::storeDragDropEvent(const QGraphicsSceneDragDropEvent *event)
|
|---|
| 698 | {
|
|---|
| 699 | delete lastDragDropEvent;
|
|---|
| 700 | lastDragDropEvent = new QGraphicsSceneDragDropEvent(event->type());
|
|---|
| 701 | lastDragDropEvent->setScenePos(event->scenePos());
|
|---|
| 702 | lastDragDropEvent->setScreenPos(event->screenPos());
|
|---|
| 703 | lastDragDropEvent->setButtons(event->buttons());
|
|---|
| 704 | lastDragDropEvent->setModifiers(event->modifiers());
|
|---|
| 705 | lastDragDropEvent->setPossibleActions(event->possibleActions());
|
|---|
| 706 | lastDragDropEvent->setProposedAction(event->proposedAction());
|
|---|
| 707 | lastDragDropEvent->setDropAction(event->dropAction());
|
|---|
| 708 | lastDragDropEvent->setMimeData(event->mimeData());
|
|---|
| 709 | lastDragDropEvent->setWidget(event->widget());
|
|---|
| 710 | lastDragDropEvent->setSource(event->source());
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | /*!
|
|---|
| 714 | \internal
|
|---|
| 715 | */
|
|---|
| 716 | void QGraphicsViewPrivate::populateSceneDragDropEvent(QGraphicsSceneDragDropEvent *dest,
|
|---|
| 717 | QDropEvent *source)
|
|---|
| 718 | {
|
|---|
| 719 | #ifndef QT_NO_DRAGANDDROP
|
|---|
| 720 | Q_Q(QGraphicsView);
|
|---|
| 721 | dest->setScenePos(q->mapToScene(source->pos()));
|
|---|
| 722 | dest->setScreenPos(q->mapToGlobal(source->pos()));
|
|---|
| 723 | dest->setButtons(source->mouseButtons());
|
|---|
| 724 | dest->setModifiers(source->keyboardModifiers());
|
|---|
| 725 | dest->setPossibleActions(source->possibleActions());
|
|---|
| 726 | dest->setProposedAction(source->proposedAction());
|
|---|
| 727 | dest->setDropAction(source->dropAction());
|
|---|
| 728 | dest->setMimeData(source->mimeData());
|
|---|
| 729 | dest->setWidget(q->viewport());
|
|---|
| 730 | dest->setSource(source->source());
|
|---|
| 731 | #else
|
|---|
| 732 | Q_UNUSED(dest)
|
|---|
| 733 | Q_UNUSED(source)
|
|---|
| 734 | #endif
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | /*!
|
|---|
| 738 | \internal
|
|---|
| 739 | */
|
|---|
| 740 | QRect QGraphicsViewPrivate::mapToViewRect(const QGraphicsItem *item, const QRectF &rect) const
|
|---|
| 741 | {
|
|---|
| 742 | Q_Q(const QGraphicsView);
|
|---|
| 743 | if (dirtyScroll)
|
|---|
| 744 | const_cast<QGraphicsViewPrivate *>(this)->updateScroll();
|
|---|
| 745 |
|
|---|
| 746 | if (item->d_ptr->itemIsUntransformable()) {
|
|---|
| 747 | QTransform itv = item->deviceTransform(q->viewportTransform());
|
|---|
| 748 | return itv.mapRect(rect).toAlignedRect();
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | // Translate-only
|
|---|
| 752 | QPointF offset;
|
|---|
| 753 | const QGraphicsItem *parentItem = item;
|
|---|
| 754 | const QGraphicsItemPrivate *itemd;
|
|---|
| 755 | do {
|
|---|
| 756 | itemd = parentItem->d_ptr;
|
|---|
| 757 | if (itemd->hasTransform)
|
|---|
| 758 | break;
|
|---|
| 759 | offset += itemd->pos;
|
|---|
| 760 | } while ((parentItem = itemd->parent));
|
|---|
| 761 |
|
|---|
| 762 | QRectF baseRect = rect.translated(offset.x(), offset.y());
|
|---|
| 763 | if (!parentItem) {
|
|---|
| 764 | if (identityMatrix) {
|
|---|
| 765 | baseRect.translate(-scrollX, -scrollY);
|
|---|
| 766 | return baseRect.toAlignedRect();
|
|---|
| 767 | }
|
|---|
| 768 | return matrix.mapRect(baseRect).translated(-scrollX, -scrollY).toAlignedRect();
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | QTransform tr = parentItem->sceneTransform();
|
|---|
| 772 | if (!identityMatrix)
|
|---|
| 773 | tr *= matrix;
|
|---|
| 774 | QRectF r = tr.mapRect(baseRect);
|
|---|
| 775 | r.translate(-scrollX, -scrollY);
|
|---|
| 776 | return r.toAlignedRect();
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | /*!
|
|---|
| 780 | \internal
|
|---|
| 781 | */
|
|---|
| 782 | QRegion QGraphicsViewPrivate::mapToViewRegion(const QGraphicsItem *item, const QRectF &rect) const
|
|---|
| 783 | {
|
|---|
| 784 | Q_Q(const QGraphicsView);
|
|---|
| 785 | if (dirtyScroll)
|
|---|
| 786 | const_cast<QGraphicsViewPrivate *>(this)->updateScroll();
|
|---|
| 787 |
|
|---|
| 788 | // Accurate bounding region
|
|---|
| 789 | QTransform itv = item->sceneTransform() * q->viewportTransform();
|
|---|
| 790 | return item->boundingRegion(itv) & itv.mapRect(rect).toAlignedRect();
|
|---|
| 791 | }
|
|---|
| 792 |
|
|---|
| 793 | // QRectF::intersects() returns false always if either the source or target
|
|---|
| 794 | // rectangle's width or height are 0. This works around that problem.
|
|---|
| 795 | static inline QRectF adjustedItemBoundingRect(const QGraphicsItem *item)
|
|---|
| 796 | {
|
|---|
| 797 | Q_ASSERT(item);
|
|---|
| 798 | QRectF boundingRect(item->boundingRect());
|
|---|
| 799 | if (!boundingRect.width())
|
|---|
| 800 | boundingRect.adjust(-0.00001, 0, 0.00001, 0);
|
|---|
| 801 | if (!boundingRect.height())
|
|---|
| 802 | boundingRect.adjust(0, -0.00001, 0, 0.00001);
|
|---|
| 803 | return boundingRect;
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | /*!
|
|---|
| 807 | \internal
|
|---|
| 808 | */
|
|---|
| 809 | void QGraphicsViewPrivate::itemUpdated(QGraphicsItem *item, const QRectF &rect)
|
|---|
| 810 | {
|
|---|
| 811 | if (fullUpdatePending || viewportUpdateMode == QGraphicsView::NoViewportUpdate)
|
|---|
| 812 | return;
|
|---|
| 813 | if (item->d_ptr->dirty)
|
|---|
| 814 | updateLater();
|
|---|
| 815 |
|
|---|
| 816 | QRectF updateRect = rect;
|
|---|
| 817 | if ((item->d_ptr->flags & QGraphicsItem::ItemClipsChildrenToShape) || item->d_ptr->children.isEmpty()) {
|
|---|
| 818 | updateRect &= adjustedItemBoundingRect(item);
|
|---|
| 819 | if (updateRect.isEmpty())
|
|---|
| 820 | return;
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|
| 823 | QGraphicsItem *clipItem = item;
|
|---|
| 824 | if (item->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) {
|
|---|
| 825 | // Minimize unnecessary redraw.
|
|---|
| 826 | QGraphicsItem *parent = item;
|
|---|
| 827 | while ((parent = parent->d_ptr->parent)) {
|
|---|
| 828 | if (parent->d_ptr->flags & QGraphicsItem::ItemClipsChildrenToShape) {
|
|---|
| 829 | // Map update rect to the current parent and itersect with its bounding rect.
|
|---|
| 830 | updateRect = clipItem->itemTransform(parent).mapRect(updateRect)
|
|---|
| 831 | & adjustedItemBoundingRect(parent);
|
|---|
| 832 | if (updateRect.isEmpty())
|
|---|
| 833 | return;
|
|---|
| 834 | clipItem = parent;
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | if (!(parent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren))
|
|---|
| 838 | break;
|
|---|
| 839 | }
|
|---|
| 840 | }
|
|---|
| 841 |
|
|---|
| 842 | // Map update rect from clipItem coordinates to view coordinates.
|
|---|
| 843 | Q_ASSERT(clipItem);
|
|---|
| 844 | if (!item->d_ptr->hasBoundingRegionGranularity)
|
|---|
| 845 | this->updateRect(mapToViewRect(clipItem, updateRect) & viewport->rect());
|
|---|
| 846 | else
|
|---|
| 847 | updateRegion(mapToViewRegion(clipItem, updateRect) & viewport->rect());
|
|---|
| 848 | }
|
|---|
| 849 |
|
|---|
| 850 | void QGraphicsViewPrivate::updateLater()
|
|---|
| 851 | {
|
|---|
| 852 | Q_Q(QGraphicsView);
|
|---|
| 853 | if (updatingLater)
|
|---|
| 854 | return;
|
|---|
| 855 | updatingLater = true;
|
|---|
| 856 | QMetaObject::invokeMethod(q, "_q_updateLaterSlot", Qt::QueuedConnection);
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | void QGraphicsViewPrivate::_q_updateLaterSlot()
|
|---|
| 860 | {
|
|---|
| 861 | Q_Q(QGraphicsView);
|
|---|
| 862 | if (!scene)
|
|---|
| 863 | return;
|
|---|
| 864 |
|
|---|
| 865 | QRect vr = viewport->rect();
|
|---|
| 866 | QTransform viewTransform = q->viewportTransform();
|
|---|
| 867 | const QList<QGraphicsItem *> &dirtyItems = scene->d_func()->dirtyItems;
|
|---|
| 868 | for (int i = 0; i < dirtyItems.size(); ++i) {
|
|---|
| 869 | const QGraphicsItem *item = dirtyItems.at(i);
|
|---|
| 870 | if (item->d_ptr->discardUpdateRequest(/*ignoreClipping=*/false,
|
|---|
| 871 | /*ignoreVisibleBit=*/false,
|
|---|
| 872 | /*ignoreDirtyBit=*/true)) {
|
|---|
| 873 | continue;
|
|---|
| 874 | }
|
|---|
| 875 | QTransform x = item->sceneTransform() * viewTransform;
|
|---|
| 876 | updateRect(x.mapRect(item->boundingRect()).toAlignedRect() & vr);
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | dirtyRectCount += dirtyRects.size();
|
|---|
| 880 |
|
|---|
| 881 | bool noUpdate = !fullUpdatePending && viewportUpdateMode == QGraphicsView::FullViewportUpdate;
|
|---|
| 882 | if ((dirtyRectCount > 0 || !dirtyBoundingRect.isEmpty()) && !fullUpdatePending && !noUpdate) {
|
|---|
| 883 | if (viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate
|
|---|
| 884 | || (viewportUpdateMode == QGraphicsView::SmartViewportUpdate
|
|---|
| 885 | && dirtyRectCount >= QGRAPHICSVIEW_REGION_RECT_THRESHOLD)) {
|
|---|
| 886 | if (!(optimizationFlags & QGraphicsView::DontAdjustForAntialiasing)) {
|
|---|
| 887 | viewport->update(dirtyBoundingRect.adjusted(-2, -2, 2, 2));
|
|---|
| 888 | } else {
|
|---|
| 889 | viewport->update(dirtyBoundingRect);
|
|---|
| 890 | }
|
|---|
| 891 | } else {
|
|---|
| 892 | // ### Improve this block, which is very slow for complex regions. We
|
|---|
| 893 | // need to strike the balance between having an accurate update
|
|---|
| 894 | // region, and running fast. The below approach is the simplest way to
|
|---|
| 895 | // create a region from a bunch of rects, but we might want to use
|
|---|
| 896 | // other approaches; e.g., a grid of a fixed size representing
|
|---|
| 897 | // quadrants of the viewport, which we mark as dirty depending on the
|
|---|
| 898 | // rectangles in the list. Perhaps this should go into a
|
|---|
| 899 | // QRegion::fromRects(rects, how) function.
|
|---|
| 900 | QRegion region;
|
|---|
| 901 | if (!(optimizationFlags & QGraphicsView::DontAdjustForAntialiasing)) {
|
|---|
| 902 | for (int i = 0; i < dirtyRegions.size(); ++i) {
|
|---|
| 903 | QVector<QRect> rects = dirtyRegions.at(i).rects();
|
|---|
| 904 | for (int j = 0; j < rects.size(); ++j)
|
|---|
| 905 | region += rects.at(j).adjusted(-2, -2, 2, 2);
|
|---|
| 906 | }
|
|---|
| 907 | for (int i = 0; i < dirtyRects.size(); ++i)
|
|---|
| 908 | region += dirtyRects.at(i).adjusted(-2, -2, 2, 2);
|
|---|
| 909 | } else {
|
|---|
| 910 | for (int i = 0; i < dirtyRegions.size(); ++i)
|
|---|
| 911 | region += dirtyRegions.at(i);
|
|---|
| 912 | for (int i = 0; i < dirtyRects.size(); ++i)
|
|---|
| 913 | region += dirtyRects.at(i);
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | viewport->update(region);
|
|---|
| 917 | }
|
|---|
| 918 | }
|
|---|
| 919 |
|
|---|
| 920 | dirtyRegions.clear();
|
|---|
| 921 | dirtyRects.clear();
|
|---|
| 922 | dirtyRectCount = 0;
|
|---|
| 923 | dirtyBoundingRect = QRect();
|
|---|
| 924 | updatingLater = false;
|
|---|
| 925 | }
|
|---|
| 926 |
|
|---|
| 927 | void QGraphicsViewPrivate::updateAll()
|
|---|
| 928 | {
|
|---|
| 929 | Q_Q(QGraphicsView);
|
|---|
| 930 | q->viewport()->update();
|
|---|
| 931 | fullUpdatePending = true;
|
|---|
| 932 | dirtyRectCount = 0;
|
|---|
| 933 | dirtyBoundingRect = QRect();
|
|---|
| 934 | updatingLater = false;
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | void QGraphicsViewPrivate::updateRegion(const QRegion &r)
|
|---|
| 938 | {
|
|---|
| 939 | if (r.isEmpty())
|
|---|
| 940 | return;
|
|---|
| 941 |
|
|---|
| 942 | Q_Q(QGraphicsView);
|
|---|
| 943 |
|
|---|
| 944 | // Rect intersects viewport - update everything?
|
|---|
| 945 | switch (viewportUpdateMode) {
|
|---|
| 946 | case QGraphicsView::FullViewportUpdate:
|
|---|
| 947 | fullUpdatePending = true;
|
|---|
| 948 | q->viewport()->update();
|
|---|
| 949 | break;
|
|---|
| 950 | case QGraphicsView::BoundingRectViewportUpdate:
|
|---|
| 951 | dirtyBoundingRect |= r.boundingRect();
|
|---|
| 952 | if (dirtyBoundingRect == q->viewport()->rect()) {
|
|---|
| 953 | fullUpdatePending = true;
|
|---|
| 954 | q->viewport()->update();
|
|---|
| 955 | } else {
|
|---|
| 956 | updateLater();
|
|---|
| 957 | }
|
|---|
| 958 | break;
|
|---|
| 959 | case QGraphicsView::SmartViewportUpdate:
|
|---|
| 960 | dirtyBoundingRect |= r.boundingRect();
|
|---|
| 961 | if ((dirtyRectCount + r.numRects()) < QGRAPHICSVIEW_REGION_RECT_THRESHOLD)
|
|---|
| 962 | dirtyRegions << r;
|
|---|
| 963 | dirtyRectCount += r.numRects();
|
|---|
| 964 | updateLater();
|
|---|
| 965 | break;
|
|---|
| 966 | case QGraphicsView::MinimalViewportUpdate:
|
|---|
| 967 | dirtyRegions << r;
|
|---|
| 968 | dirtyRectCount += r.numRects();
|
|---|
| 969 | updateLater();
|
|---|
| 970 | break;
|
|---|
| 971 | case QGraphicsView::NoViewportUpdate:
|
|---|
| 972 | // Unreachable
|
|---|
| 973 | break;
|
|---|
| 974 | }
|
|---|
| 975 |
|
|---|
| 976 | // Compress the regions...
|
|---|
| 977 | if (dirtyRectCount > QGRAPHICSVIEW_REGION_RECT_THRESHOLD && dirtyRegions.size() > 1) {
|
|---|
| 978 | QRegion masterRegion;
|
|---|
| 979 | for (int i=0; i<dirtyRegions.size(); ++i) {
|
|---|
| 980 | masterRegion |= dirtyRegions.at(i);
|
|---|
| 981 | }
|
|---|
| 982 | dirtyRectCount = masterRegion.numRects();
|
|---|
| 983 | dirtyRegions.clear();
|
|---|
| 984 | dirtyRegions << masterRegion;
|
|---|
| 985 | }
|
|---|
| 986 | }
|
|---|
| 987 |
|
|---|
| 988 | void QGraphicsViewPrivate::updateRect(const QRect &r)
|
|---|
| 989 | {
|
|---|
| 990 | if (r.isEmpty())
|
|---|
| 991 | return;
|
|---|
| 992 |
|
|---|
| 993 | Q_Q(QGraphicsView);
|
|---|
| 994 |
|
|---|
| 995 | // Rect intersects viewport - update everything?
|
|---|
| 996 | switch (viewportUpdateMode) {
|
|---|
| 997 | case QGraphicsView::FullViewportUpdate:
|
|---|
| 998 | fullUpdatePending = true;
|
|---|
| 999 | q->viewport()->update();
|
|---|
| 1000 | break;
|
|---|
| 1001 | case QGraphicsView::BoundingRectViewportUpdate:
|
|---|
| 1002 | dirtyBoundingRect |= r;
|
|---|
| 1003 | if (dirtyBoundingRect == q->viewport()->rect()) {
|
|---|
| 1004 | fullUpdatePending = true;
|
|---|
| 1005 | q->viewport()->update();
|
|---|
| 1006 | } else {
|
|---|
| 1007 | updateLater();
|
|---|
| 1008 | }
|
|---|
| 1009 | break;
|
|---|
| 1010 | case QGraphicsView::SmartViewportUpdate:
|
|---|
| 1011 | dirtyBoundingRect |= r;
|
|---|
| 1012 | if ((dirtyRectCount + dirtyRects.size()) < QGRAPHICSVIEW_REGION_RECT_THRESHOLD)
|
|---|
| 1013 | dirtyRects << r;
|
|---|
| 1014 | updateLater();
|
|---|
| 1015 | break;
|
|---|
| 1016 | case QGraphicsView::MinimalViewportUpdate:
|
|---|
| 1017 | dirtyRects << r;
|
|---|
| 1018 | updateLater();
|
|---|
| 1019 | break;
|
|---|
| 1020 | case QGraphicsView::NoViewportUpdate:
|
|---|
| 1021 | // Unreachable
|
|---|
| 1022 | break;
|
|---|
| 1023 | }
|
|---|
| 1024 | }
|
|---|
| 1025 |
|
|---|
| 1026 | QStyleOptionGraphicsItem *QGraphicsViewPrivate::allocStyleOptionsArray(int numItems)
|
|---|
| 1027 | {
|
|---|
| 1028 | if (mustAllocateStyleOptions || (numItems > styleOptions.capacity()))
|
|---|
| 1029 | // too many items, let's allocate on-the-fly
|
|---|
| 1030 | return new QStyleOptionGraphicsItem[numItems];
|
|---|
| 1031 |
|
|---|
| 1032 | // expand only whenever necessary
|
|---|
| 1033 | if (numItems > styleOptions.size())
|
|---|
| 1034 | styleOptions.resize(numItems);
|
|---|
| 1035 |
|
|---|
| 1036 | mustAllocateStyleOptions = true;
|
|---|
| 1037 | return styleOptions.data();
|
|---|
| 1038 | }
|
|---|
| 1039 |
|
|---|
| 1040 | void QGraphicsViewPrivate::freeStyleOptionsArray(QStyleOptionGraphicsItem *array)
|
|---|
| 1041 | {
|
|---|
| 1042 | mustAllocateStyleOptions = false;
|
|---|
| 1043 | if (array != styleOptions.data())
|
|---|
| 1044 | delete [] array;
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | extern QPainterPath qt_regionToPath(const QRegion ®ion);
|
|---|
| 1048 |
|
|---|
| 1049 | QList<QGraphicsItem *> QGraphicsViewPrivate::findItems(const QRegion &exposedRegion, bool *allItems) const
|
|---|
| 1050 | {
|
|---|
| 1051 | Q_Q(const QGraphicsView);
|
|---|
| 1052 | const QPainterPath exposedPath(qt_regionToPath(exposedRegion));
|
|---|
| 1053 | const QPainterPath exposedScenePath(q->mapToScene(exposedPath));
|
|---|
| 1054 |
|
|---|
| 1055 | if (exposedScenePath.contains(scene->d_func()->growingItemsBoundingRect)) {
|
|---|
| 1056 | Q_ASSERT(allItems);
|
|---|
| 1057 | *allItems = true;
|
|---|
| 1058 |
|
|---|
| 1059 | // All items are guaranteed within the exposed region, don't bother using the index.
|
|---|
| 1060 | QList<QGraphicsItem *> itemList(scene->items());
|
|---|
| 1061 | int i = 0;
|
|---|
| 1062 | while (i < itemList.size()) {
|
|---|
| 1063 | // But we only want to include items that are visible
|
|---|
| 1064 | if (!itemList.at(i)->isVisible())
|
|---|
| 1065 | itemList.removeAt(i);
|
|---|
| 1066 | else
|
|---|
| 1067 | ++i;
|
|---|
| 1068 | }
|
|---|
| 1069 |
|
|---|
| 1070 | // Sort the items.
|
|---|
| 1071 | QGraphicsScenePrivate::sortItems(&itemList, Qt::DescendingOrder, scene->d_func()->sortCacheEnabled);
|
|---|
| 1072 | return itemList;
|
|---|
| 1073 | }
|
|---|
| 1074 |
|
|---|
| 1075 | if (scene->d_func()->largestUntransformableItem.isNull()) {
|
|---|
| 1076 | if (exposedRegion.numRects() == 1 && matrix.type() <= QTransform::TxScale) {
|
|---|
| 1077 | return scene->d_func()->items_helper(exposedScenePath.controlPointRect(),
|
|---|
| 1078 | Qt::IntersectsItemBoundingRect,
|
|---|
| 1079 | Qt::DescendingOrder);
|
|---|
| 1080 | }
|
|---|
| 1081 | return scene->d_func()->items_helper(exposedScenePath,
|
|---|
| 1082 | Qt::IntersectsItemBoundingRect,
|
|---|
| 1083 | Qt::DescendingOrder);
|
|---|
| 1084 | }
|
|---|
| 1085 |
|
|---|
| 1086 | // NB! Path must be in viewport coordinates.
|
|---|
| 1087 | return itemsInArea(exposedPath, Qt::IntersectsItemBoundingRect, Qt::DescendingOrder);
|
|---|
| 1088 | }
|
|---|
| 1089 |
|
|---|
| 1090 | void QGraphicsViewPrivate::generateStyleOptions(const QList<QGraphicsItem *> &itemList,
|
|---|
| 1091 | QGraphicsItem **itemArray,
|
|---|
| 1092 | QStyleOptionGraphicsItem *styleOptionArray,
|
|---|
| 1093 | const QTransform &worldTransform,
|
|---|
| 1094 | bool allItems,
|
|---|
| 1095 | const QRegion &exposedRegion) const
|
|---|
| 1096 | {
|
|---|
| 1097 | // Two unit vectors.
|
|---|
| 1098 | QLineF v1(0, 0, 1, 0);
|
|---|
| 1099 | QLineF v2(0, 0, 0, 1);
|
|---|
| 1100 | QTransform itemToViewportTransform;
|
|---|
| 1101 | QRectF brect;
|
|---|
| 1102 | QTransform reverseMap;
|
|---|
| 1103 |
|
|---|
| 1104 | for (int i = 0; i < itemList.size(); ++i) {
|
|---|
| 1105 | QGraphicsItem *item = itemArray[i] = itemList[i];
|
|---|
| 1106 |
|
|---|
| 1107 | QStyleOptionGraphicsItem &option = styleOptionArray[i];
|
|---|
| 1108 | brect = item->boundingRect();
|
|---|
| 1109 | option.state = QStyle::State_None;
|
|---|
| 1110 | option.rect = brect.toRect();
|
|---|
| 1111 | option.exposedRect = QRectF();
|
|---|
| 1112 | if (item->d_ptr->selected)
|
|---|
| 1113 | option.state |= QStyle::State_Selected;
|
|---|
| 1114 | if (item->d_ptr->enabled)
|
|---|
| 1115 | option.state |= QStyle::State_Enabled;
|
|---|
| 1116 | if (item->hasFocus())
|
|---|
| 1117 | option.state |= QStyle::State_HasFocus;
|
|---|
| 1118 | if (scene->d_func()->hoverItems.contains(item))
|
|---|
| 1119 | option.state |= QStyle::State_MouseOver;
|
|---|
| 1120 | if (item == scene->mouseGrabberItem())
|
|---|
| 1121 | option.state |= QStyle::State_Sunken;
|
|---|
| 1122 |
|
|---|
| 1123 | // Calculate a simple level-of-detail metric.
|
|---|
| 1124 | // ### almost identical code in QGraphicsScene::render()
|
|---|
| 1125 | // and QGraphicsView::render() - consider refactoring
|
|---|
| 1126 | if (item->d_ptr->itemIsUntransformable()) {
|
|---|
| 1127 | itemToViewportTransform = item->deviceTransform(worldTransform);
|
|---|
| 1128 | } else {
|
|---|
| 1129 | itemToViewportTransform = item->sceneTransform() * worldTransform;
|
|---|
| 1130 | }
|
|---|
| 1131 |
|
|---|
| 1132 | if (itemToViewportTransform.type() <= QTransform::TxTranslate) {
|
|---|
| 1133 | // Translation and rotation only? The LOD is 1.
|
|---|
| 1134 | option.levelOfDetail = 1;
|
|---|
| 1135 | } else {
|
|---|
| 1136 | // LOD is the transformed area of a 1x1 rectangle.
|
|---|
| 1137 | option.levelOfDetail = qSqrt(itemToViewportTransform.map(v1).length() * itemToViewportTransform.map(v2).length());
|
|---|
| 1138 | }
|
|---|
| 1139 | option.matrix = itemToViewportTransform.toAffine(); //### discards perspective
|
|---|
| 1140 |
|
|---|
| 1141 | if (!allItems) {
|
|---|
| 1142 | // Determine the item's exposed area
|
|---|
| 1143 | reverseMap = itemToViewportTransform.inverted();
|
|---|
| 1144 | foreach (const QRect &rect, exposedRegion.rects()) {
|
|---|
| 1145 | option.exposedRect |= reverseMap.mapRect(QRectF(rect.adjusted(-1, -1, 1, 1)));
|
|---|
| 1146 | if (option.exposedRect.contains(brect))
|
|---|
| 1147 | break;
|
|---|
| 1148 | }
|
|---|
| 1149 | option.exposedRect &= brect;
|
|---|
| 1150 | } else {
|
|---|
| 1151 | // The whole item is exposed
|
|---|
| 1152 | option.exposedRect = brect;
|
|---|
| 1153 | }
|
|---|
| 1154 | }
|
|---|
| 1155 | }
|
|---|
| 1156 |
|
|---|
| 1157 | /*!
|
|---|
| 1158 | Constructs a QGraphicsView. \a parent is passed to QWidget's constructor.
|
|---|
| 1159 | */
|
|---|
| 1160 | QGraphicsView::QGraphicsView(QWidget *parent)
|
|---|
| 1161 | : QAbstractScrollArea(*new QGraphicsViewPrivate, parent)
|
|---|
| 1162 | {
|
|---|
| 1163 | setViewport(0);
|
|---|
| 1164 | setAcceptDrops(true);
|
|---|
| 1165 | setBackgroundRole(QPalette::Base);
|
|---|
| 1166 |
|
|---|
| 1167 | // ### Ideally this would be enabled/disabled depending on whether any
|
|---|
| 1168 | // widgets in the current scene enabled input methods. We could do that
|
|---|
| 1169 | // using a simple reference count. The same goes for acceptDrops and mouse
|
|---|
| 1170 | // tracking.
|
|---|
| 1171 | setAttribute(Qt::WA_InputMethodEnabled);
|
|---|
| 1172 | }
|
|---|
| 1173 |
|
|---|
| 1174 | /*!
|
|---|
| 1175 | Constructs a QGraphicsView and sets the visualized scene to \a
|
|---|
| 1176 | scene. \a parent is passed to QWidget's constructor.
|
|---|
| 1177 | */
|
|---|
| 1178 | QGraphicsView::QGraphicsView(QGraphicsScene *scene, QWidget *parent)
|
|---|
| 1179 | : QAbstractScrollArea(*new QGraphicsViewPrivate, parent)
|
|---|
| 1180 | {
|
|---|
| 1181 | setScene(scene);
|
|---|
| 1182 | setViewport(0);
|
|---|
| 1183 | setAcceptDrops(true);
|
|---|
| 1184 | setBackgroundRole(QPalette::Base);
|
|---|
| 1185 | setAttribute(Qt::WA_InputMethodEnabled);
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | /*!
|
|---|
| 1189 | \internal
|
|---|
| 1190 | */
|
|---|
| 1191 | QGraphicsView::QGraphicsView(QGraphicsViewPrivate &dd, QWidget *parent)
|
|---|
| 1192 | : QAbstractScrollArea(dd, parent)
|
|---|
| 1193 | {
|
|---|
| 1194 | setViewport(0);
|
|---|
| 1195 | setAcceptDrops(true);
|
|---|
| 1196 | setBackgroundRole(QPalette::Base);
|
|---|
| 1197 | setAttribute(Qt::WA_InputMethodEnabled);
|
|---|
| 1198 | }
|
|---|
| 1199 |
|
|---|
| 1200 | /*!
|
|---|
| 1201 | Destructs the QGraphicsView object.
|
|---|
| 1202 | */
|
|---|
| 1203 | QGraphicsView::~QGraphicsView()
|
|---|
| 1204 | {
|
|---|
| 1205 | Q_D(QGraphicsView);
|
|---|
| 1206 | if (d->scene)
|
|---|
| 1207 | d->scene->d_func()->views.removeAll(this);
|
|---|
| 1208 | delete d->lastDragDropEvent;
|
|---|
| 1209 | }
|
|---|
| 1210 |
|
|---|
| 1211 | /*!
|
|---|
| 1212 | \reimp
|
|---|
| 1213 | */
|
|---|
| 1214 | QSize QGraphicsView::sizeHint() const
|
|---|
| 1215 | {
|
|---|
| 1216 | Q_D(const QGraphicsView);
|
|---|
| 1217 | if (d->scene) {
|
|---|
| 1218 | QSizeF baseSize = d->matrix.mapRect(sceneRect()).size();
|
|---|
| 1219 | baseSize += QSizeF(d->frameWidth * 2, d->frameWidth * 2);
|
|---|
| 1220 | return baseSize.boundedTo((3 * QApplication::desktop()->size()) / 4).toSize();
|
|---|
| 1221 | }
|
|---|
| 1222 | return QAbstractScrollArea::sizeHint();
|
|---|
| 1223 | }
|
|---|
| 1224 |
|
|---|
| 1225 | /*!
|
|---|
| 1226 | \property QGraphicsView::renderHints
|
|---|
| 1227 | \brief the default render hints for the view
|
|---|
| 1228 |
|
|---|
| 1229 | These hints are
|
|---|
| 1230 | used to initialize QPainter before each visible item is drawn. QPainter
|
|---|
| 1231 | uses render hints to toggle rendering features such as antialiasing and
|
|---|
| 1232 | smooth pixmap transformation.
|
|---|
| 1233 |
|
|---|
| 1234 | QPainter::TextAntialiasing is enabled by default.
|
|---|
| 1235 |
|
|---|
| 1236 | Example:
|
|---|
| 1237 |
|
|---|
| 1238 | \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsview.cpp 1
|
|---|
| 1239 | */
|
|---|
| 1240 | QPainter::RenderHints QGraphicsView::renderHints() const
|
|---|
| 1241 | {
|
|---|
| 1242 | Q_D(const QGraphicsView);
|
|---|
| 1243 | return d->renderHints;
|
|---|
| 1244 | }
|
|---|
| 1245 | void QGraphicsView::setRenderHints(QPainter::RenderHints hints)
|
|---|
| 1246 | {
|
|---|
| 1247 | Q_D(QGraphicsView);
|
|---|
| 1248 | if (hints == d->renderHints)
|
|---|
| 1249 | return;
|
|---|
| 1250 | d->renderHints = hints;
|
|---|
| 1251 | viewport()->update();
|
|---|
| 1252 | }
|
|---|
| 1253 |
|
|---|
| 1254 | /*!
|
|---|
| 1255 | If \a enabled is true, the render hint \a hint is enabled; otherwise it
|
|---|
| 1256 | is disabled.
|
|---|
| 1257 |
|
|---|
| 1258 | \sa renderHints
|
|---|
| 1259 | */
|
|---|
| 1260 | void QGraphicsView::setRenderHint(QPainter::RenderHint hint, bool enabled)
|
|---|
| 1261 | {
|
|---|
| 1262 | Q_D(QGraphicsView);
|
|---|
| 1263 | QPainter::RenderHints oldHints = d->renderHints;
|
|---|
| 1264 | if (enabled)
|
|---|
| 1265 | d->renderHints |= hint;
|
|---|
| 1266 | else
|
|---|
| 1267 | d->renderHints &= ~hint;
|
|---|
| 1268 | if (oldHints != d->renderHints)
|
|---|
| 1269 | viewport()->update();
|
|---|
| 1270 | }
|
|---|
| 1271 |
|
|---|
| 1272 | /*!
|
|---|
| 1273 | \property QGraphicsView::alignment
|
|---|
| 1274 | \brief the alignment of the scene in the view when the whole
|
|---|
| 1275 | scene is visible.
|
|---|
| 1276 |
|
|---|
| 1277 | If the whole scene is visible in the view, (i.e., there are no visible
|
|---|
| 1278 | scroll bars,) the view's alignment will decide where the scene will be
|
|---|
| 1279 | rendered in the view. For example, if the alignment is Qt::AlignCenter,
|
|---|
| 1280 | which is default, the scene will be centered in the view, and if the
|
|---|
| 1281 | alignment is (Qt::AlignLeft | Qt::AlignTop), the scene will be rendered in
|
|---|
| 1282 | the top-left corner of the view.
|
|---|
| 1283 | */
|
|---|
| 1284 | Qt::Alignment QGraphicsView::alignment() const
|
|---|
| 1285 | {
|
|---|
| 1286 | Q_D(const QGraphicsView);
|
|---|
| 1287 | return d->alignment;
|
|---|
| 1288 | }
|
|---|
| 1289 | void QGraphicsView::setAlignment(Qt::Alignment alignment)
|
|---|
| 1290 | {
|
|---|
| 1291 | Q_D(QGraphicsView);
|
|---|
| 1292 | if (d->alignment != alignment) {
|
|---|
| 1293 | d->alignment = alignment;
|
|---|
| 1294 | d->recalculateContentSize();
|
|---|
| 1295 | }
|
|---|
| 1296 | }
|
|---|
| 1297 |
|
|---|
| 1298 | /*!
|
|---|
| 1299 | \property QGraphicsView::transformationAnchor
|
|---|
| 1300 | \brief how the view should position the scene during transformations.
|
|---|
| 1301 |
|
|---|
| 1302 | QGraphicsView uses this property to decide how to position the scene in
|
|---|
| 1303 | the viewport when the transformation matrix changes, and the coordinate
|
|---|
| 1304 | system of the view is transformed. The default behavior, AnchorViewCenter,
|
|---|
| 1305 | ensures that the scene point at the center of the view remains unchanged
|
|---|
| 1306 | during transformations (e.g., when rotating, the scene will appear to
|
|---|
| 1307 | rotate around the center of the view).
|
|---|
| 1308 |
|
|---|
| 1309 | Note that the effect of this property is noticeable when only a part of the
|
|---|
| 1310 | scene is visible (i.e., when there are scroll bars). Otherwise, if the
|
|---|
| 1311 | whole scene fits in the view, QGraphicsScene uses the view \l alignment to
|
|---|
| 1312 | position the scene in the view.
|
|---|
| 1313 |
|
|---|
| 1314 | \sa alignment, resizeAnchor
|
|---|
| 1315 | */
|
|---|
| 1316 | QGraphicsView::ViewportAnchor QGraphicsView::transformationAnchor() const
|
|---|
| 1317 | {
|
|---|
| 1318 | Q_D(const QGraphicsView);
|
|---|
| 1319 | return d->transformationAnchor;
|
|---|
| 1320 | }
|
|---|
| 1321 | void QGraphicsView::setTransformationAnchor(ViewportAnchor anchor)
|
|---|
| 1322 | {
|
|---|
| 1323 | Q_D(QGraphicsView);
|
|---|
| 1324 | d->transformationAnchor = anchor;
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | /*!
|
|---|
| 1328 | \property QGraphicsView::resizeAnchor
|
|---|
| 1329 | \brief how the view should position the scene when the view is resized.
|
|---|
| 1330 |
|
|---|
| 1331 | QGraphicsView uses this property to decide how to position the scene in
|
|---|
| 1332 | the viewport when the viewport widget's size changes. The default
|
|---|
| 1333 | behavior, NoAnchor, leaves the scene's position unchanged during a resize;
|
|---|
| 1334 | the top-left corner of the view will appear to be anchored while resizing.
|
|---|
| 1335 |
|
|---|
| 1336 | Note that the effect of this property is noticeable when only a part of the
|
|---|
| 1337 | scene is visible (i.e., when there are scroll bars). Otherwise, if the
|
|---|
| 1338 | whole scene fits in the view, QGraphicsScene uses the view \l alignment to
|
|---|
| 1339 | position the scene in the view.
|
|---|
| 1340 |
|
|---|
| 1341 | \sa alignment, transformationAnchor, Qt::WNorthWestGravity
|
|---|
| 1342 | */
|
|---|
| 1343 | QGraphicsView::ViewportAnchor QGraphicsView::resizeAnchor() const
|
|---|
| 1344 | {
|
|---|
| 1345 | Q_D(const QGraphicsView);
|
|---|
| 1346 | return d->resizeAnchor;
|
|---|
| 1347 | }
|
|---|
| 1348 | void QGraphicsView::setResizeAnchor(ViewportAnchor anchor)
|
|---|
| 1349 | {
|
|---|
| 1350 | Q_D(QGraphicsView);
|
|---|
| 1351 | d->resizeAnchor = anchor;
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| 1354 | /*!
|
|---|
| 1355 | \property QGraphicsView::viewportUpdateMode
|
|---|
| 1356 | \brief how the viewport should update its contents.
|
|---|
| 1357 |
|
|---|
| 1358 | \since 4.3
|
|---|
| 1359 |
|
|---|
| 1360 | QGraphicsView uses this property to decide how to update areas of the
|
|---|
| 1361 | scene that have been reexposed or changed. Usually you do not need to
|
|---|
| 1362 | modify this property, but there are some cases where doing so can improve
|
|---|
| 1363 | rendering performance. See the ViewportUpdateMode documentation for
|
|---|
| 1364 | specific details.
|
|---|
| 1365 |
|
|---|
| 1366 | The default value is MinimalViewportUpdate, where QGraphicsView will
|
|---|
| 1367 | update as small an area of the viewport as possible when the contents
|
|---|
| 1368 | change.
|
|---|
| 1369 |
|
|---|
| 1370 | \sa ViewportUpdateMode, cacheMode
|
|---|
| 1371 | */
|
|---|
| 1372 | QGraphicsView::ViewportUpdateMode QGraphicsView::viewportUpdateMode() const
|
|---|
| 1373 | {
|
|---|
| 1374 | Q_D(const QGraphicsView);
|
|---|
| 1375 | return d->viewportUpdateMode;
|
|---|
| 1376 | }
|
|---|
| 1377 | void QGraphicsView::setViewportUpdateMode(ViewportUpdateMode mode)
|
|---|
| 1378 | {
|
|---|
| 1379 | Q_D(QGraphicsView);
|
|---|
| 1380 | d->viewportUpdateMode = mode;
|
|---|
| 1381 | }
|
|---|
| 1382 |
|
|---|
| 1383 | /*!
|
|---|
| 1384 | \property QGraphicsView::optimizationFlags
|
|---|
| 1385 | \brief flags that can be used to tune QGraphicsView's performance.
|
|---|
| 1386 |
|
|---|
| 1387 | \since 4.3
|
|---|
| 1388 |
|
|---|
| 1389 | QGraphicsView uses clipping, extra bounding rect adjustments, and certain
|
|---|
| 1390 | other aids to improve rendering quality and performance for the common
|
|---|
| 1391 | case graphics scene. However, depending on the target platform, the scene,
|
|---|
| 1392 | and the viewport in use, some of these operations can degrade performance.
|
|---|
| 1393 |
|
|---|
| 1394 | The effect varies from flag to flag; see the OptimizationFlags
|
|---|
| 1395 | documentation for details.
|
|---|
| 1396 |
|
|---|
| 1397 | By default, no optimization flags are enabled.
|
|---|
| 1398 |
|
|---|
| 1399 | \sa setOptimizationFlag()
|
|---|
| 1400 | */
|
|---|
| 1401 | QGraphicsView::OptimizationFlags QGraphicsView::optimizationFlags() const
|
|---|
| 1402 | {
|
|---|
| 1403 | Q_D(const QGraphicsView);
|
|---|
| 1404 | return d->optimizationFlags;
|
|---|
| 1405 | }
|
|---|
| 1406 | void QGraphicsView::setOptimizationFlags(OptimizationFlags flags)
|
|---|
| 1407 | {
|
|---|
| 1408 | Q_D(QGraphicsView);
|
|---|
| 1409 | d->optimizationFlags = flags;
|
|---|
| 1410 | }
|
|---|
| 1411 |
|
|---|
| 1412 | /*!
|
|---|
| 1413 | Enables \a flag if \a enabled is true; otherwise disables \a flag.
|
|---|
| 1414 |
|
|---|
| 1415 | \sa optimizationFlags
|
|---|
| 1416 | */
|
|---|
| 1417 | void QGraphicsView::setOptimizationFlag(OptimizationFlag flag, bool enabled)
|
|---|
| 1418 | {
|
|---|
| 1419 | Q_D(QGraphicsView);
|
|---|
| 1420 | if (enabled)
|
|---|
| 1421 | d->optimizationFlags |= flag;
|
|---|
| 1422 | else
|
|---|
| 1423 | d->optimizationFlags &= ~flag;
|
|---|
| 1424 | }
|
|---|
| 1425 |
|
|---|
| 1426 | /*!
|
|---|
| 1427 | \property QGraphicsView::dragMode
|
|---|
| 1428 | \brief the behavior for dragging the mouse over the scene while
|
|---|
| 1429 | the left mouse button is pressed.
|
|---|
| 1430 |
|
|---|
| 1431 | This property defines what should happen when the user clicks on the scene
|
|---|
| 1432 | background and drags the mouse (e.g., scrolling the viewport contents
|
|---|
| 1433 | using a pointing hand cursor, or selecting multiple items with a rubber
|
|---|
| 1434 | band). The default value, NoDrag, does nothing.
|
|---|
| 1435 |
|
|---|
| 1436 | This behavior only affects mouse clicks that are not handled by any item.
|
|---|
| 1437 | You can define a custom behavior by creating a subclass of QGraphicsView
|
|---|
| 1438 | and reimplementing mouseMoveEvent().
|
|---|
| 1439 | */
|
|---|
| 1440 | QGraphicsView::DragMode QGraphicsView::dragMode() const
|
|---|
| 1441 | {
|
|---|
| 1442 | Q_D(const QGraphicsView);
|
|---|
| 1443 | return d->dragMode;
|
|---|
| 1444 | }
|
|---|
| 1445 | void QGraphicsView::setDragMode(DragMode mode)
|
|---|
| 1446 | {
|
|---|
| 1447 | Q_D(QGraphicsView);
|
|---|
| 1448 | if (d->dragMode == mode)
|
|---|
| 1449 | return;
|
|---|
| 1450 |
|
|---|
| 1451 | #ifndef QT_NO_CURSOR
|
|---|
| 1452 | if (d->dragMode == ScrollHandDrag)
|
|---|
| 1453 | viewport()->unsetCursor();
|
|---|
| 1454 | #endif
|
|---|
| 1455 |
|
|---|
| 1456 | // If dragMode is unset while dragging, e.g. via a keyEvent, we
|
|---|
| 1457 | // don't unset the handScrolling state. When enabling scrolling
|
|---|
| 1458 | // again the mouseMoveEvent will automatically start scrolling,
|
|---|
| 1459 | // without a mousePress
|
|---|
| 1460 | if (d->dragMode == ScrollHandDrag && mode == NoDrag && d->handScrolling)
|
|---|
| 1461 | d->handScrolling = false;
|
|---|
| 1462 |
|
|---|
| 1463 | d->dragMode = mode;
|
|---|
| 1464 |
|
|---|
| 1465 | #ifndef QT_NO_CURSOR
|
|---|
| 1466 | if (d->dragMode == ScrollHandDrag) {
|
|---|
| 1467 | // Forget the stored viewport cursor when we enter scroll hand drag mode.
|
|---|
| 1468 | d->hasStoredOriginalCursor = false;
|
|---|
| 1469 | viewport()->setCursor(Qt::OpenHandCursor);
|
|---|
| 1470 | }
|
|---|
| 1471 | #endif
|
|---|
| 1472 | }
|
|---|
| 1473 |
|
|---|
| 1474 | #ifndef QT_NO_RUBBERBAND
|
|---|
| 1475 | /*!
|
|---|
| 1476 | \property QGraphicsView::rubberBandSelectionMode
|
|---|
| 1477 | \brief the behavior for selecting items with a rubber band selection rectangle.
|
|---|
| 1478 | \since 4.3
|
|---|
| 1479 |
|
|---|
| 1480 | This property defines how items are selected when using the RubberBandDrag
|
|---|
| 1481 | drag mode.
|
|---|
| 1482 |
|
|---|
| 1483 | The default value is Qt::IntersectsItemShape; all items whose shape
|
|---|
| 1484 | intersects with or is contained by the rubber band are selected.
|
|---|
| 1485 |
|
|---|
| 1486 | \sa dragMode, items()
|
|---|
| 1487 | */
|
|---|
| 1488 | Qt::ItemSelectionMode QGraphicsView::rubberBandSelectionMode() const
|
|---|
| 1489 | {
|
|---|
| 1490 | Q_D(const QGraphicsView);
|
|---|
| 1491 | return d->rubberBandSelectionMode;
|
|---|
| 1492 | }
|
|---|
| 1493 | void QGraphicsView::setRubberBandSelectionMode(Qt::ItemSelectionMode mode)
|
|---|
| 1494 | {
|
|---|
| 1495 | Q_D(QGraphicsView);
|
|---|
| 1496 | d->rubberBandSelectionMode = mode;
|
|---|
| 1497 | }
|
|---|
| 1498 | #endif
|
|---|
| 1499 |
|
|---|
| 1500 | /*!
|
|---|
| 1501 | \property QGraphicsView::cacheMode
|
|---|
| 1502 | \brief which parts of the view are cached
|
|---|
| 1503 |
|
|---|
| 1504 | QGraphicsView can cache pre-rendered content in a QPixmap, which is then
|
|---|
| 1505 | drawn onto the viewport. The purpose of such caching is to speed up the
|
|---|
| 1506 | total rendering time for areas that are slow to render. Texture, gradient
|
|---|
| 1507 | and alpha blended backgrounds, for example, can be notibly slow to render;
|
|---|
| 1508 | especially with a transformed view. The CacheBackground flag enables
|
|---|
| 1509 | caching of the view's background. For example:
|
|---|
| 1510 |
|
|---|
| 1511 | \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsview.cpp 2
|
|---|
| 1512 |
|
|---|
| 1513 | The cache is invalidated every time the view is transformed. However, when
|
|---|
| 1514 | scrolling, only partial invalidation is required.
|
|---|
| 1515 |
|
|---|
| 1516 | By default, nothing is cached.
|
|---|
| 1517 |
|
|---|
| 1518 | \sa resetCachedContent(), QPixmapCache
|
|---|
| 1519 | */
|
|---|
| 1520 | QGraphicsView::CacheMode QGraphicsView::cacheMode() const
|
|---|
| 1521 | {
|
|---|
| 1522 | Q_D(const QGraphicsView);
|
|---|
| 1523 | return d->cacheMode;
|
|---|
| 1524 | }
|
|---|
| 1525 | void QGraphicsView::setCacheMode(CacheMode mode)
|
|---|
| 1526 | {
|
|---|
| 1527 | Q_D(QGraphicsView);
|
|---|
| 1528 | if (mode == d->cacheMode)
|
|---|
| 1529 | return;
|
|---|
| 1530 | d->cacheMode = mode;
|
|---|
| 1531 | resetCachedContent();
|
|---|
| 1532 | }
|
|---|
| 1533 |
|
|---|
| 1534 | /*!
|
|---|
| 1535 | Resets any cached content. Calling this function will clear
|
|---|
| 1536 | QGraphicsView's cache. If the current cache mode is \l CacheNone, this
|
|---|
| 1537 | function does nothing.
|
|---|
| 1538 |
|
|---|
| 1539 | This function is called automatically for you when the backgroundBrush or
|
|---|
| 1540 | QGraphicsScene::backgroundBrush properties change; you only need to call
|
|---|
| 1541 | this function if you have reimplemented QGraphicsScene::drawBackground()
|
|---|
| 1542 | or QGraphicsView::drawBackground() to draw a custom background, and need
|
|---|
| 1543 | to trigger a full redraw.
|
|---|
| 1544 |
|
|---|
| 1545 | \sa cacheMode()
|
|---|
| 1546 | */
|
|---|
| 1547 | void QGraphicsView::resetCachedContent()
|
|---|
| 1548 | {
|
|---|
| 1549 | Q_D(QGraphicsView);
|
|---|
| 1550 | if (d->cacheMode == CacheNone)
|
|---|
| 1551 | return;
|
|---|
| 1552 |
|
|---|
| 1553 | if (d->cacheMode & CacheBackground) {
|
|---|
| 1554 | // Background caching is enabled.
|
|---|
| 1555 | d->mustResizeBackgroundPixmap = true;
|
|---|
| 1556 | viewport()->update();
|
|---|
| 1557 | } else if (d->mustResizeBackgroundPixmap) {
|
|---|
| 1558 | // Background caching is disabled.
|
|---|
| 1559 | // Cleanup, free some resources.
|
|---|
| 1560 | d->mustResizeBackgroundPixmap = false;
|
|---|
| 1561 | d->backgroundPixmap = QPixmap();
|
|---|
| 1562 | d->backgroundPixmapExposed = QRegion();
|
|---|
| 1563 | }
|
|---|
| 1564 | }
|
|---|
| 1565 |
|
|---|
| 1566 | /*!
|
|---|
| 1567 | Invalidates and schedules a redraw of \a layers inside \a rect. \a rect is
|
|---|
| 1568 | in scene coordinates. Any cached content for \a layers inside \a rect is
|
|---|
| 1569 | unconditionally invalidated and redrawn.
|
|---|
| 1570 |
|
|---|
| 1571 | You can call this function to notify QGraphicsView of changes to the
|
|---|
| 1572 | background or the foreground of the scene. It is commonly used for scenes
|
|---|
| 1573 | with tile-based backgrounds to notify changes when QGraphicsView has
|
|---|
| 1574 | enabled background caching.
|
|---|
| 1575 |
|
|---|
| 1576 | Note that QGraphicsView currently supports background caching only (see
|
|---|
| 1577 | QGraphicsView::CacheBackground). This function is equivalent to calling update() if any
|
|---|
| 1578 | layer but QGraphicsScene::BackgroundLayer is passed.
|
|---|
| 1579 |
|
|---|
| 1580 | \sa QGraphicsScene::invalidate(), update()
|
|---|
| 1581 | */
|
|---|
| 1582 | void QGraphicsView::invalidateScene(const QRectF &rect, QGraphicsScene::SceneLayers layers)
|
|---|
| 1583 | {
|
|---|
| 1584 | Q_D(QGraphicsView);
|
|---|
| 1585 | if ((layers & QGraphicsScene::BackgroundLayer) && !d->mustResizeBackgroundPixmap) {
|
|---|
| 1586 | QRect viewRect = mapFromScene(rect).boundingRect();
|
|---|
| 1587 | if (viewport()->rect().intersects(viewRect)) {
|
|---|
| 1588 | // The updated background area is exposed; schedule this area for
|
|---|
| 1589 | // redrawing.
|
|---|
| 1590 | d->backgroundPixmapExposed += viewRect;
|
|---|
| 1591 | if (d->scene)
|
|---|
| 1592 | d->scene->update(rect);
|
|---|
| 1593 | }
|
|---|
| 1594 | }
|
|---|
| 1595 | }
|
|---|
| 1596 |
|
|---|
| 1597 | /*!
|
|---|
| 1598 | \property QGraphicsView::interactive
|
|---|
| 1599 | \brief whether the view allowed scene interaction.
|
|---|
| 1600 |
|
|---|
| 1601 | If enabled, this view is set to allow scene interaction. Otherwise, this
|
|---|
| 1602 | view will not allow interaction, and any mouse or key events are ignored
|
|---|
| 1603 | (i.e., it will act as a read-only view).
|
|---|
| 1604 |
|
|---|
| 1605 | By default, this property is true.
|
|---|
| 1606 | */
|
|---|
| 1607 | bool QGraphicsView::isInteractive() const
|
|---|
| 1608 | {
|
|---|
| 1609 | Q_D(const QGraphicsView);
|
|---|
| 1610 | return d->sceneInteractionAllowed;
|
|---|
| 1611 | }
|
|---|
| 1612 | void QGraphicsView::setInteractive(bool allowed)
|
|---|
| 1613 | {
|
|---|
| 1614 | Q_D(QGraphicsView);
|
|---|
| 1615 | d->sceneInteractionAllowed = allowed;
|
|---|
| 1616 | }
|
|---|
| 1617 |
|
|---|
| 1618 | /*!
|
|---|
| 1619 | Returns a pointer to the scene that is currently visualized in the
|
|---|
| 1620 | view. If no scene is currently visualized, 0 is returned.
|
|---|
| 1621 |
|
|---|
| 1622 | \sa setScene()
|
|---|
| 1623 | */
|
|---|
| 1624 | QGraphicsScene *QGraphicsView::scene() const
|
|---|
| 1625 | {
|
|---|
| 1626 | Q_D(const QGraphicsView);
|
|---|
| 1627 | return d->scene;
|
|---|
| 1628 | }
|
|---|
| 1629 |
|
|---|
| 1630 | /*!
|
|---|
| 1631 | Sets the current scene to \a scene. If \a scene is already being
|
|---|
| 1632 | viewed, this function does nothing.
|
|---|
| 1633 |
|
|---|
| 1634 | When a scene is set on a view, the QGraphicsScene::changed() signal
|
|---|
| 1635 | is automatically connected to this view's updateScene() slot, and the
|
|---|
| 1636 | view's scroll bars are adjusted to fit the size of the scene.
|
|---|
| 1637 | */
|
|---|
| 1638 | void QGraphicsView::setScene(QGraphicsScene *scene)
|
|---|
| 1639 | {
|
|---|
| 1640 | Q_D(QGraphicsView);
|
|---|
| 1641 | if (d->scene == scene)
|
|---|
| 1642 | return;
|
|---|
| 1643 |
|
|---|
| 1644 | // Always update the viewport when the scene changes.
|
|---|
| 1645 | viewport()->update();
|
|---|
| 1646 |
|
|---|
| 1647 | // Remove the previously assigned scene.
|
|---|
| 1648 | if (d->scene) {
|
|---|
| 1649 | disconnect(d->scene, SIGNAL(changed(QList<QRectF>)),
|
|---|
| 1650 | this, SLOT(updateScene(QList<QRectF>)));
|
|---|
| 1651 | disconnect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
|
|---|
| 1652 | this, SLOT(updateSceneRect(QRectF)));
|
|---|
| 1653 | d->scene->d_func()->views.removeAll(this);
|
|---|
| 1654 | }
|
|---|
| 1655 |
|
|---|
| 1656 | // Assign the new scene and update the contents (scrollbars, etc.)).
|
|---|
| 1657 | if ((d->scene = scene)) {
|
|---|
| 1658 | connect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
|
|---|
| 1659 | this, SLOT(updateSceneRect(QRectF)));
|
|---|
| 1660 | d->updateSceneSlotReimplementedChecked = false;
|
|---|
| 1661 | d->scene->d_func()->views << this;
|
|---|
| 1662 | d->recalculateContentSize();
|
|---|
| 1663 | d->lastCenterPoint = sceneRect().center();
|
|---|
| 1664 | d->keepLastCenterPoint = true;
|
|---|
| 1665 | } else {
|
|---|
| 1666 | d->recalculateContentSize();
|
|---|
| 1667 | }
|
|---|
| 1668 | }
|
|---|
| 1669 |
|
|---|
| 1670 | /*!
|
|---|
| 1671 | \property QGraphicsView::sceneRect
|
|---|
| 1672 | \brief the area of the scene visualized by this view.
|
|---|
| 1673 |
|
|---|
| 1674 | The scene rectangle defines the extent of the scene, and in the view's case,
|
|---|
| 1675 | this means the area of the scene that you can navigate using the scroll
|
|---|
| 1676 | bars.
|
|---|
| 1677 |
|
|---|
| 1678 | If unset, or if a null QRectF is set, this property has the same value as
|
|---|
| 1679 | QGraphicsScene::sceneRect, and it changes with
|
|---|
| 1680 | QGraphicsScene::sceneRect. Otherwise, the view's scene rect is unaffected
|
|---|
| 1681 | by the scene.
|
|---|
| 1682 |
|
|---|
| 1683 | Note that, although the scene supports a virtually unlimited size, the
|
|---|
| 1684 | range of the scroll bars will never exceed the range of an integer
|
|---|
| 1685 | (INT_MIN, INT_MAX). When the scene is larger than the scroll bars' values,
|
|---|
| 1686 | you can choose to use translate() to navigate the scene instead.
|
|---|
| 1687 |
|
|---|
| 1688 | By default, this property contains a rectangle at the origin with zero
|
|---|
| 1689 | width and height.
|
|---|
| 1690 |
|
|---|
| 1691 | \sa QGraphicsScene::sceneRect
|
|---|
| 1692 | */
|
|---|
| 1693 | QRectF QGraphicsView::sceneRect() const
|
|---|
| 1694 | {
|
|---|
| 1695 | Q_D(const QGraphicsView);
|
|---|
| 1696 | if (d->hasSceneRect)
|
|---|
| 1697 | return d->sceneRect;
|
|---|
| 1698 | if (d->scene)
|
|---|
| 1699 | return d->scene->sceneRect();
|
|---|
| 1700 | return QRectF();
|
|---|
| 1701 | }
|
|---|
| 1702 | void QGraphicsView::setSceneRect(const QRectF &rect)
|
|---|
| 1703 | {
|
|---|
| 1704 | Q_D(QGraphicsView);
|
|---|
| 1705 | d->hasSceneRect = !rect.isNull();
|
|---|
| 1706 | d->sceneRect = rect;
|
|---|
| 1707 | d->recalculateContentSize();
|
|---|
| 1708 | }
|
|---|
| 1709 |
|
|---|
| 1710 | /*!
|
|---|
| 1711 | Returns the current transformation matrix for the view. If no current
|
|---|
| 1712 | transformation is set, the identity matrix is returned.
|
|---|
| 1713 |
|
|---|
| 1714 | \sa setMatrix(), rotate(), scale(), shear(), translate()
|
|---|
| 1715 | */
|
|---|
| 1716 | QMatrix QGraphicsView::matrix() const
|
|---|
| 1717 | {
|
|---|
| 1718 | Q_D(const QGraphicsView);
|
|---|
| 1719 | return d->matrix.toAffine();
|
|---|
| 1720 | }
|
|---|
| 1721 |
|
|---|
| 1722 | /*!
|
|---|
| 1723 | Sets the view's current transformation matrix to \a matrix.
|
|---|
| 1724 |
|
|---|
| 1725 | If \a combine is true, then \a matrix is combined with the current matrix;
|
|---|
| 1726 | otherwise, \a matrix \e replaces the current matrix. \a combine is false
|
|---|
| 1727 | by default.
|
|---|
| 1728 |
|
|---|
| 1729 | The transformation matrix tranforms the scene into view coordinates. Using
|
|---|
| 1730 | the default transformation, provided by the identity matrix, one pixel in
|
|---|
| 1731 | the view represents one unit in the scene (e.g., a 10x10 rectangular item
|
|---|
| 1732 | is drawn using 10x10 pixels in the view). If a 2x2 scaling matrix is
|
|---|
| 1733 | applied, the scene will be drawn in 1:2 (e.g., a 10x10 rectangular item is
|
|---|
| 1734 | then drawn using 20x20 pixels in the view).
|
|---|
| 1735 |
|
|---|
| 1736 | Example:
|
|---|
| 1737 |
|
|---|
| 1738 | \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsview.cpp 3
|
|---|
| 1739 |
|
|---|
| 1740 | To simplify interation with items using a transformed view, QGraphicsView
|
|---|
| 1741 | provides mapTo... and mapFrom... functions that can translate between
|
|---|
| 1742 | scene and view coordinates. For example, you can call mapToScene() to map
|
|---|
| 1743 | a view coordinate to a floating point scene coordinate, or mapFromScene()
|
|---|
| 1744 | to map from floating point scene coordinates to view coordinates.
|
|---|
| 1745 |
|
|---|
| 1746 | \sa matrix(), rotate(), scale(), shear(), translate()
|
|---|
| 1747 | */
|
|---|
| 1748 | void QGraphicsView::setMatrix(const QMatrix &matrix, bool combine)
|
|---|
| 1749 | {
|
|---|
| 1750 | setTransform(QTransform(matrix), combine);
|
|---|
| 1751 | }
|
|---|
| 1752 |
|
|---|
| 1753 | /*!
|
|---|
| 1754 | Resets the view transformation matrix to the identity matrix.
|
|---|
| 1755 | */
|
|---|
| 1756 | void QGraphicsView::resetMatrix()
|
|---|
| 1757 | {
|
|---|
| 1758 | resetTransform();
|
|---|
| 1759 | }
|
|---|
| 1760 |
|
|---|
| 1761 | /*!
|
|---|
| 1762 | Rotates the current view transformation \a angle degrees clockwise.
|
|---|
| 1763 |
|
|---|
| 1764 | \sa setMatrix(), matrix(), scale(), shear(), translate()
|
|---|
| 1765 | */
|
|---|
| 1766 | void QGraphicsView::rotate(qreal angle)
|
|---|
| 1767 | {
|
|---|
| 1768 | Q_D(QGraphicsView);
|
|---|
| 1769 | QTransform matrix = d->matrix;
|
|---|
| 1770 | matrix.rotate(angle);
|
|---|
| 1771 | setTransform(matrix);
|
|---|
| 1772 | }
|
|---|
| 1773 |
|
|---|
| 1774 | /*!
|
|---|
| 1775 | Scales the current view transformation by (\a sx, \a sy).
|
|---|
| 1776 |
|
|---|
| 1777 | \sa setMatrix(), matrix(), rotate(), shear(), translate()
|
|---|
| 1778 | */
|
|---|
| 1779 | void QGraphicsView::scale(qreal sx, qreal sy)
|
|---|
| 1780 | {
|
|---|
| 1781 | Q_D(QGraphicsView);
|
|---|
| 1782 | QTransform matrix = d->matrix;
|
|---|
| 1783 | matrix.scale(sx, sy);
|
|---|
| 1784 | setTransform(matrix);
|
|---|
| 1785 | }
|
|---|
| 1786 |
|
|---|
| 1787 | /*!
|
|---|
| 1788 | Shears the current view transformation by (\a sh, \a sv).
|
|---|
| 1789 |
|
|---|
| 1790 | \sa setMatrix(), matrix(), rotate(), scale(), translate()
|
|---|
| 1791 | */
|
|---|
| 1792 | void QGraphicsView::shear(qreal sh, qreal sv)
|
|---|
| 1793 | {
|
|---|
| 1794 | Q_D(QGraphicsView);
|
|---|
| 1795 | QTransform matrix = d->matrix;
|
|---|
| 1796 | matrix.shear(sh, sv);
|
|---|
| 1797 | setTransform(matrix);
|
|---|
| 1798 | }
|
|---|
| 1799 |
|
|---|
| 1800 | /*!
|
|---|
| 1801 | Translates the current view transformation by (\a dx, \a dy).
|
|---|
| 1802 |
|
|---|
| 1803 | \sa setMatrix(), matrix(), rotate(), shear()
|
|---|
| 1804 | */
|
|---|
| 1805 | void QGraphicsView::translate(qreal dx, qreal dy)
|
|---|
| 1806 | {
|
|---|
| 1807 | Q_D(QGraphicsView);
|
|---|
| 1808 | QTransform matrix = d->matrix;
|
|---|
| 1809 | matrix.translate(dx, dy);
|
|---|
| 1810 | setTransform(matrix);
|
|---|
| 1811 | }
|
|---|
| 1812 |
|
|---|
| 1813 | /*!
|
|---|
| 1814 | Scrolls the contents of the viewport to ensure that the scene
|
|---|
| 1815 | coordinate \a pos, is centered in the view.
|
|---|
| 1816 |
|
|---|
| 1817 | Because \a pos is a floating point coordinate, and the scroll bars operate
|
|---|
| 1818 | on integer coordinates, the centering is only an approximation.
|
|---|
| 1819 |
|
|---|
| 1820 | \note If the item is close to or outside the border, it will be visible
|
|---|
| 1821 | in the view, but not centered.
|
|---|
| 1822 |
|
|---|
| 1823 | \sa ensureVisible()
|
|---|
| 1824 | */
|
|---|
| 1825 | void QGraphicsView::centerOn(const QPointF &pos)
|
|---|
| 1826 | {
|
|---|
| 1827 | Q_D(QGraphicsView);
|
|---|
| 1828 | qreal width = viewport()->width();
|
|---|
| 1829 | qreal height = viewport()->height();
|
|---|
| 1830 | QPointF viewPoint = d->matrix.map(pos);
|
|---|
| 1831 | QPointF oldCenterPoint = pos;
|
|---|
| 1832 |
|
|---|
| 1833 | if (!d->leftIndent) {
|
|---|
| 1834 | if (isRightToLeft()) {
|
|---|
| 1835 | qint64 horizontal = 0;
|
|---|
| 1836 | horizontal += horizontalScrollBar()->minimum();
|
|---|
| 1837 | horizontal += horizontalScrollBar()->maximum();
|
|---|
| 1838 | horizontal -= int(viewPoint.x() - width / 2.0);
|
|---|
| 1839 | horizontalScrollBar()->setValue(horizontal);
|
|---|
| 1840 | } else {
|
|---|
| 1841 | horizontalScrollBar()->setValue(int(viewPoint.x() - width / 2.0));
|
|---|
| 1842 | }
|
|---|
| 1843 | }
|
|---|
| 1844 | if (!d->topIndent)
|
|---|
| 1845 | verticalScrollBar()->setValue(int(viewPoint.y() - height / 2.0));
|
|---|
| 1846 | d->lastCenterPoint = oldCenterPoint;
|
|---|
| 1847 | }
|
|---|
| 1848 |
|
|---|
| 1849 | /*!
|
|---|
| 1850 | \fn QGraphicsView::centerOn(qreal x, qreal y)
|
|---|
| 1851 | \overload
|
|---|
| 1852 |
|
|---|
| 1853 | This function is provided for convenience. It's equivalent to calling
|
|---|
| 1854 | centerOn(QPointF(\a x, \a y)).
|
|---|
| 1855 | */
|
|---|
| 1856 |
|
|---|
| 1857 | /*!
|
|---|
| 1858 | \overload
|
|---|
| 1859 |
|
|---|
| 1860 | Scrolls the contents of the viewport to ensure that \a item
|
|---|
| 1861 | is centered in the view.
|
|---|
| 1862 |
|
|---|
| 1863 | \sa ensureVisible()
|
|---|
| 1864 | */
|
|---|
| 1865 | void QGraphicsView::centerOn(const QGraphicsItem *item)
|
|---|
| 1866 | {
|
|---|
| 1867 | centerOn(item->sceneBoundingRect().center());
|
|---|
| 1868 | }
|
|---|
| 1869 |
|
|---|
| 1870 | /*!
|
|---|
| 1871 | Scrolls the contents of the viewport so that the scene rectangle \a rect
|
|---|
| 1872 | is visible, with margins specified in pixels by \a xmargin and \a
|
|---|
| 1873 | ymargin. If the specified rect cannot be reached, the contents are
|
|---|
| 1874 | scrolled to the nearest valid position. The default value for both margins
|
|---|
| 1875 | is 50 pixels.
|
|---|
| 1876 |
|
|---|
| 1877 | \sa centerOn()
|
|---|
| 1878 | */
|
|---|
| 1879 | void QGraphicsView::ensureVisible(const QRectF &rect, int xmargin, int ymargin)
|
|---|
| 1880 | {
|
|---|
| 1881 | Q_D(QGraphicsView);
|
|---|
| 1882 | Q_UNUSED(xmargin);
|
|---|
| 1883 | Q_UNUSED(ymargin);
|
|---|
| 1884 | qreal width = viewport()->width();
|
|---|
| 1885 | qreal height = viewport()->height();
|
|---|
| 1886 | QRectF viewRect = d->matrix.mapRect(rect);
|
|---|
| 1887 |
|
|---|
| 1888 | qreal left = d->horizontalScroll();
|
|---|
| 1889 | qreal right = left + width;
|
|---|
| 1890 | qreal top = d->verticalScroll();
|
|---|
| 1891 | qreal bottom = top + height;
|
|---|
| 1892 |
|
|---|
| 1893 | if (viewRect.left() <= left + xmargin) {
|
|---|
| 1894 | // need to scroll from the left
|
|---|
| 1895 | if (!d->leftIndent)
|
|---|
| 1896 | horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
|
|---|
| 1897 | }
|
|---|
| 1898 | if (viewRect.right() >= right - xmargin) {
|
|---|
| 1899 | // need to scroll from the right
|
|---|
| 1900 | if (!d->leftIndent)
|
|---|
| 1901 | horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
|
|---|
| 1902 | }
|
|---|
| 1903 | if (viewRect.top() <= top + ymargin) {
|
|---|
| 1904 | // need to scroll from the top
|
|---|
| 1905 | if (!d->topIndent)
|
|---|
| 1906 | verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
|
|---|
| 1907 | }
|
|---|
| 1908 | if (viewRect.bottom() >= bottom - ymargin) {
|
|---|
| 1909 | // need to scroll from the bottom
|
|---|
|
|---|