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