| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/graphicsview/qgraphicslayout.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||||||||
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||||||||
| 39 | - | |||||||||||||||||||||||||
| 40 | #include "qapplication.h" | - | ||||||||||||||||||||||||
| 41 | - | |||||||||||||||||||||||||
| 42 | #ifndef QT_NO_GRAPHICSVIEW | - | ||||||||||||||||||||||||
| 43 | #include "qgraphicslayout.h" | - | ||||||||||||||||||||||||
| 44 | #include "qgraphicslayout_p.h" | - | ||||||||||||||||||||||||
| 45 | #include "qgraphicslayoutitem.h" | - | ||||||||||||||||||||||||
| 46 | #include "qgraphicslayoutitem_p.h" | - | ||||||||||||||||||||||||
| 47 | #include "qgraphicswidget.h" | - | ||||||||||||||||||||||||
| 48 | #include "qgraphicswidget_p.h" | - | ||||||||||||||||||||||||
| 49 | #include "qgraphicsscene.h" | - | ||||||||||||||||||||||||
| 50 | - | |||||||||||||||||||||||||
| 51 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
| 52 | - | |||||||||||||||||||||||||
| 53 | /*! | - | ||||||||||||||||||||||||
| 54 | \class QGraphicsLayout | - | ||||||||||||||||||||||||
| 55 | \brief The QGraphicsLayout class provides the base class for all layouts | - | ||||||||||||||||||||||||
| 56 | in Graphics View. | - | ||||||||||||||||||||||||
| 57 | \since 4.4 | - | ||||||||||||||||||||||||
| 58 | \ingroup graphicsview-api | - | ||||||||||||||||||||||||
| 59 | \inmodule QtWidgets | - | ||||||||||||||||||||||||
| 60 | - | |||||||||||||||||||||||||
| 61 | QGraphicsLayout is an abstract class that defines a virtual API for | - | ||||||||||||||||||||||||
| 62 | arranging QGraphicsWidget children and other QGraphicsLayoutItem objects | - | ||||||||||||||||||||||||
| 63 | for a QGraphicsWidget. QGraphicsWidget assigns responsibility to a | - | ||||||||||||||||||||||||
| 64 | QGraphicsLayout through QGraphicsWidget::setLayout(). As the widget | - | ||||||||||||||||||||||||
| 65 | is resized, the layout will automatically arrange the widget's children. | - | ||||||||||||||||||||||||
| 66 | QGraphicsLayout inherits QGraphicsLayoutItem, so, it can be managed by | - | ||||||||||||||||||||||||
| 67 | any layout, including its own subclasses. | - | ||||||||||||||||||||||||
| 68 | - | |||||||||||||||||||||||||
| 69 | \section1 Writing a Custom Layout | - | ||||||||||||||||||||||||
| 70 | - | |||||||||||||||||||||||||
| 71 | You can use QGraphicsLayout as a base to write your own custom layout | - | ||||||||||||||||||||||||
| 72 | (e.g., a flowlayout), but it is more common to use one of its subclasses | - | ||||||||||||||||||||||||
| 73 | instead - QGraphicsLinearLayout or QGraphicsGridLayout. When creating | - | ||||||||||||||||||||||||
| 74 | a custom layout, the following functions must be reimplemented as a bare | - | ||||||||||||||||||||||||
| 75 | minimum: | - | ||||||||||||||||||||||||
| 76 | - | |||||||||||||||||||||||||
| 77 | \table | - | ||||||||||||||||||||||||
| 78 | \header \li Function \li Description | - | ||||||||||||||||||||||||
| 79 | \row \li QGraphicsLayoutItem::setGeometry() | - | ||||||||||||||||||||||||
| 80 | \li Notifies you when the geometry of the layout is set. You can | - | ||||||||||||||||||||||||
| 81 | store the geometry in your own layout class in a reimplementation | - | ||||||||||||||||||||||||
| 82 | of this function. | - | ||||||||||||||||||||||||
| 83 | \row \li QGraphicsLayoutItem::sizeHint() | - | ||||||||||||||||||||||||
| 84 | \li Returns the layout's size hints. | - | ||||||||||||||||||||||||
| 85 | \row \li QGraphicsLayout::count() | - | ||||||||||||||||||||||||
| 86 | \li Returns the number of items in your layout. | - | ||||||||||||||||||||||||
| 87 | \row \li QGraphicsLayout::itemAt() | - | ||||||||||||||||||||||||
| 88 | \li Returns a pointer to an item in your layout. | - | ||||||||||||||||||||||||
| 89 | \row \li QGraphicsLayout::removeAt() | - | ||||||||||||||||||||||||
| 90 | \li Removes an item from your layout without destroying it. | - | ||||||||||||||||||||||||
| 91 | \endtable | - | ||||||||||||||||||||||||
| 92 | - | |||||||||||||||||||||||||
| 93 | For more details on how to implement each function, refer to the individual | - | ||||||||||||||||||||||||
| 94 | function documentation. | - | ||||||||||||||||||||||||
| 95 | - | |||||||||||||||||||||||||
| 96 | Each layout defines its own API for arranging widgets and layout items. | - | ||||||||||||||||||||||||
| 97 | For example, with a grid layout, you require a row and a | - | ||||||||||||||||||||||||
| 98 | column index with optional row and column spans, alignment, spacing, and more. | - | ||||||||||||||||||||||||
| 99 | A linear layout, however, requires a single row or column index to position its | - | ||||||||||||||||||||||||
| 100 | items. For a grid layout, the order of insertion does not affect the layout in | - | ||||||||||||||||||||||||
| 101 | any way, but for a linear layout, the order is essential. When writing your own | - | ||||||||||||||||||||||||
| 102 | layout subclass, you are free to choose the API that best suits your layout. | - | ||||||||||||||||||||||||
| 103 | - | |||||||||||||||||||||||||
| 104 | QGraphicsLayout provides the addChildLayoutItem() convenience function to add | - | ||||||||||||||||||||||||
| 105 | layout items to a custom layout. The function will automatically reparent | - | ||||||||||||||||||||||||
| 106 | graphics items, if required. | - | ||||||||||||||||||||||||
| 107 | - | |||||||||||||||||||||||||
| 108 | \section1 Activating the Layout | - | ||||||||||||||||||||||||
| 109 | - | |||||||||||||||||||||||||
| 110 | When the layout's geometry changes, QGraphicsLayout immediately rearranges | - | ||||||||||||||||||||||||
| 111 | all of its managed items by calling setGeometry() on each item. This | - | ||||||||||||||||||||||||
| 112 | rearrangement is called \e activating the layout. | - | ||||||||||||||||||||||||
| 113 | - | |||||||||||||||||||||||||
| 114 | QGraphicsLayout updates its own geometry to match the contentsRect() of the | - | ||||||||||||||||||||||||
| 115 | QGraphicsLayoutItem it is managing. Thus, it will automatically rearrange all | - | ||||||||||||||||||||||||
| 116 | its items when the widget is resized. QGraphicsLayout caches the sizes of all | - | ||||||||||||||||||||||||
| 117 | its managed items to avoid calling setGeometry() too often. | - | ||||||||||||||||||||||||
| 118 | - | |||||||||||||||||||||||||
| 119 | \note A QGraphicsLayout will have the same geometry as the contentsRect() | - | ||||||||||||||||||||||||
| 120 | of the widget (not the layout) it is assigned to. | - | ||||||||||||||||||||||||
| 121 | - | |||||||||||||||||||||||||
| 122 | \section2 Activating the Layout Implicitly | - | ||||||||||||||||||||||||
| 123 | - | |||||||||||||||||||||||||
| 124 | The layout can be activated implicitly using one of two ways: by calling | - | ||||||||||||||||||||||||
| 125 | activate() or by calling invalidate(). Calling activate() activates the layout | - | ||||||||||||||||||||||||
| 126 | immediately. In contrast, calling invalidate() is delayed, as it posts a | - | ||||||||||||||||||||||||
| 127 | \l{QEvent::LayoutRequest}{LayoutRequest} event to the managed widget. Due | - | ||||||||||||||||||||||||
| 128 | to event compression, the activate() will only be called once after control has | - | ||||||||||||||||||||||||
| 129 | returned to the event loop. This is referred to as \e invalidating the layout. | - | ||||||||||||||||||||||||
| 130 | Invalidating the layout also invalidates any cached information. Also, the | - | ||||||||||||||||||||||||
| 131 | invalidate() function is a virtual function. So, you can invalidate your own | - | ||||||||||||||||||||||||
| 132 | cache in a subclass of QGraphicsLayout by reimplementing this function. | - | ||||||||||||||||||||||||
| 133 | - | |||||||||||||||||||||||||
| 134 | \section1 Event Handling | - | ||||||||||||||||||||||||
| 135 | - | |||||||||||||||||||||||||
| 136 | QGraphicsLayout listens to events for the widget it manages through the | - | ||||||||||||||||||||||||
| 137 | virtual widgetEvent() event handler. When the layout is assigned to a | - | ||||||||||||||||||||||||
| 138 | widget, all events delivered to the widget are first processed by | - | ||||||||||||||||||||||||
| 139 | widgetEvent(). This allows the layout to be aware of any relevant state | - | ||||||||||||||||||||||||
| 140 | changes on the widget such as visibility changes or layout direction changes. | - | ||||||||||||||||||||||||
| 141 | - | |||||||||||||||||||||||||
| 142 | \section1 Margin Handling | - | ||||||||||||||||||||||||
| 143 | - | |||||||||||||||||||||||||
| 144 | The margins of a QGraphicsLayout can be modified by reimplementing | - | ||||||||||||||||||||||||
| 145 | setContentsMargins() and getContentsMargins(). | - | ||||||||||||||||||||||||
| 146 | - | |||||||||||||||||||||||||
| 147 | */ | - | ||||||||||||||||||||||||
| 148 | - | |||||||||||||||||||||||||
| 149 | /*! | - | ||||||||||||||||||||||||
| 150 | Contructs a QGraphicsLayout object. | - | ||||||||||||||||||||||||
| 151 | - | |||||||||||||||||||||||||
| 152 | \a parent is passed to QGraphicsLayoutItem's constructor and the | - | ||||||||||||||||||||||||
| 153 | QGraphicsLayoutItem's isLayout argument is set to \e true. | - | ||||||||||||||||||||||||
| 154 | - | |||||||||||||||||||||||||
| 155 | If \a parent is a QGraphicsWidget the layout will be installed | - | ||||||||||||||||||||||||
| 156 | on that widget. (Note that installing a layout will delete the old one | - | ||||||||||||||||||||||||
| 157 | installed.) | - | ||||||||||||||||||||||||
| 158 | */ | - | ||||||||||||||||||||||||
| 159 | QGraphicsLayout::QGraphicsLayout(QGraphicsLayoutItem *parent) | - | ||||||||||||||||||||||||
| 160 | : QGraphicsLayoutItem(*new QGraphicsLayoutPrivate) | - | ||||||||||||||||||||||||
| 161 | { | - | ||||||||||||||||||||||||
| 162 | setParentLayoutItem(parent); | - | ||||||||||||||||||||||||
| 163 | if (parent && !parent->isLayout()) {
| 0 | ||||||||||||||||||||||||
| 164 | // If a layout has a parent that is not a layout it must be a QGraphicsWidget. | - | ||||||||||||||||||||||||
| 165 | QGraphicsItem *itemParent = parent->graphicsItem(); | - | ||||||||||||||||||||||||
| 166 | if (itemParent && itemParent->isWidget()) {
| 0 | ||||||||||||||||||||||||
| 167 | static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this); | - | ||||||||||||||||||||||||
| 168 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 169 | qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is" | - | ||||||||||||||||||||||||
| 170 | " neither a QGraphicsWidget nor QGraphicsLayout"); | - | ||||||||||||||||||||||||
| 171 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 172 | } | - | ||||||||||||||||||||||||
| 173 | d_func()->sizePolicy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding, QSizePolicy::DefaultType); | - | ||||||||||||||||||||||||
| 174 | setOwnedByLayout(true); | - | ||||||||||||||||||||||||
| 175 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 176 | - | |||||||||||||||||||||||||
| 177 | /*! | - | ||||||||||||||||||||||||
| 178 | \internal | - | ||||||||||||||||||||||||
| 179 | */ | - | ||||||||||||||||||||||||
| 180 | QGraphicsLayout::QGraphicsLayout(QGraphicsLayoutPrivate &dd, QGraphicsLayoutItem *parent) | - | ||||||||||||||||||||||||
| 181 | : QGraphicsLayoutItem(dd) | - | ||||||||||||||||||||||||
| 182 | { | - | ||||||||||||||||||||||||
| 183 | setParentLayoutItem(parent); | - | ||||||||||||||||||||||||
| 184 | if (parent && !parent->isLayout()) {
| 0 | ||||||||||||||||||||||||
| 185 | // If a layout has a parent that is not a layout it must be a QGraphicsWidget. | - | ||||||||||||||||||||||||
| 186 | QGraphicsItem *itemParent = parent->graphicsItem(); | - | ||||||||||||||||||||||||
| 187 | if (itemParent && itemParent->isWidget()) {
| 0 | ||||||||||||||||||||||||
| 188 | static_cast<QGraphicsWidget *>(itemParent)->d_func()->setLayout_helper(this); | - | ||||||||||||||||||||||||
| 189 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 190 | qWarning("QGraphicsLayout::QGraphicsLayout: Attempt to create a layout with a parent that is" | - | ||||||||||||||||||||||||
| 191 | " neither a QGraphicsWidget nor QGraphicsLayout"); | - | ||||||||||||||||||||||||
| 192 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 193 | } | - | ||||||||||||||||||||||||
| 194 | d_func()->sizePolicy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding, QSizePolicy::DefaultType); | - | ||||||||||||||||||||||||
| 195 | setOwnedByLayout(true); | - | ||||||||||||||||||||||||
| 196 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 197 | - | |||||||||||||||||||||||||
| 198 | /*! | - | ||||||||||||||||||||||||
| 199 | Destroys the QGraphicsLayout object. | - | ||||||||||||||||||||||||
| 200 | */ | - | ||||||||||||||||||||||||
| 201 | QGraphicsLayout::~QGraphicsLayout() | - | ||||||||||||||||||||||||
| 202 | { | - | ||||||||||||||||||||||||
| 203 | } | - | ||||||||||||||||||||||||
| 204 | - | |||||||||||||||||||||||||
| 205 | /*! | - | ||||||||||||||||||||||||
| 206 | Sets the contents margins to \a left, \a top, \a right and \a bottom. The | - | ||||||||||||||||||||||||
| 207 | default contents margins for toplevel layouts are style dependent | - | ||||||||||||||||||||||||
| 208 | (by querying the pixelMetric for QStyle::PM_LayoutLeftMargin, | - | ||||||||||||||||||||||||
| 209 | QStyle::PM_LayoutTopMargin, QStyle::PM_LayoutRightMargin and | - | ||||||||||||||||||||||||
| 210 | QStyle::PM_LayoutBottomMargin). | - | ||||||||||||||||||||||||
| 211 | - | |||||||||||||||||||||||||
| 212 | For sublayouts the default margins are 0. | - | ||||||||||||||||||||||||
| 213 | - | |||||||||||||||||||||||||
| 214 | Changing the contents margins automatically invalidates the layout. | - | ||||||||||||||||||||||||
| 215 | - | |||||||||||||||||||||||||
| 216 | \sa invalidate() | - | ||||||||||||||||||||||||
| 217 | */ | - | ||||||||||||||||||||||||
| 218 | void QGraphicsLayout::setContentsMargins(qreal left, qreal top, qreal right, qreal bottom) | - | ||||||||||||||||||||||||
| 219 | { | - | ||||||||||||||||||||||||
| 220 | Q_D(QGraphicsLayout); | - | ||||||||||||||||||||||||
| 221 | if (d->left == left && d->top == top && d->right == right && d->bottom == bottom)
| 0 | ||||||||||||||||||||||||
| 222 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 223 | d->left = left; | - | ||||||||||||||||||||||||
| 224 | d->right = right; | - | ||||||||||||||||||||||||
| 225 | d->top = top; | - | ||||||||||||||||||||||||
| 226 | d->bottom = bottom; | - | ||||||||||||||||||||||||
| 227 | invalidate(); | - | ||||||||||||||||||||||||
| 228 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 229 | - | |||||||||||||||||||||||||
| 230 | /*! | - | ||||||||||||||||||||||||
| 231 | \reimp | - | ||||||||||||||||||||||||
| 232 | */ | - | ||||||||||||||||||||||||
| 233 | void QGraphicsLayout::getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const | - | ||||||||||||||||||||||||
| 234 | { | - | ||||||||||||||||||||||||
| 235 | Q_D(const QGraphicsLayout); | - | ||||||||||||||||||||||||
| 236 | d->getMargin(left, d->left, QStyle::PM_LayoutLeftMargin); | - | ||||||||||||||||||||||||
| 237 | d->getMargin(top, d->top, QStyle::PM_LayoutTopMargin); | - | ||||||||||||||||||||||||
| 238 | d->getMargin(right, d->right, QStyle::PM_LayoutRightMargin); | - | ||||||||||||||||||||||||
| 239 | d->getMargin(bottom, d->bottom, QStyle::PM_LayoutBottomMargin); | - | ||||||||||||||||||||||||
| 240 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 241 | - | |||||||||||||||||||||||||
| 242 | /*! | - | ||||||||||||||||||||||||
| 243 | Activates the layout, causing all items in the layout to be immediately | - | ||||||||||||||||||||||||
| 244 | rearranged. This function is based on calling count() and itemAt(), and | - | ||||||||||||||||||||||||
| 245 | then calling setGeometry() on all items sequentially. When activated, | - | ||||||||||||||||||||||||
| 246 | the layout will adjust its geometry to its parent's contentsRect(). | - | ||||||||||||||||||||||||
| 247 | The parent will then invalidate any layout of its own. | - | ||||||||||||||||||||||||
| 248 | - | |||||||||||||||||||||||||
| 249 | If called in sequence or recursively, e.g., by one of the arranged items | - | ||||||||||||||||||||||||
| 250 | in response to being resized, this function will do nothing. | - | ||||||||||||||||||||||||
| 251 | - | |||||||||||||||||||||||||
| 252 | Note that the layout is free to use geometry caching to optimize this | - | ||||||||||||||||||||||||
| 253 | process. To forcefully invalidate any such cache, you can call | - | ||||||||||||||||||||||||
| 254 | invalidate() before calling activate(). | - | ||||||||||||||||||||||||
| 255 | - | |||||||||||||||||||||||||
| 256 | \sa invalidate() | - | ||||||||||||||||||||||||
| 257 | */ | - | ||||||||||||||||||||||||
| 258 | void QGraphicsLayout::activate() | - | ||||||||||||||||||||||||
| 259 | { | - | ||||||||||||||||||||||||
| 260 | Q_D(QGraphicsLayout); | - | ||||||||||||||||||||||||
| 261 | if (d->activated)
| 0 | ||||||||||||||||||||||||
| 262 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 263 | - | |||||||||||||||||||||||||
| 264 | d->activateRecursive(this); | - | ||||||||||||||||||||||||
| 265 | - | |||||||||||||||||||||||||
| 266 | // we don't call activate on a sublayout, but somebody might. | - | ||||||||||||||||||||||||
| 267 | // Therefore, we walk to the parentitem of the toplevel layout. | - | ||||||||||||||||||||||||
| 268 | QGraphicsLayoutItem *parentItem = this; | - | ||||||||||||||||||||||||
| 269 | while (parentItem && parentItem->isLayout())
| 0 | ||||||||||||||||||||||||
| 270 | parentItem = parentItem->parentLayoutItem(); never executed: parentItem = parentItem->parentLayoutItem(); | 0 | ||||||||||||||||||||||||
| 271 | if (!parentItem)
| 0 | ||||||||||||||||||||||||
| 272 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 273 | Q_ASSERT(!parentItem->isLayout()); | - | ||||||||||||||||||||||||
| 274 | - | |||||||||||||||||||||||||
| 275 | if (QGraphicsLayout::instantInvalidatePropagation()) {
| 0 | ||||||||||||||||||||||||
| 276 | QGraphicsWidget *parentWidget = static_cast<QGraphicsWidget*>(parentItem); | - | ||||||||||||||||||||||||
| 277 | if (!parentWidget->parentLayoutItem()) {
| 0 | ||||||||||||||||||||||||
| 278 | // we've reached the topmost widget, resize it | - | ||||||||||||||||||||||||
| 279 | bool wasResized = parentWidget->testAttribute(Qt::WA_Resized); | - | ||||||||||||||||||||||||
| 280 | parentWidget->resize(parentWidget->size()); | - | ||||||||||||||||||||||||
| 281 | parentWidget->setAttribute(Qt::WA_Resized, wasResized); | - | ||||||||||||||||||||||||
| 282 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 283 | - | |||||||||||||||||||||||||
| 284 | setGeometry(parentItem->contentsRect()); // relayout children | - | ||||||||||||||||||||||||
| 285 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 286 | setGeometry(parentItem->contentsRect()); // relayout children | - | ||||||||||||||||||||||||
| 287 | parentLayoutItem()->updateGeometry(); | - | ||||||||||||||||||||||||
| 288 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 289 | } | - | ||||||||||||||||||||||||
| 290 | - | |||||||||||||||||||||||||
| 291 | /*! | - | ||||||||||||||||||||||||
| 292 | Returns \c true if the layout is currently being activated; otherwise, | - | ||||||||||||||||||||||||
| 293 | returns \c false. If the layout is being activated, this means that it is | - | ||||||||||||||||||||||||
| 294 | currently in the process of rearranging its items (i.e., the activate() | - | ||||||||||||||||||||||||
| 295 | function has been called, and has not yet returned). | - | ||||||||||||||||||||||||
| 296 | - | |||||||||||||||||||||||||
| 297 | \sa activate(), invalidate() | - | ||||||||||||||||||||||||
| 298 | */ | - | ||||||||||||||||||||||||
| 299 | bool QGraphicsLayout::isActivated() const | - | ||||||||||||||||||||||||
| 300 | { | - | ||||||||||||||||||||||||
| 301 | Q_D(const QGraphicsLayout); | - | ||||||||||||||||||||||||
| 302 | return d->activated; never executed: return d->activated; | 0 | ||||||||||||||||||||||||
| 303 | } | - | ||||||||||||||||||||||||
| 304 | - | |||||||||||||||||||||||||
| 305 | /*! | - | ||||||||||||||||||||||||
| 306 | Clears any cached geometry and size hint information in the layout, and | - | ||||||||||||||||||||||||
| 307 | posts a \l{QEvent::LayoutRequest}{LayoutRequest} event to the managed | - | ||||||||||||||||||||||||
| 308 | parent QGraphicsLayoutItem. | - | ||||||||||||||||||||||||
| 309 | - | |||||||||||||||||||||||||
| 310 | \sa activate(), setGeometry() | - | ||||||||||||||||||||||||
| 311 | */ | - | ||||||||||||||||||||||||
| 312 | void QGraphicsLayout::invalidate() | - | ||||||||||||||||||||||||
| 313 | { | - | ||||||||||||||||||||||||
| 314 | if (QGraphicsLayout::instantInvalidatePropagation()) {
| 0 | ||||||||||||||||||||||||
| 315 | updateGeometry(); | - | ||||||||||||||||||||||||
| 316 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 317 | // only mark layouts as invalid (activated = false) if we can post a LayoutRequest event. | - | ||||||||||||||||||||||||
| 318 | QGraphicsLayoutItem *layoutItem = this; | - | ||||||||||||||||||||||||
| 319 | while (layoutItem && layoutItem->isLayout()) {
| 0 | ||||||||||||||||||||||||
| 320 | // we could call updateGeometry(), but what if that method | - | ||||||||||||||||||||||||
| 321 | // does not call the base implementation? In addition, updateGeometry() | - | ||||||||||||||||||||||||
| 322 | // does more than we need. | - | ||||||||||||||||||||||||
| 323 | layoutItem->d_func()->sizeHintCacheDirty = true; | - | ||||||||||||||||||||||||
| 324 | layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true; | - | ||||||||||||||||||||||||
| 325 | layoutItem = layoutItem->parentLayoutItem(); | - | ||||||||||||||||||||||||
| 326 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 327 | if (layoutItem) {
| 0 | ||||||||||||||||||||||||
| 328 | layoutItem->d_func()->sizeHintCacheDirty = true; | - | ||||||||||||||||||||||||
| 329 | layoutItem->d_func()->sizeHintWithConstraintCacheDirty = true; | - | ||||||||||||||||||||||||
| 330 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 331 | - | |||||||||||||||||||||||||
| 332 | bool postIt = layoutItem ? !layoutItem->isLayout() : false;
| 0 | ||||||||||||||||||||||||
| 333 | if (postIt) {
| 0 | ||||||||||||||||||||||||
| 334 | layoutItem = this; | - | ||||||||||||||||||||||||
| 335 | while (layoutItem && layoutItem->isLayout()
| 0 | ||||||||||||||||||||||||
| 336 | && static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated) {
| 0 | ||||||||||||||||||||||||
| 337 | static_cast<QGraphicsLayout*>(layoutItem)->d_func()->activated = false; | - | ||||||||||||||||||||||||
| 338 | layoutItem = layoutItem->parentLayoutItem(); | - | ||||||||||||||||||||||||
| 339 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 340 | if (layoutItem && !layoutItem->isLayout()) {
| 0 | ||||||||||||||||||||||||
| 341 | // If a layout has a parent that is not a layout it must be a QGraphicsWidget. | - | ||||||||||||||||||||||||
| 342 | QApplication::postEvent(static_cast<QGraphicsWidget *>(layoutItem), new QEvent(QEvent::LayoutRequest)); | - | ||||||||||||||||||||||||
| 343 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 344 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 345 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 346 | } | - | ||||||||||||||||||||||||
| 347 | - | |||||||||||||||||||||||||
| 348 | /*! | - | ||||||||||||||||||||||||
| 349 | \reimp | - | ||||||||||||||||||||||||
| 350 | */ | - | ||||||||||||||||||||||||
| 351 | void QGraphicsLayout::updateGeometry() | - | ||||||||||||||||||||||||
| 352 | { | - | ||||||||||||||||||||||||
| 353 | Q_D(QGraphicsLayout); | - | ||||||||||||||||||||||||
| 354 | if (QGraphicsLayout::instantInvalidatePropagation()) {
| 0 | ||||||||||||||||||||||||
| 355 | d->activated = false; | - | ||||||||||||||||||||||||
| 356 | QGraphicsLayoutItem::updateGeometry(); | - | ||||||||||||||||||||||||
| 357 | - | |||||||||||||||||||||||||
| 358 | QGraphicsLayoutItem *parentItem = parentLayoutItem(); | - | ||||||||||||||||||||||||
| 359 | if (!parentItem)
| 0 | ||||||||||||||||||||||||
| 360 | return; never executed: return; | 0 | ||||||||||||||||||||||||
| 361 | - | |||||||||||||||||||||||||
| 362 | if (parentItem->isLayout())
| 0 | ||||||||||||||||||||||||
| 363 | static_cast<QGraphicsLayout *>(parentItem)->invalidate(); never executed: static_cast<QGraphicsLayout *>(parentItem)->invalidate(); | 0 | ||||||||||||||||||||||||
| 364 | else | - | ||||||||||||||||||||||||
| 365 | parentItem->updateGeometry(); never executed: parentItem->updateGeometry(); | 0 | ||||||||||||||||||||||||
| 366 | } else { | - | ||||||||||||||||||||||||
| 367 | QGraphicsLayoutItem::updateGeometry(); | - | ||||||||||||||||||||||||
| 368 | if (QGraphicsLayoutItem *parentItem = parentLayoutItem()) {
| 0 | ||||||||||||||||||||||||
| 369 | if (parentItem->isLayout()) {
| 0 | ||||||||||||||||||||||||
| 370 | parentItem->updateGeometry(); | - | ||||||||||||||||||||||||
| 371 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 372 | invalidate(); | - | ||||||||||||||||||||||||
| 373 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 374 | } | - | ||||||||||||||||||||||||
| 375 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 376 | } | - | ||||||||||||||||||||||||
| 377 | - | |||||||||||||||||||||||||
| 378 | /*! | - | ||||||||||||||||||||||||
| 379 | This virtual event handler receives all events for the managed | - | ||||||||||||||||||||||||
| 380 | widget. QGraphicsLayout uses this event handler to listen for layout | - | ||||||||||||||||||||||||
| 381 | related events such as geometry changes, layout changes or layout | - | ||||||||||||||||||||||||
| 382 | direction changes. | - | ||||||||||||||||||||||||
| 383 | - | |||||||||||||||||||||||||
| 384 | \a e is a pointer to the event. | - | ||||||||||||||||||||||||
| 385 | - | |||||||||||||||||||||||||
| 386 | You can reimplement this event handler to track similar events for your | - | ||||||||||||||||||||||||
| 387 | own custom layout. | - | ||||||||||||||||||||||||
| 388 | - | |||||||||||||||||||||||||
| 389 | \sa QGraphicsWidget::event(), QGraphicsItem::sceneEvent() | - | ||||||||||||||||||||||||
| 390 | */ | - | ||||||||||||||||||||||||
| 391 | void QGraphicsLayout::widgetEvent(QEvent *e) | - | ||||||||||||||||||||||||
| 392 | { | - | ||||||||||||||||||||||||
| 393 | switch (e->type()) { | - | ||||||||||||||||||||||||
| 394 | case QEvent::GraphicsSceneResize: never executed: case QEvent::GraphicsSceneResize: | 0 | ||||||||||||||||||||||||
| 395 | if (isActivated()) {
| 0 | ||||||||||||||||||||||||
| 396 | setGeometry(parentLayoutItem()->contentsRect()); | - | ||||||||||||||||||||||||
| 397 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
| 398 | activate(); // relies on that activate() will call updateGeometry() | - | ||||||||||||||||||||||||
| 399 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 400 | break; never executed: break; | 0 | ||||||||||||||||||||||||
| 401 | case QEvent::LayoutRequest: never executed: case QEvent::LayoutRequest: | 0 | ||||||||||||||||||||||||
| 402 | activate(); | - | ||||||||||||||||||||||||
| 403 | break; never executed: break; | 0 | ||||||||||||||||||||||||
| 404 | case QEvent::LayoutDirectionChange: never executed: case QEvent::LayoutDirectionChange: | 0 | ||||||||||||||||||||||||
| 405 | invalidate(); | - | ||||||||||||||||||||||||
| 406 | break; never executed: break; | 0 | ||||||||||||||||||||||||
| 407 | default: never executed: default: | 0 | ||||||||||||||||||||||||
| 408 | break; never executed: break; | 0 | ||||||||||||||||||||||||
| 409 | } | - | ||||||||||||||||||||||||
| 410 | } | - | ||||||||||||||||||||||||
| 411 | - | |||||||||||||||||||||||||
| 412 | /*! | - | ||||||||||||||||||||||||
| 413 | \fn virtual int QGraphicsLayout::count() const = 0 | - | ||||||||||||||||||||||||
| 414 | - | |||||||||||||||||||||||||
| 415 | This pure virtual function must be reimplemented in a subclass of | - | ||||||||||||||||||||||||
| 416 | QGraphicsLayout to return the number of items in the layout. | - | ||||||||||||||||||||||||
| 417 | - | |||||||||||||||||||||||||
| 418 | The subclass is free to decide how to store the items. | - | ||||||||||||||||||||||||
| 419 | - | |||||||||||||||||||||||||
| 420 | \sa itemAt(), removeAt() | - | ||||||||||||||||||||||||
| 421 | */ | - | ||||||||||||||||||||||||
| 422 | - | |||||||||||||||||||||||||
| 423 | /*! | - | ||||||||||||||||||||||||
| 424 | \fn virtual QGraphicsLayoutItem *QGraphicsLayout::itemAt(int i) const = 0 | - | ||||||||||||||||||||||||
| 425 | - | |||||||||||||||||||||||||
| 426 | This pure virtual function must be reimplemented in a subclass of | - | ||||||||||||||||||||||||
| 427 | QGraphicsLayout to return a pointer to the item at index \a i. The | - | ||||||||||||||||||||||||
| 428 | reimplementation can assume that \a i is valid (i.e., it respects the | - | ||||||||||||||||||||||||
| 429 | value of count()). | - | ||||||||||||||||||||||||
| 430 | Together with count(), it is provided as a means of iterating over all items in a layout. | - | ||||||||||||||||||||||||
| 431 | - | |||||||||||||||||||||||||
| 432 | The subclass is free to decide how to store the items, and the visual arrangement | - | ||||||||||||||||||||||||
| 433 | does not have to be reflected through this function. | - | ||||||||||||||||||||||||
| 434 | - | |||||||||||||||||||||||||
| 435 | \sa count(), removeAt() | - | ||||||||||||||||||||||||
| 436 | */ | - | ||||||||||||||||||||||||
| 437 | - | |||||||||||||||||||||||||
| 438 | /*! | - | ||||||||||||||||||||||||
| 439 | \fn virtual void QGraphicsLayout::removeAt(int index) = 0 | - | ||||||||||||||||||||||||
| 440 | - | |||||||||||||||||||||||||
| 441 | This pure virtual function must be reimplemented in a subclass of | - | ||||||||||||||||||||||||
| 442 | QGraphicsLayout to remove the item at \a index. The | - | ||||||||||||||||||||||||
| 443 | reimplementation can assume that \a index is valid (i.e., it | - | ||||||||||||||||||||||||
| 444 | respects the value of count()). | - | ||||||||||||||||||||||||
| 445 | - | |||||||||||||||||||||||||
| 446 | The implementation must ensure that the parentLayoutItem() of | - | ||||||||||||||||||||||||
| 447 | the removed item does not point to this layout, since the item is | - | ||||||||||||||||||||||||
| 448 | considered to be removed from the layout hierarchy. | - | ||||||||||||||||||||||||
| 449 | - | |||||||||||||||||||||||||
| 450 | If the layout is to be reused between applications, we recommend | - | ||||||||||||||||||||||||
| 451 | that the layout deletes the item, but the graphics view framework | - | ||||||||||||||||||||||||
| 452 | does not depend on this. | - | ||||||||||||||||||||||||
| 453 | - | |||||||||||||||||||||||||
| 454 | The subclass is free to decide how to store the items. | - | ||||||||||||||||||||||||
| 455 | - | |||||||||||||||||||||||||
| 456 | \sa itemAt(), count() | - | ||||||||||||||||||||||||
| 457 | */ | - | ||||||||||||||||||||||||
| 458 | - | |||||||||||||||||||||||||
| 459 | /*! | - | ||||||||||||||||||||||||
| 460 | \since 4.6 | - | ||||||||||||||||||||||||
| 461 | - | |||||||||||||||||||||||||
| 462 | This function is a convenience function provided for custom layouts, and will go through | - | ||||||||||||||||||||||||
| 463 | all items in the layout and reparent their graphics items to the closest QGraphicsWidget | - | ||||||||||||||||||||||||
| 464 | ancestor of the layout. | - | ||||||||||||||||||||||||
| 465 | - | |||||||||||||||||||||||||
| 466 | If \a layoutItem is already in a different layout, it will be removed from that layout. | - | ||||||||||||||||||||||||
| 467 | - | |||||||||||||||||||||||||
| 468 | If custom layouts want special behaviour they can ignore to use this function, and implement | - | ||||||||||||||||||||||||
| 469 | their own behaviour. | - | ||||||||||||||||||||||||
| 470 | - | |||||||||||||||||||||||||
| 471 | \sa graphicsItem() | - | ||||||||||||||||||||||||
| 472 | */ | - | ||||||||||||||||||||||||
| 473 | void QGraphicsLayout::addChildLayoutItem(QGraphicsLayoutItem *layoutItem) | - | ||||||||||||||||||||||||
| 474 | { | - | ||||||||||||||||||||||||
| 475 | Q_D(QGraphicsLayout); | - | ||||||||||||||||||||||||
| 476 | d->addChildLayoutItem(layoutItem); | - | ||||||||||||||||||||||||
| 477 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 478 | - | |||||||||||||||||||||||||
| 479 | static bool g_instantInvalidatePropagation = false; | - | ||||||||||||||||||||||||
| 480 | - | |||||||||||||||||||||||||
| 481 | /*! | - | ||||||||||||||||||||||||
| 482 | \internal | - | ||||||||||||||||||||||||
| 483 | \since 4.8 | - | ||||||||||||||||||||||||
| 484 | \sa instantInvalidatePropagation() | - | ||||||||||||||||||||||||
| 485 | - | |||||||||||||||||||||||||
| 486 | Calling this function with \a enable set to true will enable a feature that | - | ||||||||||||||||||||||||
| 487 | makes propagation of invalidation up to ancestor layout items to be done in | - | ||||||||||||||||||||||||
| 488 | one go. It will propagate up the parentLayoutItem() hierarchy until it has | - | ||||||||||||||||||||||||
| 489 | reached the root. If the root item is a QGraphicsWidget, it will *post* a | - | ||||||||||||||||||||||||
| 490 | layout request to it. When the layout request is consumed it will traverse | - | ||||||||||||||||||||||||
| 491 | down the hierarchy of layouts and widgets and activate all layouts that is | - | ||||||||||||||||||||||||
| 492 | invalid (not activated). This is the recommended behaviour. | - | ||||||||||||||||||||||||
| 493 | - | |||||||||||||||||||||||||
| 494 | If not set it will also propagate up the parentLayoutItem() hierarchy, but | - | ||||||||||||||||||||||||
| 495 | it will stop at the \e{first widget} it encounters, and post a layout | - | ||||||||||||||||||||||||
| 496 | request to the widget. When the layout request is consumed, this might | - | ||||||||||||||||||||||||
| 497 | cause it to continue propagation up to the parentLayoutItem() of the | - | ||||||||||||||||||||||||
| 498 | widget. It will continue in this fashion until it has reached a widget with | - | ||||||||||||||||||||||||
| 499 | no parentLayoutItem(). This strategy might cause drawing artifacts, since | - | ||||||||||||||||||||||||
| 500 | it is not done in one go, and the consumption of layout requests might be | - | ||||||||||||||||||||||||
| 501 | interleaved by consumption of paint events, which might cause significant | - | ||||||||||||||||||||||||
| 502 | flicker. | - | ||||||||||||||||||||||||
| 503 | Note, this is not the recommended behavior, but for compatibility reasons | - | ||||||||||||||||||||||||
| 504 | this is the default behaviour. | - | ||||||||||||||||||||||||
| 505 | */ | - | ||||||||||||||||||||||||
| 506 | void QGraphicsLayout::setInstantInvalidatePropagation(bool enable) | - | ||||||||||||||||||||||||
| 507 | { | - | ||||||||||||||||||||||||
| 508 | g_instantInvalidatePropagation = enable; | - | ||||||||||||||||||||||||
| 509 | } never executed: end of block | 0 | ||||||||||||||||||||||||
| 510 | - | |||||||||||||||||||||||||
| 511 | /*! | - | ||||||||||||||||||||||||
| 512 | \internal | - | ||||||||||||||||||||||||
| 513 | \since 4.8 | - | ||||||||||||||||||||||||
| 514 | \sa setInstantInvalidatePropagation() | - | ||||||||||||||||||||||||
| 515 | - | |||||||||||||||||||||||||
| 516 | returns \c true if the complete widget/layout hierarchy is rearranged in one go. | - | ||||||||||||||||||||||||
| 517 | */ | - | ||||||||||||||||||||||||
| 518 | bool QGraphicsLayout::instantInvalidatePropagation() | - | ||||||||||||||||||||||||
| 519 | { | - | ||||||||||||||||||||||||
| 520 | return g_instantInvalidatePropagation; never executed: return g_instantInvalidatePropagation; | 0 | ||||||||||||||||||||||||
| 521 | } | - | ||||||||||||||||||||||||
| 522 | - | |||||||||||||||||||||||||
| 523 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
| 524 | - | |||||||||||||||||||||||||
| 525 | #endif //QT_NO_GRAPHICSVIEW | - | ||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |