source: trunk/src/gui/graphicsview/qgraphicsgridlayout.cpp@ 135

Last change on this file since 135 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 18.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \class QGraphicsGridLayout
44 \brief The QGraphicsGridLayout class provides a grid layout for managing
45 widgets in Graphics View.
46 \since 4.4
47 \ingroup multimedia
48 \ingroup graphicsview-api
49
50 The most common way to use QGraphicsGridLayout is to construct an object
51 on the heap with no parent, add widgets and layouts by calling addItem(),
52 and finally assign the layout to a widget by calling
53 QGraphicsWidget::setLayout(). QGraphicsGridLayout automatically computes
54 the dimensions of the grid as you add items.
55
56 \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicsgridlayout.cpp 0
57
58 The layout takes ownership of the items. In some cases when the layout
59 item also inherits from QGraphicsItem (such as QGraphicsWidget) there will be a
60 ambiguity in ownership because the layout item belongs to two ownership hierarchies.
61 See the documentation of QGraphicsLayoutItem::setOwnedByLayout() how to handle
62 this.
63 You can access each item in the layout by calling count() and itemAt(). Calling
64 removeAt() will remove an item from the layout, without
65 destroying it.
66
67 \sa QGraphicsLinearLayout, QGraphicsWidget
68*/
69
70#include "qglobal.h"
71
72#ifndef QT_NO_GRAPHICSVIEW
73
74#include "qapplication.h"
75#include "qwidget.h"
76#include "qgraphicslayout_p.h"
77#include "qgraphicslayoutitem.h"
78#include "qgraphicsgridlayout.h"
79#include "qgraphicswidget.h"
80#include "qgridlayoutengine_p.h"
81#include <QtCore/qdebug.h>
82
83QT_BEGIN_NAMESPACE
84
85class QGraphicsGridLayoutPrivate : public QGraphicsLayoutPrivate
86{
87public:
88 QGraphicsGridLayoutPrivate() { }
89 QLayoutStyleInfo styleInfo() const;
90
91 QGridLayoutEngine engine;
92#ifdef QT_DEBUG
93 void dump(int indent) const;
94#endif
95};
96
97QLayoutStyleInfo QGraphicsGridLayoutPrivate::styleInfo() const
98{
99 static QWidget *wid = 0;
100 if (!wid)
101 wid = new QWidget;
102 QGraphicsItem *item = parentItem();
103 QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : qApp->style();
104 return QLayoutStyleInfo(style, wid);
105}
106
107/*!
108 Constructs a QGraphicsGridLayout instance. \a parent is passed to
109 QGraphicsLayout's constructor.
110*/
111QGraphicsGridLayout::QGraphicsGridLayout(QGraphicsLayoutItem *parent)
112 : QGraphicsLayout(*new QGraphicsGridLayoutPrivate(), parent)
113{
114}
115
116/*!
117 Destroys the QGraphicsGridLayout object.
118*/
119QGraphicsGridLayout::~QGraphicsGridLayout()
120{
121 for (int i = count() - 1; i >= 0; --i) {
122 QGraphicsLayoutItem *item = itemAt(i);
123 // The following lines can be removed, but this removes the item
124 // from the layout more efficiently than the implementation of
125 // ~QGraphicsLayoutItem.
126 removeAt(i);
127 if (item) {
128 item->setParentLayoutItem(0);
129 if (item->ownedByLayout())
130 delete item;
131 }
132 }
133}
134
135/*!
136 Adds \a item to the grid on \a row and \a column. You can specify a
137 \a rowSpan and \a columnSpan and an optional \a alignment.
138*/
139void QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column,
140 int rowSpan, int columnSpan, Qt::Alignment alignment)
141{
142 Q_D(QGraphicsGridLayout);
143 if (row < 0 || column < 0) {
144 qWarning("QGraphicsGridLayout::addItem: invalid row/column: %d",
145 row < 0 ? row : column);
146 return;
147 }
148 if (columnSpan < 1 || rowSpan < 1) {
149 qWarning("QGraphicsGridLayout::addItem: invalid row span/column span: %d",
150 rowSpan < 1 ? rowSpan : columnSpan);
151 return;
152 }
153 if (!item) {
154 qWarning("QGraphicsGridLayout::addItem: cannot add null item");
155 return;
156 }
157
158 d->addChildLayoutItem(item);
159
160 new QGridLayoutItem(&d->engine, item, row, column, rowSpan, columnSpan, alignment);
161 invalidate();
162}
163
164/*!
165 \fn QGraphicsGridLayout::addItem(QGraphicsLayoutItem *item, int row, int column, Qt::Alignment alignment = 0)
166
167 Adds \a item to the grid on \a row and \a column. You can specify
168 an optional \a alignment for \a item.
169*/
170
171/*!
172 Sets the default horizontal spacing for the grid layout to \a spacing.
173*/
174void QGraphicsGridLayout::setHorizontalSpacing(qreal spacing)
175{
176 Q_D(QGraphicsGridLayout);
177 d->engine.setSpacing(spacing, Qt::Horizontal);
178 invalidate();
179}
180
181/*!
182 Returns the default horizontal spacing for the grid layout.
183*/
184qreal QGraphicsGridLayout::horizontalSpacing() const
185{
186 Q_D(const QGraphicsGridLayout);
187 return d->engine.spacing(d->styleInfo(), Qt::Horizontal);
188}
189
190/*!
191 Sets the default vertical spacing for the grid layout to \a spacing.
192*/
193void QGraphicsGridLayout::setVerticalSpacing(qreal spacing)
194{
195 Q_D(QGraphicsGridLayout);
196 d->engine.setSpacing(spacing, Qt::Vertical);
197 invalidate();
198}
199
200/*!
201 Returns the default vertical spacing for the grid layout.
202*/
203qreal QGraphicsGridLayout::verticalSpacing() const
204{
205 Q_D(const QGraphicsGridLayout);
206 return d->engine.spacing(d->styleInfo(), Qt::Vertical);
207}
208
209/*!
210 Sets the grid layout's default spacing, both vertical and
211 horizontal, to \a spacing.
212
213 \sa rowSpacing(), columnSpacing()
214*/
215void QGraphicsGridLayout::setSpacing(qreal spacing)
216{
217 Q_D(QGraphicsGridLayout);
218 d->engine.setSpacing(spacing, Qt::Horizontal | Qt::Vertical);
219 invalidate();
220}
221
222/*!
223 Sets the spacing for \a row to \a spacing.
224*/
225void QGraphicsGridLayout::setRowSpacing(int row, qreal spacing)
226{
227 Q_D(QGraphicsGridLayout);
228 d->engine.setRowSpacing(row, spacing, Qt::Vertical);
229 invalidate();
230}
231
232/*!
233 Returns the row spacing for \a row.
234*/
235qreal QGraphicsGridLayout::rowSpacing(int row) const
236{
237 Q_D(const QGraphicsGridLayout);
238 return d->engine.rowSpacing(row, Qt::Vertical);
239}
240
241/*!
242 Sets the spacing for \a column to \a spacing.
243*/
244void QGraphicsGridLayout::setColumnSpacing(int column, qreal spacing)
245{
246 Q_D(QGraphicsGridLayout);
247 d->engine.setRowSpacing(column, spacing, Qt::Horizontal);
248 invalidate();
249}
250
251/*!
252 Returns the column spacing for \a column.
253*/
254qreal QGraphicsGridLayout::columnSpacing(int column) const
255{
256 Q_D(const QGraphicsGridLayout);
257 return d->engine.rowSpacing(column, Qt::Horizontal);
258}
259
260/*!
261 Sets the stretch factor for \a row to \a stretch.
262*/
263void QGraphicsGridLayout::setRowStretchFactor(int row, int stretch)
264{
265 Q_D(QGraphicsGridLayout);
266 d->engine.setRowStretchFactor(row, stretch, Qt::Vertical);
267 invalidate();
268}
269
270/*!
271 Returns the stretch factor for \a row.
272*/
273int QGraphicsGridLayout::rowStretchFactor(int row) const
274{
275 Q_D(const QGraphicsGridLayout);
276 return d->engine.rowStretchFactor(row, Qt::Vertical);
277}
278
279/*!
280 Sets the stretch factor for \a column to \a stretch.
281*/
282void QGraphicsGridLayout::setColumnStretchFactor(int column, int stretch)
283{
284 Q_D(QGraphicsGridLayout);
285 d->engine.setRowStretchFactor(column, stretch, Qt::Horizontal);
286 invalidate();
287}
288
289/*!
290 Returns the stretch factor for \a column.
291*/
292int QGraphicsGridLayout::columnStretchFactor(int column) const
293{
294 Q_D(const QGraphicsGridLayout);
295 return d->engine.rowStretchFactor(column, Qt::Horizontal);
296}
297
298/*!
299 Sets the minimum height for row, \a row, to \a height.
300*/
301void QGraphicsGridLayout::setRowMinimumHeight(int row, qreal height)
302{
303 Q_D(QGraphicsGridLayout);
304 d->engine.setRowSizeHint(Qt::MinimumSize, row, height, Qt::Vertical);
305 invalidate();
306}
307
308/*!
309 Returns the minimum height for row, \a row.
310*/
311qreal QGraphicsGridLayout::rowMinimumHeight(int row) const
312{
313 Q_D(const QGraphicsGridLayout);
314 return d->engine.rowSizeHint(Qt::MinimumSize, row, Qt::Vertical);
315}
316
317/*!
318 Sets the preferred height for row, \a row, to \a height.
319*/
320void QGraphicsGridLayout::setRowPreferredHeight(int row, qreal height)
321{
322 Q_D(QGraphicsGridLayout);
323 d->engine.setRowSizeHint(Qt::PreferredSize, row, height, Qt::Vertical);
324 invalidate();
325}
326
327/*!
328 Returns the preferred height for row, \a row.
329*/
330qreal QGraphicsGridLayout::rowPreferredHeight(int row) const
331{
332 Q_D(const QGraphicsGridLayout);
333 return d->engine.rowSizeHint(Qt::PreferredSize, row, Qt::Vertical);
334}
335
336/*!
337 Sets the maximum height for row, \a row, to \a height.
338*/
339void QGraphicsGridLayout::setRowMaximumHeight(int row, qreal height)
340{
341 Q_D(QGraphicsGridLayout);
342 d->engine.setRowSizeHint(Qt::MaximumSize, row, height, Qt::Vertical);
343 invalidate();
344}
345
346/*!
347 Returns the maximum height for row, \a row.
348*/
349qreal QGraphicsGridLayout::rowMaximumHeight(int row) const
350{
351 Q_D(const QGraphicsGridLayout);
352 return d->engine.rowSizeHint(Qt::MaximumSize, row, Qt::Vertical);
353}
354
355/*!
356 Sets the fixed height for row, \a row, to \a height.
357*/
358void QGraphicsGridLayout::setRowFixedHeight(int row, qreal height)
359{
360 Q_D(QGraphicsGridLayout);
361 d->engine.setRowSizeHint(Qt::MinimumSize, row, height, Qt::Vertical);
362 d->engine.setRowSizeHint(Qt::MaximumSize, row, height, Qt::Vertical);
363 invalidate();
364}
365
366/*!
367 Sets the minimum width for \a column to \a width.
368*/
369void QGraphicsGridLayout::setColumnMinimumWidth(int column, qreal width)
370{
371 Q_D(QGraphicsGridLayout);
372 d->engine.setRowSizeHint(Qt::MinimumSize, column, width, Qt::Horizontal);
373 invalidate();
374}
375
376/*!
377 Returns the minimum width for \a column.
378*/
379qreal QGraphicsGridLayout::columnMinimumWidth(int column) const
380{
381 Q_D(const QGraphicsGridLayout);
382 return d->engine.rowSizeHint(Qt::MinimumSize, column, Qt::Horizontal);
383}
384
385/*!
386 Sets the preferred width for \a column to \a width.
387*/
388void QGraphicsGridLayout::setColumnPreferredWidth(int column, qreal width)
389{
390 Q_D(QGraphicsGridLayout);
391 d->engine.setRowSizeHint(Qt::PreferredSize, column, width, Qt::Horizontal);
392 invalidate();
393}
394
395/*!
396 Returns the preferred width for \a column.
397*/
398qreal QGraphicsGridLayout::columnPreferredWidth(int column) const
399{
400 Q_D(const QGraphicsGridLayout);
401 return d->engine.rowSizeHint(Qt::PreferredSize, column, Qt::Horizontal);
402}
403
404/*!
405 Sets the maximum width of \a column to \a width.
406*/
407void QGraphicsGridLayout::setColumnMaximumWidth(int column, qreal width)
408{
409 Q_D(QGraphicsGridLayout);
410 d->engine.setRowSizeHint(Qt::MaximumSize, column, width, Qt::Horizontal);
411 invalidate();
412}
413
414/*!
415 Returns the maximum width for \a column.
416*/
417qreal QGraphicsGridLayout::columnMaximumWidth(int column) const
418{
419 Q_D(const QGraphicsGridLayout);
420 return d->engine.rowSizeHint(Qt::MaximumSize, column, Qt::Horizontal);
421}
422
423/*!
424 Sets the fixed width of \a column to \a width.
425*/
426void QGraphicsGridLayout::setColumnFixedWidth(int column, qreal width)
427{
428 Q_D(QGraphicsGridLayout);
429 d->engine.setRowSizeHint(Qt::MinimumSize, column, width, Qt::Horizontal);
430 d->engine.setRowSizeHint(Qt::MaximumSize, column, width, Qt::Horizontal);
431 invalidate();
432}
433
434/*!
435 Sets the alignment of \a row to \a alignment.
436*/
437void QGraphicsGridLayout::setRowAlignment(int row, Qt::Alignment alignment)
438{
439 Q_D(QGraphicsGridLayout);
440 d->engine.setRowAlignment(row, alignment, Qt::Vertical);
441 invalidate();
442}
443
444/*!
445 Returns the alignment of \a row.
446*/
447Qt::Alignment QGraphicsGridLayout::rowAlignment(int row) const
448{
449 Q_D(const QGraphicsGridLayout);
450 return d->engine.rowAlignment(row, Qt::Vertical);
451}
452
453/*!
454 Sets the alignment for \a column to \a alignment.
455*/
456void QGraphicsGridLayout::setColumnAlignment(int column, Qt::Alignment alignment)
457{
458 Q_D(QGraphicsGridLayout);
459 d->engine.setRowAlignment(column, alignment, Qt::Horizontal);
460 invalidate();
461}
462
463/*!
464 Returns the alignment for \a column.
465*/
466Qt::Alignment QGraphicsGridLayout::columnAlignment(int column) const
467{
468 Q_D(const QGraphicsGridLayout);
469 return d->engine.rowAlignment(column, Qt::Horizontal);
470}
471
472/*!
473 Sets the alignment for \a item to \a alignment.
474*/
475void QGraphicsGridLayout::setAlignment(QGraphicsLayoutItem *item, Qt::Alignment alignment)
476{
477 Q_D(QGraphicsGridLayout);
478 d->engine.setAlignment(item, alignment);
479 invalidate();
480}
481
482/*!
483 Returns the alignment for \a item.
484*/
485Qt::Alignment QGraphicsGridLayout::alignment(QGraphicsLayoutItem *item) const
486{
487 Q_D(const QGraphicsGridLayout);
488 return d->engine.alignment(item);
489}
490
491/*!
492 Returns the number of rows in the grid layout. This is always one more
493 than the index of the last row that is occupied by a layout item (empty
494 rows are counted except for those at the end).
495*/
496int QGraphicsGridLayout::rowCount() const
497{
498 Q_D(const QGraphicsGridLayout);
499 return d->engine.effectiveLastRow(Qt::Vertical) + 1;
500}
501
502/*!
503 Returns the number of columns in the grid layout. This is always one more
504 than the index of the last column that is occupied by a layout item (empty
505 columns are counted except for those at the end).
506*/
507int QGraphicsGridLayout::columnCount() const
508{
509 Q_D(const QGraphicsGridLayout);
510 return d->engine.effectiveLastRow(Qt::Horizontal) + 1;
511}
512
513/*!
514 Returns a pointer to the layout item at (\a row, \a column).
515*/
516QGraphicsLayoutItem *QGraphicsGridLayout::itemAt(int row, int column) const
517{
518 Q_D(const QGraphicsGridLayout);
519 if (row < 0 || row >= rowCount() || column < 0 || column >= columnCount()) {
520 qWarning("QGraphicsGridLayout::itemAt: invalid row, column %d, %d", row, column);
521 return 0;
522 }
523 if (QGridLayoutItem *item = d->engine.itemAt(row, column))
524 return item->layoutItem();
525 return 0;
526}
527
528/*!
529 Returns the number of layout items in this grid layout.
530*/
531int QGraphicsGridLayout::count() const
532{
533 Q_D(const QGraphicsGridLayout);
534 return d->engine.itemCount();
535}
536
537/*!
538 Returns the layout item at \a index, or 0 if there is no layout item at
539 this index.
540*/
541QGraphicsLayoutItem *QGraphicsGridLayout::itemAt(int index) const
542{
543 Q_D(const QGraphicsGridLayout);
544 if (index < 0 || index >= d->engine.itemCount()) {
545 qWarning("QGraphicsGridLayout::itemAt: invalid index %d", index);
546 return 0;
547 }
548 QGraphicsLayoutItem *item = 0;
549 if (QGridLayoutItem *gridItem = d->engine.itemAt(index))
550 item = gridItem->layoutItem();
551 return item;
552}
553
554/*!
555 Removes the layout item at \a index without destroying it. Ownership of
556 the item is transferred to the caller.
557
558 \sa addItem()
559*/
560void QGraphicsGridLayout::removeAt(int index)
561{
562 Q_D(QGraphicsGridLayout);
563 if (index < 0 || index >= d->engine.itemCount()) {
564 qWarning("QGraphicsGridLayout::removeAt: invalid index %d", index);
565 return;
566 }
567 if (QGridLayoutItem *gridItem = d->engine.itemAt(index)) {
568 if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem())
569 layoutItem->setParentLayoutItem(0);
570 d->engine.removeItem(gridItem);
571 delete gridItem;
572 invalidate();