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 QGraphicsLinearLayout
|
---|
44 | \brief The QGraphicsLinearLayout class provides a horizontal or vertical
|
---|
45 | layout for managing widgets in Graphics View.
|
---|
46 | \since 4.4
|
---|
47 | \ingroup multimedia
|
---|
48 | \ingroup graphicsview-api
|
---|
49 |
|
---|
50 | The default orientation for a linear layout is Qt::Horizontal. You can
|
---|
51 | choose a vertical orientation either by calling setOrientation(), or by
|
---|
52 | passing Qt::Vertical to QGraphicsLinearLayout's constructor.
|
---|
53 |
|
---|
54 | The most common way to use QGraphicsLinearLayout is to construct an object
|
---|
55 | on the heap with no parent, add widgets and layouts by calling addItem(),
|
---|
56 | and finally assign the layout to a widget by calling
|
---|
57 | QGraphicsWidget::setLayout().
|
---|
58 |
|
---|
59 | \snippet doc/src/snippets/code/src_gui_graphicsview_qgraphicslinearlayout.cpp 0
|
---|
60 |
|
---|
61 | You can add widgets, layouts, stretches (addStretch(), insertStretch() or
|
---|
62 | setStretchFactor()), and spacings (setItemSpacing()) to a linear
|
---|
63 | layout. The layout takes ownership of the items. In some cases when the layout
|
---|
64 | item also inherits from QGraphicsItem (such as QGraphicsWidget) there will be a
|
---|
65 | ambiguity in ownership because the layout item belongs to two ownership hierarchies.
|
---|
66 | See the documentation of QGraphicsLayoutItem::setOwnedByLayout() how to handle
|
---|
67 | this.
|
---|
68 | You can access each item in the layout by calling count() and itemAt(). Calling
|
---|
69 | removeAt() or removeItem() will remove an item from the layout, without
|
---|
70 | destroying it.
|
---|
71 |
|
---|
72 | \section1 Size Hints and Size Policies in QGraphicsLinearLayout
|
---|
73 |
|
---|
74 | QGraphicsLinearLayout respects each item's size hints and size policies,
|
---|
75 | and when the layout contains more space than the items can fill, each item
|
---|
76 | is arranged according to the layout's alignment for that item. You can set
|
---|
77 | an alignment for each item by calling setAlignment(), and check the
|
---|
78 | alignment for any item by calling alignment(). By default, items are
|
---|
79 | centered both vertically and horizontally.
|
---|
80 |
|
---|
81 | \section1 Spacing within QGraphicsLinearLayout
|
---|
82 |
|
---|
83 | Between the items, the layout distributes some space. The actual amount of
|
---|
84 | space depends on the managed widget's current style, but the common
|
---|
85 | spacing is 4. You can also set your own spacing by calling setSpacing(),
|
---|
86 | and get the current spacing value by calling spacing(). If you want to
|
---|
87 | configure individual spacing for your items, you can call setItemSpacing().
|
---|
88 |
|
---|
89 | \section1 Stretch Factor in QGraphicsLinearLayout
|
---|
90 |
|
---|
91 | You can assign a stretch factor to each item to control how much space it
|
---|
92 | will get compared to the other items. By default, two identical widgets
|
---|
93 | arranged in a linear layout will have the same size, but if the first
|
---|
94 | widget has a stretch factor of 1 and the second widget has a stretch
|
---|
95 | factor of 2, the first widget will get 1/3 of the available space, and the
|
---|
96 | second will get 2/3.
|
---|
97 |
|
---|
98 | QGraphicsLinearLayout calculates the distribution of sizes by adding up
|
---|
99 | the stretch factors of all items, and then dividing the available space
|
---|
100 | accordingly. The default stretch factor is 0 for all items; a factor of 0
|
---|
101 | means the item does not have any defined stretch factor; effectively this
|
---|
102 | is the same as setting the stretch factor to 1. The stretch factor only
|
---|
103 | applies to the available space in the lengthwise direction of the layout
|
---|
104 | (following its orientation). If you want to control both the item's
|
---|
105 | horizontal and vertical stretch, you can use QGraphicsGridLayout instead.
|
---|
106 |
|
---|
107 | \section1 QGraphicsLinearLayout Compared to Other Layouts
|
---|
108 |
|
---|
109 | QGraphicsLinearLayout is very similar to QVBoxLayout and QHBoxLayout, but
|
---|
110 | in contrast to these classes, it is used to manage QGraphicsWidget and
|
---|
111 | QGraphicsLayout instead of QWidget and QLayout.
|
---|
112 |
|
---|
113 | \sa QGraphicsGridLayout, QGraphicsWidget
|
---|
114 | */
|
---|
115 |
|
---|
116 | #include "qapplication.h"
|
---|
117 |
|
---|
118 | #ifndef QT_NO_GRAPHICSVIEW
|
---|
119 |
|
---|
120 | #include "qwidget.h"
|
---|
121 | #include "qgraphicslayout_p.h"
|
---|
122 | #include "qgraphicslayoutitem.h"
|
---|
123 | #include "qgraphicslinearlayout.h"
|
---|
124 | #include "qgraphicswidget.h"
|
---|
125 | #include "qgridlayoutengine_p.h"
|
---|
126 | #ifdef QT_DEBUG
|
---|
127 | #include <QtCore/qdebug.h>
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | QT_BEGIN_NAMESPACE
|
---|
131 |
|
---|
132 | class QGraphicsLinearLayoutPrivate : public QGraphicsLayoutPrivate
|
---|
133 | {
|
---|
134 | public:
|
---|
135 | QGraphicsLinearLayoutPrivate(Qt::Orientation orientation) : orientation(orientation) { }
|
---|
136 |
|
---|
137 | void removeGridItem(QGridLayoutItem *gridItem);
|
---|
138 | QLayoutStyleInfo styleInfo() const;
|
---|
139 | void fixIndex(int *index) const;
|
---|
140 | int gridRow(int index) const;
|
---|
141 | int gridColumn(int index) const;
|
---|
142 |
|
---|
143 | Qt::Orientation orientation;
|
---|
144 | QGridLayoutEngine engine;
|
---|
145 | };
|
---|
146 |
|
---|
147 | void QGraphicsLinearLayoutPrivate::removeGridItem(QGridLayoutItem *gridItem)
|
---|
148 | {
|
---|
149 | int index = gridItem->firstRow(orientation);
|
---|
150 | engine.removeItem(gridItem);
|
---|
151 | engine.removeRow(index, orientation);
|
---|
152 | }
|
---|
153 |
|
---|
154 | void QGraphicsLinearLayoutPrivate::fixIndex(int *index) const
|
---|
155 | {
|
---|
156 | int count = engine.rowCount(orientation);
|
---|
157 | if (uint(*index) > uint(count))
|
---|
158 | *index = count;
|
---|
159 | }
|
---|
160 |
|
---|
161 | int QGraphicsLinearLayoutPrivate::gridRow(int index) const
|
---|
162 | {
|
---|
163 | if (orientation == Qt::Horizontal)
|
---|
164 | return 0;
|
---|
165 | return int(qMin(uint(index), uint(engine.rowCount())));
|
---|
166 | }
|
---|
167 |
|
---|
168 | int QGraphicsLinearLayoutPrivate::gridColumn(int index) const
|
---|
169 | {
|
---|
170 | if (orientation == Qt::Vertical)
|
---|
171 | return 0;
|
---|
172 | return int(qMin(uint(index), uint(engine.columnCount())));
|
---|
173 | }
|
---|
174 |
|
---|
175 | QLayoutStyleInfo QGraphicsLinearLayoutPrivate::styleInfo() const
|
---|
176 | {
|
---|
177 | static QWidget *wid = 0;
|
---|
178 | if (!wid)
|
---|
179 | wid = new QWidget;
|
---|
180 | QGraphicsItem *item = parentItem();
|
---|
181 | QStyle *style = (item && item->isWidget()) ? static_cast<QGraphicsWidget*>(item)->style() : qApp->style();
|
---|
182 | return QLayoutStyleInfo(style, wid);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*!
|
---|
186 | Constructs a QGraphicsLinearLayout instance. You can pass the
|
---|
187 | \a orientation for the layout, either horizontal or vertical, and
|
---|
188 | \a parent is passed to QGraphicsLayout's constructor.
|
---|
189 | */
|
---|
190 | QGraphicsLinearLayout::QGraphicsLinearLayout(Qt::Orientation orientation, QGraphicsLayoutItem *parent)
|
---|
191 | : QGraphicsLayout(*new QGraphicsLinearLayoutPrivate(orientation), parent)
|
---|
192 | {
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*!
|
---|
196 | Constructs a QGraphicsLinearLayout instance using Qt::Horizontal
|
---|
197 | orientation. \a parent is passed to QGraphicsLayout's constructor.
|
---|
198 | */
|
---|
199 | QGraphicsLinearLayout::QGraphicsLinearLayout(QGraphicsLayoutItem *parent)
|
---|
200 | : QGraphicsLayout(*new QGraphicsLinearLayoutPrivate(Qt::Horizontal), parent)
|
---|
201 | {
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*!
|
---|
205 | Destroys the QGraphicsLinearLayout object.
|
---|
206 | */
|
---|
207 | QGraphicsLinearLayout::~QGraphicsLinearLayout()
|
---|
208 | {
|
---|
209 | for (int i = count() - 1; i >= 0; --i) {
|
---|
210 | QGraphicsLayoutItem *item = itemAt(i);
|
---|
211 | // The following lines can be removed, but this removes the item
|
---|
212 | // from the layout more efficiently than the implementation of
|
---|
213 | // ~QGraphicsLayoutItem.
|
---|
214 | removeAt(i);
|
---|
215 | if (item) {
|
---|
216 | item->setParentLayoutItem(0);
|
---|
217 | if (item->ownedByLayout())
|
---|
218 | delete item;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
|
---|