static inline qreal hcenter(QQuickItem *item)
{
- return item->width() / 2;
+ qreal width = item->width();
+ if (QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors) {
+ if (!QQuickAnchorsPrivate::get(anchors)->centerAligned)
+ return width / 2;
+ }
+ int iw = width;
+ if (iw % 2)
+ return (width + 1) / 2;
+ else
+ return width / 2;
}
static inline qreal vcenter(QQuickItem *item)
{
- return item->height() / 2;
+ qreal height = item->height();
+ if (QQuickAnchors *anchors = QQuickItemPrivate::get(item)->_anchors) {
+ if (!QQuickAnchorsPrivate::get(anchors)->centerAligned)
+ return height / 2;
+ }
+ int ih = height;
+ if (ih % 2)
+ return (height + 1) / 2;
+ else
+ return height / 2;
}
//### const item?
return QQuickItemPrivate::get(d->item)->effectiveLayoutMirror;
}
+bool QQuickAnchors::alignWhenCentered() const
+{
+ Q_D(const QQuickAnchors);
+ return d->centerAligned;
+}
+
+void QQuickAnchors::setAlignWhenCentered(bool aligned)
+{
+ Q_D(QQuickAnchors);
+ if (aligned == d->centerAligned)
+ return;
+ d->centerAligned = aligned;
+ emit centerAlignedChanged();
+ if (d->centerIn) {
+ d->centerInChanged();
+ } else {
+ if (d->usedAnchors & QQuickAnchors::VCenterAnchor)
+ d->updateVerticalAnchors();
+ else if (d->usedAnchors & QQuickAnchors::HCenterAnchor)
+ d->updateHorizontalAnchors();
+ }
+}
+
bool QQuickAnchorsPrivate::isItemComplete() const
{
return componentComplete;
Q_PROPERTY(QQuickItem *fill READ fill WRITE setFill RESET resetFill NOTIFY fillChanged)
Q_PROPERTY(QQuickItem *centerIn READ centerIn WRITE setCenterIn RESET resetCenterIn NOTIFY centerInChanged)
Q_PROPERTY(bool mirrored READ mirrored NOTIFY mirroredChanged)
+ Q_PROPERTY(bool alignWhenCentered READ alignWhenCentered WRITE setAlignWhenCentered NOTIFY centerAlignedChanged)
public:
QQuickAnchors(QQuickItem *item, QObject *parent=0);
bool mirrored();
+ bool alignWhenCentered() const;
+ void setAlignWhenCentered(bool);
+
void classBegin();
void componentComplete();
void horizontalCenterOffsetChanged();
void baselineOffsetChanged();
void mirroredChanged();
+ void centerAlignedChanged();
private:
friend class QQuickItemPrivate;
Q_DECLARE_PUBLIC(QQuickAnchors)
public:
QQuickAnchorsPrivate(QQuickItem *i)
- : componentComplete(true), updatingMe(false), inDestructor(false), updatingHorizontalAnchor(0),
+ : componentComplete(true), updatingMe(false), inDestructor(false), centerAligned(true), updatingHorizontalAnchor(0),
updatingVerticalAnchor(0), updatingFill(0), updatingCenterIn(0), item(i), usedAnchors(0), fill(0),
centerIn(0), leftMargin(0), rightMargin(0), topMargin(0), bottomMargin(0),
margins(0), vCenterOffset(0), hCenterOffset(0), baselineOffset(0)
bool componentComplete:1;
bool updatingMe:1;
bool inDestructor:1;
+ bool centerAligned:1;
uint updatingHorizontalAnchor:2;
uint updatingVerticalAnchor:2;
uint updatingFill:2;
\qmlproperty real QtQuick2::Item::anchors.baselineOffset
\qmlproperty bool QtQuick2::Item::anchors.mirrored
+ \qmlproperty bool QtQuick2::Item::anchors.alignWhenCentered
Anchors provide a way to position an item by specifying its
relationship with other items.
\c anchors.mirrored returns true it the layout has been \l {LayoutMirroring}{mirrored}.
+ \c anchors.alignWhenCentered (default true) forces centered anchors to align to a
+ whole pixel, i.e. if the item being centered has an odd width/height the item
+ will be positioned on a whole pixel rather than being placed on a half-pixel.
+ This ensures the item is painted crisply. There are cases where this is not
+ desirable, for example when rotating the item jitters may be apparent as the
+ center is rounded.
+
\note You can only anchor an item to siblings or a parent.
For more information see \l {anchor-layout}{Anchor Layouts}.
width: 11; height: 11; color: "green"
anchors.centerIn: parent;
}
+
+ Rectangle {
+ objectName: "centered3"
+ width: 11; height: 11; color: "green"
+ anchors.centerIn: parent;
+ anchors.alignWhenCentered: false
+ }
}
rotation: 90
width: 101; height: 101; color: "blue"
anchors.centerIn: parent;
+ anchors.alignWhenCentered: false
Rectangle {
objectName: "inner"
QCOMPARE(rect->x(), 75.0 - 20.0);
QCOMPARE(rect->y(), 75.0 - 10.0);
- //QTBUG-21730 (use actual center to prevent animation jitter)
+ // By default center aligned to pixel
QQuickRectangle* rect2 = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered2"));
- QCOMPARE(rect2->x(), 94.5);
- QCOMPARE(rect2->y(), 94.5);
+ QCOMPARE(rect2->x(), 94.0);
+ QCOMPARE(rect2->y(), 94.0);
+
+ //QTBUG-21730 (use actual center to prevent animation jitter)
+ QQuickRectangle* rect3 = findItem<QQuickRectangle>(view->rootObject(), QLatin1String("centered3"));
+ QCOMPARE(rect3->x(), 94.5);
+ QCOMPARE(rect3->y(), 94.5);
delete view;
}