Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgraphicslayoutitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qglobal.h"
5
6#include "qgraphicslayout.h"
7#include "qgraphicsscene.h"
10#include "qwidget.h"
11#include "qgraphicswidget.h"
12#include "qgraphicsitem_p.h"
13
14#include <QtDebug>
15
17
18/*
19 COMBINE_SIZE() is identical to combineSize(), except that it
20 doesn't evaluate 'size' unless necessary.
21*/
22#define COMBINE_SIZE(result, size) \
23 do { \
24 if ((result).width() < 0 || (result).height() < 0) \
25 combineSize((result), (size)); \
26 } while (false)
27
28static void combineSize(QSizeF &result, const QSizeF &size)
29{
30 if (result.width() < 0)
31 result.setWidth(size.width());
32 if (result.height() < 0)
33 result.setHeight(size.height());
34}
35
36static void boundSize(QSizeF &result, const QSizeF &size)
37{
38 if (size.width() >= 0 && size.width() < result.width())
39 result.setWidth(size.width());
40 if (size.height() >= 0 && size.height() < result.height())
41 result.setHeight(size.height());
42}
43
44static void expandSize(QSizeF &result, const QSizeF &size)
45{
46 if (size.width() >= 0 && size.width() > result.width())
47 result.setWidth(size.width());
48 if (size.height() >= 0 && size.height() > result.height())
49 result.setHeight(size.height());
50}
51
52static void normalizeHints(qreal &minimum, qreal &preferred, qreal &maximum, qreal &descent)
53{
54 if (minimum >= 0 && maximum >= 0 && minimum > maximum)
55 minimum = maximum;
56
57 if (preferred >= 0) {
58 if (minimum >= 0 && preferred < minimum) {
59 preferred = minimum;
60 } else if (maximum >= 0 && preferred > maximum) {
61 preferred = maximum;
62 }
63 }
64
65 if (minimum >= 0 && descent > minimum)
66 descent = minimum;
67}
68
73 : parent(par), userSizeHints(nullptr), isLayout(layout), ownedByLayout(false), graphicsItem(nullptr)
74{
75}
76
81{
82 // Remove any lazily allocated data
83 delete[] userSizeHints;
84}
85
94
99{
100 Q_Q(const QGraphicsLayoutItem);
101 QSizeF *sizeHintCache;
102 const bool hasConstraint = constraint.width() >= 0 || constraint.height() >= 0;
103 if (hasConstraint) {
106 sizeHintCache = cachedSizeHintsWithConstraints;
107 } else {
109 return cachedSizeHints;
110 sizeHintCache = cachedSizeHints;
111 }
112
113 for (int i = 0; i < Qt::NSizeHints; ++i) {
114 sizeHintCache[i] = constraint;
115 if (userSizeHints)
116 combineSize(sizeHintCache[i], userSizeHints[i]);
117 }
118
119 QSizeF &minS = sizeHintCache[Qt::MinimumSize];
120 QSizeF &prefS = sizeHintCache[Qt::PreferredSize];
121 QSizeF &maxS = sizeHintCache[Qt::MaximumSize];
122 QSizeF &descentS = sizeHintCache[Qt::MinimumDescent];
123
124 normalizeHints(minS.rwidth(), prefS.rwidth(), maxS.rwidth(), descentS.rwidth());
125 normalizeHints(minS.rheight(), prefS.rheight(), maxS.rheight(), descentS.rheight());
126
127 // if the minimum, preferred and maximum sizes contradict each other
128 // (e.g. the minimum is larger than the maximum) we give priority to
129 // the maximum size, then the minimum size and finally the preferred size
130 COMBINE_SIZE(maxS, q->sizeHint(Qt::MaximumSize, maxS));
132 expandSize(maxS, prefS);
133 expandSize(maxS, minS);
135
136 COMBINE_SIZE(minS, q->sizeHint(Qt::MinimumSize, minS));
137 expandSize(minS, QSizeF(0, 0));
138 boundSize(minS, prefS);
139 boundSize(minS, maxS);
140
141 COMBINE_SIZE(prefS, q->sizeHint(Qt::PreferredSize, prefS));
142 expandSize(prefS, minS);
143 boundSize(prefS, maxS);
144
145 // Not supported yet
146 // COMBINE_SIZE(descentS, q->sizeHint(Qt::MinimumDescent, constraint));
147
148 if (hasConstraint) {
149 cachedConstraint = constraint;
151 } else {
152 sizeHintCacheDirty = false;
153 }
154 return sizeHintCache;
155}
156
157
175{
176 Q_Q(const QGraphicsLayoutItem);
177
179 while (parent && parent->isLayout()) {
181 }
182 return parent ? parent->graphicsItem() : nullptr;
183}
184
196
203{
205
206 if (userSizeHints) {
207 if (size == userSizeHints[which])
208 return;
209 } else if (size.width() < 0 && size.height() < 0) {
210 return;
211 }
212
214 userSizeHints[which] = size;
215 q->updateGeometry();
216}
217
225{
228 qreal &userValue = (component == Width)
229 ? userSizeHints[which].rwidth()
230 : userSizeHints[which].rheight();
231 if (value == userValue)
232 return;
233 userValue = value;
234 q->updateGeometry();
235}
236
237
239{
240 Q_Q(const QGraphicsLayoutItem);
241 if (isLayout) {
242 const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
243 for (int i = l->count() - 1; i >= 0; --i) {
244 if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasHeightForWidth())
245 return true;
246 }
247 } else if (QGraphicsItem *item = q->graphicsItem()) {
248 if (item->isWidget()) {
249 QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
250 if (w->layout()) {
251 return QGraphicsLayoutItemPrivate::get(w->layout())->hasHeightForWidth();
252 }
253 }
254 }
255 return q->sizePolicy().hasHeightForWidth();
256}
257
259{
260 Q_Q(const QGraphicsLayoutItem);
261 if (isLayout) {
262 const QGraphicsLayout *l = static_cast<const QGraphicsLayout *>(q);
263 for (int i = l->count() - 1; i >= 0; --i) {
264 if (QGraphicsLayoutItemPrivate::get(l->itemAt(i))->hasWidthForHeight())
265 return true;
266 }
267 } else if (QGraphicsItem *item = q->graphicsItem()) {
268 if (item->isWidget()) {
269 QGraphicsWidget *w = static_cast<QGraphicsWidget *>(item);
270 if (w->layout()) {
271 return QGraphicsLayoutItemPrivate::get(w->layout())->hasWidthForHeight();
272 }
273 }
274 }
275 return q->sizePolicy().hasWidthForHeight();
276}
277
355 : d_ptr(new QGraphicsLayoutItemPrivate(parent, isLayout))
356{
358 d->init();
360 d->q_ptr = this;
361}
362
367 : d_ptr(&dd)
368{
370 d->init();
371 d->q_ptr = this;
372}
373
378{
380 if (parentLI && parentLI->isLayout()) {
381 QGraphicsLayout *lay = static_cast<QGraphicsLayout*>(parentLI);
382 // this is not optimal
383 for (int i = lay->count() - 1; i >= 0; --i) {
384 if (lay->itemAt(i) == this) {
385 lay->removeAt(i);
386 break;
387 }
388 }
389 }
390}
391
418{
420 if (d->sizePolicy == policy)
421 return;
422 d->sizePolicy = policy;
424}
425
435 QSizePolicy::Policy vPolicy,
436 QSizePolicy::ControlType controlType)
437{
438 setSizePolicy(QSizePolicy(hPolicy, vPolicy, controlType));
439}
440
447{
448 Q_D(const QGraphicsLayoutItem);
449 return d->sizePolicy;
450}
451
465
485
495
505
506
519
539
549
559
573
593
603
613
680{
684 d->geom = QRectF(rect.topLeft(), effectiveSize);
685}
686
696{
697 Q_D(const QGraphicsLayoutItem);
698 return d->geom;
699}
700
710{
711 if (left)
712 *left = 0;
713 if (top)
714 *top = 0;
715 if (right)
716 *right = 0;
717 if (bottom)
718 *bottom = 0;
719}
720
738
768{
769 Q_D(const QGraphicsLayoutItem);
770
771 if (!d->userSizeHints && constraint.isValid())
772 return constraint;
773
774 // ### should respect size policy???
775 return d_ptr->effectiveSizeHints(constraint)[which];
776}
777
787{
789 d->sizeHintCacheDirty = true;
790 d->sizeHintWithConstraintCacheDirty = true;
791}
792
805{
806 bool isHidden = false;
808 isHidden = QGraphicsItemPrivate::get(item)->explicitlyHidden;
809
810 return isHidden && !sizePolicy().retainSizeWhenHidden();
811}
812
822{
823 return d_func()->parent;
824}
825
832{
833 d_func()->parent = parent;
834}
835
844{
845 return d_func()->isLayout;
846}
847
873{
874 return d_func()->ownedByLayout;
875}
884{
885 d_func()->ownedByLayout = ownership;
886}
887
896{
897 return d_func()->graphicsItem;
898}
899
911{
912 d_func()->graphicsItem = item;
913}
914
static const QGraphicsItemPrivate * get(const QGraphicsItem *item)
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
bool isWidget() const
QSizeF * effectiveSizeHints(const QSizeF &constraint) const
void setSizeComponent(Qt::SizeHint which, SizeComponent component, qreal value)
void setSize(Qt::SizeHint which, const QSizeF &size)
QGraphicsLayoutItemPrivate(QGraphicsLayoutItem *parent, bool isLayout)
static QGraphicsLayoutItemPrivate * get(QGraphicsLayoutItem *q)
QGraphicsItem * parentItem() const
QSizeF cachedSizeHints[Qt::NSizeHints]
QSizeF cachedSizeHintsWithConstraints[Qt::NSizeHints]
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
void setPreferredWidth(qreal width)
Sets the preferred width to width.
void setMaximumWidth(qreal width)
Sets the maximum width to width.
void setGraphicsItem(QGraphicsItem *item)
If the QGraphicsLayoutItem represents a QGraphicsItem, and it wants to take advantage of the automati...
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
QSizeF effectiveSizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Returns the effective size hint for this QGraphicsLayoutItem.
QScopedPointer< QGraphicsLayoutItemPrivate > d_ptr
void setMaximumSize(const QSizeF &size)
Sets the maximum size to size.
void setSizePolicy(const QSizePolicy &policy)
Sets the size policy to policy.
void setOwnedByLayout(bool ownedByLayout)
virtual void updateGeometry()
This virtual function discards any cached size hint information.
virtual bool isEmpty() const
QRectF geometry() const
Returns the item's geometry (e.g., position and size) as a QRectF.
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
void setPreferredHeight(qreal height)
Sets the preferred height to height.
void setPreferredSize(const QSizeF &size)
Sets the preferred size to size.
QGraphicsLayoutItem * parentLayoutItem() const
Returns the parent of this QGraphicsLayoutItem, or \nullptr if there is no parent,...
void setMinimumSize(const QSizeF &size)
Sets the minimum size to size.
void setMaximumHeight(qreal height)
Sets the maximum height to height.
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
virtual void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
This virtual function provides the left, top, right and bottom contents margins for this QGraphicsLay...
QRectF contentsRect() const
Returns the contents rect in local coordinates.
QSizeF minimumSize() const
Returns the minimum size.
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
virtual ~QGraphicsLayoutItem()
Destroys the QGraphicsLayoutItem object.
QSizePolicy sizePolicy() const
Returns the current size policy.
QSizeF preferredSize() const
Returns the preferred size.
QSizeF maximumSize() const
Returns the maximum size.
void setMinimumWidth(qreal width)
Sets the minimum width to width.
void setMinimumHeight(qreal height)
Sets the minimum height to height.
QGraphicsLayoutItem(QGraphicsLayoutItem *parent=nullptr, bool isLayout=false)
Constructs the QGraphicsLayoutItem object.
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
virtual int count() const =0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return the numbe...
virtual QGraphicsLayoutItem * itemAt(int i) const =0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to return a pointer...
virtual void removeAt(int index)=0
This pure virtual function must be reimplemented in a subclass of QGraphicsLayout to remove the item ...
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:813
\inmodule QtCore
Definition qsize.h:208
constexpr qreal & rwidth() noexcept
Returns a reference to the width.
Definition qsize.h:356
constexpr bool isValid() const noexcept
Returns true if both the width and height are equal to or greater than 0; otherwise returns false.
Definition qsize.h:329
constexpr qreal & rheight() noexcept
Returns a reference to the height.
Definition qsize.h:359
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
constexpr QSizeF expandedTo(const QSizeF &) const noexcept
Returns a size holding the maximum width and height of this size and the given otherSize.
Definition qsize.h:391
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:335
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
constexpr bool retainSizeWhenHidden() const noexcept
Definition qsizepolicy.h:97
Policy
This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.
Definition qsizepolicy.h:29
rect
[4]
Combined button and popup list for selecting options.
@ MaximumSize
@ PreferredSize
@ MinimumDescent
@ MinimumSize
@ NSizeHints
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define COMBINE_SIZE(result, size)
static void normalizeHints(qreal &minimum, qreal &preferred, qreal &maximum, qreal &descent)
static void boundSize(QSizeF &result, const QSizeF &size)
static void combineSize(QSizeF &result, const QSizeF &size)
static void expandSize(QSizeF &result, const QSizeF &size)
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint GLsizei width
GLint left
GLint GLint bottom
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
static qreal component(const QPointF &point, unsigned int i)
double qreal
Definition qtypes.h:187
#define QWIDGETSIZE_MAX
Definition qwidget.h:917
QObject::connect nullptr
QVBoxLayout * layout
size rwidth()+
QGraphicsItem * item
QSizePolicy policy