|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 |
|
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 QLayoutStyleInfo |
|
132 { |
|
133 public: |
|
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 |
|
148 private: |
|
149 bool q_valid; |
|
150 QStyle *q_style; |
|
151 QWidget *q_widget; |
|
152 }; |
|
153 |
|
154 class QGridLayoutBox |
|
155 { |
|
156 public: |
|
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 |
|
227 bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2); |
|
228 inline bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2) |
|
229 { return !operator==(box1, box2); } |
|
230 |
|
231 class QGridLayoutMultiCellData |
|
232 { |
|
233 public: |
|
234 inline QGridLayoutMultiCellData() : q_stretch(-1) {} |
|
235 |
|
236 QGridLayoutBox q_box; |
|
237 int q_stretch; |
|
238 }; |
|
239 |
|
240 typedef QMap<QPair<int, int>, QGridLayoutMultiCellData> MultiCellMap; |
|
241 |
|
242 class QGridLayoutRowData |
|
243 { |
|
244 public: |
|
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 |
|
264 class QGridLayoutEngine; |
|
265 |
|
266 class QGridLayoutItem |
|
267 { |
|
268 public: |
|
269 QGridLayoutItem(QGridLayoutEngine *engine, QGraphicsLayoutItem *layoutItem, int row, int column, |
|
270 int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0, |
|
271 int itemAtIndex = -1); |
|
272 |
|
273 inline int firstRow() const { return q_firstRows[Ver]; } |
|
274 inline int firstColumn() const { return q_firstRows[Hor]; } |
|
275 inline int rowSpan() const { return q_rowSpans[Ver]; } |
|
276 inline int columnSpan() const { return q_rowSpans[Hor]; } |
|
277 inline int lastRow() const { return firstRow() + rowSpan() - 1; } |
|
278 inline int lastColumn() const { return firstColumn() + columnSpan() - 1; } |
|
279 |
|
280 int firstRow(Qt::Orientation orientation) const; |
|
281 int firstColumn(Qt::Orientation orientation) const; |
|
282 int lastRow(Qt::Orientation orientation) const; |
|
283 int lastColumn(Qt::Orientation orientation) const; |
|
284 int rowSpan(Qt::Orientation orientation) const; |
|
285 int columnSpan(Qt::Orientation orientation) const; |
|
286 void setFirstRow(int row, Qt::Orientation orientation = Qt::Vertical); |
|
287 void setRowSpan(int rowSpan, Qt::Orientation orientation = Qt::Vertical); |
|
288 |
|
289 int stretchFactor(Qt::Orientation orientation) const; |
|
290 void setStretchFactor(int stretch, Qt::Orientation orientation); |
|
291 |
|
292 inline Qt::Alignment alignment() const { return q_alignment; } |
|
293 inline void setAlignment(Qt::Alignment alignment) { q_alignment = alignment; } |
|
294 |
|
295 QSizePolicy::Policy sizePolicy(Qt::Orientation orientation) const; |
|
296 QSizePolicy::ControlTypes controlTypes(LayoutSide side) const; |
|
297 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; |
|
298 QGridLayoutBox box(Qt::Orientation orientation, qreal constraint = -1.0) const; |
|
299 QRectF geometryWithin(qreal x, qreal y, qreal width, qreal height, qreal rowDescent) const; |
|
300 |
|
301 QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; } |
|
302 |
|
303 void setGeometry(const QRectF &rect); |
|
304 void transpose(); |
|
305 void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical); |
|
306 QSizeF effectiveMaxSize() const; |
|
307 |
|
308 #ifdef QT_DEBUG |
|
309 void dump(int indent = 0) const; |
|
310 #endif |
|
311 |
|
312 private: |
|
313 QGridLayoutEngine *q_engine; // ### needed? |
|
314 QGraphicsLayoutItem *q_layoutItem; |
|
315 int q_firstRows[NOrientations]; |
|
316 int q_rowSpans[NOrientations]; |
|
317 int q_stretches[NOrientations]; |
|
318 Qt::Alignment q_alignment; |
|
319 }; |
|
320 |
|
321 class QGridLayoutRowInfo |
|
322 { |
|
323 public: |
|
324 inline QGridLayoutRowInfo() : count(0) {} |
|
325 |
|
326 void insertOrRemoveRows(int row, int delta); |
|
327 |
|
328 #ifdef QT_DEBUG |
|
329 void dump(int indent = 0) const; |
|
330 #endif |
|
331 |
|
332 int count; |
|
333 QVector<QStretchParameter> stretches; |
|
334 QVector<QLayoutParameter<qreal> > spacings; |
|
335 QVector<Qt::Alignment> alignments; |
|
336 QVector<QGridLayoutBox> boxes; |
|
337 }; |
|
338 |
|
339 class QGridLayoutEngine |
|
340 { |
|
341 public: |
|
342 QGridLayoutEngine(); |
|
343 inline ~QGridLayoutEngine() { qDeleteAll(q_items); } |
|
344 |
|
345 int rowCount(Qt::Orientation orientation) const; |
|
346 int columnCount(Qt::Orientation orientation) const; |
|
347 inline int rowCount() const { return q_infos[Ver].count; } |
|
348 inline int columnCount() const { return q_infos[Hor].count; } |
|
349 // returns the number of items inserted, which may be less than (rowCount * columnCount) |
|
350 int itemCount() const; |
|
351 QGridLayoutItem *itemAt(int index) const; |
|
352 |
|
353 int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const; |
|
354 int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const; |
|
355 |
|
356 void setSpacing(qreal spacing, Qt::Orientations orientations); |
|
357 qreal spacing(const QLayoutStyleInfo &styleInfo, Qt::Orientation orientation) const; |
|
358 // ### setSpacingAfterRow(), spacingAfterRow() |
|
359 void setRowSpacing(int row, qreal spacing, Qt::Orientation orientation = Qt::Vertical); |
|
360 qreal rowSpacing(int row, Qt::Orientation orientation = Qt::Vertical) const; |
|
361 |
|
362 void setRowStretchFactor(int row, int stretch, Qt::Orientation orientation = Qt::Vertical); |
|
363 int rowStretchFactor(int row, Qt::Orientation orientation = Qt::Vertical) const; |
|
364 |
|
365 void setStretchFactor(QGraphicsLayoutItem *layoutItem, int stretch, |
|
366 Qt::Orientation orientation); |
|
367 int stretchFactor(QGraphicsLayoutItem *layoutItem, Qt::Orientation orientation) const; |
|
368 |
|
369 void setRowSizeHint(Qt::SizeHint which, int row, qreal size, |
|
370 Qt::Orientation orientation = Qt::Vertical); |
|
371 qreal rowSizeHint(Qt::SizeHint which, int row, |
|
372 Qt::Orientation orientation = Qt::Vertical) const; |
|
373 |
|
374 void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation); |
|
375 Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const; |
|
376 |
|
377 void setAlignment(QGraphicsLayoutItem *layoutItem, Qt::Alignment alignment); |
|
378 Qt::Alignment alignment(QGraphicsLayoutItem *layoutItem) const; |
|
379 Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const; |
|
380 |
|
381 |
|
382 void insertItem(QGridLayoutItem *item, int index); |
|
383 void addItem(QGridLayoutItem *item); |
|
384 void removeItem(QGridLayoutItem *item); |
|
385 QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const; |
|
386 QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const; |
|
387 inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical) |
|
388 { insertOrRemoveRows(row, +1, orientation); } |
|
389 inline void removeRow(int row, Qt::Orientation orientation = Qt::Vertical) |
|
390 { insertOrRemoveRows(row, -1, orientation); } |
|
391 |
|
392 void invalidate(); |
|
393 void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry); |
|
394 QRectF cellRect(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry, int row, |
|
395 int column, int rowSpan, int columnSpan) const; |
|
396 QSizeF sizeHint(const QLayoutStyleInfo &styleInfo, Qt::SizeHint which, |
|
397 const QSizeF &constraint) const; |
|
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 |
|
406 private: |
|
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 Qt::Orientation orientation = Qt::Vertical) const; |
|
417 void ensureEffectiveFirstAndLastRows() const; |
|
418 void ensureColumnAndRowData(const QLayoutStyleInfo &styleInfo) const; |
|
419 void ensureGeometries(const QLayoutStyleInfo &styleInfo, const QSizeF &size) const; |
|
420 |
|
421 // User input |
|
422 QVector<QGridLayoutItem *> q_grid; |
|
423 QList<QGridLayoutItem *> q_items; |
|
424 QLayoutParameter<qreal> q_defaultSpacings[NOrientations]; |
|
425 QGridLayoutRowInfo q_infos[NOrientations]; |
|
426 Qt::LayoutDirection m_visualDirection; |
|
427 |
|
428 // Lazily computed from the above user input |
|
429 mutable int q_cachedEffectiveFirstRows[NOrientations]; |
|
430 mutable int q_cachedEffectiveLastRows[NOrientations]; |
|
431 |
|
432 // Layout item input |
|
433 mutable QLayoutStyleInfo q_cachedDataForStyleInfo; |
|
434 mutable QGridLayoutRowData q_columnData; |
|
435 mutable QGridLayoutRowData q_rowData; |
|
436 mutable QGridLayoutBox q_totalBoxes[NOrientations]; |
|
437 |
|
438 // Output |
|
439 mutable QSizeF q_cachedSize; |
|
440 mutable QVector<qreal> q_xx; |
|
441 mutable QVector<qreal> q_yy; |
|
442 mutable QVector<qreal> q_widths; |
|
443 mutable QVector<qreal> q_heights; |
|
444 mutable QVector<qreal> q_descents; |
|
445 |
|
446 friend class QGridLayoutItem; |
|
447 }; |
|
448 |
|
449 QT_END_NAMESPACE |
|
450 |
|
451 #endif |