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
qgraphicsanchorlayout.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
102
104
105QGraphicsAnchor::QGraphicsAnchor(QGraphicsAnchorLayout *parentLayout)
107{
108 Q_D(QGraphicsAnchor);
109 Q_ASSERT(parentLayout);
110 d->layoutPrivate = parentLayout->d_func();
111}
112
119
140
142{
143 Q_D(const QGraphicsAnchor);
144 return d->sizePolicy;
145}
146
157{
158 Q_D(QGraphicsAnchor);
159 d->setSpacing(spacing);
160}
161
163{
164 Q_D(const QGraphicsAnchor);
165 return d->spacing();
166}
167
169{
170 Q_D(QGraphicsAnchor);
171 d->unsetSpacing();
172}
173
184
189{
191
192 for (int i = count() - 1; i >= 0; --i) {
193 QGraphicsLayoutItem *item = d->items.at(i);
194 removeAt(i);
195 if (item) {
196 if (item->ownedByLayout())
197 delete item;
198 }
199 }
200
201 d->removeCenterConstraints(this, Qt::Horizontal);
202 d->removeCenterConstraints(this, Qt::Vertical);
203 d->deleteLayoutEdges();
204
205 Q_ASSERT(d->itemCenterConstraints[Qt::Horizontal].isEmpty());
206 Q_ASSERT(d->itemCenterConstraints[Qt::Vertical].isEmpty());
207 Q_ASSERT(d->items.isEmpty());
208 Q_ASSERT(d->m_vertexList.isEmpty());
209}
210
235 QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
236{
238 QGraphicsAnchor *a = d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);
239 invalidate();
240 return a;
241}
242
249 QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
250{
252 return d->getAnchor(firstItem, firstEdge, secondItem, secondEdge);
253}
254
278 Qt::Corner firstCorner,
279 QGraphicsLayoutItem *secondItem,
280 Qt::Corner secondCorner)
281{
283
284 // Horizontal anchor
285 Qt::AnchorPoint firstEdge = (firstCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
286 Qt::AnchorPoint secondEdge = (secondCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
287 if (d->addAnchor(firstItem, firstEdge, secondItem, secondEdge)) {
288 // Vertical anchor
289 firstEdge = (firstCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
290 secondEdge = (secondCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
291 d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);
292
293 invalidate();
294 }
295}
296
314 QGraphicsLayoutItem *secondItem,
315 Qt::Orientations orientations)
316{
317 bool ok = true;
318 if (orientations & Qt::Horizontal) {
319 // Currently, if the first is ok, then the rest of the calls should be ok
320 ok = addAnchor(secondItem, Qt::AnchorLeft, firstItem, Qt::AnchorLeft) != nullptr;
321 if (ok)
322 addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight);
323 }
324 if (orientations & Qt::Vertical && ok) {
325 addAnchor(secondItem, Qt::AnchorTop, firstItem, Qt::AnchorTop);
326 addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom);
327 }
328}
329
342
355
368{
370
371 d->spacings = {spacing, spacing};
372 invalidate();
373}
374
381{
382 Q_D(const QGraphicsAnchorLayout);
383 return d->styleInfo().defaultSpacing(Qt::Horizontal);
384}
385
392{
393 Q_D(const QGraphicsAnchorLayout);
394 return d->styleInfo().defaultSpacing(Qt::Vertical);
395}
396
401{
403
405 d->calculateVertexPositions(Qt::Horizontal);
406 d->calculateVertexPositions(Qt::Vertical);
407 d->setItemsGeometries(geom);
408}
409
419{
421 QGraphicsLayoutItem *item = d->items.value(index);
422
423 if (!item)
424 return;
425
426 // Removing an item affects both horizontal and vertical graphs
427 d->removeCenterConstraints(item, Qt::Horizontal);
428 d->removeCenterConstraints(item, Qt::Vertical);
429 d->removeAnchors(item);
430 d->items.remove(index);
431
432 item->setParentLayoutItem(nullptr);
433 invalidate();
434}
435
440{
441 Q_D(const QGraphicsAnchorLayout);
442 return d->items.size();
443}
444
449{
450 Q_D(const QGraphicsAnchorLayout);
451 return d->items.value(index);
452}
453
458{
461 d->calculateGraphCacheDirty = true;
462 d->styleInfoDirty = true;
463}
464
469{
470 Q_UNUSED(constraint);
471 Q_D(const QGraphicsAnchorLayout);
472
473 // Some setup calculations are delayed until the information is
474 // actually needed, avoiding unnecessary recalculations when
475 // adding multiple anchors.
476
477 // sizeHint() / effectiveSizeHint() already have a cache
478 // mechanism, using invalidate() to force recalculation. However
479 // sizeHint() is called three times after invalidation (for max,
480 // min and pref), but we just need do our setup once.
481
482 const_cast<QGraphicsAnchorLayoutPrivate *>(d)->calculateGraphs();
483
484 // ### apply constraint!
485 QSizeF engineSizeHint{d->sizeHints[Qt::Horizontal][which],
486 d->sizeHints[Qt::Vertical][which]};
487
490
491 return engineSizeHint + QSizeF(left + right, top + bottom);
492}
493
495
496#include "moc_qgraphicsanchorlayout.cpp"
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
void setGeometry(const QRectF &rect) override
\reimp
QGraphicsAnchor * addAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
Creates an anchor between the edge firstEdge of item firstItem and the edge secondEdge of item second...
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
\reimp
qreal verticalSpacing() const
Returns the default vertical spacing for the anchor layout.
qreal horizontalSpacing() const
Returns the default horizontal spacing for the anchor layout.
void setSpacing(qreal spacing)
Sets the default horizontal and the default vertical spacing for the anchor layout to spacing.
virtual ~QGraphicsAnchorLayout()
Destroys the QGraphicsAnchorLayout object.
void addCornerAnchors(QGraphicsLayoutItem *firstItem, Qt::Corner firstCorner, QGraphicsLayoutItem *secondItem, Qt::Corner secondCorner)
Creates two anchors between firstItem and secondItem specified by the corners, firstCorner and second...
void setHorizontalSpacing(qreal spacing)
Sets the default horizontal spacing for the anchor layout to spacing.
void addAnchors(QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem, Qt::Orientations orientations=Qt::Horizontal|Qt::Vertical)
Anchors two or four edges of firstItem with the corresponding edges of secondItem,...
void setVerticalSpacing(qreal spacing)
Sets the default vertical spacing for the anchor layout to spacing.
QGraphicsLayoutItem * itemAt(int index) const override
\reimp
QGraphicsAnchorLayout(QGraphicsLayoutItem *parent=nullptr)
Constructs a QGraphicsAnchorLayout instance.
QGraphicsAnchor * anchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
Returns the anchor between the anchor points defined by firstItem and firstEdge and secondItem and se...
void removeAt(int index) override
Removes the layout item at index without destroying it.
void invalidate() override
\reimp
int count() const override
\reimp
The QGraphicsAnchor class represents an anchor between two items in a QGraphicsAnchorLayout.
~QGraphicsAnchor()
Removes the QGraphicsAnchor object from the layout and destroys it.
void setSizePolicy(QSizePolicy::Policy policy)
QSizePolicy::Policy sizePolicy
the size policy for the QGraphicsAnchor.
qreal spacing
the preferred space between items in the QGraphicsAnchorLayout.
void setSpacing(qreal spacing)
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a \l{QEvent::LayoutRequ...
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const override
\reimp
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore
Definition qsize.h:208
Policy
This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.
Definition qsizepolicy.h:29
qreal spacing
Combined button and popup list for selecting options.
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
AnchorPoint
@ AnchorRight
@ AnchorBottom
@ AnchorTop
@ AnchorLeft
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLint GLint bottom
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QGraphicsItem * item
QSizePolicy policy