Qt 4.8
qgraphicsgridlayout.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
84 #include "qglobal.h"
85 
86 #ifndef QT_NO_GRAPHICSVIEW
87 
88 #include "qapplication.h"
89 #include "qwidget.h"
90 #include "qgraphicslayout_p.h"
91 #include "qgraphicslayoutitem.h"
92 #include "qgraphicsgridlayout.h"
93 #include "qgraphicswidget.h"
94 #include "qgridlayoutengine_p.h"
95 #include <QtCore/qdebug.h>
96 
98 
100 {
101 public:
103  QLayoutStyleInfo styleInfo() const;
104 
106 #ifdef QT_DEBUG
107  void dump(int indent) const;
108 #endif
109 };
110 
111 Q_GLOBAL_STATIC(QWidget, globalStyleInfoWidget);
112 
114 {
115  QGraphicsItem *item = parentItem();
116  QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : QApplication::style();
117  return QLayoutStyleInfo(style, globalStyleInfoWidget());
118 }
119 
126 {
127 }
128 
133 {
134  for (int i = count() - 1; i >= 0; --i) {
135  QGraphicsLayoutItem *item = itemAt(i);
136  // The following lines can be removed, but this removes the item
137  // from the layout more efficiently than the implementation of
138  // ~QGraphicsLayoutItem.
139  removeAt(i);
140  if (item) {
141  item->setParentLayoutItem(0);
142  if (item->ownedByLayout())
143  delete item;
144  }
145  }
146 }
147 
152 void QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column,
153  int rowSpan, int columnSpan, Qt::Alignment alignment)
154 {
156  if (row < 0 || column < 0) {
157  qWarning("QGraphicsGridLayout::addItem: invalid row/column: %d",
158  row < 0 ? row : column);
159  return;
160  }
161  if (columnSpan < 1 || rowSpan < 1) {
162  qWarning("QGraphicsGridLayout::addItem: invalid row span/column span: %d",
163  rowSpan < 1 ? rowSpan : columnSpan);
164  return;
165  }
166  if (!item) {
167  qWarning("QGraphicsGridLayout::addItem: cannot add null item");
168  return;
169  }
170  if (item == this) {
171  qWarning("QGraphicsGridLayout::addItem: cannot insert itself");
172  return;
173  }
174 
175  d->addChildLayoutItem(item);
176 
177  new QGridLayoutItem(&d->engine, item, row, column, rowSpan, columnSpan, alignment);
178  invalidate();
179 }
180 
192 {
194  d->engine.setSpacing(spacing, Qt::Horizontal);
195  invalidate();
196 }
197 
202 {
203  Q_D(const QGraphicsGridLayout);
204  return d->engine.spacing(d->styleInfo(), Qt::Horizontal);
205 }
206 
211 {
213  d->engine.setSpacing(spacing, Qt::Vertical);
214  invalidate();
215 }
216 
221 {
222  Q_D(const QGraphicsGridLayout);
223  return d->engine.spacing(d->styleInfo(), Qt::Vertical);
224 }
225 
233 {
235  d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical);
236  invalidate();
237 }
238 
243 {
245  d->engine.setRowSpacing(row, spacing, Qt::Vertical);
246  invalidate();
247 }
248 
253 {
254  Q_D(const QGraphicsGridLayout);
255  return d->engine.rowSpacing(row, Qt::Vertical);
256 }
257 
262 {
264  d->engine.setRowSpacing(column, spacing, Qt::Horizontal);
265  invalidate();
266 }
267 
272 {
273  Q_D(const QGraphicsGridLayout);
274  return d->engine.rowSpacing(column, Qt::Horizontal);
275 }
276 
281 {
283  d->engine.setRowStretchFactor(row, stretch, Qt::Vertical);
284  invalidate();
285 }
286 
291 {
292  Q_D(const QGraphicsGridLayout);
293  return d->engine.rowStretchFactor(row, Qt::Vertical);
294 }
295 
299 void QGraphicsGridLayout::setColumnStretchFactor(int column, int stretch)
300 {
302  d->engine.setRowStretchFactor(column, stretch, Qt::Horizontal);
303  invalidate();
304 }
305 
310 {
311  Q_D(const QGraphicsGridLayout);
312  return d->engine.rowStretchFactor(column, Qt::Horizontal);
313 }
314 
319 {
321  d->engine.setRowSizeHint(Qt::MinimumSize, row, height, Qt::Vertical);
322  invalidate();
323 }
324 
329 {
330  Q_D(const QGraphicsGridLayout);
331  return d->engine.rowSizeHint(Qt::MinimumSize, row, Qt::Vertical);
332 }
333 
338 {
340  d->engine.setRowSizeHint(Qt::PreferredSize, row, height, Qt::Vertical);
341  invalidate();
342 }
343 
348 {
349  Q_D(const QGraphicsGridLayout);
350  return d->engine.rowSizeHint(Qt::PreferredSize, row, Qt::Vertical);
351 }
352 
357 {
359  d->engine.setRowSizeHint(Qt::MaximumSize, row, height, Qt::Vertical);
360  invalidate();
361 }
362 
367 {
368  Q_D(const QGraphicsGridLayout);
369  return d->engine.rowSizeHint(Qt::MaximumSize, row, Qt::Vertical);
370 }
371 
376 {
378  d->engine.setRowSizeHint(Qt::MinimumSize, row, height, Qt::Vertical);
379  d->engine.setRowSizeHint(Qt::MaximumSize, row, height, Qt::Vertical);
380  invalidate();
381 }
382 
387 {
389  d->engine.setRowSizeHint(Qt::MinimumSize, column, width, Qt::Horizontal);
390  invalidate();
391 }
392 
397 {
398  Q_D(const QGraphicsGridLayout);
399  return d->engine.rowSizeHint(Qt::MinimumSize, column, Qt::Horizontal);
400 }
401 
406 {
408  d->engine.setRowSizeHint(Qt::PreferredSize, column, width, Qt::Horizontal);
409  invalidate();
410 }
411 
416 {
417  Q_D(const QGraphicsGridLayout);
418  return d->engine.rowSizeHint(Qt::PreferredSize, column, Qt::Horizontal);
419 }
420 
425 {
427  d->engine.setRowSizeHint(Qt::MaximumSize, column, width, Qt::Horizontal);
428  invalidate();
429 }
430 
435 {
436  Q_D(const QGraphicsGridLayout);
437  return d->engine.rowSizeHint(Qt::MaximumSize, column, Qt::Horizontal);
438 }
439 
444 {
446  d->engine.setRowSizeHint(Qt::MinimumSize, column, width, Qt::Horizontal);
447  d->engine.setRowSizeHint(Qt::MaximumSize, column, width, Qt::Horizontal);
448  invalidate();
449 }
450 
455 {
457  d->engine.setRowAlignment(row, alignment, Qt::Vertical);
458  invalidate();
459 }
460 
464 Qt::Alignment QGraphicsGridLayout::rowAlignment(int row) const
465 {
466  Q_D(const QGraphicsGridLayout);
467  return d->engine.rowAlignment(row, Qt::Vertical);
468 }
469 
473 void QGraphicsGridLayout::setColumnAlignment(int column, Qt::Alignment alignment)
474 {
476  d->engine.setRowAlignment(column, alignment, Qt::Horizontal);
477  invalidate();
478 }
479 
483 Qt::Alignment QGraphicsGridLayout::columnAlignment(int column) const
484 {
485  Q_D(const QGraphicsGridLayout);
486  return d->engine.rowAlignment(column, Qt::Horizontal);
487 }
488 
493 {
495  d->engine.setAlignment(item, alignment);
496  invalidate();
497 }
498 
503 {
504  Q_D(const QGraphicsGridLayout);
505  return d->engine.alignment(item);
506 }
507 
514 {
515  Q_D(const QGraphicsGridLayout);
516  return d->engine.effectiveLastRow(Qt::Vertical) + 1;
517 }
518 
525 {
526  Q_D(const QGraphicsGridLayout);
527  return d->engine.effectiveLastRow(Qt::Horizontal) + 1;
528 }
529 
534 {
535  Q_D(const QGraphicsGridLayout);
536  if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount()) {
537  qWarning("QGraphicsGridLayout::itemAt: invalid row, column %d, %d", row, column);
538  return 0;
539  }
540  if (QGridLayoutItem *item = d->engine.itemAt(row, column))
541  return item->layoutItem();
542  return 0;
543 }
544 
549 {
550  Q_D(const QGraphicsGridLayout);
551  return d->engine.itemCount();
552 }
553 
559 {
560  Q_D(const QGraphicsGridLayout);
561  if (index < 0 || index >= d->engine.itemCount()) {
562  qWarning("QGraphicsGridLayout::itemAt: invalid index %d", index);
563  return 0;
564  }
565  QGraphicsLayoutItem *item = 0;
566  if (QGridLayoutItem *gridItem = d->engine.itemAt(index))
567  item = gridItem->layoutItem();
568  return item;
569 }
570 
578 {
580  if (index < 0 || index >= d->engine.itemCount()) {
581  qWarning("QGraphicsGridLayout::removeAt: invalid index %d", index);
582  return;
583  }
584  if (QGridLayoutItem *gridItem = d->engine.itemAt(index)) {
585  if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
586  layoutItem->setParentLayoutItem(0);
587  d->engine.removeItem(gridItem);
588 
589  // recalculate rowInfo.count if we remove an item that is on the right/bottommost row
590  for (int j = 0; j < NOrientations; ++j) {
591  // 0: Hor, 1: Ver
592  const Qt::Orientation orient = (j == 0 ? Qt::Horizontal : Qt::Vertical);
593  const int oldCount = d->engine.rowCount(orient);
594  if (gridItem->lastRow(orient) == oldCount - 1) {
595  const int newCount = d->engine.effectiveLastRow(orient) + 1;
596  d->engine.removeRows(newCount, oldCount - newCount, orient);
597  }
598  }
599 
600  delete gridItem;
601  invalidate();
602  }
603 }
604 
617 {
619  int index = d->engine.indexOf(item);
620  removeAt(index);
621 }
626 {
628  d->engine.invalidate();
630 }
631 
632 #ifdef QT_DEBUG
633 void QGraphicsGridLayoutPrivate::dump(int indent) const
634 {
635  if (qt_graphicsLayoutDebug()) {
636  engine.dump(indent + 1);
637  }
638 }
639 #endif
640 
645 {
648  QRectF effectiveRect = geometry();
649  qreal left, top, right, bottom;
650  getContentsMargins(&left, &top, &right, &bottom);
651  Qt::LayoutDirection visualDir = d->visualDirection();
652  d->engine.setVisualDirection(visualDir);
653  if (visualDir == Qt::RightToLeft)
654  qSwap(left, right);
655  effectiveRect.adjust(+left, +top, -right, -bottom);
656  d->engine.setGeometries(d->styleInfo(), effectiveRect);
657 #ifdef QT_DEBUG
658  if (qt_graphicsLayoutDebug()) {
659  static int counter = 0;
660  qDebug("==== BEGIN DUMP OF QGraphicsGridLayout (%d)====", counter++);
661  d->dump(1);
662  qDebug("==== END DUMP OF QGraphicsGridLayout ====");
663  }
664 #endif
665 }
666 
671 {
672  Q_D(const QGraphicsGridLayout);
673  qreal left, top, right, bottom;
674  getContentsMargins(&left, &top, &right, &bottom);
675  const QSizeF extraMargins(left + right, top + bottom);
676  return d->engine.sizeHint(d->styleInfo(), which , constraint - extraMargins) + extraMargins;
677 }
678 
679 
680 #if 0
681 // ### kill? (implement and kill?)
682 QRect QGraphicsGridLayout::cellRect(int row, int column, int rowSpan, int columnSpan) const
683 {
684  Q_D(const QGraphicsGridLayout);
685  return QRect();
686 // return d->engine.cellRect(parentLayoutable(), contentsGeometry(), row, column, rowSpan, columnSpan);
687 }
688 
689 QSizePolicy::ControlTypes QGraphicsGridLayout::controlTypes(LayoutSide side) const
690 {
691  Q_D(const QGraphicsGridLayout);
692  return d->engine.controlTypes(side);
693 }
694 #endif
695 
697 
698 #endif //QT_NO_GRAPHICSVIEW
QGraphicsGridLayout(QGraphicsLayoutItem *parent=0)
Constructs a QGraphicsGridLayout instance.
double d
Definition: qnumeric_p.h:62
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
QLayoutStyleInfo styleInfo() const
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setColumnStretchFactor(int column, int stretch)
Sets the stretch factor for column to stretch.
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
Reimplemented Function
qreal columnSpacing(int column) const
Returns the column spacing for column.
void setRowStretchFactor(int row, int stretch)
Sets the stretch factor for row to stretch.
void setSpacing(qreal spacing)
Sets the grid layout&#39;s default spacing, both vertical and horizontal, to spacing. ...
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
bool qt_graphicsLayoutDebug()
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QGraphicsGridLayout class provides a grid layout for managing widgets in Graphics View...
qreal rowSpacing(int row) const
Returns the row spacing for row.
void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
Sets the alignment for item to alignment.
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
static QStyle * style()
Returns the application&#39;s style object.
qreal rowMaximumHeight(int row) const
Returns the maximum height for row, row.
qreal verticalSpacing() const
Returns the default vertical spacing for the grid layout.
Qt::Alignment columnAlignment(int column) const
Returns the alignment for column.
#define Q_D(Class)
Definition: qglobal.h:2482
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void setColumnAlignment(int column, Qt::Alignment alignment)
Sets the alignment for column to alignment.
void addItem(QGraphicsLayoutItem *item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment=0)
Adds item to the grid on row and column.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
Q_GLOBAL_STATIC(QWidget, globalStyleInfoWidget)
Q_CORE_EXPORT void qDebug(const char *,...)
QRectF geometry() const
Returns the item&#39;s geometry (e.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
qreal rowPreferredHeight(int row) const
Returns the preferred height for row, row.
Qt::Alignment rowAlignment(int row) const
Returns the alignment of row.
qreal columnMaximumWidth(int column) const
Returns the maximum width for column.
LayoutDirection
Definition: qnamespace.h:1580
void setRowAlignment(int row, Qt::Alignment alignment)
Sets the alignment of row to alignment.
Q_CORE_EXPORT void qWarning(const char *,...)
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
void setVerticalSpacing(qreal spacing)
Sets the default vertical spacing for the grid layout to spacing.
qreal rowMinimumHeight(int row) const
Returns the minimum height for row, row.
bool ownedByLayout() const
Returns whether a layout should delete this item in its destructor.
QGraphicsItem * parentItem() const
Returns the parent item of this layout, or 0 if this layout is not installed on any widget...
qreal columnMinimumWidth(int column) const
Returns the minimum width for column.
void setRowFixedHeight(int row, qreal height)
Sets the fixed height for row, row, to height.
int rowStretchFactor(int row) const
Returns the stretch factor for row.
qreal columnPreferredWidth(int column) const
Returns the preferred width for column.
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
void setRowPreferredHeight(int row, qreal height)
Sets the preferred height for row, row, to height.
int count() const
Returns the number of layout items in this grid layout.
void setColumnFixedWidth(int column, qreal width)
Sets the fixed width of column to width.
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Reimplemented Function
Qt::Alignment alignment(QGraphicsLayoutItem *item) const
Returns the alignment for item.
void setRowMinimumHeight(int row, qreal height)
Sets the minimum height for row, row, to height.
SizeHint
Definition: qnamespace.h:1708
int columnStretchFactor(int column) const
Returns the stretch factor for column.
void setRowMaximumHeight(int row, qreal height)
Sets the maximum height for row, row, to height.
void removeItem(QGraphicsLayoutItem *item)
Removes the layout item item without destroying it.
void setGeometry(const QRectF &rect)
Sets the bounding geometry of the grid layout to rect.
void invalidate()
Reimplemented Function
void adjust(qreal x1, qreal y1, qreal x2, qreal y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:778
void setColumnMaximumWidth(int column, qreal width)
Sets the maximum width of column to width.
int columnCount() const
Returns the number of columns in the grid layout.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool isWidget() const
Returns true if this item is a widget (i.
void setColumnSpacing(int column, qreal spacing)
Sets the spacing for column to spacing.
void removeAt(int index)
Removes the layout item at index without destroying it.
int rowCount() const
Returns the number of rows in the grid layout.
quint16 index
void setColumnMinimumWidth(int column, qreal width)
Sets the minimum width for column to width.
void setRowSpacing(int row, qreal spacing)
Sets the spacing for row to spacing.
qreal horizontalSpacing() const
Returns the default horizontal spacing for the grid layout.
QGraphicsLayoutItem * itemAt(int row, int column) const
Returns a pointer to the layout item at (row, column).
Orientation
Definition: qnamespace.h:174
void setColumnPreferredWidth(int column, qreal width)
Sets the preferred width for column to width.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
void setHorizontalSpacing(qreal spacing)
Sets the default horizontal spacing for the grid layout to spacing.
virtual ~QGraphicsGridLayout()
Destroys the QGraphicsGridLayout object.
QGraphicsLayoutItem * parent