1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qglobal.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_GRAPHICSVIEW
|
---|
45 |
|
---|
46 | #include "qgraphicslayout.h"
|
---|
47 | #include "qgraphicsscene.h"
|
---|
48 | #include "qgraphicslayoutitem.h"
|
---|
49 | #include "qgraphicslayoutitem_p.h"
|
---|
50 | #include "qwidget.h"
|
---|
51 | #include "qgraphicswidget.h"
|
---|
52 |
|
---|
53 | #include <QtDebug>
|
---|
54 |
|
---|
55 | QT_BEGIN_NAMESPACE
|
---|
56 |
|
---|
57 | /*
|
---|
58 | COMBINE_SIZE() is identical to combineSize(), except that it
|
---|
59 | doesn't evaluate 'size' unless necessary.
|
---|
60 | */
|
---|
61 | #define COMBINE_SIZE(result, size) \
|
---|
62 | do { \
|
---|
63 | if ((result).width() < 0 || (result).height() < 0) \
|
---|
64 | combineSize((result), (size)); \
|
---|
65 | } while (false)
|
---|
66 |
|
---|
67 | static void combineSize(QSizeF &result, const QSizeF &size)
|
---|
68 | {
|
---|
69 | if (result.width() < 0)
|
---|
70 | result.setWidth(size.width());
|
---|
71 | if (result.height() < 0)
|
---|
72 | result.setHeight(size.height());
|
---|
73 | }
|
---|
74 |
|
---|
75 | static void boundSize(QSizeF &result, const QSizeF &size)
|
---|
76 | {
|
---|
77 | if (size.width() >= 0 && size.width() < result.width())
|
---|
78 | result.setWidth(size.width());
|
---|
79 | if (size.height() >= 0 && size.height() < result.height())
|
---|
80 | result.setHeight(size.height());
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void expandSize(QSizeF &result, const QSizeF &size)
|
---|
84 | {
|
---|
85 | if (size.width() >= 0 && size.width() > result.width())
|
---|
86 | result.setWidth(size.width());
|
---|
87 | if (size.height() >= 0 && size.height() > result.height())
|
---|
88 | result.setHeight(size.height());
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void normalizeHints(qreal &minimum, qreal &preferred, qreal &maximum, qreal &descent)
|
---|
92 | {
|
---|
93 | if (minimum >= 0 && maximum >= 0 && minimum > maximum)
|
---|
94 | minimum = maximum;
|
---|
95 |
|
---|
96 | if (preferred >= 0) {
|
---|
97 | if (minimum >= 0 && preferred < minimum) {
|
---|
98 | preferred = minimum;
|
---|
99 | } else if (maximum >= 0 && preferred > maximum) {
|
---|
100 | preferred = maximum;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (minimum >= 0 && descent > minimum)
|
---|
105 | descent = minimum;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*!
|
---|
109 | \internal
|
---|
110 | */
|
---|
111 | QGraphicsLayoutItemPrivate::QGraphicsLayoutItemPrivate(QGraphicsLayoutItem *par, bool layout)
|
---|
112 | : parent(par), userSizeHints(0), isLayout(layout), ownedByLayout(false), graphicsItem(0)
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*!
|
---|
117 | \internal
|
---|
118 | */
|
---|
119 | QGraphicsLayoutItemPrivate::~QGraphicsLayoutItemPrivate()
|
---|
120 | {
|
---|
121 | // Remove any lazily allocated data
|
---|
122 | delete[] userSizeHints;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*!
|
---|
126 | \internal
|
---|
127 | */
|
---|
128 | void QGraphicsLayoutItemPrivate::init()
|
---|
129 | {
|
---|
130 | sizeHintCacheDirty = true;
|
---|
131 | sizeHintWithConstraintCacheDirty = true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*!
|
---|
135 | \internal
|
---|
136 | */
|
---|
137 | QSizeF *QGraphicsLayoutItemPrivate::effectiveSizeHints(const QSizeF &constraint) const
|
---|
138 | {
|
---|
139 | Q_Q(const QGraphicsLayoutItem);
|
---|
140 | QSizeF *sizeHintCache;
|
---|
141 | const bool hasConstraint = constraint.width() >= 0 || constraint.height() >= 0;
|
---|
142 | if (hasConstraint) {
|
---|
143 | if (!sizeHintWithConstraintCacheDirty && constraint == cachedConstraint)
|
---|
144 | return cachedSizeHintsWithConstraints;
|
---|
145 | sizeHintCache = cachedSizeHintsWithConstraints;
|
---|
146 | } else {
|
---|
147 | if (!sizeHintCacheDirty)
|
---|
148 | return cachedSizeHints;
|
---|
149 | sizeHintCache = cachedSizeHints;
|
---|
150 | }
|
---|
151 |
|
---|
152 | for (int i = 0; i < Qt::NSizeHints; ++i) {
|
---|
153 | sizeHintCache[i] = constraint;
|
---|
154 | if (userSizeHints)
|
---|
155 | combineSize(sizeHintCache[i], userSizeHints[i]);
|
---|
156 | }
|
---|
157 |
|
---|
158 | QSizeF &minS = sizeHintCache[Qt::MinimumSize];
|
---|
159 | QSizeF &prefS = sizeHintCache[Qt::PreferredSize];
|
---|
160 | QSizeF &maxS = sizeHintCache[Qt::MaximumSize];
|
---|
161 | QSizeF &descentS = sizeHintCache[Qt::MinimumDescent];
|
---|
162 |
|
---|
163 | normalizeHints(minS.rwidth(), prefS.rwidth(), maxS.rwidth(), descentS.rwidth());
|
---|
164 | normalizeHints(minS.rheight(), prefS.rheight(), maxS.rheight(), descentS.rheight());
|
---|
165 |
|
---|
166 | // if the minimum, preferred and maximum sizes contradict each other
|
---|
167 | // (e.g. the minimum is larger than the maximum) we give priority to
|
---|
168 | // the maximum size, then the minimum size and finally the preferred size
|
---|
169 | COMBINE_SIZE(maxS, q->sizeHint(Qt::MaximumSize, maxS));
|
---|
170 | combineSize(maxS, QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
|
---|
171 | expandSize(maxS, prefS);
|
---|
172 | expandSize(maxS, minS);
|
---|
173 | boundSize(maxS, QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
|
---|
174 |
|
---|
175 | COMBINE_SIZE(minS, q->sizeHint(Qt::MinimumSize, minS));
|
---|
176 | expandSize(minS, QSizeF(0, 0));
|
---|
177 | boundSize(minS, prefS);
|
---|
178 | boundSize(minS, maxS);
|
---|
179 |
|
---|
180 | COMBINE_SIZE(prefS, q->sizeHint(Qt::PreferredSize, prefS));
|
---|
181 | expandSize(prefS, minS);
|
---|
182 | boundSize(prefS, maxS);
|
---|
183 |
|
---|
184 | // Not supported yet
|
---|
185 | // COMBINE_SIZE(descentS, q->sizeHint(Qt::MinimumDescent, constraint));
|
---|
186 |
|
---|
187 | if (hasConstraint) {
|
---|
188 | cachedConstraint = constraint;
|
---|
189 | sizeHintWithConstraintCacheDirty = false;
|
---|
190 | } else {
|
---|
191 | sizeHintCacheDirty = false;
|
---|
192 | }
|
---|
193 | return sizeHintCache;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /*!
|
---|
198 | \internal
|
---|
199 |
|
---|
200 | Returns the parent item of this layout, or 0 if this layout is
|
---|
201 | not installed on any widget.
|
---|
202 |
|
---|
203 | If this is the item that the layout is installed on, it will return "itself".
|
---|
204 |
|
---|
205 | If the layout is a sub-layout, this function returns the parent
|
---|
206 | widget of the parent layout.
|
---|
207 |
|
---|
208 | Note that it will traverse up the layout item hierarchy instead of just calling
|
---|
209 | QGraphicsItem::parentItem(). This is on purpose.
|
---|
210 |
|
---|
211 | \sa parent()
|
---|
212 | */
|
---|
213 | QGraphicsItem *QGraphicsLayoutItemPrivate::parentItem() const
|
---|
214 | {
|
---|
215 | Q_Q(const QGraphicsLayoutItem);
|
---|
216 |
|
---|
217 | const QGraphicsLayoutItem *parent = q;
|
---|
218 | while (parent && parent->isLayout()) {
|
---|
219 | parent = parent->parentLayoutItem();
|
---|
220 | }
|
---|
221 | return parent ? parent->graphicsItem() : 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /*!
|
---|
225 | \internal
|
---|
226 |
|
---|
227 | Ensures that userSizeHints is allocated.
|
---|
228 | This function must be called before any dereferencing.
|
---|
229 | */
|
---|
230 | void QGraphicsLayoutItemPrivate::ensureUserSizeHints()
|
---|
231 | {
|
---|
232 | if (!userSizeHints)
|
---|
233 | userSizeHints = new QSizeF[Qt::NSizeHints];
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*!
|
---|
237 | \internal
|
---|
238 |
|
---|
239 | Sets the user size hint \a which to \a size. Use an invalid size to unset the size hint.
|
---|
240 | */
|
---|
241 | void QGraphicsLayoutItemPrivate::setSize(Qt::SizeHint which, const QSizeF &size)
|
---|
242 | {
|
---|
243 | Q_Q(QGraphicsLayoutItem);
|
---|
244 |
|
---|
245 | if (userSizeHints) {
|
---|
246 | if (size == userSizeHints[which])
|
---|
247 | return;
|
---|
248 | } else if (size.width() < 0 && size.height() < 0) {
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | ensureUserSizeHints();
|
---|
253 | userSizeHints[which] = size;
|
---|
254 | q->updateGeometry();
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*!
|
---|
258 | \internal
|
---|
259 |
|
---|
260 | Sets the width of the user size hint \a which to \a width.
|
---|
261 | */
|
---|
262 | void QGraphicsLayoutItemPrivate::setSizeComponent(
|
---|
263 | Qt::SizeHint which, SizeComponent component, qreal value)
|
---|
264 | {
|
---|
265 | Q_Q(QGraphicsLayoutItem);
|
---|
266 | ensureUserSizeHints();
|
---|
267 | qreal &userValue = (component == Width)
|
---|
268 | ? userSizeHints[which].rwidth()
|
---|
269 | : userSizeHints[which].rheight();
|
---|
270 | if (value == userValue)
|
---|
271 | return;
|
---|
272 | userValue = value;
|
---|
273 | q->updateGeometry();
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | bool QGraphicsLayoutItemPrivate::hasHeightForWidth() const
|
---|
278 | {
|
---|
279 | Q_Q(const QGraphicsLayoutItem);
|
---|
280 | if (isLayout) {
|
---|
281 | const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
|
---|
282 | for (int i = l->count() - 1; i >= 0; --i) {
|
---|
283 | if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasHeightForWidth())
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 | } else if (QGraphicsItem *item = q->graphicsItem()) {
|
---|
287 | if (item->isWidget()) {
|
---|
288 | QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
|
---|
289 | if (w->layout()) {
|
---|
290 | return QGraphicsLayoutItemPrivate::get(w->layout())->hasHeightForWidth();
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 | return q->sizePolicy().hasHeightForWidth();
|
---|
295 | }
|
---|
296 |
|
---|
297 | bool QGraphicsLayoutItemPrivate::hasWidthForHeight() const
|
---|
298 | {
|
---|
299 | // enable this code when we add QSizePolicy::hasWidthForHeight() (For 4.8)
|
---|
300 | #if 1
|
---|
301 | return false;
|
---|
302 | #else
|
---|
303 | Q_Q(const QGraphicsLayoutItem);
|
---|
304 | if (isLayout) {
|
---|
305 | const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
|
---|
306 | for (int i = l->count() - 1; i >= 0; --i) {
|
---|
307 | if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasWidthForHeight())
|
---|
308 | return true;
|
---|
309 | }
|
---|
310 | } else if (QGraphicsItem *item = q->graphicsItem()) {
|
---|
311 | if (item->isWidget()) {
|
---|
312 | QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
|
---|
313 | if (w->layout()) {
|
---|
314 | return QGraphicsLayoutItemPrivate::get(w->layout())->hasWidthForHeight();
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return q->sizePolicy().hasWidthForHeight();
|
---|
319 | #endif
|
---|
320 | }
|
---|
321 |
|
---|
322 | /*!
|
---|
323 | \class QGraphicsLayoutItem
|
---|
324 | \brief The QGraphicsLayoutItem class can be inherited to allow your custom
|
---|
325 | items to be managed by layouts.
|
---|
326 | \since 4.4
|
---|
327 | \ingroup graphicsview-api
|
---|
328 |
|
---|
329 | QGraphicsLayoutItem is an abstract class that defines a set of virtual
|
---|
330 | functions describing sizes, size policies, and size hints for any object
|
---|
331 | arranged by QGraphicsLayout. The API contains functions relevant
|
---|
332 | for both the item itself and for the user of the item as most of
|
---|
333 | QGraphicsLayoutItem's functions are also part of the subclass' public API.
|
---|
334 |
|
---|
335 | In most cases, existing layout-aware classes such as QGraphicsWidget and
|
---|
336 | QGraphicsLayout already provide the functionality you require. However,
|
---|
337 | subclassing these classes will enable you to create both graphical
|
---|
338 | elements that work well with layouts (QGraphicsWidget) or custom layouts
|
---|
339 | (QGraphicsLayout).
|
---|
340 |
|
---|
341 | \section1 Subclassing QGraphicsLayoutItem
|
---|
342 |
|
---|
343 | If you create a subclass of QGraphicsLayoutItem and reimplement its
|
---|
344 | virtual functions, you will enable the layout to resize and position your
|
---|
345 | item along with other QGraphicsLayoutItems including QGraphicsWidget
|
---|
346 | and QGraphicsLayout.
|
---|
347 |
|
---|
348 | You can start by reimplementing important functions: the protected
|
---|
349 | sizeHint() function, as well as the public setGeometry()
|
---|
350 | function. If you want your items to be aware of immediate geometry
|
---|
351 | changes, you can also reimplement updateGeometry().
|
---|
352 |
|
---|
353 | The geometry, size hint, and size policy affect the item's size and
|
---|
354 | position. Calling setGeometry() will always resize and reposition the item
|
---|
355 | immediately. Normally, this function is called by QGraphicsLayout after
|
---|
356 | the layout has been activated, but it can also be called by the item's user
|
---|
357 | at any time.
|
---|
358 |
|
---|
359 | The sizeHint() function returns the item' minimum, preferred and maximum
|
---|
360 | size hints. You can override these properties by calling setMinimumSize(),
|
---|
361 | setPreferredSize() or setMaximumSize(). You can also use functions such as
|
---|
362 | setMinimumWidth() or setMaximumHeight() to set only the width or height
|
---|
363 | component if desired.
|
---|
364 |
|
---|
365 | The effectiveSizeHint() function, on the other hand, returns a size hint
|
---|
366 | for any given Qt::SizeHint, and guarantees that the returned size is bound
|
---|
367 | to the minimum and maximum sizes and size hints. You can set the item's
|
---|
368 | vertical and horizontal size policy by calling setSizePolicy(). The
|
---|
369 | sizePolicy property is used by the layout system to describe how this item
|
---|
370 | prefers to grow or shrink.
|
---|
371 |
|
---|
372 | \section1 Nesting QGraphicsLayoutItems
|
---|
373 |
|
---|
374 | QGraphicsLayoutItems can be nested within other QGraphicsLayoutItems,
|
---|
375 | similar to layouts that can contain sublayouts. This is done either by
|
---|
376 | passing a QGraphicsLayoutItem pointer to QGraphicsLayoutItem's
|
---|
377 | protected constructor, or by calling setParentLayoutItem(). The
|
---|
378 | parentLayoutItem() function returns a pointer to the item's layoutItem
|
---|
379 | parent. If the item's parent is 0 or if the parent does not inherit
|
---|
380 | from QGraphicsItem, the parentLayoutItem() function then returns 0.
|
---|
381 | isLayout() returns true if the QGraphicsLayoutItem subclass is itself a
|
---|
382 | layout, or false otherwise.
|
---|
383 |
|
---|
384 | Qt uses QGraphicsLayoutItem to provide layout functionality in the
|
---|
385 | \l{Graphics View Framework}, but in the future its use may spread
|
---|
386 | throughout Qt itself.
|
---|
387 |
|
---|
388 | \sa QGraphicsWidget, QGraphicsLayout, QGraphicsLinearLayout,
|
---|
389 | QGraphicsGridLayout
|
---|
390 | */
|
---|
391 |
|
---|
392 | /*!
|
---|
393 | Constructs the QGraphicsLayoutItem object. \a parent becomes the object's
|
---|
394 | parent. If \a isLayout is true the item is a layout, otherwise
|
---|
395 | \a isLayout is false.
|
---|
396 | */
|
---|
397 | QGraphicsLayoutItem::QGraphicsLayoutItem(QGraphicsLayoutItem *parent, bool isLayout)
|
---|
398 | : d_ptr(new QGraphicsLayoutItemPrivate(parent, isLayout))
|
---|
399 | {
|
---|
400 | Q_D(QGraphicsLayoutItem);
|
---|
401 | d->init();
|
---|
402 | d->sizePolicy = QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
---|
403 | d->q_ptr = this;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /*!
|
---|
407 | \internal
|
---|
408 | */
|
---|
409 | QGraphicsLayoutItem::QGraphicsLayoutItem(QGraphicsLayoutItemPrivate &dd)
|
---|
410 | : d_ptr(&dd)
|
---|
411 | {
|
---|
412 | Q_D(QGraphicsLayoutItem);
|
---|
413 | d->init();
|
---|
414 | d->q_ptr = this;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /*!
|
---|
418 | Destroys the QGraphicsLayoutItem object.
|
---|
419 | */
|
---|
420 | QGraphicsLayoutItem::~QGraphicsLayoutItem()
|
---|
421 | {
|
---|
422 | QGraphicsLayoutItem *parentLI = parentLayoutItem();
|
---|
423 | if (parentLI && parentLI->isLayout()) {
|
---|
424 | QGraphicsLayout *lay = static_cast<QGraphicsLayout*>(parentLI);
|
---|
425 | // this is not optimal
|
---|
426 | for (int i = lay->count() - 1; i >= 0; --i) {
|
---|
427 | if (lay->itemAt(i) == this) {
|
---|
428 | lay->removeAt(i);
|
---|
429 | break;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | /*!
|
---|
436 | \fn virtual QSizeF QGraphicsLayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const = 0;
|
---|
437 |
|
---|
438 | This pure virtual function returns the size hint for \a which of the
|
---|
439 | QGraphicsLayoutItem, using the width or height of \a constraint to
|
---|
440 | constrain the output.
|
---|
441 |
|
---|
442 | Reimplement this function in a subclass of QGraphicsLayoutItem to
|
---|
443 | provide the necessary size hints for your items.
|
---|
444 |
|
---|
445 | \sa effectiveSizeHint()
|
---|
446 | */
|
---|
447 |
|
---|
448 | /*!
|
---|
449 | Sets the size policy to \a policy. The size policy describes how the item
|
---|
450 | should grow horizontally and vertically when arranged in a layout.
|
---|
451 |
|
---|
452 | QGraphicsLayoutItem's default size policy is (QSizePolicy::Fixed,
|
---|
453 | QSizePolicy::Fixed, QSizePolicy::DefaultType), but it is common for
|
---|
454 | subclasses to change the default. For example, QGraphicsWidget defaults
|
---|
455 | to (QSizePolicy::Preferred, QSizePolicy::Preferred,
|
---|
456 | QSizePolicy::DefaultType).
|
---|
457 |
|
---|
458 | \sa sizePolicy(), QWidget::sizePolicy()
|
---|
459 | */
|
---|
460 | void QGraphicsLayoutItem::setSizePolicy(const QSizePolicy &policy)
|
---|
461 | {
|
---|
462 | Q_D(QGraphicsLayoutItem);
|
---|
463 | if (d->sizePolicy == policy)
|
---|
464 | return;
|
---|
465 | d->sizePolicy = policy;
|
---|
466 | updateGeometry();
|
---|
467 | }
|
---|
468 |
|
---|
469 | /*!
|
---|
470 | \overload
|
---|
471 |
|
---|
472 | This function is equivalent to calling
|
---|
473 | setSizePolicy(QSizePolicy(\a hPolicy, \a vPolicy, \a controlType)).
|
---|
474 |
|
---|
475 | \sa sizePolicy(), QWidget::sizePolicy()
|
---|
476 | */
|
---|
477 | void QGraphicsLayoutItem::setSizePolicy(QSizePolicy::Policy hPolicy,
|
---|
478 | QSizePolicy::Policy vPolicy,
|
---|
479 | QSizePolicy::ControlType controlType)
|
---|
480 | {
|
---|
481 | setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType));
|
---|
482 | }
|
---|
483 |
|
---|
484 | /*!
|
---|
485 | Returns the current size policy.
|
---|
486 |
|
---|
487 | \sa setSizePolicy(), QWidget::sizePolicy()
|
---|
488 | */
|
---|
489 | QSizePolicy QGraphicsLayoutItem::sizePolicy() const
|
---|
490 | {
|
---|
491 | Q_D(const QGraphicsLayoutItem);
|
---|
492 | return d->sizePolicy;
|
---|
493 | }
|
---|
494 |
|
---|
495 | /*!
|
---|
496 | Sets the minimum size to \a size. This property overrides sizeHint() for
|
---|
497 | Qt::MinimumSize and ensures that effectiveSizeHint() will never return
|
---|
498 | a size smaller than \a size. In order to unset the minimum size, use an
|
---|
499 | invalid size.
|
---|
500 |
|
---|
501 | \sa minimumSize(), maximumSize(), preferredSize(), Qt::MinimumSize,
|
---|
502 | sizeHint(), setMinimumWidth(), setMinimumHeight()
|
---|
503 | */
|
---|
504 | void QGraphicsLayoutItem::setMinimumSize(const QSizeF &size)
|
---|
505 | {
|
---|
506 | d_ptr->setSize(Qt::MinimumSize, size);
|
---|
507 | }
|
---|
508 |
|
---|
509 | /*!
|
---|
510 | \fn QGraphicsLayoutItem::setMinimumSize(qreal w, qreal h)
|
---|
511 |
|
---|
512 | This convenience function is equivalent to calling
|
---|
513 | setMinimumSize(QSizeF(\a w, \a h)).
|
---|
514 |
|
---|
515 | \sa minimumSize(), setMaximumSize(), setPreferredSize(), sizeHint()
|
---|
516 | */
|
---|
517 |
|
---|
518 | /*!
|
---|
519 | Returns the minimum size.
|
---|
520 |
|
---|
521 | \sa setMinimumSize(), preferredSize(), maximumSize(), Qt::MinimumSize,
|
---|
522 | sizeHint()
|
---|
523 | */
|
---|
524 | QSizeF QGraphicsLayoutItem::minimumSize() const
|
---|
525 | {
|
---|
526 | return effectiveSizeHint(Qt::MinimumSize);
|
---|
527 | }
|
---|
528 |
|
---|
529 | /*!
|
---|
530 | Sets the minimum width to \a width.
|
---|
531 |
|
---|
532 | \sa minimumWidth(), setMinimumSize(), minimumSize()
|
---|
533 | */
|
---|
534 | void QGraphicsLayoutItem::setMinimumWidth(qreal width)
|
---|
535 | {
|
---|
536 | d_ptr->setSizeComponent(Qt::MinimumSize, d_ptr->Width, width);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /*!
|
---|
540 | Sets the minimum height to \a height.
|
---|
541 |
|
---|
542 | \sa minimumHeight(), setMinimumSize(), minimumSize()
|
---|
543 | */
|
---|
544 | void QGraphicsLayoutItem::setMinimumHeight(qreal height)
|
---|
545 | {
|
---|
546 | d_ptr->setSizeComponent(Qt::MinimumSize, d_ptr->Height, height);
|
---|
547 | }
|
---|
548 |
|
---|
549 |
|
---|
550 | /*!
|
---|
551 | Sets the preferred size to \a size. This property overrides sizeHint() for
|
---|
552 | Qt::PreferredSize and provides the default value for effectiveSizeHint().
|
---|
553 | In order to unset the preferred size, use an invalid size.
|
---|
554 |
|
---|
555 | \sa preferredSize(), minimumSize(), maximumSize(), Qt::PreferredSize,
|
---|
556 | sizeHint()
|
---|
557 | */
|
---|
558 | void QGraphicsLayoutItem::setPreferredSize(const QSizeF &size)
|
---|
559 | {
|
---|
560 | d_ptr->setSize(Qt::PreferredSize, size);
|
---|
561 | }
|
---|
562 |
|
---|
563 | /*!
|
---|
564 | \fn QGraphicsLayoutItem::setPreferredSize(qreal w, qreal h)
|
---|
565 |
|
---|
566 | This convenience function is equivalent to calling
|
---|
567 | setPreferredSize(QSizeF(\a w, \a h)).
|
---|
568 |
|
---|
569 | \sa preferredSize(), setMaximumSize(), setMinimumSize(), sizeHint()
|
---|
570 | */
|
---|
571 |
|
---|
572 | /*!
|
---|
573 | Returns the preferred size.
|
---|
574 |
|
---|
575 | \sa setPreferredSize(), minimumSize(), maximumSize(), Qt::PreferredSize,
|
---|
576 | sizeHint()
|
---|
577 | */
|
---|
578 | QSizeF QGraphicsLayoutItem::preferredSize() const
|
---|
579 | {
|
---|
580 | return effectiveSizeHint(Qt::PreferredSize);
|
---|
581 | }
|
---|
582 |
|
---|
583 | /*!
|
---|
584 | Sets the preferred height to \a height.
|
---|
585 |
|
---|
586 | \sa preferredWidth(), setPreferredSize(), preferredSize()
|
---|
587 | */
|
---|
588 | void QGraphicsLayoutItem::setPreferredHeight(qreal height)
|
---|
589 | {
|
---|
590 | d_ptr->setSizeComponent(Qt::PreferredSize, d_ptr->Height, height);
|
---|
591 | }
|
---|
592 |
|
---|
593 | /*!
|
---|
594 | Sets the preferred width to \a width.
|
---|
595 |
|
---|
596 | \sa preferredHeight(), setPreferredSize(), preferredSize()
|
---|
597 | */
|
---|
598 | void QGraphicsLayoutItem::setPreferredWidth(qreal width)
|
---|
599 | {
|
---|
600 | d_ptr->setSizeComponent(Qt::PreferredSize, d_ptr->Width, width);
|
---|
601 | }
|
---|
602 |
|
---|
603 | /*!
|
---|
604 | Sets the maximum size to \a size. This property overrides sizeHint() for
|
---|
605 | Qt::MaximumSize and ensures that effectiveSizeHint() will never return a
|
---|
606 | size larger than \a size. In order to unset the maximum size, use an
|
---|
607 | invalid size.
|
---|
608 |
|
---|
609 | \sa maximumSize(), minimumSize(), preferredSize(), Qt::MaximumSize,
|
---|
610 | sizeHint()
|
---|
611 | */
|
---|
612 | void QGraphicsLayoutItem::setMaximumSize(const QSizeF &size)
|
---|
613 | {
|
---|
614 | d_ptr->setSize(Qt::MaximumSize, size);
|
---|
615 | }
|
---|
616 |
|
---|
617 | /*!
|
---|
618 | \fn QGraphicsLayoutItem::setMaximumSize(qreal w, qreal h)
|
---|
619 |
|
---|
620 | This convenience function is equivalent to calling
|
---|
621 | setMaximumSize(QSizeF(\a w, \a h)).
|
---|
622 |
|
---|
623 | \sa maximumSize(), setMinimumSize(), setPreferredSize(), sizeHint()
|
---|
624 | */
|
---|
625 |
|
---|
626 | /*!
|
---|
627 | Returns the maximum size.
|
---|
628 |
|
---|
629 | \sa setMaximumSize(), minimumSize(), preferredSize(), Qt::MaximumSize,
|
---|
630 | sizeHint()
|
---|
631 | */
|
---|
632 | QSizeF QGraphicsLayoutItem::maximumSize() const
|
---|
633 | {
|
---|
634 | return effectiveSizeHint(Qt::MaximumSize);
|
---|
635 | }
|
---|
636 |
|
---|
637 | /*!
|
---|
638 | Sets the maximum width to \a width.
|
---|
639 |
|
---|
640 | \sa maximumWidth(), setMaximumSize(), maximumSize()
|
---|
641 | */
|
---|
642 | void QGraphicsLayoutItem::setMaximumWidth(qreal width)
|
---|
643 | {
|
---|
644 | d_ptr->setSizeComponent(Qt::MaximumSize, d_ptr->Width, width);
|
---|
645 | }
|
---|
646 |
|
---|
647 | /*!
|
---|
648 | Sets the maximum height to \a height.
|
---|
649 |
|
---|
650 | \sa maximumHeight(), setMaximumSize(), maximumSize()
|
---|
651 | */
|
---|
652 | void QGraphicsLayoutItem::setMaximumHeight(qreal height)
|
---|
653 | {
|
---|
654 | d_ptr->setSizeComponent(Qt::MaximumSize, d_ptr->Height, height);
|
---|
655 | }
|
---|
656 |
|
---|
657 | /*!
|
---|
658 | \fn qreal QGraphicsLayoutItem::minimumWidth() const
|
---|
659 |
|
---|
660 | Returns the minimum width.
|
---|
661 |
|
---|
662 | \sa setMinimumWidth(), setMinimumSize(), minimumSize()
|
---|
663 | */
|
---|
664 |
|
---|
665 | /*!
|
---|
666 | \fn qreal QGraphicsLayoutItem::minimumHeight() const
|
---|
667 |
|
---|
668 | Returns the minimum height.
|
---|
669 |
|
---|
670 | \sa setMinimumHeight(), setMinimumSize(), minimumSize()
|
---|
671 | */
|
---|
672 |
|
---|
673 | /*!
|
---|
674 | \fn qreal QGraphicsLayoutItem::preferredWidth() const
|
---|
675 |
|
---|
676 | Returns the preferred width.
|
---|
677 |
|
---|
678 | \sa setPreferredWidth(), setPreferredSize(), preferredSize()
|
---|
679 | */
|
---|
680 |
|
---|
681 | /*!
|
---|
682 | \fn qreal QGraphicsLayoutItem::preferredHeight() const
|
---|
683 |
|
---|
684 | Returns the preferred height.
|
---|
685 |
|
---|
686 | \sa setPreferredHeight(), setPreferredSize(), preferredSize()
|
---|
687 | */
|
---|
688 |
|
---|
689 | /*!
|
---|
690 | \fn qreal QGraphicsLayoutItem::maximumWidth() const
|
---|
691 |
|
---|
692 | Returns the maximum width.
|
---|
693 |
|
---|
694 | \sa setMaximumWidth(), setMaximumSize(), maximumSize()
|
---|
695 | */
|
---|
696 |
|
---|
697 | /*!
|
---|
698 | \fn qreal QGraphicsLayoutItem::maximumHeight() const
|
---|
699 |
|
---|
700 | Returns the maximum height.
|
---|
701 |
|
---|
702 | \sa setMaximumHeight(), setMaximumSize(), maximumSize()
|
---|
703 | */
|
---|
704 |
|
---|
705 | /*!
|
---|
706 | \fn virtual void QGraphicsLayoutItem::setGeometry(const QRectF &rect)
|
---|
707 |
|
---|
708 | This virtual function sets the geometry of the QGraphicsLayoutItem to
|
---|
709 | \a rect, which is in parent coordinates (e.g., the top-left corner of \a rect
|
---|
710 | is equivalent to the item's position in parent coordinates).
|
---|
711 |
|
---|
712 | You must reimplement this function in a subclass of QGraphicsLayoutItem to
|
---|
713 | receive geometry updates. The layout will call this function when it does a
|
---|
714 | rearrangement.
|
---|
715 |
|
---|
716 | If \a rect is outside of the bounds of minimumSize and maximumSize, it
|
---|
717 | will be adjusted to its closest size so that it is within the legal
|
---|
718 | bounds.
|
---|
719 |
|
---|
720 | \sa geometry()
|
---|
721 | */
|
---|
722 | void QGraphicsLayoutItem::setGeometry(const QRectF &rect)
|
---|
723 | {
|
---|
724 | Q_D(QGraphicsLayoutItem);
|
---|
725 | QSizeF effectiveSize = rect.size().expandedTo(effectiveSizeHint(Qt::MinimumSize))
|
---|
726 | .boundedTo(effectiveSizeHint(Qt::MaximumSize));
|
---|
727 | d->geom = QRectF(rect.topLeft(), effectiveSize);
|
---|
728 | }
|
---|
729 |
|
---|
730 | /*!
|
---|
731 | \fn QRectF QGraphicsLayoutItem::geometry() const
|
---|
732 |
|
---|
733 | Returns the item's geometry (e.g., position and size) as a
|
---|
734 | QRectF. This function is equivalent to QRectF(pos(), size()).
|
---|
735 |
|
---|
736 | \sa setGeometry()
|
---|
737 | */
|
---|
738 | QRectF QGraphicsLayoutItem::geometry() const
|
---|
739 | {
|
---|
740 | Q_D(const QGraphicsLayoutItem);
|
---|
741 | return d->geom;
|
---|
742 | }
|
---|
743 |
|
---|
744 | /*!
|
---|
745 | This virtual function provides the \a left, \a top, \a right and \a bottom
|
---|
746 | contents margins for this QGraphicsLayoutItem. The default implementation
|
---|
747 | assumes all contents margins are 0. The parameters point to values stored
|
---|
748 | in qreals. If any of the pointers is 0, that value will not be updated.
|
---|
749 |
|
---|
750 | \sa QGraphicsWidget::setContentsMargins()
|
---|
751 | */
|
---|
752 | void QGraphicsLayoutItem::getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
|
---|
753 | {
|
---|
754 | if (left)
|
---|
755 | *left = 0;
|
---|
756 | if (top)
|
---|
757 | *top = 0;
|
---|
758 | if (right)
|
---|
759 | *right = 0;
|
---|
760 | if (bottom)
|
---|
761 | *bottom = 0;
|
---|
762 | }
|
---|
763 |
|
---|
764 | /*!
|
---|
765 | Returns the contents rect in local coordinates.
|
---|
766 |
|
---|
767 | The contents rect defines the subrectangle used by an associated layout
|
---|
768 | when arranging subitems. This function is a convenience function that
|
---|
769 | adjusts the item's geometry() by its contents margins. Note that
|
---|
770 | getContentsMargins() is a virtual function that you can reimplement to
|
---|
771 | return the item's contents margins.
|
---|
772 |
|
---|
773 | \sa getContentsMargins(), geometry()
|
---|
774 | */
|
---|
775 | QRectF QGraphicsLayoutItem::contentsRect() const
|
---|
776 | {
|
---|
777 | qreal left, top, right, bottom;
|
---|
778 | getContentsMargins(&left, &top, &right, &bottom);
|
---|
779 | return QRectF(QPointF(), geometry().size()).adjusted(+left, +top, -right, -bottom);
|
---|
780 | }
|
---|
781 |
|
---|
782 | /*!
|
---|
783 | Returns the effective size hint for this QGraphicsLayoutItem.
|
---|
784 |
|
---|
785 | \a which is the size hint in question.
|
---|
786 | \a constraint is an optional argument that defines a special constrain
|
---|
787 | when calculating the effective size hint. By default, \a constraint is
|
---|
788 | QSizeF(-1, -1), which means there is no constraint to the size hint.
|
---|
789 |
|
---|
790 | If you want to specify the widget's size hint for a given width or height,
|
---|
791 | you can provide the fixed dimension in \a constraint. This is useful for
|
---|
792 | widgets that can grow only either vertically or horizontally, and need to
|
---|
793 | set either their width or their height to a special value.
|
---|
794 |
|
---|
795 | For example, a text paragraph item fit into a column width of 200 may
|
---|
796 | grow vertically. You can pass QSizeF(200, -1) as a constraint to get a
|
---|
797 | suitable minimum, preferred and maximum height).
|
---|
798 |
|
---|
799 | You can adjust the effective size hint by reimplementing sizeHint()
|
---|
800 | in a QGraphicsLayoutItem subclass, or by calling one of the following
|
---|
801 | functions: setMinimumSize(), setPreferredSize, or setMaximumSize()
|
---|
802 | (or a combination of both).
|
---|
803 |
|
---|
804 | This function caches each of the size hints and guarantees that
|
---|
805 | sizeHint() will be called only once for each value of \a which - unless
|
---|
806 | \a constraint is not specified and updateGeometry() has been called.
|
---|
807 |
|
---|
808 | \sa sizeHint()
|
---|
809 | */
|
---|
810 | QSizeF QGraphicsLayoutItem::effectiveSizeHint(Qt::SizeHint which, const QSizeF &constraint) const
|
---|
811 | {
|
---|
812 | Q_D(const QGraphicsLayoutItem);
|
---|
813 |
|
---|
814 | if (!d->userSizeHints && constraint.isValid())
|
---|
815 | return constraint;
|
---|
816 |
|
---|
817 | // ### should respect size policy???
|
---|
818 | return d_ptr->effectiveSizeHints(constraint)[which];
|
---|
819 | }
|
---|
820 |
|
---|
821 | /*!
|
---|
822 | This virtual function discards any cached size hint information. You
|
---|
823 | should always call this function if you change the return value of the
|
---|
824 | sizeHint() function. Subclasses must always call the base implementation
|
---|
825 | when reimplementing this function.
|
---|
826 |
|
---|
827 | \sa effectiveSizeHint()
|
---|
828 | */
|
---|
829 | void QGraphicsLayoutItem::updateGeometry()
|
---|
830 | {
|
---|
831 | Q_D(QGraphicsLayoutItem);
|
---|
832 | d->sizeHintCacheDirty = true;
|
---|
833 | d->sizeHintWithConstraintCacheDirty = true;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /*!
|
---|
837 | Returns the parent of this QGraphicsLayoutItem, or 0 if there is no parent,
|
---|
838 | or if the parent does not inherit from QGraphicsLayoutItem
|
---|
839 | (QGraphicsLayoutItem is often used through multiple inheritance with
|
---|
840 | QObject-derived classes).
|
---|
841 |
|
---|
842 | \sa setParentLayoutItem()
|
---|
843 | */
|
---|
844 | QGraphicsLayoutItem *QGraphicsLayoutItem::parentLayoutItem() const
|
---|
845 | {
|
---|
846 | return d_func()->parent;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*!
|
---|
850 | Sets the parent of this QGraphicsLayoutItem to \a parent.
|
---|
851 |
|
---|
852 | \sa parentLayoutItem()
|
---|
853 | */
|
---|
854 | void QGraphicsLayoutItem::setParentLayoutItem(QGraphicsLayoutItem *parent)
|
---|
855 | {
|
---|
856 | d_func()->parent = parent;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /*!
|
---|
860 | Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited
|
---|
861 | by an object that arranges other QGraphicsLayoutItem objects); otherwise
|
---|
862 | returns false.
|
---|
863 |
|
---|
864 | \sa QGraphicsLayout
|
---|
865 | */
|
---|
866 | bool QGraphicsLayoutItem::isLayout() const
|
---|
867 | {
|
---|
868 | return d_func()->isLayout;
|
---|
869 | }
|
---|
870 |
|
---|
871 | /*!
|
---|
872 | \since 4.6
|
---|
873 |
|
---|
874 | Returns whether a layout should delete this item in its destructor.
|
---|
875 | If its true, then the layout will delete it. If its false, then it is
|
---|
876 | assumed that another object has the ownership of it, and the layout won't
|
---|
877 | delete this item.
|
---|
878 |
|
---|
879 | If the item inherits both QGraphicsItem and QGraphicsLayoutItem (such
|
---|
880 | as QGraphicsWidget does) the item is really part of two ownership
|
---|
881 | hierarchies. This property informs what the layout should do with its
|
---|
882 | child items when it is destructed. In the case of QGraphicsWidget, it
|
---|
883 | is preferred that when the layout is deleted it won't delete its children
|
---|
884 | (since they are also part of the graphics item hierarchy).
|
---|
885 |
|
---|
886 | By default this value is initialized to false in QGraphicsLayoutItem,
|
---|
887 | but it is overridden by QGraphicsLayout to return true. This is because
|
---|
888 | QGraphicsLayout is not normally part of the QGraphicsItem hierarchy, so the
|
---|
889 | parent layout should delete it.
|
---|
890 | Subclasses might override this default behaviour by calling
|
---|
891 | setOwnedByLayout(true).
|
---|
892 |
|
---|
893 | \sa setOwnedByLayout()
|
---|
894 | */
|
---|
895 | bool QGraphicsLayoutItem::ownedByLayout() const
|
---|
896 | {
|
---|
897 | return d_func()->ownedByLayout;
|
---|
898 | }
|
---|
899 | /*!
|
---|
900 | \since 4.6
|
---|
901 |
|
---|
902 | Sets whether a layout should delete this item in its destructor or not.
|
---|
903 | \a ownership must be true to in order for the layout to delete it.
|
---|
904 | \sa ownedByLayout()
|
---|
905 | */
|
---|
906 | void QGraphicsLayoutItem::setOwnedByLayout(bool ownership)
|
---|
907 | {
|
---|
908 | d_func()->ownedByLayout = ownership;
|
---|
909 | }
|
---|
910 |
|
---|
911 | /*!
|
---|
912 | * Returns the QGraphicsItem that this layout item represents.
|
---|
913 | * For QGraphicsWidget it will return itself. For custom items it can return an
|
---|
914 | * aggregated value.
|
---|
915 | *
|
---|
916 | * \sa setGraphicsItem()
|
---|
917 | */
|
---|
918 | QGraphicsItem *QGraphicsLayoutItem::graphicsItem() const
|
---|
919 | {
|
---|
920 | return d_func()->graphicsItem;
|
---|
921 | }
|
---|
922 |
|
---|
923 | /*!
|
---|
924 | * If the QGraphicsLayoutItem represents a QGraphicsItem, and it wants to take
|
---|
925 | * advantage of the automatic reparenting capabilities of QGraphicsLayout it
|
---|
926 | * should set this value.
|
---|
927 | * Note that if you delete \a item and not delete the layout item, you are
|
---|
928 | * responsible of calling setGraphicsItem(0) in order to avoid having a
|
---|
929 | * dangling pointer.
|
---|
930 | *
|
---|
931 | * \sa graphicsItem()
|
---|
932 | */
|
---|
933 | void QGraphicsLayoutItem::setGraphicsItem(QGraphicsItem *item)
|
---|
934 | {
|
---|
935 | d_func()->graphicsItem = item;
|
---|
936 | }
|
---|
937 |
|
---|
938 | QT_END_NAMESPACE
|
---|
939 |
|
---|
940 | #endif //QT_NO_GRAPHICSVIEW
|
---|