diff options
Diffstat (limited to 'src/gui/graphicsview/qgraphicsanchorlayout.cpp')
-rw-r--r-- | src/gui/graphicsview/qgraphicsanchorlayout.cpp | 309 |
1 files changed, 150 insertions, 159 deletions
diff --git a/src/gui/graphicsview/qgraphicsanchorlayout.cpp b/src/gui/graphicsview/qgraphicsanchorlayout.cpp index 13abd52..fdb1708 100644 --- a/src/gui/graphicsview/qgraphicsanchorlayout.cpp +++ b/src/gui/graphicsview/qgraphicsanchorlayout.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -20,10 +21,9 @@ ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this -** package. +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** If you have questions regarding the use of this file, please contact ** Nokia at qt-info@nokia.com. @@ -80,10 +80,77 @@ PM_LayoutHorizontalSpacing (or PM_LayoutVerticalSpacing for vertical anchors). */ +/*! + \class QGraphicsAnchor + \brief The QGraphicsAnchor class represents an anchor between two items in a + QGraphicsAnchorLayout. + \since 4.6 + \ingroup appearance + \ingroup geomanagement + \ingroup graphicsview-api + + The graphics anchor provides an API that enables you to query and manipulate the + properties an anchor has. When an anchor is added to the layout with + QGraphicsAnchorLayout::addAnchor(), a QGraphicsAnchor instance is returned where the properties + are initialized to their default values. The properties can then be further changed, and they + will be picked up the next time the layout is activated. + + \sa QGraphicsAnchorLayout::anchor() + +*/ #include "qgraphicsanchorlayout_p.h" QT_BEGIN_NAMESPACE +QGraphicsAnchor::QGraphicsAnchor(QGraphicsAnchorLayout *parentLayout) + : QObject(*(new QGraphicsAnchorPrivate)) +{ + Q_D(QGraphicsAnchor); + Q_ASSERT(parentLayout); + d->layoutPrivate = parentLayout->d_func(); +} + +/*! + Removes the QGraphicsAnchor object from the layout and destroys it. +*/ +QGraphicsAnchor::~QGraphicsAnchor() +{ +} + +/*! + Set the spacing for the anchor to \a spacing. + + \sa spacing(), unsetSpacing() +*/ +void QGraphicsAnchor::setSpacing(qreal spacing) +{ + Q_D(QGraphicsAnchor); + d->setSpacing(spacing); +} + +/*! + Returns the spacing for the anchor + + \sa setSpacing() +*/ +qreal QGraphicsAnchor::spacing() const +{ + Q_D(const QGraphicsAnchor); + return d->spacing(); +} + +/*! + Resets the spacing of the anchor point to be the default spacing. Depending on the anchor type, + the default spacing is either 0 or a value returned from the style. + + \sa setSpacing(), spacing(), QGraphicsAnchorLayout::anchor() +*/ +void QGraphicsAnchor::unsetSpacing() +{ + Q_D(QGraphicsAnchor); + d->unsetSpacing(); +} + /*! Constructs a QGraphicsAnchorLayout instance. \a parent is passed to QGraphicsLayout's constructor. @@ -122,58 +189,71 @@ QGraphicsAnchorLayout::~QGraphicsAnchorLayout() } /*! - * Creates an anchor between the edge \a firstEdge of item \a firstItem and the edge \a secondEdge - * of item \a secondItem. The magnitude of the anchor is picked up from the style. Anchors - * between a layout edge and an item edge will have a size of 0. - * If there is already an anchor between the edges, the the new anchor will replace the old one. - * - * \a firstItem and \a secondItem are automatically added to the layout if they are not part - * of the layout. This means that count() can increase with up to 2. - * - * The spacing an anchor will get depends on the type of anchor. For instance, anchors from the - * Right edge of one item to the Left edge of another (or vice versa) will use the default - * horizontal spacing. The same behaviour applies to Bottom to Top anchors, (but they will use - * the default vertical spacing). For all other anchor combinations, the spacing will be 0. - * All anchoring functions will follow this rule. - * - * The spacing can also be set manually by using setAnchorSpacing() method. - * - * \sa removeAnchor(), addCornerAnchors(), addLeftAndRightAnchors(), addTopAndBottomAnchors(), - * addAllAnchors() + Creates an anchor between the edge \a firstEdge of item \a firstItem and the edge \a secondEdge + of item \a secondItem. The magnitude of the anchor is picked up from the style. Anchors + between a layout edge and an item edge will have a size of 0. + If there is already an anchor between the edges, the the new anchor will replace the old one. + + \a firstItem and \a secondItem are automatically added to the layout if they are not part + of the layout. This means that count() can increase with up to 2. + + The spacing an anchor will get depends on the type of anchor. For instance, anchors from the + Right edge of one item to the Left edge of another (or vice versa) will use the default + horizontal spacing. The same behaviour applies to Bottom to Top anchors, (but they will use + the default vertical spacing). For all other anchor combinations, the spacing will be 0. + All anchoring functions will follow this rule. + + The spacing can also be set manually by using QGraphicsAnchor::setSpacing() method. + + \sa addCornerAnchors(), addAnchors() */ -void QGraphicsAnchorLayout::addAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, - QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) +QGraphicsAnchor * +QGraphicsAnchorLayout::addAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, + QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) { Q_D(QGraphicsAnchorLayout); - d->anchor(firstItem, firstEdge, secondItem, secondEdge); + QGraphicsAnchor *a = d->addAnchor(firstItem, firstEdge, secondItem, secondEdge); invalidate(); + return a; } /*! - * Creates two anchors between \a firstItem and \a secondItem, where one is for the horizontal - * edge and another one for the vertical edge that the corners \a firstCorner and \a - * secondCorner specifies. - * The magnitude of the anchors is picked up from the style. - * - * This is a convenience function, since anchoring corners can be expressed as anchoring two edges. - * For instance, - * \code - * layout->addAnchor(layout, Qt::AnchorTop, b, Qt::AnchorTop); - * layout->addAnchor(layout, Qt::AnchorLeft, b, Qt::AnchorLeft); - * \endcode - * - * has the same effect as - * - * \code - * layout->addCornerAnchors(layout, Qt::TopLeft, b, Qt::TopLeft); - * \endcode - * - * If there is already an anchor between the edge pairs, it will be replaced by the anchors that - * this function specifies. - * - * \a firstItem and \a secondItem are automatically added to the layout if they are not part - * of the layout. This means that count() can increase with up to 2. - */ + Returns the anchor between the anchor points defined by \a firstItem and \a firstEdge and + \a secondItem and \a secondEdge. If there is no such anchor, the function will return 0. +*/ +QGraphicsAnchor * +QGraphicsAnchorLayout::anchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, + QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) +{ + Q_D(QGraphicsAnchorLayout); + return d->getAnchor(firstItem, firstEdge, secondItem, secondEdge); +} + +/*! + Creates two anchors between \a firstItem and \a secondItem, where one is for the horizontal + edge and another one for the vertical edge that the corners \a firstCorner and \a + secondCorner specifies. + The magnitude of the anchors is picked up from the style. + + This is a convenience function, since anchoring corners can be expressed as anchoring two edges. + For instance, + \code + layout->addAnchor(layout, Qt::AnchorTop, b, Qt::AnchorTop); + layout->addAnchor(layout, Qt::AnchorLeft, b, Qt::AnchorLeft); + \endcode + + has the same effect as + + \code + layout->addCornerAnchors(layout, Qt::TopLeft, b, Qt::TopLeft); + \endcode + + If there is already an anchor between the edge pairs, it will be replaced by the anchors that + this function specifies. + + \a firstItem and \a secondItem are automatically added to the layout if they are not part of the + layout. This means that count() can increase with up to 2. +*/ void QGraphicsAnchorLayout::addCornerAnchors(QGraphicsLayoutItem *firstItem, Qt::Corner firstCorner, QGraphicsLayoutItem *secondItem, @@ -184,133 +264,45 @@ void QGraphicsAnchorLayout::addCornerAnchors(QGraphicsLayoutItem *firstItem, // Horizontal anchor Qt::AnchorPoint firstEdge = (firstCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft); Qt::AnchorPoint secondEdge = (secondCorner & 1 ? Qt::AnchorRight: Qt::AnchorLeft); - d->anchor(firstItem, firstEdge, secondItem, secondEdge); + d->addAnchor(firstItem, firstEdge, secondItem, secondEdge); // Vertical anchor firstEdge = (firstCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop); secondEdge = (secondCorner & 2 ? Qt::AnchorBottom: Qt::AnchorTop); - d->anchor(firstItem, firstEdge, secondItem, secondEdge); + d->addAnchor(firstItem, firstEdge, secondItem, secondEdge); invalidate(); } /*! - \fn QGraphicsAnchorLayout::addLeftAndRightAnchors(QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem) + Anchors two or four edges of \a firstItem with the corresponding + edges of \a secondItem, so that \a firstItem has the same size as + \a secondItem in the dimensions specified by \a orientations. - Anchors the left and right edges of \a firstItem to the same edges of - \a secondItem. - - This convenience function is equivalent to calling - \code - l->addAnchor(firstItem, Qt::AnchorLeft, secondItem, Qt::AnchorLeft); - l->addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight); - \endcode -*/ - -/*! - \fn QGraphicsAnchorLayout::addTopAndBottomAnchors(QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem) - - Anchors the top and bottom edges of \a firstItem to the same edges of - \a secondItem. - - This convenience function is equivalent to calling + Calling this convenience function with the following arguments \code - l->addAnchor(firstItem, Qt::AnchorTop, secondItem, Qt::AnchorTop); - l->addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom); + l->addAnchors(firstItem, secondItem, Qt::Horizontal) \endcode -*/ -/*! - \fn QGraphicsAnchorLayout::addAllAnchors(QGraphicsLayoutItem *firstItem, QGraphicsLayoutItem *secondItem) - - Anchors all edges (left, right, top and bottom) of \a firstItem to the same edges of - \a secondItem. + is the same as - This convenience function is equivalent to calling \code - l->addLeftAndRightAnchors(firstItem, secondItem); - l->addTopAndBottomAnchors(firstItem, secondItem); + l->addAnchor(firstItem, Qt::AnchorLeft, secondItem, Qt::AnchorLeft); + l->addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight); \endcode */ - -/*! - Set the spacing between the anchor point defined by \a firstItem and \a firstEdge and - \a secondItem and \a secondEdge to be \a spacing. -*/ -void QGraphicsAnchorLayout::setAnchorSpacing(const QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, - const QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge, - qreal spacing) +void QGraphicsAnchorLayout::addAnchors(QGraphicsLayoutItem *firstItem, + QGraphicsLayoutItem *secondItem, + Qt::Orientations orientations) { - Q_D(QGraphicsAnchorLayout); - - if (!d->setAnchorSize(firstItem, firstEdge, secondItem, secondEdge, &spacing)) { - qWarning("setAnchorSpacing: The anchor does not exist."); - return; + if (orientations & Qt::Horizontal) { + addAnchor(secondItem, Qt::AnchorLeft, firstItem, Qt::AnchorLeft); + addAnchor(firstItem, Qt::AnchorRight, secondItem, Qt::AnchorRight); } - invalidate(); -} - -/*! - Returns the spacing between the anchor point defined by \a firstItem and \a firstEdge and - \a secondItem and \a secondEdge. The anchor must exist. -*/ -qreal QGraphicsAnchorLayout::anchorSpacing(const QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, - const QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) const -{ - Q_D(const QGraphicsAnchorLayout); - qreal size = 0; - if (!d->anchorSize(firstItem, firstEdge, secondItem, secondEdge, 0, &size)) { - qWarning("anchorSpacing: The anchor does not exist."); + if (orientations & Qt::Vertical) { + addAnchor(secondItem, Qt::AnchorTop, firstItem, Qt::AnchorTop); + addAnchor(firstItem, Qt::AnchorBottom, secondItem, Qt::AnchorBottom); } - return size; -} - -/*! - Resets the spacing between the anchor point defined by \a firstItem and \a firstEdge and - \a secondItem and \a secondEdge to be the default spacing. Depending on the anchor type, the - default spacing is either 0 or a value returned from the style. - - \sa setAnchorSpacing(), anchorSpacing(), addAnchor() -*/ -void QGraphicsAnchorLayout::unsetAnchorSpacing(const QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, - const QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) -{ - Q_D(QGraphicsAnchorLayout); - - if (!d->setAnchorSize(firstItem, firstEdge, secondItem, secondEdge, 0)) { - qWarning("unsetAnchorSpacing: The anchor does not exist."); - } - invalidate(); -} - -/*! - Removes the anchor between the edge \a firstEdge of item \a firstItem and the edge \a secondEdge - of item \a secondItem. If such an anchor does not exist, the layout will be left unchanged. -*/ -void QGraphicsAnchorLayout::removeAnchor(QGraphicsLayoutItem *firstItem, Qt::AnchorPoint firstEdge, - QGraphicsLayoutItem *secondItem, Qt::AnchorPoint secondEdge) -{ - Q_D(QGraphicsAnchorLayout); - if ((firstItem == 0) || (secondItem == 0)) { - qWarning("QGraphicsAnchorLayout::removeAnchor: " - "Cannot remove anchor between NULL items"); - return; - } - - if (firstItem == secondItem) { - qWarning("QGraphicsAnchorLayout::removeAnchor: " - "Cannot remove anchor from the item to itself"); - return; - } - - if (d->edgeOrientation(secondEdge) != d->edgeOrientation(firstEdge)) { - qWarning("QGraphicsAnchorLayout::removeAnchor: " - "Cannot remove anchor from edges of different orientations"); - return; - } - - d->removeAnchor(firstItem, firstEdge, secondItem, secondEdge); - invalidate(); } /*! @@ -374,7 +366,7 @@ qreal QGraphicsAnchorLayout::verticalSpacing() const } /*! - \reimp + \reimp */ void QGraphicsAnchorLayout::setGeometry(const QRectF &geom) { @@ -383,7 +375,7 @@ void QGraphicsAnchorLayout::setGeometry(const QRectF &geom) QGraphicsLayout::setGeometry(geom); d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Horizontal); d->calculateVertexPositions(QGraphicsAnchorLayoutPrivate::Vertical); - d->setItemsGeometries(); + d->setItemsGeometries(geom); } /*! @@ -448,7 +440,6 @@ void QGraphicsAnchorLayout::invalidate() */ QSizeF QGraphicsAnchorLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { - Q_UNUSED(which); Q_UNUSED(constraint); Q_D(const QGraphicsAnchorLayout); |