| [556] | 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| [651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| [556] | 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 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 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \class QGraphicsAnchorLayout
|
|---|
| 44 | \brief The QGraphicsAnchorLayout class provides a layout where one can anchor widgets
|
|---|
| 45 | together in Graphics View.
|
|---|
| 46 | \since 4.6
|
|---|
| 47 | \ingroup appearance
|
|---|
| 48 | \ingroup geomanagement
|
|---|
| 49 | \ingroup graphicsview-api
|
|---|
| 50 |
|
|---|
| 51 | The anchor layout allows developers to specify how widgets should be placed relative to
|
|---|
| 52 | each other, and to the layout itself. The specification is made by adding anchors to the
|
|---|
| 53 | layout by calling addAnchor(), addAnchors() or addCornerAnchors().
|
|---|
| 54 |
|
|---|
| 55 | Existing anchors in the layout can be accessed with the anchor() function.
|
|---|
| 56 | Items that are anchored are automatically added to the layout, and if items
|
|---|
| 57 | are removed, all their anchors will be automatically removed.
|
|---|
| 58 |
|
|---|
| 59 | \beginfloatleft
|
|---|
| 60 | \inlineimage simpleanchorlayout-example.png Using an anchor layout to align simple colored widgets.
|
|---|
| 61 | \endfloat
|
|---|
| 62 |
|
|---|
| 63 | Anchors are always set up between edges of an item, where the "center" is also considered to
|
|---|
| 64 | be an edge. Consider the following example:
|
|---|
| 65 |
|
|---|
| 66 | \snippet examples/graphicsview/simpleanchorlayout/main.cpp adding anchors
|
|---|
| 67 |
|
|---|
| 68 | Here, the right edge of item \c a is anchored to the left edge of item \c b and the bottom
|
|---|
| 69 | edge of item \c a is anchored to the top edge of item \c b, with the result that
|
|---|
| 70 | item \c b will be placed diagonally to the right and below item \c b.
|
|---|
| 71 |
|
|---|
| 72 | The addCornerAnchors() function provides a simpler way of anchoring the corners
|
|---|
| 73 | of two widgets than the two individual calls to addAnchor() shown in the code
|
|---|
| 74 | above. Here, we see how a widget can be anchored to the top-left corner of the enclosing
|
|---|
| 75 | layout:
|
|---|
| 76 |
|
|---|
| 77 | \snippet examples/graphicsview/simpleanchorlayout/main.cpp adding a corner anchor
|
|---|
| 78 |
|
|---|
| 79 | In cases where anchors are used to match the widths or heights of widgets, it is
|
|---|
| 80 | convenient to use the addAnchors() function. As with the other functions for specifying
|
|---|
| 81 | anchors, it can also be used to anchor a widget to a layout.
|
|---|
| 82 |
|
|---|
| 83 | \clearfloat
|
|---|
| 84 | \section1 Size Hints and Size Policies in an Anchor Layout
|
|---|
| 85 |
|
|---|
| 86 | QGraphicsAnchorLayout respects each item's size hints and size policies.
|
|---|
| 87 | Note that there are some properties of QSizePolicy that are \l{Known issues}{not respected}.
|
|---|
| 88 |
|
|---|
| 89 | \section1 Spacing within an Anchor Layout
|
|---|
| 90 |
|
|---|
| 91 | The layout may distribute some space between the items. If the spacing has not been
|
|---|
| 92 | explicitly specified, the actual amount of space will usually be 0.
|
|---|
| 93 |
|
|---|
| 94 | However, if the first edge is the \e opposite of the second edge (e.g., the right edge
|
|---|
| 95 | of the first widget is anchored to the left edge of the second widget), the size of the
|
|---|
| 96 | anchor will be queried from the style through a pixel metric:
|
|---|
| 97 | \l{QStyle::}{PM_LayoutHorizontalSpacing} for horizontal anchors and
|
|---|
| 98 | \l{QStyle::}{PM_LayoutVerticalSpacing} for vertical anchors.
|
|---|
| 99 |
|
|---|
| 100 | If the spacing is negative, the items will overlap to some extent.
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | \section1 Known issues
|
|---|
| 104 | There are some features that QGraphicsAnchorLayout currently does not support.
|
|---|
| 105 | This might change in the future, so avoid using these features if you want to
|
|---|
| 106 | avoid any future regressions in behaviour:
|
|---|
| 107 | \list
|
|---|
| 108 |
|
|---|
| 109 | \o Stretch factors are not respected.
|
|---|
| 110 |
|
|---|
| 111 | \o QSizePolicy::ExpandFlag is not respected.
|
|---|
| 112 |
|
|---|
| 113 | \o Height for width is not respected.
|
|---|
| 114 |
|
|---|
| 115 | \endlist
|
|---|
| 116 |
|
|---|
| 117 | \sa QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayout
|
|---|
| 118 | */
|
|---|
| 119 |
|
|---|
| 120 | /*!
|
|---|
| 121 | \class QGraphicsAnchor
|
|---|
| 122 | \brief The QGraphicsAnchor class represents an anchor between two items in a
|
|---|
| 123 | QGraphicsAnchorLayout.
|
|---|
| 124 | \since 4.6
|
|---|
| 125 | \ingroup appearance
|
|---|
| 126 | \ingroup geomanagement
|
|---|
| 127 | \ingroup graphicsview-api
|
|---|
| 128 |
|
|---|
| 129 | The graphics anchor provides an API that enables you to query and manipulate the
|
|---|
| 130 | properties an anchor has. When an anchor is added to the layout with
|
|---|
| 131 | QGraphicsAnchorLayout::addAnchor(), a QGraphicsAnchor instance is returned where the properties
|
|---|
| 132 | are initialized to their default values. The properties can then be further changed, and they
|
|---|
| 133 | will be picked up the next time the layout is activated.
|
|---|
| 134 |
|
|---|
| 135 | \sa QGraphicsAnchorLayout::anchor()
|
|---|
| 136 |
|
|---|
| 137 | */
|
|---|
| 138 | #include "qgraphicsanchorlayout_p.h"
|
|---|
| 139 | #ifndef QT_NO_GRAPHICSVIEW
|
|---|
| 140 | QT_BEGIN_NAMESPACE
|
|---|
| 141 |
|
|---|
| 142 | QGraphicsAnchor::QGraphicsAnchor(QGraphicsAnchorLayout *parentLayout)
|
|---|
| 143 | : QObject(*(new QGraphicsAnchorPrivate))
|
|---|
| 144 | {
|
|---|
| 145 | Q_D(QGraphicsAnchor);
|
|---|
| 146 | Q_ASSERT(parentLayout);
|
|---|
| 147 | d->layoutPrivate = parentLayout->d_func();
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /*!
|
|---|
| 151 | Removes the QGraphicsAnchor object from the layout and destroys it.
|
|---|
| 152 | */
|
|---|
| 153 | QGraphicsAnchor::~QGraphicsAnchor()
|
|---|
| 154 | {
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /*!
|
|---|
| 158 | \property QGraphicsAnchor::sizePolicy
|
|---|
| 159 | \brief the size policy for the QGraphicsAnchor.
|
|---|
| 160 |
|
|---|
| 161 | By setting the size policy on an anchor you can configure how the anchor can resize itself
|
|---|
| 162 | from its preferred spacing. For instance, if the anchor has the size policy
|
|---|
| 163 | QSizePolicy::Minimum, the spacing is the minimum size of the anchor. However, its size
|
|---|
| 164 | can grow up to the anchors maximum size. If the default size policy is QSizePolicy::Fixed,
|
|---|
| 165 | the anchor can neither grow or shrink, which means that the only size the anchor can have
|
|---|
| 166 | is the spacing. QSizePolicy::Fixed is the default size policy.
|
|---|
| 167 | QGraphicsAnchor always has a minimum spacing of 0 and a very large maximum spacing.
|
|---|
| 168 |
|
|---|
| 169 | \sa QGraphicsAnchor::spacing
|
|---|
| 170 | */
|
|---|
| 171 |
|
|---|
| 172 | void QGraphicsAnchor::setSizePolicy(QSizePolicy::Policy policy)
|
|---|
| 173 | {
|
|---|
| 174 | Q_D(QGraphicsAnchor);
|
|---|
| 175 | d->setSizePolicy(policy);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | QSizePolicy::Policy QGraphicsAnchor::sizePolicy() const
|
|---|
| 179 | {
|
|---|
| 180 | Q_D(const QGraphicsAnchor);
|
|---|
| |
|---|