Qt 4.8
qgraphicsanchorlayout.cpp
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 
144 #include "qgraphicsanchorlayout_p.h"
145 #ifndef QT_NO_GRAPHICSVIEW
147 
150 {
152  Q_ASSERT(parentLayout);
153  d->layoutPrivate = parentLayout->d_func();
154 }
155 
160 {
161 }
162 
182 {
184  d->setSizePolicy(policy);
185 }
186 
188 {
189  Q_D(const QGraphicsAnchor);
190  return d->sizePolicy;
191 }
192 
206 {
208  d->setSpacing(spacing);
209 }
210 
212 {
213  Q_D(const QGraphicsAnchor);
214  return d->spacing();
215 }
216 
218 {
220  d->unsetSpacing();
221 }
222 
229 {
231  d->createLayoutEdges();
232 }
233 
238 {
240 
241  for (int i = count() - 1; i >= 0; --i) {
242  QGraphicsLayoutItem *item = d->items.at(i);
243  removeAt(i);
244  if (item) {
245  if (item->ownedByLayout())
246  delete item;
247  }
248  }
249 
250  d->removeCenterConstraints(this, QGraphicsAnchorLayoutPrivate::Horizontal);
251  d->removeCenterConstraints(this, QGraphicsAnchorLayoutPrivate::Vertical);
252  d->deleteLayoutEdges();
253 
254  Q_ASSERT(d->itemCenterConstraints[0].isEmpty());
255  Q_ASSERT(d->itemCenterConstraints[1].isEmpty());
256  Q_ASSERT(d->items.isEmpty());
257  Q_ASSERT(d->m_vertexList.isEmpty());
258 }
259 
284  QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
285 {
287  QGraphicsAnchor *a = d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);
288  invalidate();
289  return a;
290 }
291 
298  QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
299 {
301  return d->getAnchor(firstItem, firstEdge, secondItem, secondEdge);
302 }
303 
327  Qt::Corner firstCorner,
328  QGraphicsLayoutItem *secondItem,
329  Qt::Corner secondCorner)
330 {
332 
333  // Horizontal anchor
334  Qt::AnchorPoint firstEdge = (firstCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
335  Qt::AnchorPoint secondEdge = (secondCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft);
336  if (d->addAnchor(firstItem, firstEdge, secondItem, secondEdge)) {
337  // Vertical anchor
338  firstEdge = (firstCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
339  secondEdge = (secondCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop);
340  d->addAnchor(firstItem, firstEdge, secondItem, secondEdge);
341 
342  invalidate();
343  }
344 }
345 
363  QGraphicsLayoutItem *secondItem,
364  Qt::Orientations orientations)
365 {
366  bool ok = true;
367  if (orientations & Qt::Horizontal) {
368  // Currently, if the first is ok, then the rest of the calls should be ok
369  ok = addAnchor(secondItem, Qt::AnchorLeft, firstItem, Qt::AnchorLeft) != 0;
370  if (ok)
371  addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight);
372  }
373  if (orientations & Qt::Vertical && ok) {
374  addAnchor(secondItem, Qt::AnchorTop, firstItem, Qt::AnchorTop);
375  addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom);
376  }
377 }
378 
385 {
387 
388  d->spacings[0] = spacing;
389  invalidate();
390 }
391 
398 {
400 
401  d->spacings[1] = spacing;
402  invalidate();
403 }
404 
417 {
419 
420  d->spacings[0] = d->spacings[1] = spacing;
421  invalidate();
422 }
423 
430 {
431  Q_D(const QGraphicsAnchorLayout);
432  return d->styleInfo().defaultSpacing(Qt::Horizontal);
433 }
434 
441 {
442  Q_D(const QGraphicsAnchorLayout);
443  return d->styleInfo().defaultSpacing(Qt::Vertical);
444 }
445 
450 {
452 
454  d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Horizontal);
455  d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Vertical);
456  d->setItemsGeometries(geom);
457 }
458 
468 {
470  QGraphicsLayoutItem *item = d->items.value(index);
471 
472  if (!item)
473  return;
474 
475  // Removing an item affects both horizontal and vertical graphs
476  d->removeCenterConstraints(item, QGraphicsAnchorLayoutPrivate::Horizontal);
477  d->removeCenterConstraints(item, QGraphicsAnchorLayoutPrivate::Vertical);
478  d->removeAnchors(item);
479  d->items.remove(index);
480 
481  item->setParentLayoutItem(0);
482  invalidate();
483 }
484 
489 {
490  Q_D(const QGraphicsAnchorLayout);
491  return d->items.size();
492 }
493 
498 {
499  Q_D(const QGraphicsAnchorLayout);
500  return d->items.value(index);
501 }
502 
507 {
510  d->calculateGraphCacheDirty = true;
511  d->styleInfoDirty = true;
512 }
513 
518 {
519  Q_UNUSED(constraint);
520  Q_D(const QGraphicsAnchorLayout);
521 
522  // Some setup calculations are delayed until the information is
523  // actually needed, avoiding unnecessary recalculations when
524  // adding multiple anchors.
525 
526  // sizeHint() / effectiveSizeHint() already have a cache
527  // mechanism, using invalidate() to force recalculation. However
528  // sizeHint() is called three times after invalidation (for max,
529  // min and pref), but we just need do our setup once.
530 
531  const_cast<QGraphicsAnchorLayoutPrivate *>(d)->calculateGraphs();
532 
533  // ### apply constraint!
534  QSizeF engineSizeHint(
535  d->sizeHints[QGraphicsAnchorLayoutPrivate::Horizontal][which],
536  d->sizeHints[QGraphicsAnchorLayoutPrivate::Vertical][which]);
537 
538  qreal left, top, right, bottom;
539  getContentsMargins(&left, &top, &right, &bottom);
540 
541  return engineSizeHint + QSizeF(left + right, top + bottom);
542 }
543 
545 #endif //QT_NO_GRAPHICSVIEW
double d
Definition: qnumeric_p.h:62
void setGeometry(const QRectF &rect)
Reimplemented Function
QGraphicsAnchor(QGraphicsAnchorLayout *parent)
virtual void setGeometry(const QRectF &rect)
This virtual function sets the geometry of the QGraphicsLayoutItem to rect, which is in parent coordi...
double qreal
Definition: qglobal.h:1193
QGraphicsAnchorLayout(QGraphicsLayoutItem *parent=0)
Constructs a QGraphicsAnchorLayout instance.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void getContentsMargins(qreal *left, qreal *top, qreal *right, qreal *bottom) const
Reimplemented Function
qreal horizontalSpacing() const
Returns the default horizontal spacing for the anchor layout.
qreal spacing() const
void setSpacing(qreal spacing)
Sets the default horizontal and the default vertical spacing for the anchor layout to spacing...
virtual ~QGraphicsAnchorLayout()
Destroys the QGraphicsAnchorLayout object.
The QGraphicsAnchor class represents an anchor between two items in a QGraphicsAnchorLayout.
long ASN1_INTEGER_get ASN1_INTEGER * a
void setParentLayoutItem(QGraphicsLayoutItem *parent)
Sets the parent of this QGraphicsLayoutItem to parent.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void setSizePolicy(QSizePolicy::Policy policy)
#define Q_D(Class)
Definition: qglobal.h:2482
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void addCornerAnchors(QGraphicsLayoutItem *firstItem, Qt::Corner firstCorner, QGraphicsLayoutItem *secondItem, Qt::Corner secondCorner)
Creates two anchors between firstItem and secondItem specified by the corners, firstCorner and second...
virtual void invalidate()
Clears any cached geometry and size hint information in the layout, and posts a LayoutRequest event t...
AnchorPoint
Definition: qnamespace.h:1586
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Reimplemented Function
#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
void invalidate()
Reimplemented Function
QGraphicsLayoutItem * itemAt(int index) const
Reimplemented Function
The QGraphicsLayoutItem class can be inherited to allow your custom items to be managed by layouts...
QGraphicsAnchor * addAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
Creates an anchor between the edge firstEdge of item firstItem and the edge secondEdge of item second...
bool ownedByLayout() const
Returns whether a layout should delete this item in its destructor.
void setSpacing(qreal spacing)
void setVerticalSpacing(qreal spacing)
Sets the default vertical spacing for the anchor layout to spacing.
void addAnchors(QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem, Qt::Orientations orientations=Qt::Horizontal|Qt::Vertical)
Anchors two or four edges of firstItem with the corresponding edges of secondItem, so that firstItem has the same size as secondItem in the dimensions specified by orientations.
The QGraphicsAnchorLayout class provides a layout where one can anchor widgets together in Graphics V...
Corner
Definition: qnamespace.h:1456
SizeHint
Definition: qnamespace.h:1708
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
qreal verticalSpacing() const
Returns the default vertical spacing for the anchor layout.
The QGraphicsLayout class provides the base class for all layouts in Graphics View.
int count() const
Reimplemented Function
void setHorizontalSpacing(qreal spacing)
Sets the default horizontal spacing for the anchor layout to spacing.
QSizePolicy::Policy sizePolicy() const
quint16 index
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
~QGraphicsAnchor()
Removes the QGraphicsAnchor object from the layout and destroys it.
QGraphicsAnchorLayout private methods and attributes.
void removeAt(int index)
Removes the layout item at index without destroying it.
QGraphicsAnchor * anchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge)
Returns the anchor between the anchor points defined by firstItem and firstEdge and secondItem and se...