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
qgraphicsgridlayout.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
47#include "qglobal.h"
48
49#include "qapplication.h"
50#include "qwidget.h"
51#include "qgraphicslayout_p.h"
52#include "qgraphicslayoutitem.h"
53#include "qgraphicsgridlayout.h"
54#include "qgraphicswidget.h"
57#include "qscopedpointer.h"
58#ifdef QT_DEBUG
59# include <QtCore/qdebug.h>
60#endif
61
63
65{
66public:
69
70 mutable QScopedPointer<QGraphicsLayoutStyleInfo> m_styleInfo;
72
73#ifdef QGRIDLAYOUTENGINE_DEBUG
74 void dump(int indent) const;
75#endif
76};
77
78
85
94
99{
100 for (int i = count() - 1; i >= 0; --i) {
102 // The following lines can be removed, but this removes the item
103 // from the layout more efficiently than the implementation of
104 // ~QGraphicsLayoutItem.
105 removeAt(i);
106 if (item) {
107 item->setParentLayoutItem(nullptr);
108 if (item->ownedByLayout())
109 delete item;
110 }
111 }
112}
113
119 int rowSpan, int columnSpan, Qt::Alignment alignment)
120{
122 if (row < 0 || column < 0) {
123 qWarning("QGraphicsGridLayout::addItem: invalid row/column: %d",
124 row < 0 ? row : column);
125 return;
126 }
127 if (columnSpan < 1 || rowSpan < 1) {
128 qWarning("QGraphicsGridLayout::addItem: invalid row span/column span: %d",
129 rowSpan < 1 ? rowSpan : columnSpan);
130 return;
131 }
132 if (!item) {
133 qWarning("QGraphicsGridLayout::addItem: cannot add null item");
134 return;
135 }
136 if (item == this) {
137 qWarning("QGraphicsGridLayout::addItem: cannot insert itself");
138 return;
139 }
140
141 d->addChildLayoutItem(item);
142
143 QGraphicsGridLayoutEngineItem *gridEngineItem = new QGraphicsGridLayoutEngineItem(item, row, column, rowSpan, columnSpan, alignment);
144 d->engine.insertItem(gridEngineItem, -1);
145 invalidate();
146}
147
164
169{
170 Q_D(const QGraphicsGridLayout);
171 return d->engine.spacing(Qt::Horizontal, d->styleInfo());
172}
173
183
188{
189 Q_D(const QGraphicsGridLayout);
190 return d->engine.spacing(Qt::Vertical, d->styleInfo());
191}
192
205
210{
212 d->engine.setRowSpacing(row, spacing, Qt::Vertical);
213 invalidate();
214}
215
220{
221 Q_D(const QGraphicsGridLayout);
222 return d->engine.rowSpacing(row, Qt::Vertical);
223}
224
229{
231 d->engine.setRowSpacing(column, spacing, Qt::Horizontal);
232 invalidate();
233}
234
239{
240 Q_D(const QGraphicsGridLayout);
241 return d->engine.rowSpacing(column, Qt::Horizontal);
242}
243
248{
250 d->engine.setRowStretchFactor(row, stretch, Qt::Vertical);
251 invalidate();
252}
253
258{
259 Q_D(const QGraphicsGridLayout);
260 return d->engine.rowStretchFactor(row, Qt::Vertical);
261}
262
267{
269 d->engine.setRowStretchFactor(column, stretch, Qt::Horizontal);
270 invalidate();
271}
272
277{
278 Q_D(const QGraphicsGridLayout);
279 return d->engine.rowStretchFactor(column, Qt::Horizontal);
280}
281
291
296{
297 Q_D(const QGraphicsGridLayout);
298 return d->engine.rowSizeHint(Qt::MinimumSize, row, Qt::Vertical);
299}
300
310
315{
316 Q_D(const QGraphicsGridLayout);
317 return d->engine.rowSizeHint(Qt::PreferredSize, row, Qt::Vertical);
318}
319
329
334{
335 Q_D(const QGraphicsGridLayout);
336 return d->engine.rowSizeHint(Qt::MaximumSize, row, Qt::Vertical);
337}
338
343{
345 d->engine.setRowSizeHint(Qt::MinimumSize, row, height, Qt::Vertical);
346 d->engine.setRowSizeHint(Qt::MaximumSize, row, height, Qt::Vertical);
347 invalidate();
348}
349
359
364{
365 Q_D(const QGraphicsGridLayout);
366 return d->engine.rowSizeHint(Qt::MinimumSize, column, Qt::Horizontal);
367}
368
378
383{
384 Q_D(const QGraphicsGridLayout);
385 return d->engine.rowSizeHint(Qt::PreferredSize, column, Qt::Horizontal);
386}
387
397
402{
403 Q_D(const QGraphicsGridLayout);
404 return d->engine.rowSizeHint(Qt::MaximumSize, column, Qt::Horizontal);
405}
406
411{
413 d->engine.setRowSizeHint(Qt::MinimumSize, column, width, Qt::Horizontal);
414 d->engine.setRowSizeHint(Qt::MaximumSize, column, width, Qt::Horizontal);
415 invalidate();
416}
417
422{
424 d->engine.setRowAlignment(row, alignment, Qt::Vertical);
425 invalidate();
426}
427
432{
433 Q_D(const QGraphicsGridLayout);
434 return d->engine.rowAlignment(row, Qt::Vertical);
435}
436
441{
443 d->engine.setRowAlignment(column, alignment, Qt::Horizontal);
444 invalidate();
445}
446
451{
452 Q_D(const QGraphicsGridLayout);
453 return d->engine.rowAlignment(column, Qt::Horizontal);
454}
455
460{
462 d->engine.setAlignment(item, alignment);
463 invalidate();
464}
465
470{
471 Q_D(const QGraphicsGridLayout);
472 return d->engine.alignment(item);
473}
474
481{
482 Q_D(const QGraphicsGridLayout);
483 return d->engine.effectiveLastRow(Qt::Vertical) + 1;
484}
485
492{
493 Q_D(const QGraphicsGridLayout);
494 return d->engine.effectiveLastRow(Qt::Horizontal) + 1;
495}
496
501{
502 Q_D(const QGraphicsGridLayout);
503 if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount()) {
504 qWarning("QGraphicsGridLayout::itemAt: invalid row, column %d, %d", row, column);
505 return nullptr;
506 }
507 if (QGraphicsGridLayoutEngineItem *engineItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(row, column)))
508 return engineItem->layoutItem();
509 return nullptr;
510}
511
516{
517 Q_D(const QGraphicsGridLayout);
518 return d->engine.itemCount();
519}
520
526{
527 Q_D(const QGraphicsGridLayout);
528 if (index < 0 || index >= d->engine.itemCount()) {
529 qWarning("QGraphicsGridLayout::itemAt: invalid index %d", index);
530 return nullptr;
531 }
532 if (QGraphicsGridLayoutEngineItem *engineItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(index)))
533 return engineItem->layoutItem();
534 return nullptr;
535}
536
544{
546 if (index < 0 || index >= d->engine.itemCount()) {
547 qWarning("QGraphicsGridLayout::removeAt: invalid index %d", index);
548 return;
549 }
550
551 if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(index))) {
552 if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
553 layoutItem->setParentLayoutItem(nullptr);
554 d->engine.removeItem(gridItem);
555
556 // recalculate rowInfo.count if we remove an item that is on the right/bottommost row
557 for (const Qt::Orientation orient : {Qt::Horizontal, Qt::Vertical}) {
558 const int oldCount = d->engine.rowCount(orient);
559 if (gridItem->lastRow(orient) == oldCount - 1) {
560 const int newCount = d->engine.effectiveLastRow(orient) + 1;
561 d->engine.removeRows(newCount, oldCount - newCount, orient);
562 }
563 }
564
565 delete gridItem;
566 invalidate();
567 }
568}
569
577{
579 int index = d->engine.indexOf(item);
581}
586{
588 d->engine.invalidate();
589 if (d->m_styleInfo)
590 d->m_styleInfo->invalidate();
592}
593
594#ifdef QGRIDLAYOUTENGINE_DEBUG
595void QGraphicsGridLayoutPrivate::dump(int indent) const
596{
597 if (qt_graphicsLayoutDebug()) {
598 engine.dump(indent + 1);
599 }
600}
601#endif
602
607{
610 QRectF effectiveRect = geometry();
613 Qt::LayoutDirection visualDir = d->visualDirection();
614 d->engine.setVisualDirection(visualDir);
615 if (visualDir == Qt::RightToLeft)
616 qSwap(left, right);
617 effectiveRect.adjust(+left, +top, -right, -bottom);
618 d->engine.setGeometries(effectiveRect, d->styleInfo());
619#ifdef QGRIDLAYOUTENGINE_DEBUG
620 if (qt_graphicsLayoutDebug()) {
621 static int counter = 0;
622 qDebug("==== BEGIN DUMP OF QGraphicsGridLayout (%d)====", counter++);
623 d->dump(1);
624 qDebug("==== END DUMP OF QGraphicsGridLayout ====");
625 }
626#endif
627}
628
633{
634 Q_D(const QGraphicsGridLayout);
637 const QSizeF extraMargins(left + right, top + bottom);
638 return d->engine.sizeHint(which , constraint - extraMargins, d->styleInfo()) + extraMargins;
639}
640
641
642#if 0
643// ### kill? (implement and kill?)
644QRect QGraphicsGridLayout::cellRect(int row, int column, int rowSpan, int columnSpan) const
645{
646 Q_D(const QGraphicsGridLayout);
647 return QRect();
648// return d->engine.cellRect(parentLayoutable(), contentsGeometry(), row, column, rowSpan, columnSpan);
649}
650
651QSizePolicy::ControlTypes QGraphicsGridLayout::controlTypes(LayoutSide side) const
652{
653 Q_D(const QGraphicsGridLayout);
654 return d->engine.controlTypes(side);
655}
656#endif
657
QGraphicsGridLayoutEngine engine
QGraphicsLayoutStyleInfo * styleInfo() const
QScopedPointer< QGraphicsLayoutStyleInfo > m_styleInfo
The QGraphicsGridLayout class provides a grid layout for managing widgets in Graphics View.
void setRowFixedHeight(int row, qreal height)
Sets the fixed height for row, row, to height.
void setRowSpacing(int row, qreal spacing)
Sets the spacing for row to spacing.
void setHorizontalSpacing(qreal spacing)
Sets the default horizontal spacing for the grid layout to spacing.
Qt::Alignment rowAlignment(int row) const
Returns the alignment of row.
void setColumnMaximumWidth(int column, qreal width)
Sets the maximum width of column to width.
void setGeometry(const QRectF &rect) override
Sets the bounding geometry of the grid layout to rect.
int columnCount() const
Returns the number of columns in the grid layout.
qreal rowMinimumHeight(int row) const
Returns the minimum height for row, row.
void removeAt(int index) override
Removes the layout item at index without destroying it.
void setColumnMinimumWidth(int column, qreal width)
Sets the minimum width for column to width.
Qt::Alignment columnAlignment(int column) const
Returns the alignment for column.
qreal rowMaximumHeight(int row) const
Returns the maximum height for row, row.
int rowStretchFactor(int row) const
Returns the stretch factor for row.
void setRowMinimumHeight(int row, qreal height)
Sets the minimum height for row, row, to height.
void setColumnSpacing(int column, qreal spacing)
Sets the spacing for column to spacing.
void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
Sets the alignment for item to alignment.
void invalidate() override
\reimp
void setColumnStretchFactor(int column, int stretch)
Sets the stretch factor for column to stretch.
void setVerticalSpacing(qreal spacing)
Sets the default vertical spacing for the grid layout to spacing.
void setRowMaximumHeight(int row, qreal height)
Sets the maximum height for row, row, to height.
void addItem(QGraphicsLayoutItem *item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment=Qt::Alignment())
Adds item to the grid on row and column.
void setRowPreferredHeight(int row, qreal height)
Sets the preferred height for row, row, to height.
virtual ~QGraphicsGridLayout()
Destroys the QGraphicsGridLayout object.
qreal columnPreferredWidth(int column) const
Returns the preferred width for column.
void setRowStretchFactor(int row, int stretch)
Sets the stretch factor for row to stretch.
QGraphicsLayoutItem * itemAt(int row, int column) const
Returns a pointer to the layout item at (row, column).
qreal columnMaximumWidth(int column) const
Returns the maximum width for column.
qreal rowSpacing(int row) const
Returns the row spacing for row.
void setColumnAlignment(int column, Qt::Alignment alignment)
Sets the alignment for column to alignment.
QGraphicsGridLayout(QGraphicsLayoutItem *parent=nullptr)
Constructs a QGraphicsGridLayout instance.
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
\reimp
Qt::Alignment alignment(QGraphicsLayoutItem *item) const
Returns the alignment for item.
void removeItem(QGraphicsLayoutItem *item)
Removes the layout item item without destroying it.
int columnStretchFactor(int column) const
Returns the stretch factor for column.
void setColumnPreferredWidth(int column, qreal width)
Sets the preferred width for column to width.
qreal columnMinimumWidth(int column) const
Returns the minimum width for column.
int rowCount() const
Returns the number of rows in the grid layout.
void setColumnFixedWidth(int column, qreal width)
Sets the fixed width of column to width.
void setSpacing(qreal spacing)
Sets the grid layout's default spacing, both vertical and horizontal, to spacing.
void setRowAlignment(int row, Qt::Alignment alignment)
Sets the alignment of row to alignment.
int count() const override
Returns the number of layout items in this grid layout.
qreal rowPreferredHeight(int row) const
Returns the preferred height for row, row.
qreal horizontalSpacing() const
Returns the default horizontal spacing for the grid layout.
qreal verticalSpacing() const
Returns the default vertical spacing for the grid layout.
qreal columnSpacing(int column) const
Returns the column spacing for column.
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
QRectF geometry() const
Returns the item's geometry (e.g., position and size) as a QRectF.
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\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
T * data() const noexcept
Returns the value of the pointer referenced by this object.
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
\inmodule QtCore
Definition qsize.h:208
qreal spacing
rect
[4]
uint alignment
Combined button and popup list for selecting options.
LayoutDirection
@ RightToLeft
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
@ MaximumSize
@ PreferredSize
@ MinimumSize
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei height
GLuint index
[2]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint GLsizei width
GLint left
GLint GLint bottom
GLuint counter
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
static void layoutItem(QQuickItem *item, qreal y, qreal width)
static QString dump(const QByteArray &)
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
double qreal
Definition qtypes.h:187
QGraphicsItem * item