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