source: trunk/src/gui/graphicsview/qgridlayoutengine_p.h@ 460

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

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

File size: 15.0 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#ifndef QGRIDLAYOUTENGINE_P_H
43#define QGRIDLAYOUTENGINE_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists for the convenience
50// of the graphics view layout classes. This header
51// file may change from version to version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "qalgorithms.h"
57#include "qbitarray.h"
58#include "qlist.h"
59#include "qmap.h"
60#include "qpair.h"
61#include "qvector.h"
62
63#include <float.h>
64
65QT_BEGIN_NAMESPACE
66
67class QGraphicsLayoutItem;
68class QStyle;
69class QWidget;
70
71// ### deal with Descent in a similar way
72enum {
73 MinimumSize = Qt::MinimumSize,
74 PreferredSize = Qt::PreferredSize,
75 MaximumSize = Qt::MaximumSize,
76 NSizes
77};
78
79// do not reorder
80enum {
81 Hor,
82 Ver,
83 NOrientations
84};
85
86// do not reorder
87enum LayoutSide {
88 Left,
89 Top,
90 Right,
91 Bottom
92};
93
94template <typename T>
95class QLayoutParameter
96{
97public:
98 enum State { Default, User, Cached };
99
100 inline QLayoutParameter() : q_value(T()), q_state(Default) {}
101 inline QLayoutParameter(T value, State state = Default) : q_value(value), q_state(state) {}
102
103 inline void setUserValue(T value) {
104 q_value = value;
105 q_state = User;
106 }
107 inline void setCachedValue(T value) const {
108 if (q_state != User) {
109 q_value = value;
110 q_state = Cached;
111 }
112 }
113 inline T value() const { return q_value; }
114 inline T value(T defaultValue) const { return isUser() ? q_value : defaultValue; }
115 inline bool isDefault() const { return q_state == Default; }
116 inline bool isUser() const { return q_state == User; }
117 inline bool isCached() const { return q_state == Cached; }
118
119private:
120 mutable T q_value;
121 mutable State q_state;
122};
123
124class QStretchParameter : public QLayoutParameter<int>
125{
126public:
127 QStretchParameter() : QLayoutParameter<int>(-1) {}
128
129};
130
131class QLayoutStyleInfo
132{
133public:
134 inline QLayoutStyleInfo() { invalidate(); }
135 inline QLayoutStyleInfo(QStyle *style, QWidget *widget)
136 : q_valid(true), q_style(style), q_widget(widget) {}
137
138 inline void invalidate() { q_valid = false; q_style = 0; q_widget = 0; }
139
140 inline QStyle *style() const { return q_style; }
141 inline QWidget *widget() const { return q_widget; }
142
143 inline bool operator==(const QLayoutStyleInfo &other)
144 { return q_style == other.q_style && q_widget == other.q_widget; }
145 inline bool operator!=(const QLayoutStyleInfo &other)
146 { return !(*this == other); }
147
148private:
149 bool q_valid;
150 QStyle *q_style;
151 QWidget *q_widget;
152};
153
154class QGridLayoutBox
155{
156public:
157 inline QGridLayoutBox()
158 : q_minimumSize(0), q_preferredSize(0), q_maximumSize(FLT_MAX),
159 q_minimumDescent(-1), q_minimumAscent(-1) {}
160
161 void add(const QGridLayoutBox &other, int stretch, qreal spacing);
162 void combine(const QGridLayoutBox &other);
163 void normalize();
164
165#ifdef QT_DEBUG
166 void dump(int indent = 0) const;
167#endif
168 // This code could use the union-struct-array trick, but a compiler
169 // bug prevents this from working.
170 qreal q_minimumSize;
171 qreal q_preferredSize;
172 qreal q_maximumSize;
173 qreal q_minimumDescent;
174 qreal q_minimumAscent;
175 inline qreal &q_sizes(int which)
176 {
177 qreal *t;
178 switch (which) {
179 case Qt::MinimumSize:
180 t = &q_minimumSize;
181 break;
182 case Qt::PreferredSize:
183 t = &q_preferredSize;
184 break;
185 case Qt::MaximumSize:
186 t = &q_maximumSize;
187 break;
188 case Qt::MinimumDescent:
189 t = &q_minimumDescent;
190 break;
191 case (Qt::MinimumDescent + 1):
192 t = &q_minimumAscent;
193 break;
194 default:
195 t = 0;
196 break;
197 }
198 return *t;
199 }
200 inline const qreal &q_sizes(int which) const
201 {
202 const qreal *t;
203 switch (which) {
204 case Qt::MinimumSize:
205 t = &q_minimumSize;
206 break;
207 case Qt::PreferredSize:
208 t = &q_preferredSize;
209 break;
210 case Qt::MaximumSize:
211 t = &q_maximumSize;
212 break;
213 case Qt::MinimumDescent:
214 t = &q_minimumDescent;
215 break;
216 case (Qt::MinimumDescent + 1):
217 t = &q_minimumAscent;
218 break;
219 default:
220 t = 0;
221 break;
222 }
223 return *t;
224 }
225};
226
227bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2);
228inline bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2)
229 { return !operator==(box1, box2); }
230
231class QGridLayoutMultiCellData
232{
233public:
234 inline QGridLayoutMultiCellData() : q_stretch(-1) {}
235
236 QGridLayoutBox q_box;
237 int q_stretch;
238};
239
240typedef QMap<QPair<int, int>, QGridLayoutMultiCellData> MultiCellMap;
241
242class QGridLayoutRowData
243{
244public:
245 void reset(int count);
246 void distributeMultiCells();
247 void calculateGeometries(int start, int end, qreal targetSize, qreal *positions, qreal *sizes,
248 qreal *descents, const QGridLayoutBox &totalBox);
249 QGridLayoutBox totalBox(int start, int end) const;
250 void stealBox(int start, int end, int which, qreal *positions, qreal *sizes);
251
252#ifdef QT_DEBUG
253 void dump(int indent = 0) const;
254#endif
255
256 QBitArray ignore; // ### rename q_
257 QVector<QGridLayoutBox> boxes;
258 MultiCellMap multiCellMap;
259 QVector<int> stretches;
260 QVector<qreal> spacings;
261 bool hasIgnoreFlag;
262};
263
264class QGridLayoutEngine;
265
266class QGridLayoutItem
267{
268public:
269 QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem, int row, int column,
270 int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0);
271
272 inline int firstRow() const { return q_firstRows[Ver]; }
273 inline int firstColumn() const { return q_firstRows[Hor]; }
274 inline int rowSpan() const { return q_rowSpans[Ver]; }
275 inline int columnSpan() const { return q_rowSpans[Hor]; }
276 inline int lastRow() const { return firstRow() + rowSpan() - 1; }
277 inline int lastColumn() const { return firstColumn() + columnSpan() - 1; }
278
279 int firstRow(Qt::Orientation orientation) const;
280 int firstColumn(Qt::Orientation orientation) const;
281 int lastRow(Qt::Orientation orientation) const;
282 int lastColumn(Qt::Orientation orientation) const;
283 int rowSpan(Qt::Orientation orientation) const;
284 int columnSpan(Qt::Orientation orientation) const;
285 void setFirstRow(int row, Qt::Orientation orientation = Qt::Vertical);
286 void setRowSpan(int rowSpan, Qt::Orientation orientation = Qt::Vertical);
287
288 int stretchFactor(Qt::Orientation orientation) const;
289 void setStretchFactor(int stretch, Qt::Orientation orientation);
290
291 inline Qt::Alignment alignment() const { return q_alignment; }
292 inline void setAlignment(Qt::Alignment alignment) { q_alignment = alignment; }
293
294 QSizePolicy::Policy sizePolicy(Qt::Orientation orientation) const;
295 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const;
296 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
297 QGridLayoutBox box(Qt::Orientation orientation, qreal constraint = -1.0) const;
298 QRectF geometryWithin(qreal x, qreal y, qreal width, qreal height, qreal rowDescent) const;
299
300 QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; }
301
302 void setGeometry(const QRectF &rect);
303 void transpose();
304 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical);
305 QSizeF effectiveMaxSize() const;
306
307#ifdef QT_DEBUG
308 void dump(int indent = 0) const;
309#endif
310
311private:
312 QGridLayoutEngine *q_engine; // ### needed?
313 QGraphicsLayoutItem *q_layoutItem;
314 int q_firstRows[NOrientations];
315 int q_rowSpans[NOrientations];
316 int q_stretches[NOrientations];
317 Qt::Alignment q_alignment;
318};
319
320class QGridLayoutRowInfo
321{
322public:
323 inline QGridLayoutRowInfo() : count(0) {}
324
325 void insertOrRemoveRows(int row, int delta);
326
327#ifdef QT_DEBUG
328 void dump(int indent = 0) const;
329#endif
330
331 int count;
332 QVector<QStretchParameter> stretches;
333 QVector<QLayoutParameter<qreal> > spacings;
334 QVector<Qt::Alignment> alignments;
335 QVector<QGridLayoutBox> boxes;
336};
337
338class QGridLayoutEngine
339{
340public:
341 QGridLayoutEngine();
342 inline ~QGridLayoutEngine() { qDeleteAll(q_items); }
343
344 int rowCount(Qt::Orientation orientation) const;
345 int columnCount(Qt::Orientation orientation) const;
346 inline int rowCount() const { return q_infos[Ver].count; }
347 inline int columnCount() const { return q_infos[Hor].count; }
348 // returns the number of items inserted, which may be less than (rowCount * columnCount)
349 int itemCount() const;
350 QGridLayoutItem *itemAt(int index) const;
351
352 int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const;
353 int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const;
354
355 void setSpacing(qreal spacing, Qt::Orientations orientations);
356 qreal spacing(const QLayoutStyleInfo &styleInfo, Qt::Orientation orientation) const;
357 // ### setSpacingAfterRow(), spacingAfterRow()
358 void setRowSpacing(int row, qreal spacing, Qt::Orientation orientation = Qt::Vertical);
359 qreal rowSpacing(int row, Qt::Orientation orientation = Qt::Vertical) const;
360
361 void setRowStretchFactor(int row, int stretch, Qt::Orientation orientation = Qt::Vertical);
362 int rowStretchFactor(int row, Qt::Orientation orientation = Qt::Vertical) const;
363
364 void setStretchFactor(QGraphicsLayoutItem *layoutItem, int stretch,
365 Qt::Orientation orientation);
366 int stretchFactor(QGraphicsLayoutItem *layoutItem, Qt::Orientation orientation) const;
367
368 void setRowSizeHint(Qt::SizeHint which, int row, qreal size,
369 Qt::Orientation orientation = Qt::Vertical);
370 qreal rowSizeHint(Qt::SizeHint which, int row,
371 Qt::Orientation orientation = Qt::Vertical) const;
372
373 void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation);
374 Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const;
375
376 void setAlignment(QGraphicsLayoutItem *layoutItem, Qt::Alignment alignment);
377 Qt::Alignment alignment(QGraphicsLayoutItem *layoutItem) const;
378 Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const;
379
380
381 void addItem(QGridLayoutItem *item);
382 void removeItem(QGridLayoutItem *item);
383 QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const;
384 QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const;
385 inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical)
386 { insertOrRemoveRows(row, +1, orientation); }
387 inline void removeRow(int row, Qt::Orientation orientation = Qt::Vertical)
388 { insertOrRemoveRows(row, -1, orientation); }
389
390 void invalidate();
391 void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry);
392 QRectF cellRect(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry, int row,
393 int column, int rowSpan, int columnSpan) const;
394 QSizeF sizeHint(const QLayoutStyleInfo &styleInfo, Qt::SizeHint which,
395 const QSizeF &constraint) const;
396 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const;
397 void transpose();
398 void setVisualDirection(Qt::LayoutDirection direction);
399 Qt::LayoutDirection visualDirection() const;
400#ifdef QT_DEBUG
401 void dump(int indent = 0) const;
402#endif
403
404private:
405 static int grossRoundUp(int n) { return ((n + 2) | 0x3) - 2; }
406
407 void maybeExpandGrid(int row, int column, Qt::Orientation orientation = Qt::Vertical);
408 void regenerateGrid();
409 inline int internalGridRowCount() const { return grossRoundUp(rowCount()); }
410 inline int internalGridColumnCount() const { return grossRoundUp(columnCount()); }
411 void setItemAt(int row, int column, QGridLayoutItem *item);
412 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical);
413 void fillRowData(QGridLayoutRowData *rowData, const QLayoutStyleInfo &styleInfo,
414 Qt::Orientation orientation = Qt::Vertical) const;
415 void ensureEffectiveFirstAndLastRows() const;
416 void ensureColumnAndRowData(const QLayoutStyleInfo &styleInfo) const;
417 void ensureGeometries(const QLayoutStyleInfo &styleInfo, const QSizeF &size) const;
418
419 // User input
420 QVector<QGridLayoutItem *> q_grid;
421 QList<QGridLayoutItem *> q_items;
422 QLayoutParameter<qreal> q_defaultSpacings[NOrientations];
423 QGridLayoutRowInfo q_infos[NOrientations];
424 Qt::LayoutDirection m_visualDirection;
425
426 // Lazily computed from the above user input
427 mutable int q_cachedEffectiveFirstRows[NOrientations];
428 mutable int q_cachedEffectiveLastRows[NOrientations];
429
430 // Layout item input
431 mutable QLayoutStyleInfo q_cachedDataForStyleInfo;
432 mutable QGridLayoutRowData q_columnData;
433 mutable QGridLayoutRowData q_rowData;
434 mutable QGridLayoutBox q_totalBoxes[NOrientations];
435
436 // Output
437 mutable QSizeF q_cachedSize;
438 mutable QVector<qreal> q_xx;
439 mutable QVector<qreal> q_yy;
440 mutable QVector<qreal> q_widths;
441 mutable QVector<qreal> q_heights;
442 mutable QVector<qreal> q_descents;
443
444 friend class QGridLayoutItem;
445};
446
447QT_END_NAMESPACE
448
449#endif
Note: See TracBrowser for help on using the repository browser.