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
qgraphicslinearlayout.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
82#include "qapplication.h"
83
84#include "qwidget.h"
85#include "qgraphicslayout_p.h"
86#include "qgraphicslayoutitem.h"
88#include "qgraphicswidget.h"
91#include "qscopedpointer.h"
92#ifdef QT_DEBUG
93#include <QtCore/qdebug.h>
94#endif
95
97
99{
100public:
104
105 void removeGridItem(QGridLayoutItem *gridItem);
107 void fixIndex(int *index) const;
108 int gridRow(int index) const;
109 int gridColumn(int index) const;
110
112 mutable QScopedPointer<QGraphicsLayoutStyleInfo> m_styleInfo;
114};
115
117{
118 int index = gridItem->firstRow(orientation);
119 engine.removeItem(gridItem);
121}
122
124{
126 if (uint(*index) > uint(count))
127 *index = count;
128}
129
131{
133 return 0;
134 return int(qMin(uint(index), uint(engine.rowCount())));
135}
136
138{
140 return 0;
141 return int(qMin(uint(index), uint(engine.columnCount())));
142}
143
150
160
169
174{
175 for (int i = count() - 1; i >= 0; --i) {
177 // The following lines can be removed, but this removes the item
178 // from the layout more efficiently than the implementation of
179 // ~QGraphicsLayoutItem.
180 removeAt(i);
181 if (item) {
182 item->setParentLayoutItem(nullptr);
183 if (item->ownedByLayout())
184 delete item;
185 }
186 }
187}
188
196{
198 if (orientation != d->orientation) {
199 d->engine.transpose();
200 d->orientation = orientation;
201 invalidate();
202 }
203}
204
210{
211 Q_D(const QGraphicsLinearLayout);
212 return d->orientation;
213}
214
236{
238 if (!item) {
239 qWarning("QGraphicsLinearLayout::insertItem: cannot insert null item");
240 return;
241 }
242 if (item == this) {
243 qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself");
244 return;
245 }
246 d->addChildLayoutItem(item);
247
248 Q_ASSERT(item);
249 d->fixIndex(&index);
250 d->engine.insertRow(index, d->orientation);
251 QGraphicsGridLayoutEngineItem *gridEngineItem = new QGraphicsGridLayoutEngineItem(item, d->gridRow(index), d->gridColumn(index), 1, 1, { });
252 d->engine.insertItem(gridEngineItem, index);
253 invalidate();
254}
255
263{
265 d->fixIndex(&index);
266 d->engine.insertRow(index, d->orientation);
267 d->engine.setRowStretchFactor(index, stretch, d->orientation);
268 invalidate();
269}
270
278{
280 if (QGraphicsGridLayoutEngineItem *gridItem = d->engine.findLayoutItem(item)) {
281 item->setParentLayoutItem(nullptr);
282 d->removeGridItem(gridItem);
283 delete gridItem;
284 invalidate();
285 }
286}
287
295{
297 if (index < 0 || index >= d->engine.itemCount()) {
298 qWarning("QGraphicsLinearLayout::removeAt: invalid index %d", index);
299 return;
300 }
301
302 if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(index))) {
303 if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
304 layoutItem->setParentLayoutItem(nullptr);
305 d->removeGridItem(gridItem);
306 delete gridItem;
307 invalidate();
308 }
309}
310
318{
320 if (spacing < 0) {
321 qWarning("QGraphicsLinearLayout::setSpacing: invalid spacing %g", spacing);
322 return;
323 }
324 d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical);
325 invalidate();
326}
327
335{
336 Q_D(const QGraphicsLinearLayout);
337 return d->engine.spacing(d->orientation, d->styleInfo());
338}
339
344{
346 d->engine.setRowSpacing(index, spacing, d->orientation);
347 invalidate();
348}
353{
354 Q_D(const QGraphicsLinearLayout);
355 return d->engine.rowSpacing(index, d->orientation);
356}
357
368{
370 if (!item) {
371 qWarning("QGraphicsLinearLayout::setStretchFactor: cannot assign"
372 " a stretch factor to a null item");
373 return;
374 }
375 if (stretchFactor(item) == stretch)
376 return;
377 d->engine.setStretchFactor(item, stretch, d->orientation);
378 invalidate();
379}
380
388{
389 Q_D(const QGraphicsLinearLayout);
390 if (!item) {
391 qWarning("QGraphicsLinearLayout::setStretchFactor: cannot return"
392 " a stretch factor for a null item");
393 return 0;
394 }
395 return d->engine.stretchFactor(item, d->orientation);
396}
397
405{
407 if (this->alignment(item) == alignment)
408 return;
409 d->engine.setAlignment(item, alignment);
410 invalidate();
411}
412
424{
425 Q_D(const QGraphicsLinearLayout);
426 return d->engine.alignment(item);
427}
428
429#if 0 // ###
430QSizePolicy::ControlTypes QGraphicsLinearLayout::controlTypes(LayoutSide side) const
431{
432 return d->engine.controlTypes(side);
433}
434#endif
435
440{
441 Q_D(const QGraphicsLinearLayout);
442 return d->engine.itemCount();
443}
444
450{
451 Q_D(const QGraphicsLinearLayout);
452 if (index < 0 || index >= d->engine.itemCount()) {
453 qWarning("QGraphicsLinearLayout::itemAt: invalid index %d", index);
454 return nullptr;
455 }
456 QGraphicsLayoutItem *item = nullptr;
457 if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem *>(d->engine.itemAt(index)))
458 item = gridItem->layoutItem();
459 return item;
460}
461
466{
469 QRectF effectiveRect = geometry();
472 Qt::LayoutDirection visualDir = d->visualDirection();
473 d->engine.setVisualDirection(visualDir);
474 if (visualDir == Qt::RightToLeft)
475 qSwap(left, right);
476 effectiveRect.adjust(+left, +top, -right, -bottom);
477#ifdef QGRIDLAYOUTENGINE_DEBUG
478 if (qt_graphicsLayoutDebug()) {
479 static int counter = 0;
480 qDebug() << counter++ << "QGraphicsLinearLayout::setGeometry - " << rect;
481 dump(1);
482 }
483#endif
484 d->engine.setGeometries(effectiveRect, d->styleInfo());
485#ifdef QGRIDLAYOUTENGINE_DEBUG
486 if (qt_graphicsLayoutDebug()) {
487 qDebug("post dump");
488 dump(1);
489 }
490#endif
491}
492
497{
498 Q_D(const QGraphicsLinearLayout);
501 const QSizeF extraMargins(left + right, top + bottom);
502 return d->engine.sizeHint(which , constraint - extraMargins, d->styleInfo()) + extraMargins;
503}
504
509{
511 d->engine.invalidate();
512 if (d->m_styleInfo)
513 d->m_styleInfo->invalidate();
515}
516
520void QGraphicsLinearLayout::dump(int indent) const
521{
522#ifdef QGRIDLAYOUTENGINE_DEBUG
523 if (qt_graphicsLayoutDebug()) {
524 Q_D(const QGraphicsLinearLayout);
525 qDebug("%*s%s layout", indent, "",
526 d->orientation == Qt::Horizontal ? "Horizontal" : "Vertical");
527 d->engine.dump(indent + 1);
528 }
529#else
530 Q_UNUSED(indent);
531#endif
532}
533
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
QGraphicsLinearLayoutPrivate(Qt::Orientation orientation)
QScopedPointer< QGraphicsLayoutStyleInfo > m_styleInfo
void removeGridItem(QGridLayoutItem *gridItem)
QGraphicsLayoutStyleInfo * styleInfo() const
The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Grap...
void removeItem(QGraphicsLayoutItem *item)
Removes item from the layout without destroying it.
Qt::Alignment alignment(QGraphicsLayoutItem *item) const
Returns the alignment for item.
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
\reimp
void setStretchFactor(QGraphicsLayoutItem *item, int stretch)
Sets the stretch factor for item to stretch.
qreal spacing() const
Returns the layout's spacing.
void setItemSpacing(int index, qreal spacing)
Sets the spacing after item at index to spacing.
void setOrientation(Qt::Orientation orientation)
Change the layout orientation to orientation.
void setSpacing(qreal spacing)
Sets the layout's spacing to spacing.
void removeAt(int index) override
Removes the item at index without destroying it.
QGraphicsLinearLayout(QGraphicsLayoutItem *parent=nullptr)
Constructs a QGraphicsLinearLayout instance using Qt::Horizontal orientation.
void setGeometry(const QRectF &rect) override
\reimp
void insertStretch(int index, int stretch=1)
Inserts a stretch of stretch at index, or before any item that is currently at index.
int stretchFactor(QGraphicsLayoutItem *item) const
Returns the stretch factor for item.
virtual ~QGraphicsLinearLayout()
Destroys the QGraphicsLinearLayout object.
void invalidate() override
\reimp
qreal itemSpacing(int index) const
Returns the spacing after item at index.
void insertItem(int index, QGraphicsLayoutItem *item)
Inserts item into the layout at index, or before any item that is currently at index.
void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
Sets the alignment of item to alignment.
void dump(int indent=0) const
QGraphicsLayoutItem * itemAt(int index) const override
\reimp When iterating from 0 and up, it will return the items in the visual arranged order.
Qt::Orientation orientation() const
Returns the layout orientation.
int count() const override
\reimp
void removeItem(QGridLayoutItem *item)
int columnCount(Qt::Orientation orientation) const
void removeRows(int row, int count, Qt::Orientation orientation)
int rowCount(Qt::Orientation orientation) const
\inmodule QtCore\reentrant
Definition qrect.h:484
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.
Definition qcompare.h:63
LayoutDirection
@ RightToLeft
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLuint index
[2]
GLdouble GLdouble GLdouble GLdouble top
GLenum GLenum GLsizei count
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLuint counter
static void layoutItem(QQuickItem *item, qreal y, qreal width)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
QGraphicsItem * item