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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 15.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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#include "qgraphicslayout_p.h"
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
94enum {
95 NoConstraint,
96 HorizontalConstraint, // Width depends on the height
97 VerticalConstraint, // Height depends on the width
98 UnknownConstraint, // need to update cache
99 UnfeasibleConstraint // not feasible, it be has some items with Vertical and others with Horizontal constraints
100};
101
102template <typename T>
103class QLayoutParameter
104{
105public:
106 enum State { Default, User, Cached };
107
108 inline QLayoutParameter() : q_value(T()), q_state(Default) {}
109 inline QLayoutParameter(T value, State state = Default) : q_value(value), q_state(state) {}
110
111 inline void setUserValue(T value) {
112 q_value = value;
113 q_state = User;
114 }
115 inline void setCachedValue(T value) const {
116 if (q_state != User) {
117 q_value = value;
118 q_state = Cached;
119 }
120 }
121 inline T value() const { return q_value; }
122 inline T value(T defaultValue) const { return isUser() ? q_value : defaultValue; }
123 inline bool isDefault() const { return q_state == Default; }
124 inline bool isUser() const { return q_state == User; }
125 inline bool isCached() const { return q_state == Cached; }
126
127private:
128 mutable T q_value;
129 mutable State q_state;
130};
131
132class QStretchParameter : public QLayoutParameter<int>
133{
134public:
135 QStretchParameter() : QLayoutParameter<int>(-1) {}
136
137};
138
139class QGridLayoutBox
140{
141public:
142 inline QGridLayoutBox()
143 : q_minimumSize(0), q_preferredSize(0), q_maximumSize(FLT_MAX),
144 q_minimumDescent(-1), q_minimumAscent(-1) {}
145
146 void add(const QGridLayoutBox &other, int stretch, qreal spacing);
147 void combine(const QGridLayoutBox &other);
148 void normalize();
149
150#ifdef QT_DEBUG
151 void dump(int indent = 0) const;
152#endif
153 // This code could use the union-struct-array trick, but a compiler
154 // bug prevents this from working.
155 qreal q_minimumSize;
156 qreal q_preferredSize;
157 qreal q_maximumSize;
158 qreal q_minimumDescent;
159 qreal q_minimumAscent;
160 inline qreal &q_sizes(int which)
161 {
162 qreal *t;
163 switch (which) {
164 case Qt::MinimumSize:
165 t = &q_minimumSize;
166 break;
167 case Qt::PreferredSize:
168 t = &q_preferredSize;
169 break;
170 case Qt::MaximumSize:
171 t = &q_maximumSize;
172 break;
173 case Qt::MinimumDescent:
174 t = &q_minimumDescent;
175 break;
176 case (Qt::MinimumDescent + 1):
177 t = &q_minimumAscent;
178 break;
179 default:
180 t = 0;
181 break;
182 }
183 return *t;
184 }
185 inline const qreal &q_sizes(int which) const
186 {
187 const qreal *t;
188 switch (which) {
189 case Qt::MinimumSize:
190 t = &q_minimumSize;
191 break;
192 case Qt::PreferredSize:
193 t = &q_preferredSize;
194 break;
195 case Qt::MaximumSize:
196 t = &q_maximumSize;
197 break;
198 case Qt::MinimumDescent:
199 t = &q_minimumDescent;
200 break;
201 case (Qt::MinimumDescent + 1):
202 t = &q_minimumAscent;
203 break;
204 default:
205 t = 0;
206 break;
207 }
208 return *t;
209 }
210};
211
212bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2);
213inline bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2)
214 { return !operator==(box1, box2); }
215
216class QGridLayoutMultiCellData
217{
218public:
219 inline QGridLayoutMultiCellData() : q_stretch(-1) {}
220
221 QGridLayoutBox q_box;
222 int q_stretch;
223};
224
225typedef QMap<QPair<int, int>, QGridLayoutMultiCellData> MultiCellMap;
226
227class QGridLayoutRowInfo;
228
229class QGridLayoutRowData
230{
231public:
232 void reset(int count);
233 void distributeMultiCells(const QGridLayoutRowInfo &rowInfo);
234 void calculateGeometries(int start, int end, qreal targetSize, qreal *positions, qreal *sizes,
235 qreal *descents, const QGridLayoutBox &totalBox,
236 const QGridLayoutRowInfo &rowInfo);
237 QGridLayoutBox totalBox(int start, int end) const;
238 void stealBox(int start, int end, int which, qreal *positions, qreal *sizes);
239
240#ifdef QT_DEBUG
241 void dump(int indent = 0) const;
242#endif
243
244 QBitArray ignore; // ### rename q_
245 QVector<QGridLayoutBox> boxes;
246 MultiCellMap multiCellMap;
247 QVector<int> stretches;
248 QVector<qreal> spacings;
249 bool hasIgnoreFlag;
250};
251
252class QGridLayoutEngine;
253
254class QGridLayoutItem
255{
256public:
257 QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem, int row, int column,
258 int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0,
259 int itemAtIndex = -1);
260
261 inline int firstRow() const { return q_firstRows[Ver]; }
262 inline int firstColumn() const { return q_firstRows[Hor]; }
263 inline int rowSpan() const { return q_rowSpans[Ver]; }
264 inline int columnSpan() const { return q_rowSpans[Hor]; }
265 inline int lastRow() const { return firstRow() + rowSpan() - 1; }
266 inline int lastColumn() const { return firstColumn() + columnSpan() - 1; }
267
268 int firstRow(Qt::Orientation orientation) const;
269 int firstColumn(Qt::Orientation orientation) const;
270 int lastRow(Qt::Orientation orientation) const;
271 int lastColumn(Qt::Orientation orientation) const;
272 int rowSpan(Qt::Orientation orientation) const;
273 int columnSpan(Qt::Orientation orientation) const;
274 void setFirstRow(int row, Qt::Orientation orientation = Qt::Vertical);
275 void setRowSpan(int rowSpan, Qt::Orientation orientation = Qt::Vertical);
276
277 int stretchFactor(Qt::Orientation orientation) const;
278 void setStretchFactor(int stretch, Qt::Orientation orientation);
279
280 inline Qt::Alignment alignment() const { return q_alignment; }
281 inline void setAlignment(Qt::Alignment alignment) { q_alignment = alignment; }
282
283 QSizePolicy::Policy sizePolicy(Qt::Orientation orientation) const;
284
285 bool hasDynamicConstraint() const;
286 Qt::Orientation dynamicConstraintOrientation() const;
287
288 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const;
289 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
290 QGridLayoutBox box(Qt::Orientation orientation, qreal constraint = -1.0) const;
291 QRectF geometryWithin(qreal x, qreal y, qreal width, qreal height, qreal rowDescent) const;
292
293 QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; }
294
295 void setGeometry(const QRectF &rect);
296 void transpose();
297 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical);
298 QSizeF effectiveMaxSize(const QSizeF &constraint) const;
299
300#ifdef QT_DEBUG
301 void dump(int indent = 0) const;
302#endif
303
304private:
305 QGridLayoutEngine *q_engine; // ### needed?
306 QGraphicsLayoutItem *q_layoutItem;
307 int q_firstRows[NOrientations];
308 int q_rowSpans[NOrientations];
309 int q_stretches[NOrientations];
310 Qt::Alignment q_alignment;
311};
312
313class QGridLayoutRowInfo
314{
315public:
316 inline QGridLayoutRowInfo() : count(0) {}
317
318 void insertOrRemoveRows(int row, int delta);
319
320#ifdef QT_DEBUG
321 void dump(int indent = 0) const;
322#endif
323
324 int count;
325 QVector<QStretchParameter> stretches;
326 QVector<QLayoutParameter<qreal> > spacings;
327 QVector<Qt::Alignment> alignments;
328 QVector<QGridLayoutBox> boxes;
329};
330
331class QGridLayoutEngine
332{
333public:
334 QGridLayoutEngine();
335 inline ~QGridLayoutEngine() { qDeleteAll(q_items); }
336
337 int rowCount(Qt::Orientation orientation) const;
338 int columnCount(Qt::Orientation orientation) const;
339 inline int rowCount() const { return q_infos[Ver].count; }
340 inline int columnCount() const { return q_infos[Hor].count; }
341 // returns the number of items inserted, which may be less than (rowCount * columnCount)
342 int itemCount() const;
343 QGridLayoutItem *itemAt(int index) const;
344
345 int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const;
346 int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const;
347
348 void setSpacing(qreal spacing, Qt::Orientations orientations);
349 qreal spacing(const QLayoutStyleInfo &styleInfo, Qt::Orientation orientation) const;
350 // ### setSpacingAfterRow(), spacingAfterRow()
351 void setRowSpacing(int row, qreal spacing, Qt::Orientation orientation = Qt::Vertical);
352 qreal rowSpacing(int row, Qt::Orientation orientation = Qt::Vertical) const;
353
354 void setRowStretchFactor(int row, int stretch, Qt::Orientation orientation = Qt::Vertical);
355 int rowStretchFactor(int row, Qt::Orientation orientation = Qt::Vertical) const;
356
357 void setStretchFactor(QGraphicsLayoutItem *layoutItem, int stretch,
358 Qt::Orientation orientation);
359 int stretchFactor(QGraphicsLayoutItem *layoutItem, Qt::Orientation orientation) const;
360
361 void setRowSizeHint(Qt::SizeHint which, int row, qreal size,
362 Qt::Orientation orientation = Qt::Vertical);
363 qreal rowSizeHint(Qt::SizeHint which, int row,
364 Qt::Orientation orientation = Qt::Vertical) const;
365
366 void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation);
367 Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const;
368
369 void setAlignment(QGraphicsLayoutItem *layoutItem, Qt::Alignment alignment);
370 Qt::Alignment alignment(QGraphicsLayoutItem *layoutItem) const;
371 Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const;
372
373
374 void insertItem(QGridLayoutItem *item, int index);
375 void addItem(QGridLayoutItem *item);
376 void removeItem(QGridLayoutItem *item);
377 QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const;
378 QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const;
379 inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical)
380 { insertOrRemoveRows(row, +1, orientation); }
381 inline void removeRows(int row, int count, Qt::Orientation orientation)
382 { insertOrRemoveRows(row, -count, orientation); }
383
384 void invalidate();
385 void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry);
386 QRectF cellRect(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry, int row,
387 int column, int rowSpan, int columnSpan) const;
388 QSizeF sizeHint(const QLayoutStyleInfo &styleInfo, Qt::SizeHint which,
389 const QSizeF &constraint) const;
390
391 // heightForWidth / widthForHeight support
392 QSizeF dynamicallyConstrainedSizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
393 bool ensureDynamicConstraint() const;
394 bool hasDynamicConstraint() const;
395 Qt::Orientation constraintOrientation() const;
396
397
398 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const;
399 void transpose();
400 void setVisualDirection(Qt::LayoutDirection direction);
401 Qt::LayoutDirection visualDirection() const;
402#ifdef QT_DEBUG
403 void dump(int indent = 0) const;
404#endif
405
406private:
407 static int grossRoundUp(int n) { return ((n + 2) | 0x3) - 2; }
408
409 void maybeExpandGrid(int row, int column, Qt::Orientation orientation = Qt::Vertical);
410 void regenerateGrid();
411 inline int internalGridRowCount() const { return grossRoundUp(rowCount()); }
412 inline int internalGridColumnCount() const { return grossRoundUp(columnCount()); }
413 void setItemAt(int row, int column, QGridLayoutItem *item);
414 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical);
415 void fillRowData(QGridLayoutRowData *rowData, const QLayoutStyleInfo &styleInfo,
416 qreal *colPositions, qreal *colSizes,
417 Qt::Orientation orientation = Qt::Vertical) const;
418 void ensureEffectiveFirstAndLastRows() const;
419 void ensureColumnAndRowData(QGridLayoutRowData *rowData, QGridLayoutBox *totalBox,
420 const QLayoutStyleInfo &styleInfo,
421 qreal *colPositions, qreal *colSizes,
422 Qt::Orientation orientation) const;
423
424 void ensureGeometries(const QLayoutStyleInfo &styleInfo, const QSizeF &size) const;
425
426 // User input
427 QVector<QGridLayoutItem *> q_grid;
428 QList<QGridLayoutItem *> q_items;
429 QLayoutParameter<qreal> q_defaultSpacings[NOrientations];
430 QGridLayoutRowInfo q_infos[NOrientations];
431 Qt::LayoutDirection m_visualDirection;
432
433 // Lazily computed from the above user input
434 mutable int q_cachedEffectiveFirstRows[NOrientations];
435 mutable int q_cachedEffectiveLastRows[NOrientations];
436 mutable quint8 q_cachedConstraintOrientation : 3;
437
438 // Layout item input
439 mutable QLayoutStyleInfo q_cachedDataForStyleInfo;
440 mutable QGridLayoutRowData q_columnData;
441 mutable QGridLayoutRowData q_rowData;
442 mutable QGridLayoutBox q_totalBoxes[NOrientations];
443
444 // Output
445 mutable QSizeF q_cachedSize;
446 mutable QVector<qreal> q_xx;
447 mutable QVector<qreal> q_yy;
448 mutable QVector<qreal> q_widths;
449 mutable QVector<qreal> q_heights;
450 mutable QVector<qreal> q_descents;
451
452 friend class QGridLayoutItem;
453};
454
455QT_END_NAMESPACE
456
457#endif
Note: See TracBrowser for help on using the repository browser.