Qt 4.8
qgraphicsanchorlayout_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 QGRAPHICSANCHORLAYOUT_P_H
43 #define QGRAPHICSANCHORLAYOUT_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 purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include <QGraphicsWidget>
57 #include <private/qobject_p.h>
58 
59 #include "qgraphicslayout_p.h"
60 #include "qgraphicsanchorlayout.h"
61 #include "qgraph_p.h"
62 #include "qsimplex_p.h"
63 #ifndef QT_NO_GRAPHICSVIEW
65 
66 /*
67  The public QGraphicsAnchorLayout interface represents an anchorage point
68  as a pair of a <QGraphicsLayoutItem *> and a <Qt::AnchorPoint>.
69 
70  Internally though, it has a graph of anchorage points (vertices) and
71  anchors (edges), represented by the AnchorVertex and AnchorData structs
72  respectively.
73 */
74 
80 struct AnchorVertex {
81  enum Type {
82  Normal = 0,
84  };
85 
87  : m_item(item), m_edge(edge), m_type(Normal) {}
88 
90  : m_item(0), m_edge(Qt::AnchorPoint(0)), m_type(Normal) {}
91 
92 #ifdef QT_DEBUG
93  inline QString toString() const;
94 #endif
95 
98  uint m_type : 1;
99 
100  // Current distance from this vertex to the layout edge (Left or Top)
101  // Value is calculated from the current anchors sizes.
103 };
104 
113 struct AnchorData : public QSimplexVariable {
114  enum Type {
115  Normal = 0,
117  Parallel
118  };
119 
120  enum Dependency {
121  Independent = 0,
123  Slave
124  };
125 
127  : QSimplexVariable(), from(0), to(0),
128  minSize(0), prefSize(0), maxSize(0),
129  minPrefSize(0), maxPrefSize(0),
130  sizeAtMinimum(0), sizeAtPreferred(0),
131  sizeAtMaximum(0), item(0), graphicsAnchor(0),
132  type(Normal), isLayoutAnchor(false),
133  isCenterAnchor(false), orientation(0),
134  dependency(Independent) {}
135  virtual ~AnchorData();
136 
137  virtual void updateChildrenSizes() {}
138  void refreshSizeHints(const QLayoutStyleInfo *styleInfo = 0);
139 
140 #ifdef QT_DEBUG
141  void dump(int indent = 2);
142  inline QString toString() const;
144 #endif
145 
146  // Anchor is semantically directed
149 
150  // Nominal sizes
151  // These are the intrinsic size restrictions for a given item. They are
152  // used as input for the calculation of the actual sizes.
153  // These values are filled by the refreshSizeHints method, based on the
154  // anchor size policy, the size hints of the item it (possibly) represents
155  // and the layout spacing information.
159 
162 
163  // Calculated sizes
164  // These attributes define which sizes should that anchor be in when the
165  // layout is at its minimum, preferred or maximum sizes. Values are
166  // calculated by the Simplex solver based on the current layout setup.
170 
171  // References to the classes that represent this anchor in the public world
172  // An anchor may represent a LayoutItem, it may also be acessible externally
173  // through a GraphicsAnchor "handler".
176 
177  uint type : 2; // either Normal, Sequential or Parallel
178  uint isLayoutAnchor : 1; // if this anchor is an internal layout anchor
181  uint dependency : 2; // either Independent, Master or Slave
182 };
183 
184 #ifdef QT_DEBUG
186 {
187  return QString::fromAscii("Anchor(%1)").arg(name);
188 }
189 #endif
190 
192 {
194  : AnchorData(), m_children(vertices), m_edges(edges)
195  {
197  orientation = m_edges.at(0)->orientation;
198 #ifdef QT_DEBUG
199  name = QString::fromAscii("%1 -- %2").arg(vertices.first()->toString(), vertices.last()->toString());
200 #endif
201  }
202 
203  virtual void updateChildrenSizes();
204  void calculateSizeHints();
205 
206  QVector<AnchorVertex*> m_children; // list of vertices in the sequence
207  QVector<AnchorData*> m_edges; // keep the list of edges too.
208 };
209 
211 {
213  : AnchorData(), firstEdge(first), secondEdge(second)
214  {
216  orientation = first->orientation;
217 
218  // This assert whether the child anchors share their vertices
219  Q_ASSERT(((first->from == second->from) && (first->to == second->to)) ||
220  ((first->from == second->to) && (first->to == second->from)));
221 
222  // Our convention will be that the parallel group anchor will have the same
223  // direction as the first anchor.
224  from = first->from;
225  to = first->to;
226 #ifdef QT_DEBUG
227  name = QString::fromAscii("%1 | %2").arg(first->toString(), second->toString());
228 #endif
229  }
230 
231  virtual void updateChildrenSizes();
232  bool calculateSizeHints();
233 
234  bool secondForward() const {
235  // We have the convention that the first children will define the direction of the
236  // pararell group. Note that we can't rely on 'this->from' or 'this->to' because they
237  // might be changed by vertex simplification.
238  return firstEdge->from == secondEdge->from;
239  }
240 
243 
246 };
247 
250  : AnchorVertex(), m_first(v1), m_second(v2), m_removedAnchor(data) {
252  }
253 
256 
260 };
261 
262 #ifdef QT_DEBUG
264 {
265  if (!this) {
266  return QLatin1String("NULL");
267  } else if (m_type == Pair) {
268  const AnchorVertexPair *vp = static_cast<const AnchorVertexPair *>(this);
269  return QString::fromAscii("(%1, %2)").arg(vp->m_first->toString()).arg(vp->m_second->toString());
270  } else if (!m_item) {
271  return QString::fromAscii("NULL_%1").arg(quintptr(this));
272  }
273  QString edge;
274  switch (m_edge) {
275  case Qt::AnchorLeft:
276  edge = QLatin1String("Left");
277  break;
279  edge = QLatin1String("HorizontalCenter");
280  break;
281  case Qt::AnchorRight:
282  edge = QLatin1String("Right");
283  break;
284  case Qt::AnchorTop:
285  edge = QLatin1String("Top");
286  break;
288  edge = QLatin1String("VerticalCenter");
289  break;
290  case Qt::AnchorBottom:
291  edge = QLatin1String("Bottom");
292  break;
293  default:
294  edge = QLatin1String("None");
295  break;
296  }
297  QString itemName;
298  if (m_item->isLayout()) {
299  itemName = QLatin1String("layout");
300  } else {
301  if (QGraphicsItem *item = m_item->graphicsItem()) {
302  itemName = item->data(0).toString();
303  }
304  }
305  edge.insert(0, QLatin1String("%1_"));
306  return edge.arg(itemName);
307 }
308 #endif
309 
325 {
326 public:
328 
329  QSimplexConstraint *constraint(const GraphPath &path) const;
330 #ifdef QT_DEBUG
331  QString toString() const;
332 #endif
335 };
336 
342 {
344 
345 public:
346  explicit QGraphicsAnchorPrivate(int version = QObjectPrivateVersion);
348 
349  void setSpacing(qreal value);
350  void unsetSpacing();
351  qreal spacing() const;
352 
353  void setSizePolicy(QSizePolicy::Policy policy);
354 
357 
358  // Size information for user controlled anchor
361 
362  uint hasSize : 1; // if false, get size from style.
363 };
364 
365 
366 
367 
377 {
379 
380 public:
381  // When the layout geometry is different from its Minimum, Preferred
382  // or Maximum values, interpolation is used to calculate the geometries
383  // of the items.
384  //
385  // Interval represents which interpolation interval are we operating in.
386  enum Interval {
387  MinimumToMinPreferred = 0,
390  MaxPreferredToMaximum
391  };
392 
393  // Several structures internal to the layout are duplicated to handle
394  // both Horizontal and Vertical restrictions.
395  //
396  // Orientation is used to reference the right structure in each context
397  enum Orientation {
401  };
402 
404 
406  {
407  return q ? q->d_func() : 0;
408  }
409 
410  static Qt::AnchorPoint oppositeEdge(
411  Qt::AnchorPoint edge);
412 
413  static Orientation edgeOrientation(Qt::AnchorPoint edge);
414 
416  {
417  if (orientation == Vertical && int(edge) <= 2)
418  return (Qt::AnchorPoint)(edge + 3);
419  else if (orientation == Horizontal && int(edge) >= 3) {
420  return (Qt::AnchorPoint)(edge - 3);
421  }
422  return edge;
423  }
424 
425  // Init methods
426  void createLayoutEdges();
427  void deleteLayoutEdges();
428  void createItemEdges(QGraphicsLayoutItem *item);
429  void createCenterAnchors(QGraphicsLayoutItem *item, Qt::AnchorPoint centerEdge);
430  void removeCenterAnchors(QGraphicsLayoutItem *item, Qt::AnchorPoint centerEdge, bool substitute = true);
431  void removeCenterConstraints(QGraphicsLayoutItem *item, Orientation orientation);
432 
434  {
436  if (!data->graphicsAnchor) {
437  data->graphicsAnchor = new QGraphicsAnchor(q);
438  data->graphicsAnchor->d_func()->data = data;
439  }
440  return data->graphicsAnchor;
441  }
442 
443  // function used by the 4 API functions
444  QGraphicsAnchor *addAnchor(QGraphicsLayoutItem *firstItem,
445  Qt::AnchorPoint firstEdge,
446  QGraphicsLayoutItem *secondItem,
447  Qt::AnchorPoint secondEdge,
448  qreal *spacing = 0);
449 
450  // Helper for Anchor Manipulation methods
451  void addAnchor_helper(QGraphicsLayoutItem *firstItem,
452  Qt::AnchorPoint firstEdge,
453  QGraphicsLayoutItem *secondItem,
454  Qt::AnchorPoint secondEdge,
455  AnchorData *data);
456 
457  QGraphicsAnchor *getAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge,
458  QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge);
459 
460  void removeAnchor(AnchorVertex *firstVertex, AnchorVertex *secondVertex);
461  void removeAnchor_helper(AnchorVertex *v1, AnchorVertex *v2);
462 
463  void removeAnchors(QGraphicsLayoutItem *item);
464 
465  void removeVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge);
466 
467  void correctEdgeDirection(QGraphicsLayoutItem *&firstItem,
468  Qt::AnchorPoint &firstEdge,
469  QGraphicsLayoutItem *&secondItem,
470  Qt::AnchorPoint &secondEdge);
471 
472  QLayoutStyleInfo &styleInfo() const;
473 
474  AnchorData *addAnchorMaybeParallel(AnchorData *newAnchor, bool *feasible);
475 
476  // Activation
477  void calculateGraphs();
478  void calculateGraphs(Orientation orientation);
479 
480  // Simplification
481  bool simplifyGraph(Orientation orientation);
482  bool simplifyVertices(Orientation orientation);
483  bool simplifyGraphIteration(Orientation orientation, bool *feasible);
484 
485  bool replaceVertex(Orientation orientation, AnchorVertex *oldV,
486  AnchorVertex *newV, const QList<AnchorData *> &edges);
487 
488 
489  void restoreSimplifiedGraph(Orientation orientation);
490  void restoreSimplifiedAnchor(AnchorData *edge);
491  void restoreSimplifiedConstraints(ParallelAnchorData *parallel);
492  void restoreVertices(Orientation orientation);
493 
494  bool calculateTrunk(Orientation orientation, const GraphPath &trunkPath,
495  const QList<QSimplexConstraint *> &constraints,
496  const QList<AnchorData *> &variables);
497  bool calculateNonTrunk(const QList<QSimplexConstraint *> &constraints,
498  const QList<AnchorData *> &variables);
499 
500  // Support functions for calculateGraph()
501  void refreshAllSizeHints(Orientation orientation);
502  void findPaths(Orientation orientation);
503  void constraintsFromPaths(Orientation orientation);
504  void updateAnchorSizes(Orientation orientation);
505  QList<QSimplexConstraint *> constraintsFromSizeHints(const QList<AnchorData *> &anchors);
506  QList<QList<QSimplexConstraint *> > getGraphParts(Orientation orientation);
507  void identifyFloatItems(const QSet<AnchorData *> &visited, Orientation orientation);
508  void identifyNonFloatItems_helper(const AnchorData *ad, QSet<QGraphicsLayoutItem *> *nonFloatingItemsIdentifiedSoFar);
509 
511  {
512  return m_vertexList.value(itemEdge).first;
513  }
514 
516  {
517  return internalVertex(qMakePair(const_cast<QGraphicsLayoutItem *>(item), edge));
518  }
519 
520  inline void changeLayoutVertex(Orientation orientation, AnchorVertex *oldV, AnchorVertex *newV)
521  {
522  if (layoutFirstVertex[orientation] == oldV)
523  layoutFirstVertex[orientation] = newV;
524  else if (layoutCentralVertex[orientation] == oldV)
525  layoutCentralVertex[orientation] = newV;
526  else if (layoutLastVertex[orientation] == oldV)
527  layoutLastVertex[orientation] = newV;
528  }
529 
530 
531  AnchorVertex *addInternalVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge);
532  void removeInternalVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge);
533 
534  // Geometry interpolation methods
535  void setItemsGeometries(const QRectF &geom);
536 
537  void calculateVertexPositions(Orientation orientation);
538  void setupEdgesInterpolation(Orientation orientation);
539  void interpolateEdge(AnchorVertex *base, AnchorData *edge);
540 
541  // Linear Programming solver methods
542  bool solveMinMax(const QList<QSimplexConstraint *> &constraints,
543  GraphPath path, qreal *min, qreal *max);
544  bool solvePreferred(const QList<QSimplexConstraint *> &constraints,
545  const QList<AnchorData *> &variables);
546  bool hasConflicts() const;
547 
548 #ifdef QT_DEBUG
549  void dumpGraph(const QString &name = QString());
550 #endif
551 
552 
553  qreal spacings[NOrientations];
554  // Size hints from simplex engine
555  qreal sizeHints[2][3];
556 
557  // Items
559 
560  // Mapping between high level anchorage points (Item, Edge) to low level
561  // ones (Graph Vertices)
562 
564 
565  // Internal graph of anchorage points and anchors, for both orientations
567 
568  AnchorVertex *layoutFirstVertex[2];
569  AnchorVertex *layoutCentralVertex[2];
570  AnchorVertex *layoutLastVertex[2];
571 
572  // Combined anchors in order of creation
573  QList<AnchorVertexPair *> simplifiedVertices[2];
574  QList<AnchorData *> anchorsFromSimplifiedVertices[2];
575 
576  // Graph paths and constraints, for both orientations
579  QList<QSimplexConstraint *> itemCenterConstraints[2];
580 
581  // The interpolation interval and progress based on the current size
582  // as well as the key values (minimum, preferred and maximum)
583  Interval interpolationInterval[2];
584  qreal interpolationProgress[2];
585 
586  bool graphHasConflicts[2];
588 
589 #if defined(QT_DEBUG) || defined(Q_AUTOTEST_EXPORT)
590  bool lastCalculationUsedSimplex[2];
591 #endif
592 
594  mutable uint styleInfoDirty : 1;
596 
598 };
599 
601 #endif //QT_NO_GRAPHICSVIEW
602 
603 #endif
Represents an edge (anchor) in the internal graph.
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
QGraphicsAnchor * acquireGraphicsAnchor(AnchorData *data)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
QList< QSimplexConstraint * > m_firstConstraints
AnchorVertex * internalVertex(const QGraphicsLayoutItem *item, Qt::AnchorPoint edge) const
QGraphicsItem * graphicsItem() const
Returns the QGraphicsItem that this layout item represents.
QGraphicsLayoutItem * item
Qt::AnchorPoint m_edge
T & first()
Returns a reference to the first item in the vector.
Definition: qvector.h:260
The QGraphicsAnchor class represents an anchor between two items in a QGraphicsAnchorLayout.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
QHash< QPair< QGraphicsLayoutItem *, Qt::AnchorPoint >, QPair< AnchorVertex *, int > > m_vertexList
static Qt::AnchorPoint pickEdge(Qt::AnchorPoint edge, Orientation orientation)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QGraphicsAnchor * graphicsAnchor
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
Representation of a valid path for a given vertex in the graph.
QGraphicsAnchorLayoutPrivate * layoutPrivate
static const uint base
Definition: qurl.cpp:268
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
ParallelAnchorData(AnchorData *first, AnchorData *second)
#define Q_Q(Class)
Definition: qglobal.h:2483
QList< QSimplexConstraint * > m_secondConstraints
AnchorPoint
Definition: qnamespace.h:1586
QSet< AnchorData * > negatives
#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
QString toString() const
const char * name
QList< AnchorData * > m_secondAnchors
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
SequentialAnchorData(const QVector< AnchorVertex *> &vertices, const QVector< AnchorData *> &edges)
QGraphicsLayoutItem * m_item
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
QVector< QGraphicsLayoutItem * > items
QList< AnchorData * > m_firstAnchors
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
bool isLayout() const
Returns true if this QGraphicsLayoutItem is a layout (e.g., is inherited by an object that arranges o...
AnchorVertex * to
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
AnchorVertexPair(AnchorVertex *v1, AnchorVertex *v2, AnchorData *data)
AnchorVertex * from
QSet< AnchorData * > positives
QString toString() const
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
Definition: qpair.h:102
QVector< AnchorVertex * > m_children
T & last()
Returns a reference to the last item in the vector.
Definition: qvector.h:262
Definition: qnamespace.h:54
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
static QString dump(const QByteArray &)
void changeLayoutVertex(Orientation orientation, AnchorVertex *oldV, AnchorVertex *newV)
AnchorVertex(QGraphicsLayoutItem *item, Qt::AnchorPoint edge)
QSizePolicy::Policy sizePolicy
AnchorVertex * internalVertex(const QPair< QGraphicsLayoutItem *, Qt::AnchorPoint > &itemEdge) const
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
QGraphicsAnchorLayout private methods and attributes.
QVector< AnchorData * > m_edges
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
virtual void updateChildrenSizes()