Qt 4.8
qgraphicslinearlayout.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 
118 #include "qapplication.h"
119 
120 #ifndef QT_NO_GRAPHICSVIEW
121 
122 #include "qwidget.h"
123 #include "qgraphicslayout_p.h"
124 #include "qgraphicslayoutitem.h"
125 #include "qgraphicslinearlayout.h"
126 #include "qgraphicswidget.h"
127 #include "qgridlayoutengine_p.h"
128 #ifdef QT_DEBUG
129 #include <QtCore/qdebug.h>
130 #endif
131 
133 
135 {
136 public:
138 
139  void removeGridItem(QGridLayoutItem *gridItem);
140  QLayoutStyleInfo styleInfo() const;
141  void fixIndex(int *index) const;
142  int gridRow(int index) const;
143  int gridColumn(int index) const;
144 
147 };
148 
150 {
151  int index = gridItem->firstRow(orientation);
152  engine.removeItem(gridItem);
153  engine.removeRows(index, 1, orientation);
154 }
155 
157 {
158  int count = engine.rowCount(orientation);
159  if (uint(*index) > uint(count))
160  *index = count;
161 }
162 
164 {
166  return 0;
167  return int(qMin(uint(index), uint(engine.rowCount())));
168 }
169 
171 {
172  if (orientation == Qt::Vertical)
173  return 0;
174  return int(qMin(uint(index), uint(engine.columnCount())));
175 }
176 
177 Q_GLOBAL_STATIC(QWidget, globalStyleInfoWidget)
178 
180 {
181  QGraphicsItem *item = parentItem();
182  QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : QApplication::style();
183  return QLayoutStyleInfo(style, globalStyleInfoWidget());
184 }
185 
192  : QGraphicsLayout(*new QGraphicsLinearLayoutPrivate(orientation), parent)
193 {
194 }
195 
202 {
203 }
204 
209 {
210  for (int i = count() - 1; i >= 0; --i) {
211  QGraphicsLayoutItem *item = itemAt(i);
212  // The following lines can be removed, but this removes the item
213  // from the layout more efficiently than the implementation of
214  // ~QGraphicsLayoutItem.
215  removeAt(i);
216  if (item) {
217  item->setParentLayoutItem(0);
218  if (item->ownedByLayout())
219  delete item;
220  }
221  }
222 }
223 
231 {
233  if (orientation != d->orientation) {
234  d->engine.transpose();
235  d->orientation = orientation;
236  invalidate();
237  }
238 }
239 
245 {
246  Q_D(const QGraphicsLinearLayout);
247  return d->orientation;
248 }
249 
274 {
276  if (!item) {
277  qWarning("QGraphicsLinearLayout::insertItem: cannot insert null item");
278  return;
279  }
280  if (item == this) {
281  qWarning("QGraphicsLinearLayout::insertItem: cannot insert itself");
282  return;
283  }
284  d->addChildLayoutItem(item);
285 
286  Q_ASSERT(item);
287  d->fixIndex(&index);
288  d->engine.insertRow(index, d->orientation);
289  new QGridLayoutItem(&d->engine, item, d->gridRow(index), d->gridColumn(index), 1, 1, 0, index);
290  invalidate();
291 }
292 
300 {
302  d->fixIndex(&index);
303  d->engine.insertRow(index, d->orientation);
304  d->engine.setRowStretchFactor(index, stretch, d->orientation);
305  invalidate();
306 }
307 
315 {
317  if (QGridLayoutItem *gridItem = d->engine.findLayoutItem(item)) {
318  item->setParentLayoutItem(0);
319  d->removeGridItem(gridItem);
320  delete gridItem;
321  invalidate();
322  }
323 }
324 
332 {
334  if (index < 0 || index >= d->engine.itemCount()) {
335  qWarning("QGraphicsLinearLayout::removeAt: invalid index %d", index);
336  return;
337  }
338  if (QGridLayoutItem *gridItem = d->engine.itemAt(index)) {
339  if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
340  layoutItem->setParentLayoutItem(0);
341  d->removeGridItem(gridItem);
342  delete gridItem;
343  invalidate();
344  }
345 }
346 
354 {
356  if (spacing < 0) {
357  qWarning("QGraphicsLinearLayout::setSpacing: invalid spacing %g", spacing);
358  return;
359  }
360  d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical);
361  invalidate();
362 }
363 
371 {
372  Q_D(const QGraphicsLinearLayout);
373  return d->engine.spacing(d->styleInfo(), d->orientation);
374 }
375 
380 {
382  d->engine.setRowSpacing(index, spacing, d->orientation);
383  invalidate();
384 }
389 {
390  Q_D(const QGraphicsLinearLayout);
391  return d->engine.rowSpacing(index, d->orientation);
392 }
393 
404 {
406  if (!item) {
407  qWarning("QGraphicsLinearLayout::setStretchFactor: cannot assign"
408  " a stretch factor to a null item");
409  return;
410  }
411  if (stretchFactor(item) == stretch)
412  return;
413  d->engine.setStretchFactor(item, stretch, d->orientation);
414  invalidate();
415 }
416 
424 {
425  Q_D(const QGraphicsLinearLayout);
426  if (!item) {
427  qWarning("QGraphicsLinearLayout::setStretchFactor: cannot return"
428  " a stretch factor for a null item");
429  return 0;
430  }
431  return d->engine.stretchFactor(item, d->orientation);
432 }
433 
441 {
443  if (this->alignment(item) == alignment)
444  return;
445  d->engine.setAlignment(item, alignment);
446  invalidate();
447 }
448 
460 {
461  Q_D(const QGraphicsLinearLayout);
462  return d->engine.alignment(item);
463 }
464 
465 #if 0 // ###
466 QSizePolicy::ControlTypes QGraphicsLinearLayout::controlTypes(LayoutSide side) const
467 {
468  return d->engine.controlTypes(side);
469 }
470 #endif
471 
476 {
477  Q_D(const QGraphicsLinearLayout);
478  return d->engine.itemCount();
479 }
480 
489 {
490  Q_D(const QGraphicsLinearLayout);
491  if (index < 0 || index >= d->engine.itemCount()) {
492  qWarning("QGraphicsLinearLayout::itemAt: invalid index %d", index);
493  return 0;
494  }
495  QGraphicsLayoutItem *item = 0;
496  if (QGridLayoutItem *gridItem = d->engine.itemAt(index))
497  item = gridItem->layoutItem();
498  return item;
499 }
500 
505 {
508  QRectF effectiveRect = geometry();
509  qreal left, top, right, bottom;
510  getContentsMargins(&left, &top, &right, &bottom);
511  Qt::LayoutDirection visualDir = d->visualDirection();
512  d->engine.setVisualDirection(visualDir);
513  if (visualDir == Qt::RightToLeft)
514  qSwap(left, right);
515  effectiveRect.adjust(+left, +top, -right, -bottom);
516 #ifdef QT_DEBUG
517  if (qt_graphicsLayoutDebug()) {
518  static int counter = 0;
519  qDebug() << counter++ << "QGraphicsLinearLayout::setGeometry - " << rect;
520  dump(1);
521  }
522 #endif
523  d->engine.setGeometries(d->styleInfo(), effectiveRect);
524 #ifdef QT_DEBUG
525  if (qt_graphicsLayoutDebug()) {
526  qDebug() << "post dump";
527  dump(1);
528  }
529 #endif
530 }
531 
536 {
537  Q_D(const QGraphicsLinearLayout);
538  qreal left, top, right, bottom;
539  getContentsMargins(&left, &top, &right, &bottom);
540  const QSizeF extraMargins(left + right, top + bottom);
541  return d->engine.sizeHint(d->styleInfo(), which , constraint - extraMargins) + extraMargins;
542 }
543 
548 {
550  d->engine.invalidate();
552 }
553 
557 void QGraphicsLinearLayout::dump(int indent) const
558 {
559 #ifdef QT_DEBUG
560  if (qt_graphicsLayoutDebug()) {
561  Q_D(const QGraphicsLinearLayout);
562  qDebug("%*s%s layout", indent, "",
563  d->orientation == Qt::Horizontal ? "Horizontal" : "Vertical");
564  d->engine.dump(indent + 1);
565  }
566 #else
567  Q_UNUSED(indent);
568 #endif
569 }
570 
572 
573 #endif //QT_NO_GRAPHICSVIEW
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...
double qreal
Definition: qglobal.h:1193
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
Reimplemented Function
void setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
Sets the alignment of item to alignment.
void removeItem(QGridLayoutItem *item)
void setItemSpacing(int index, qreal spacing)
Sets the spacing after item at index 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
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Reimplemented Function
void removeGridItem(QGridLayoutItem *gridItem)
void setStretchFactor(QGraphicsLayoutItem *item, int stretch)
Sets the stretch factor for item to stretch.
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
static QStyle * style()
Returns the application&#39;s style object.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#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
void insertStretch(int index, int stretch=1)
Inserts a stretch of stretch at index, or before any item that is currently at index.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
qreal itemSpacing(int index) const
Returns the spacing after item at index.
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
virtual ~QGraphicsLinearLayout()
Destroys the QGraphicsLinearLayout object.
Q_CORE_EXPORT void qDebug(const char *,...)
The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Grap...
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
QGraphicsLinearLayout(QGraphicsLayoutItem *parent=0)
Constructs a QGraphicsLinearLayout instance using Qt::Horizontal orientation.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
LayoutDirection
Definition: qnamespace.h:1580
Q_CORE_EXPORT void qWarning(const char *,...)
void removeRows(int row, int count, Qt::Orientation orientation)
unsigned int uint
Definition: qglobal.h:996
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
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...
void setSpacing(qreal spacing)
Sets the layout&#39;s spacing to spacing.
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
SizeHint
Definition: qnamespace.h:1708
QGraphicsLayoutItem * itemAt(int index) const
When iterating from 0 and up, it will return the items in the visual arranged order.
QGraphicsLinearLayoutPrivate(Qt::Orientation orientation)
void invalidate()
Reimplemented Function
void removeItem(QGraphicsLayoutItem *item)
Removes item from the layout without destroying it.
qreal spacing() const
Returns the layout&#39;s spacing.
int columnCount(Qt::Orientation orientation) const
QLayoutStyleInfo styleInfo() const
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
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.
Definition: qnamespace.h:54
bool isWidget() const
Returns true if this item is a widget (i.
Qt::Orientation orientation() const
Returns the layout orientation.
quint16 index
void removeAt(int index)
Removes the item at index without destroying it.
void setGeometry(const QRectF &rect)
Reimplemented Function
Qt::Alignment alignment(QGraphicsLayoutItem *item) const
Returns the alignment for item.
void dump(int indent=0) const
int stretchFactor(QGraphicsLayoutItem *item) const
Returns the stretch factor for item.
int count() const
Reimplemented Function
void insertItem(int index, QGraphicsLayoutItem *item)
Inserts item into the layout at index, or before any item that is currently at index.
Orientation
Definition: qnamespace.h:174
int rowCount(Qt::Orientation orientation) const
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QGraphicsLayoutItem * parent
void setOrientation(Qt::Orientation orientation)
Change the layout orientation to orientation.