QtBase  v6.3.1
qgraphicslinearlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
114 #include "qapplication.h"
115 
116 #include "qwidget.h"
117 #include "qgraphicslayout_p.h"
118 #include "qgraphicslayoutitem.h"
119 #include "qgraphicslinearlayout.h"
120 #include "qgraphicswidget.h"
123 #include "qscopedpointer.h"
124 #ifdef QT_DEBUG
125 #include <QtCore/qdebug.h>
126 #endif
127 
129 
131 {
132 public:
135  { }
136 
137  void removeGridItem(QGridLayoutItem *gridItem);
139  void fixIndex(int *index) const;
140  int gridRow(int index) const;
141  int gridColumn(int index) const;
142 
146 };
147 
149 {
150  int index = gridItem->firstRow(orientation);
151  engine.removeItem(gridItem);
153 }
154 
156 {
158  if (uint(*index) > uint(count))
159  *index = count;
160 }
161 
163 {
165  return 0;
166  return int(qMin(uint(index), uint(engine.rowCount())));
167 }
168 
170 {
171  if (orientation == Qt::Vertical)
172  return 0;
173  return int(qMin(uint(index), uint(engine.columnCount())));
174 }
175 
177 {
178  if (!m_styleInfo)
180  return m_styleInfo.data();
181 }
182 
190 {
191 }
192 
199 {
200 }
201 
206 {
207  for (int i = count() - 1; i >= 0; --i) {
209  // The following lines can be removed, but this removes the item
210  // from the layout more efficiently than the implementation of
211  // ~QGraphicsLayoutItem.
212  removeAt(i);
213  if (item) {
214  item->setParentLayoutItem(nullptr);
215  if (item->ownedByLayout())
216  delete item;
217  }
218  }
219 }
220 
228 {
230  if (orientation != d->orientation) {
231  d->engine.transpose();
232  d->orientation = orientation;
233  invalidate();
234  }
235 }
236 
242 {
243  Q_D(const QGraphicsLinearLayout);
244  return d->orientation;
245 }
246 
268 {
270  if (!item) {
271  qWarning("QGraphicsLinearLayout::insertItem: cannot insert null item");
272  return;
273  }
274  if (item == this) {
275  qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself");
276  return;
277  }
278  d->addChildLayoutItem(item);
279 
280  Q_ASSERT(item);
281  d->fixIndex(&index);
282  d->engine.insertRow(index, d->orientation);
283  QGraphicsGridLayoutEngineItem *gridEngineItem = new QGraphicsGridLayoutEngineItem(item, d->gridRow(index), d->gridColumn(index), 1, 1, { });
284  d->engine.insertItem(gridEngineItem, index);
285  invalidate();
286 }
287 
295 {
297  d->fixIndex(&index);
298  d->engine.insertRow(index, d->orientation);
299  d->engine.setRowStretchFactor(index, stretch, d->orientation);
300  invalidate();
301 }
302 
310 {
312  if (QGraphicsGridLayoutEngineItem *gridItem = d->engine.findLayoutItem(item)) {
313  item->setParentLayoutItem(nullptr);
314  d->removeGridItem(gridItem);
315  delete gridItem;
316  invalidate();
317  }
318 }
319 
327 {
329  if (index < 0 || index >= d->engine.itemCount()) {
330  qWarning("QGraphicsLinearLayout::removeAt: invalid index %d", index);
331  return;
332  }
333 
334  if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem*>(d->engine.itemAt(index))) {
335  if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
336  layoutItem->setParentLayoutItem(nullptr);
337  d->removeGridItem(gridItem);
338  delete gridItem;
339  invalidate();
340  }
341 }
342 
350 {
352  if (spacing < 0) {
353  qWarning("QGraphicsLinearLayout::setSpacing: invalid spacing %g", spacing);
354  return;
355  }
356  d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical);
357  invalidate();
358 }
359 
367 {
368  Q_D(const QGraphicsLinearLayout);
369  return d->engine.spacing(d->orientation, d->styleInfo());
370 }
371 
376 {
378  d->engine.setRowSpacing(index, spacing, d->orientation);
379  invalidate();
380 }
385 {
386  Q_D(const QGraphicsLinearLayout);
387  return d->engine.rowSpacing(index, d->orientation);
388 }
389 
400 {
402  if (!item) {
403  qWarning("QGraphicsLinearLayout::setStretchFactor: cannot assign"
404  " a stretch factor to a null item");
405  return;
406  }
407  if (stretchFactor(item) == stretch)
408  return;
409  d->engine.setStretchFactor(item, stretch, d->orientation);
410  invalidate();
411 }
412 
420 {
421  Q_D(const QGraphicsLinearLayout);
422  if (!item) {
423  qWarning("QGraphicsLinearLayout::setStretchFactor: cannot return"
424  " a stretch factor for a null item");
425  return 0;
426  }
427  return d->engine.stretchFactor(item, d->orientation);
428 }
429 
437 {
439  if (this->alignment(item) == alignment)
440  return;
441  d->engine.setAlignment(item, alignment);
442  invalidate();
443 }
444 
456 {
457  Q_D(const QGraphicsLinearLayout);
458  return d->engine.alignment(item);
459 }
460 
461 #if 0 // ###
462 QSizePolicy::ControlTypes QGraphicsLinearLayout::controlTypes(LayoutSide side) const
463 {
464  return d->engine.controlTypes(side);
465 }
466 #endif
467 
472 {
473  Q_D(const QGraphicsLinearLayout);
474  return d->engine.itemCount();
475 }
476 
482 {
483  Q_D(const QGraphicsLinearLayout);
484  if (index < 0 || index >= d->engine.itemCount()) {
485  qWarning("QGraphicsLinearLayout::itemAt: invalid index %d", index);
486  return nullptr;
487  }
488  QGraphicsLayoutItem *item = nullptr;
489  if (QGraphicsGridLayoutEngineItem *gridItem = static_cast<QGraphicsGridLayoutEngineItem *>(d->engine.itemAt(index)))
490  item = gridItem->layoutItem();
491  return item;
492 }
493 
498 {
501  QRectF effectiveRect = geometry();
504  Qt::LayoutDirection visualDir = d->visualDirection();
505  d->engine.setVisualDirection(visualDir);
506  if (visualDir == Qt::RightToLeft)
507  qSwap(left, right);
508  effectiveRect.adjust(+left, +top, -right, -bottom);
509 #ifdef QGRIDLAYOUTENGINE_DEBUG
510  if (qt_graphicsLayoutDebug()) {
511  static int counter = 0;
512  qDebug() << counter++ << "QGraphicsLinearLayout::setGeometry - " << rect;
513  dump(1);
514  }
515 #endif
516  d->engine.setGeometries(effectiveRect, d->styleInfo());
517 #ifdef QGRIDLAYOUTENGINE_DEBUG
518  if (qt_graphicsLayoutDebug()) {
519  qDebug("post dump");
520  dump(1);
521  }
522 #endif
523 }
524 
529 {
530  Q_D(const QGraphicsLinearLayout);
533  const QSizeF extraMargins(left + right, top + bottom);
534  return d->engine.sizeHint(which , constraint - extraMargins, d->styleInfo()) + extraMargins;
535 }
536 
541 {
543  d->engine.invalidate();
544  if (d->m_styleInfo)
545  d->m_styleInfo->invalidate();
547 }
548 
552 void QGraphicsLinearLayout::dump(int indent) const
553 {
554 #ifdef QGRIDLAYOUTENGINE_DEBUG
555  if (qt_graphicsLayoutDebug()) {
556  Q_D(const QGraphicsLinearLayout);
557  qDebug("%*s%s layout", indent, "",
558  d->orientation == Qt::Horizontal ? "Horizontal" : "Vertical");
559  d->engine.dump(indent + 1);
560  }
561 #else
562  Q_UNUSED(indent);
563 #endif
564 }
565 
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
virtual void invalidate()
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const override
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts.
virtual void setGeometry(const QRectF &rect)
The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Grap...
void removeItem(QGraphicsLayoutItem *item)
Qt::Alignment alignment(QGraphicsLayoutItem *item) const
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const override
void setStretchFactor(QGraphicsLayoutItem *item, int stretch)
void setItemSpacing(int index, qreal spacing)
void setOrientation(Qt::Orientation orientation)
void setSpacing(qreal spacing)
void removeAt(int index) override
QGraphicsLinearLayout(QGraphicsLayoutItem *parent=nullptr)
void setGeometry(const QRectF &rect) override
void insertStretch(int index, int stretch=1)
int stretchFactor(QGraphicsLayoutItem *item) const
qreal itemSpacing(int index) const
void insertItem(int index, QGraphicsLayoutItem *item)
void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
void dump(int indent=0) const
QGraphicsLayoutItem * itemAt(int index) const override
Qt::Orientation orientation() const
int count() const override
QGraphicsLinearLayoutPrivate(Qt::Orientation orientation)
QScopedPointer< QGraphicsLayoutStyleInfo > m_styleInfo
void removeGridItem(QGridLayoutItem *gridItem)
QGraphicsLayoutStyleInfo * styleInfo() const
QGraphicsGridLayoutEngine engine
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
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
constexpr void adjust(qreal x1, qreal y1, qreal x2, qreal y2) noexcept
Definition: qrect.h:814
T * data() const noexcept
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
qreal spacing
qSwap(pi, e)
rect
[4]
uint alignment
union Alignment_ Alignment
Definition: qnamespace.h:55
LayoutDirection
Definition: qnamespace.h:1462
@ RightToLeft
Definition: qnamespace.h:1464
Orientation
Definition: qnamespace.h:123
@ Horizontal
Definition: qnamespace.h:124
@ Vertical
Definition: qnamespace.h:125
SizeHint
Definition: qnamespace.h:1589
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
unsigned int uint
Definition: qglobal.h:334
#define qDebug
[1]
Definition: qlogging.h:177
#define qWarning
Definition: qlogging.h:179
GLuint index
[2]
GLdouble GLdouble GLdouble GLdouble top
GLenum GLenum GLsizei count
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLuint counter
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
Q_UNUSED(salary)
[21]
QGraphicsItem * item
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent