|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 |
|
65 QT_BEGIN_NAMESPACE |
|
66 |
|
67 class QGraphicsLayoutItem; |
|
68 class QStyle; |
|
69 class QWidget; |
|
70 |
|
71 // ### deal with Descent in a similar way |
|
72 enum { |
|
73 MinimumSize = Qt::MinimumSize, |
|
74 PreferredSize = Qt::PreferredSize, |
|
75 MaximumSize = Qt::MaximumSize, |
|
76 NSizes |
|
77 }; |
|
78 |
|
79 // do not reorder |
|
80 enum { |
|
81 Hor, |
|
82 Ver, |
|
83 NOrientations |
|
84 }; |
|
85 |
|
86 // do not reorder |
|
87 enum LayoutSide { |
|
88 Left, |
|
89 Top, |
|
90 Right, |
|
91 Bottom |
|
92 }; |
|
93 |
|
94 template <typename T> |
|
95 class QLayoutParameter |
|
96 { |
|
97 public: |
|
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 |
|
119 private: |
|
120 mutable T q_value; |
|
121 mutable State q_state; |
|
122 }; |
|
123 |
|
124 class QStretchParameter : public QLayoutParameter<int> |
|
125 { |
|
126 public: |
|
127 QStretchParameter() : QLayoutParameter<int>(-1) {} |
|
128 |
|
129 }; |
|
130 |
|
131 class QGridLayoutBox |
|
132 { |
|
133 public: |
|
134 inline QGridLayoutBox() |
|
135 : q_minimumSize(0), q_preferredSize(0), q_maximumSize(FLT_MAX), |
|
136 q_minimumDescent(-1), q_minimumAscent(-1) {} |
|
137 |
|
138 void add(const QGridLayoutBox &other, int stretch, qreal spacing); |
|
139 void combine(const QGridLayoutBox &other); |
|
140 void normalize(); |
|
141 |
|
142 #ifdef QT_DEBUG |
|
143 void dump(int indent = 0) const; |
|
144 #endif |
|
145 // This code could use the union-struct-array trick, but a compiler |
|
146 // bug prevents this from working. |
|
147 qreal q_minimumSize; |
|
148 qreal q_preferredSize; |
|
149 qreal q_maximumSize; |
|
150 qreal q_minimumDescent; |
|
151 qreal q_minimumAscent; |
|
152 inline qreal &q_sizes(int which) |
|
153 { |
|
154 qreal *t; |
|
155 switch (which) { |
|
156 case Qt::MinimumSize: |
|
157 t = &q_minimumSize; |
|
158 break; |
|
159 case Qt::PreferredSize: |
|
160 t = &q_preferredSize; |
|
161 break; |
|
162 case Qt::MaximumSize: |
|
163 t = &q_maximumSize; |
|
164 break; |
|
165 case Qt::MinimumDescent: |
|
166 t = &q_minimumDescent; |
|
167 break; |
|
168 case (Qt::MinimumDescent + 1): |
|
169 t = &q_minimumAscent; |
|
170 break; |
|
171 default: |
|
172 t = 0; |
|
173 break; |
|
174 } |
|
175 return *t; |
|
176 } |
|
177 inline const qreal &q_sizes(int which) const |
|
178 { |
|
179 const qreal *t; |
|
180 switch (which) { |
|
181 case Qt::MinimumSize: |
|
182 t = &q_minimumSize; |
|
183 break; |
|
184 case Qt::PreferredSize: |
|
185 t = &q_preferredSize; |
|
186 break; |
|
187 case Qt::MaximumSize: |
|
188 t = &q_maximumSize; |
|
189 break; |
|
190 case Qt::MinimumDescent: |
|
191 t = &q_minimumDescent; |
|
192 break; |
|
193 case (Qt::MinimumDescent + 1): |
|
194 t = &q_minimumAscent; |
|
195 break; |
|
196 default: |
|
197 t = 0; |
|
198 break; |
|
199 } |
|
200 return *t; |
|
201 } |
|
202 }; |
|
203 |
|
204 bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2); |
|
205 inline bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2) |
|
206 { return !operator==(box1, box2); } |
|
207 |
|
208 class QGridLayoutMultiCellData |
|
209 { |
|
210 public: |
|
211 inline QGridLayoutMultiCellData() : q_stretch(-1) {} |
|
212 |
|
213 QGridLayoutBox q_box; |
|
214 int q_stretch; |
|
215 }; |
|
216 |
|
217 typedef QMap<QPair<int, int>, QGridLayoutMultiCellData> MultiCellMap; |
|
218 |
|
219 class QGridLayoutRowData |
|
220 { |
|
221 public: |
|
222 void reset(int count); |
|
223 void distributeMultiCells(); |
|
224 void calculateGeometries(int start, int end, qreal targetSize, qreal *positions, qreal *sizes, |
|
225 qreal *descents, const QGridLayoutBox &totalBox); |
|
226 QGridLayoutBox totalBox(int start, int end) const; |
|
227 void stealBox(int start, int end, int which, qreal *positions, qreal *sizes); |
|
228 |
|
229 #ifdef QT_DEBUG |
|
230 void dump(int indent = 0) const; |
|
231 #endif |
|
232 |
|
233 QBitArray ignore; // ### rename q_ |
|
234 QVector<QGridLayoutBox> boxes; |
|
235 MultiCellMap multiCellMap; |
|
236 QVector<int> stretches; |
|
237 QVector<qreal> spacings; |
|
238 bool hasIgnoreFlag; |
|
239 }; |
|
240 |
|
241 class QGridLayoutEngine; |
|
242 |
|
243 class QGridLayoutItem |
|
244 { |
|
245 public: |
|
246 QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem, int row, int column, |
|
247 int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0, |
|
248 int itemAtIndex = -1); |
|
249 |
|
250 inline int firstRow() const { return q_firstRows[Ver]; } |
|
251 inline int firstColumn() const { return q_firstRows[Hor]; } |
|
252 inline int rowSpan() const { return q_rowSpans[Ver]; } |
|
253 inline int columnSpan() const { return q_rowSpans[Hor]; } |
|
254 inline int lastRow() const { return firstRow() + rowSpan() - 1; } |
|
255 inline int lastColumn() const { return firstColumn() + columnSpan() - 1; } |
|
256 |
|
257 int firstRow(Qt::Orientation orientation) const; |
|
258 int firstColumn(Qt::Orientation orientation) const; |
|
259 int lastRow(Qt::Orientation orientation) const; |
|
260 int lastColumn(Qt::Orientation orientation) const; |
|
261 int rowSpan(Qt::Orientation orientation) const; |
|
262 int columnSpan(Qt::Orientation orientation) const; |
|
263 void setFirstRow(int row, Qt::Orientation orientation = Qt::Vertical); |
|
264 void setRowSpan(int rowSpan, Qt::Orientation orientation = Qt::Vertical); |
|
265 |
|
266 int stretchFactor(Qt::Orientation orientation) const; |
|
267 void setStretchFactor(int stretch, Qt::Orientation orientation); |
|
268 |
|
269 inline Qt::Alignment alignment() const { return q_alignment; } |
|
270 inline void setAlignment(Qt::Alignment alignment) { q_alignment = alignment; } |
|
271 |
|
272 QSizePolicy::Policy sizePolicy(Qt::Orientation orientation) const; |
|
273 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const; |
|
274 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; |
|
275 QGridLayoutBox box(Qt::Orientation orientation, qreal constraint = -1.0) const; |
|
276 QRectF geometryWithin(qreal x, qreal y, qreal width, qreal height, qreal rowDescent) const; |
|
277 |
|
278 QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; } |
|
279 |
|
280 void setGeometry(const QRectF &rect); |
|
281 void transpose(); |
|
282 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical); |
|
283 QSizeF effectiveMaxSize() const; |
|
284 |
|
285 #ifdef QT_DEBUG |
|
286 void dump(int indent = 0) const; |
|
287 #endif |
|
288 |
|
289 private: |
|
290 QGridLayoutEngine *q_engine; // ### needed? |
|
291 QGraphicsLayoutItem *q_layoutItem; |
|
292 int q_firstRows[NOrientations]; |
|
293 int q_rowSpans[NOrientations]; |
|
294 int q_stretches[NOrientations]; |
|
295 Qt::Alignment q_alignment; |
|
296 }; |
|
297 |
|
298 class QGridLayoutRowInfo |
|
299 { |
|
300 public: |
|
301 inline QGridLayoutRowInfo() : count(0) {} |
|
302 |
|
303 void insertOrRemoveRows(int row, int delta); |
|
304 |
|
305 #ifdef QT_DEBUG |
|
306 void dump(int indent = 0) const; |
|
307 #endif |
|
308 |
|
309 int count; |
|
310 QVector<QStretchParameter> stretches; |
|
311 QVector<QLayoutParameter<qreal> > spacings; |
|
312 QVector<Qt::Alignment> alignments; |
|
313 QVector<QGridLayoutBox> boxes; |
|
314 }; |
|
315 |
|
316 class QGridLayoutEngine |
|
317 { |
|
318 public: |
|
319 QGridLayoutEngine(); |
|
320 inline ~QGridLayoutEngine() { qDeleteAll(q_items); } |
|
321 |
|
322 int rowCount(Qt::Orientation orientation) const; |
|
323 int columnCount(Qt::Orientation orientation) const; |
|
324 inline int rowCount() const { return q_infos[Ver].count; } |
|
325 inline int columnCount() const { return q_infos[Hor].count; } |
|
326 // returns the number of items inserted, which may be less than (rowCount * columnCount) |
|
327 int itemCount() const; |
|
328 QGridLayoutItem *itemAt(int index) const; |
|
329 |
|
330 int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const; |
|
331 int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const; |
|
332 |
|
333 void setSpacing(qreal spacing, Qt::Orientations orientations); |
|
334 qreal spacing(const QLayoutStyleInfo &styleInfo, Qt::Orientation orientation) const; |
|
335 // ### setSpacingAfterRow(), spacingAfterRow() |
|
336 void setRowSpacing(int row, qreal spacing, Qt::Orientation orientation = Qt::Vertical); |
|
337 qreal rowSpacing(int row, Qt::Orientation orientation = Qt::Vertical) const; |
|
338 |
|
339 void setRowStretchFactor(int row, int stretch, Qt::Orientation orientation = Qt::Vertical); |
|
340 int rowStretchFactor(int row, Qt::Orientation orientation = Qt::Vertical) const; |
|
341 |
|
342 void setStretchFactor(QGraphicsLayoutItem *layoutItem, int stretch, |
|
343 Qt::Orientation orientation); |
|
344 int stretchFactor(QGraphicsLayoutItem *layoutItem, Qt::Orientation orientation) const; |
|
345 |
|
346 void setRowSizeHint(Qt::SizeHint which, int row, qreal size, |
|
347 Qt::Orientation orientation = Qt::Vertical); |
|
348 qreal rowSizeHint(Qt::SizeHint which, int row, |
|
349 Qt::Orientation orientation = Qt::Vertical) const; |
|
350 |
|
351 void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation); |
|
352 Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const; |
|
353 |
|
354 void setAlignment(QGraphicsLayoutItem *layoutItem, Qt::Alignment alignment); |
|
355 Qt::Alignment alignment(QGraphicsLayoutItem *layoutItem) const; |
|
356 Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const; |
|
357 |
|
358 |
|
359 void insertItem(QGridLayoutItem *item, int index); |
|
360 void addItem(QGridLayoutItem *item); |
|
361 void removeItem(QGridLayoutItem *item); |
|
362 QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const; |
|
363 QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const; |
|
364 inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical) |
|
365 { insertOrRemoveRows(row, +1, orientation); } |
|
366 inline void removeRow(int row, Qt::Orientation orientation = Qt::Vertical) |
|
367 { insertOrRemoveRows(row, -1, orientation); } |
|
368 |
|
369 void invalidate(); |
|
370 void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry); |
|
371 QRectF cellRect(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry, int row, |
|
372 int column, int rowSpan, int columnSpan) const; |
|
373 QSizeF sizeHint(const QLayoutStyleInfo &styleInfo, Qt::SizeHint which, |
|
374 const QSizeF &constraint) const; |
|
375 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const; |
|
376 void transpose(); |
|
377 void setVisualDirection(Qt::LayoutDirection direction); |
|
378 Qt::LayoutDirection visualDirection() const; |
|
379 #ifdef QT_DEBUG |
|
380 void dump(int indent = 0) const; |
|
381 #endif |
|
382 |
|
383 private: |
|
384 static int grossRoundUp(int n) { return ((n + 2) | 0x3) - 2; } |
|
385 |
|
386 void maybeExpandGrid(int row, int column, Qt::Orientation orientation = Qt::Vertical); |
|
387 void regenerateGrid(); |
|
388 inline int internalGridRowCount() const { return grossRoundUp(rowCount()); } |
|
389 inline int internalGridColumnCount() const { return grossRoundUp(columnCount()); } |
|
390 void setItemAt(int row, int column, QGridLayoutItem *item); |
|
391 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical); |
|
392 void fillRowData(QGridLayoutRowData *rowData, const QLayoutStyleInfo &styleInfo, |
|
393 Qt::Orientation orientation = Qt::Vertical) const; |
|
394 void ensureEffectiveFirstAndLastRows() const; |
|
395 void ensureColumnAndRowData(const QLayoutStyleInfo &styleInfo) const; |
|
396 void ensureGeometries(const QLayoutStyleInfo &styleInfo, const QSizeF &size) const; |
|
397 |
|
398 // User input |
|
399 QVector<QGridLayoutItem *> q_grid; |
|
400 QList<QGridLayoutItem *> q_items; |
|
401 QLayoutParameter<qreal> q_defaultSpacings[NOrientations]; |
|
402 QGridLayoutRowInfo q_infos[NOrientations]; |
|
403 Qt::LayoutDirection m_visualDirection; |
|
404 |
|
405 // Lazily computed from the above user input |
|
406 mutable int q_cachedEffectiveFirstRows[NOrientations]; |
|
407 mutable int q_cachedEffectiveLastRows[NOrientations]; |
|
408 |
|
409 // Layout item input |
|
410 mutable QLayoutStyleInfo q_cachedDataForStyleInfo; |
|
411 mutable QGridLayoutRowData q_columnData; |
|
412 mutable QGridLayoutRowData q_rowData; |
|
413 mutable QGridLayoutBox q_totalBoxes[NOrientations]; |
|
414 |
|
415 // Output |
|
416 mutable QSizeF q_cachedSize; |
|
417 mutable QVector<qreal> q_xx; |
|
418 mutable QVector<qreal> q_yy; |
|
419 mutable QVector<qreal> q_widths; |
|
420 mutable QVector<qreal> q_heights; |
|
421 mutable QVector<qreal> q_descents; |
|
422 |
|
423 friend class QGridLayoutItem; |
|
424 }; |
|
425 |
|
426 QT_END_NAMESPACE |
|
427 |
|
428 #endif |