summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan-Arve Sæther <jan-arve.saether@nokia.com>2009-10-16 15:31:10 (GMT)
committerJan-Arve Sæther <jan-arve.saether@nokia.com>2009-10-16 16:04:41 (GMT)
commit299fd2c5d3c901890c0bc2c36c4b5f48d46859dc (patch)
treeafd629dfa2eedfa9923ccb121fd86ddf54f2283c /src
parente0a5d1f18d5dee85ddc312094f5bb4d49ea3d286 (diff)
downloadQt-299fd2c5d3c901890c0bc2c36c4b5f48d46859dc.zip
Qt-299fd2c5d3c901890c0bc2c36c4b5f48d46859dc.tar.gz
Qt-299fd2c5d3c901890c0bc2c36c4b5f48d46859dc.tar.bz2
API addition: Make it possible to set the size policy of an anchor.
After this commit, when you modify the spacing of an anchor you are effectively modifying the preferred size of the anchor, since all anchors (except internal ones) have their minimumSizeHint to 0 and maximumSizeHint to QWIDGETSIZE_MAX. I also changed the sizeHintsFromItem to be more generic so that I could use it for anchors. (Thus, it was renamed to "internalSizeHints"). It now only takes care of setting the min/pref/exp/maxSize of AnchorData based on the anchor/item size hint and their size policies. As a consequence of all of this, setFixedSize changed behaviour and became setPreferredSize (since setSpacing is basically setPreferredSize). The implementation of that now only sets the prefSize. The patch also has an unrelated fix for IgnoreFlag, where it will (again) return the minimumSize for sizeHint(Qt::PreferredSize) instead of the maximumSize. This was to be more consistent with qgridlayoutengine.cpp. The docs are not very clear on this behaviour unfortunately. This API change has been discussed. Reviewed-by: alexis
Diffstat (limited to 'src')
-rw-r--r--src/gui/graphicsview/qgraphicsanchorlayout.cpp17
-rw-r--r--src/gui/graphicsview/qgraphicsanchorlayout.h2
-rw-r--r--src/gui/graphicsview/qgraphicsanchorlayout_p.cpp109
-rw-r--r--src/gui/graphicsview/qgraphicsanchorlayout_p.h14
4 files changed, 88 insertions, 54 deletions
diff --git a/src/gui/graphicsview/qgraphicsanchorlayout.cpp b/src/gui/graphicsview/qgraphicsanchorlayout.cpp
index 9f4a19b..c39e8a6 100644
--- a/src/gui/graphicsview/qgraphicsanchorlayout.cpp
+++ b/src/gui/graphicsview/qgraphicsanchorlayout.cpp
@@ -118,6 +118,23 @@ QGraphicsAnchor::~QGraphicsAnchor()
}
/*!
+ Sets the size policy of the anchor to \a policy.
+*/
+void QGraphicsAnchor::setSizePolicy(QSizePolicy::Policy policy)
+{
+ Q_D(QGraphicsAnchor);
+ d->setSizePolicy(policy);
+}
+/*!
+ Returns the size policy of the anchor. The default size policy is QSizePolicy::Fixed
+*/
+QSizePolicy::Policy QGraphicsAnchor::sizePolicy() const
+{
+ Q_D(const QGraphicsAnchor);
+ return d->sizePolicy;
+}
+
+/*!
\property QGraphicsAnchor::spacing
\brief the space between items in the QGraphicsAnchorLayout.
diff --git a/src/gui/graphicsview/qgraphicsanchorlayout.h b/src/gui/graphicsview/qgraphicsanchorlayout.h
index 99dbf92..f09ac43 100644
--- a/src/gui/graphicsview/qgraphicsanchorlayout.h
+++ b/src/gui/graphicsview/qgraphicsanchorlayout.h
@@ -65,6 +65,8 @@ class Q_GUI_EXPORT QGraphicsAnchor : public QObject
public:
void setSpacing(qreal spacing);
void unsetSpacing();
+ void setSizePolicy(QSizePolicy::Policy policy);
+ QSizePolicy::Policy sizePolicy() const;
qreal spacing() const;
~QGraphicsAnchor();
private:
diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp
index e1db939..8c8c303 100644
--- a/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp
+++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.cpp
@@ -53,7 +53,8 @@ QT_BEGIN_NAMESPACE
QGraphicsAnchorPrivate::QGraphicsAnchorPrivate(int version)
- : QObjectPrivate(version), layoutPrivate(0), data(0)
+ : QObjectPrivate(version), layoutPrivate(0), data(0),
+ sizePolicy(QSizePolicy::Fixed)
{
}
@@ -62,6 +63,14 @@ QGraphicsAnchorPrivate::~QGraphicsAnchorPrivate()
layoutPrivate->removeAnchor(data->from, data->to);
}
+void QGraphicsAnchorPrivate::setSizePolicy(QSizePolicy::Policy policy)
+{
+ if (sizePolicy != policy) {
+ sizePolicy = policy;
+ layoutPrivate->q_func()->invalidate();
+ }
+}
+
void QGraphicsAnchorPrivate::setSpacing(qreal value)
{
if (data) {
@@ -92,28 +101,11 @@ qreal QGraphicsAnchorPrivate::spacing() const
}
-static void sizeHintsFromItem(QGraphicsLayoutItem *item,
- const QGraphicsAnchorLayoutPrivate::Orientation orient,
- qreal *minSize, qreal *prefSize,
- qreal *expSize, qreal *maxSize)
+static void internalSizeHints(QSizePolicy::Policy policy,
+ qreal minSizeHint, qreal prefSizeHint, qreal maxSizeHint,
+ qreal *minSize, qreal *prefSize,
+ qreal *expSize, qreal *maxSize)
{
- QSizePolicy::Policy policy;
- qreal minSizeHint;
- qreal prefSizeHint;
- qreal maxSizeHint;
-
- if (orient == QGraphicsAnchorLayoutPrivate::Horizontal) {
- policy = item->sizePolicy().horizontalPolicy();
- minSizeHint = item->effectiveSizeHint(Qt::MinimumSize).width();
- prefSizeHint = item->effectiveSizeHint(Qt::PreferredSize).width();
- maxSizeHint = item->effectiveSizeHint(Qt::MaximumSize).width();
- } else {
- policy = item->sizePolicy().verticalPolicy();
- minSizeHint = item->effectiveSizeHint(Qt::MinimumSize).height();
- prefSizeHint = item->effectiveSizeHint(Qt::PreferredSize).height();
- maxSizeHint = item->effectiveSizeHint(Qt::MaximumSize).height();
- }
-
// minSize, prefSize and maxSize are initialized
// with item's preferred Size: this is QSizePolicy::Fixed.
//
@@ -139,7 +131,7 @@ static void sizeHintsFromItem(QGraphicsLayoutItem *item,
// Note that these two initializations are affected by the previous flags
if (policy & QSizePolicy::IgnoreFlag)
- *prefSize = *maxSize;
+ *prefSize = *minSize;
else
*prefSize = prefSizeHint;
@@ -153,38 +145,63 @@ void AnchorData::refreshSizeHints(qreal effectiveSpacing)
{
const bool isInternalAnchor = from->m_item == to->m_item;
+ QSizePolicy::Policy policy;
+ qreal minSizeHint;
+ qreal prefSizeHint;
+ qreal maxSizeHint;
+
if (isInternalAnchor) {
const QGraphicsAnchorLayoutPrivate::Orientation orient =
QGraphicsAnchorLayoutPrivate::edgeOrientation(from->m_edge);
+ const Qt::AnchorPoint centerEdge =
+ QGraphicsAnchorLayoutPrivate::pickEdge(Qt::AnchorHorizontalCenter, orient);
+ bool hasCenter = (from->m_edge == centerEdge || to->m_edge == centerEdge);
if (isLayoutAnchor) {
minSize = 0;
prefSize = 0;
expSize = 0;
maxSize = QWIDGETSIZE_MAX;
+ if (hasCenter)
+ maxSize /= 2;
+ return;
} else {
- QGraphicsLayoutItem *item = from->m_item;
- sizeHintsFromItem(item, orient, &minSize, &prefSize, &expSize, &maxSize);
- }
- const Qt::AnchorPoint centerEdge =
- QGraphicsAnchorLayoutPrivate::pickEdge(Qt::AnchorHorizontalCenter, orient);
- bool hasCenter = (from->m_edge == centerEdge || to->m_edge == centerEdge);
+ QGraphicsLayoutItem *item = from->m_item;
+ if (orient == QGraphicsAnchorLayoutPrivate::Horizontal) {
+ policy = item->sizePolicy().horizontalPolicy();
+ minSizeHint = item->effectiveSizeHint(Qt::MinimumSize).width();
+ prefSizeHint = item->effectiveSizeHint(Qt::PreferredSize).width();
+ maxSizeHint = item->effectiveSizeHint(Qt::MaximumSize).width();
+ } else {
+ policy = item->sizePolicy().verticalPolicy();
+ minSizeHint = item->effectiveSizeHint(Qt::MinimumSize).height();
+ prefSizeHint = item->effectiveSizeHint(Qt::PreferredSize).height();
+ maxSizeHint = item->effectiveSizeHint(Qt::MaximumSize).height();
+ }
- if (hasCenter) {
- minSize /= 2;
- prefSize /= 2;
- expSize /= 2;
- maxSize /= 2;
+ if (hasCenter) {
+ minSizeHint /= 2;
+ prefSizeHint /= 2;
+ maxSizeHint /= 2;
+ }
}
-
- } else if (!hasSize) {
- // Anchor has no size defined, use given default information
- minSize = effectiveSpacing;
- prefSize = effectiveSpacing;
- expSize = effectiveSpacing;
- maxSize = effectiveSpacing;
+ } else {
+ Q_ASSERT(graphicsAnchor);
+ policy = graphicsAnchor->sizePolicy();
+ minSizeHint = 0;
+ if (hasSize) {
+ // One can only configure the preferred size of a normal anchor. Their minimum and
+ // maximum "size hints" are always 0 and QWIDGETSIZE_MAX, correspondingly. However,
+ // their effective size hints might be narrowed down due to their size policies.
+ prefSizeHint = prefSize;
+ } else {
+ prefSizeHint = effectiveSpacing;
+ }
+ maxSizeHint = QWIDGETSIZE_MAX;
}
+ internalSizeHints(policy, minSizeHint, prefSizeHint, maxSizeHint,
+ &minSize, &prefSize, &expSize, &maxSize);
// Set the anchor effective sizes to preferred.
//
@@ -1191,18 +1208,18 @@ QGraphicsAnchor *QGraphicsAnchorLayoutPrivate::addAnchor(QGraphicsLayoutItem *fi
|| secondItem == q
|| pickEdge(firstEdge, Horizontal) == Qt::AnchorHorizontalCenter
|| oppositeEdge(firstEdge) != secondEdge) {
- data->setFixedSize(0);
+ data->setPreferredSize(0);
} else {
data->unsetSize();
}
addAnchor_helper(firstItem, firstEdge, secondItem, secondEdge, data);
} else if (*spacing >= 0) {
- data->setFixedSize(*spacing);
+ data->setPreferredSize(*spacing);
addAnchor_helper(firstItem, firstEdge, secondItem, secondEdge, data);
} else {
- data->setFixedSize(-*spacing);
+ data->setPreferredSize(-*spacing);
addAnchor_helper(secondItem, secondEdge, firstItem, firstEdge, data);
}
@@ -1389,9 +1406,9 @@ void QGraphicsAnchorLayoutPrivate::setAnchorSize(AnchorData *data, const qreal *
// positive by definition.
// "negative spacing" is handled by inverting the standard item order.
if (*anchorSize >= 0) {
- data->setFixedSize(*anchorSize);
+ data->setPreferredSize(*anchorSize);
} else {
- data->setFixedSize(-*anchorSize);
+ data->setPreferredSize(-*anchorSize);
qSwap(data->from, data->to);
}
} else {
diff --git a/src/gui/graphicsview/qgraphicsanchorlayout_p.h b/src/gui/graphicsview/qgraphicsanchorlayout_p.h
index 3a23677..d45c004 100644
--- a/src/gui/graphicsview/qgraphicsanchorlayout_p.h
+++ b/src/gui/graphicsview/qgraphicsanchorlayout_p.h
@@ -169,16 +169,9 @@ struct AnchorData : public QSimplexVariable {
QString name;
#endif
- inline void setFixedSize(qreal size)
+ inline void setPreferredSize(qreal size)
{
- minSize = size;
prefSize = size;
- expSize = size;
- maxSize = size;
- sizeAtMinimum = size;
- sizeAtPreferred = size;
- sizeAtExpanding = size;
- sizeAtMaximum = size;
hasSize = true;
}
@@ -316,8 +309,11 @@ public:
void unsetSpacing();
qreal spacing() const;
+ void setSizePolicy(QSizePolicy::Policy policy);
+
QGraphicsAnchorLayoutPrivate *layoutPrivate;
AnchorData *data;
+ QSizePolicy::Policy sizePolicy;
};
@@ -528,6 +524,8 @@ public:
#endif
uint calculateGraphCacheDirty : 1;
+
+ friend class QGraphicsAnchorPrivate;
};
QT_END_NAMESPACE