Qt 4.8
qgridlayoutengine_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
66 
68 class QStyle;
69 class QWidget;
70 
71 // ### deal with Descent in a similar way
72 enum {
77 };
78 
79 // do not reorder
80 enum {
81  Hor,
82  Ver,
84 };
85 
86 // do not reorder
87 enum LayoutSide {
89  Top,
92 };
93 
94 enum {
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 
102 template <typename T>
104 {
105 public:
106  enum State { Default, User, Cached };
107 
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 
127 private:
128  mutable T q_value;
129  mutable State q_state;
130 };
131 
133 {
134 public:
136 
137 };
138 
140 {
141 public:
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.
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 
212 bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2);
213 inline bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2)
214  { return !operator==(box1, box2); }
215 
217 {
218 public:
219  inline QGridLayoutMultiCellData() : q_stretch(-1) {}
220 
223 };
224 
226 
227 class QGridLayoutRowInfo;
228 
230 {
231 public:
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_
250 };
251 
252 class QGridLayoutEngine;
253 
255 {
256 public:
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 
304 private:
305  QGridLayoutEngine *q_engine; // ### needed?
307  int q_firstRows[NOrientations];
308  int q_rowSpans[NOrientations];
309  int q_stretches[NOrientations];
310  Qt::Alignment q_alignment;
311 };
312 
314 {
315 public:
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;
329 };
330 
332 {
333 public:
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  int indexOf(QGraphicsLayoutItem *item) const;
345 
346  int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const;
347  int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const;
348 
349  void setSpacing(qreal spacing, Qt::Orientations orientations);
350  qreal spacing(const QLayoutStyleInfo &styleInfo, Qt::Orientation orientation) const;
351  // ### setSpacingAfterRow(), spacingAfterRow()
352  void setRowSpacing(int row, qreal spacing, Qt::Orientation orientation = Qt::Vertical);
353  qreal rowSpacing(int row, Qt::Orientation orientation = Qt::Vertical) const;
354 
355  void setRowStretchFactor(int row, int stretch, Qt::Orientation orientation = Qt::Vertical);
356  int rowStretchFactor(int row, Qt::Orientation orientation = Qt::Vertical) const;
357 
358  void setStretchFactor(QGraphicsLayoutItem *layoutItem, int stretch,
359  Qt::Orientation orientation);
360  int stretchFactor(QGraphicsLayoutItem *layoutItem, Qt::Orientation orientation) const;
361 
362  void setRowSizeHint(Qt::SizeHint which, int row, qreal size,
363  Qt::Orientation orientation = Qt::Vertical);
364  qreal rowSizeHint(Qt::SizeHint which, int row,
365  Qt::Orientation orientation = Qt::Vertical) const;
366 
367  void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation);
368  Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const;
369 
370  void setAlignment(QGraphicsLayoutItem *layoutItem, Qt::Alignment alignment);
371  Qt::Alignment alignment(QGraphicsLayoutItem *layoutItem) const;
372  Qt::Alignment effectiveAlignment(const QGridLayoutItem *layoutItem) const;
373 
374 
375  void insertItem(QGridLayoutItem *item, int index);
376  void addItem(QGridLayoutItem *item);
377  void removeItem(QGridLayoutItem *item);
378  QGridLayoutItem *findLayoutItem(QGraphicsLayoutItem *layoutItem) const;
379  QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const;
380  inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical)
381  { insertOrRemoveRows(row, +1, orientation); }
382  inline void removeRows(int row, int count, Qt::Orientation orientation)
383  { insertOrRemoveRows(row, -count, orientation); }
384 
385  void invalidate();
386  void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry);
387  QRectF cellRect(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry, int row,
388  int column, int rowSpan, int columnSpan) const;
389  QSizeF sizeHint(const QLayoutStyleInfo &styleInfo, Qt::SizeHint which,
390  const QSizeF &constraint) const;
391 
392  // heightForWidth / widthForHeight support
393  QSizeF dynamicallyConstrainedSizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
394  bool ensureDynamicConstraint() const;
395  bool hasDynamicConstraint() const;
396  Qt::Orientation constraintOrientation() const;
397 
398 
399  QSizePolicy::ControlTypes controlTypes(LayoutSide side) const;
400  void transpose();
401  void setVisualDirection(Qt::LayoutDirection direction);
402  Qt::LayoutDirection visualDirection() const;
403 #ifdef QT_DEBUG
404  void dump(int indent = 0) const;
405 #endif
406 
407 private:
408  static int grossRoundUp(int n) { return ((n + 2) | 0x3) - 2; }
409 
410  void maybeExpandGrid(int row, int column, Qt::Orientation orientation = Qt::Vertical);
411  void regenerateGrid();
412  inline int internalGridRowCount() const { return grossRoundUp(rowCount()); }
413  inline int internalGridColumnCount() const { return grossRoundUp(columnCount()); }
414  void setItemAt(int row, int column, QGridLayoutItem *item);
415  void insertOrRemoveRows(int row, int delta, Qt::Orientation orientation = Qt::Vertical);
416  void fillRowData(QGridLayoutRowData *rowData, const QLayoutStyleInfo &styleInfo,
417  qreal *colPositions, qreal *colSizes,
418  Qt::Orientation orientation = Qt::Vertical) const;
419  void ensureEffectiveFirstAndLastRows() const;
420  void ensureColumnAndRowData(QGridLayoutRowData *rowData, QGridLayoutBox *totalBox,
421  const QLayoutStyleInfo &styleInfo,
422  qreal *colPositions, qreal *colSizes,
423  Qt::Orientation orientation) const;
424 
425  void ensureGeometries(const QLayoutStyleInfo &styleInfo, const QSizeF &size) const;
426 
427  // User input
433 
434  // Lazily computed from the above user input
435  mutable int q_cachedEffectiveFirstRows[NOrientations];
436  mutable int q_cachedEffectiveLastRows[NOrientations];
438 
439  // Layout item input
443  mutable QGridLayoutBox q_totalBoxes[NOrientations];
444 
445  // Output
452 
453  friend class QGridLayoutItem;
454 };
455 
457 
458 #endif
QGridLayoutRowData q_columnData
QList< QGridLayoutItem * > q_items
void setCachedValue(T value) const
bool operator!=(const QGridLayoutBox &box1, const QGridLayoutBox &box2)
double qreal
Definition: qglobal.h:1193
QGridLayoutEngine * q_engine
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QLayoutParameter(T value, State state=Default)
#define add(aName)
QVector< qreal > q_descents
QMap< QPair< int, int >, QGridLayoutMultiCellData > MultiCellMap
Qt::Alignment q_alignment
Q_CORE_EXPORT QTextStream & reset(QTextStream &s)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
unsigned char quint8
Definition: qglobal.h:934
int internalGridColumnCount() const
QVector< qreal > q_yy
QVector< QGridLayoutItem * > q_grid
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
QVector< Qt::Alignment > alignments
QGridLayoutRowData q_rowData
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QVector< qreal > q_xx
QVector< QGridLayoutBox > boxes
QVector< QStretchParameter > stretches
void setUserValue(T value)
static const QCssKnownValue positions[NumKnownPositionModes - 1]
Definition: qcssparser.cpp:329
LayoutDirection
Definition: qnamespace.h:1580
bool operator==(const QGridLayoutBox &box1, const QGridLayoutBox &box2)
void removeRows(int row, int count, Qt::Orientation orientation)
QVector< int > stretches
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
The State element defines configurations of objects and properties.
QVector< QLayoutParameter< qreal > > spacings
QGraphicsLayoutItem * layoutItem() const
QVector< qreal > spacings
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
SizeHint
Definition: qnamespace.h:1708
void setAlignment(Qt::Alignment alignment)
qreal & q_sizes(int which)
const qreal & q_sizes(int which) const
QVector< qreal > q_heights
static int grossRoundUp(int n)
int internalGridRowCount() const
QVector< qreal > q_widths
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
static QString dump(const QByteArray &)
quint16 index
Qt::Alignment alignment() const
void insertRow(int row, Qt::Orientation orientation=Qt::Vertical)
Qt::LayoutDirection m_visualDirection
T value(T defaultValue) const
QVector< QGridLayoutBox > boxes
static const KeyPair *const end
Orientation
Definition: qnamespace.h:174
static void normalize(double &x, double &y)
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
QGraphicsLayoutItem * q_layoutItem
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
QLayoutStyleInfo q_cachedDataForStyleInfo
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
Qt::LayoutDirection direction