From b428301a2af0a68ff40d75003ed85aafb19f03f7 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Mon, 14 Feb 2011 17:42:03 +1000 Subject: Add support for anchor mirroring Task-number: QTBUG-15879 Reviewed-by: Michael Brasser Change-Id: I833132153ba95437a86bb840a96d5507612d3fa8 --- .../graphicsitems/qdeclarativeanchors.cpp | 111 +- .../graphicsitems/qdeclarativeanchors_p.h | 5 + .../graphicsitems/qdeclarativeanchors_p_p.h | 5 +- .../declarative/qdeclarativeanchors/data/fill.qml | 8 +- .../qdeclarativeanchors/data/margins.qml | 6 +- .../tst_qdeclarativeanchors.cpp | 201 ++- .../qdeclarativestates/tst_qdeclarativestates.cpp | 122 ++ .../animation/reanchorRTL/data/reanchor.0.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.1.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.10.png | Bin 0 -> 647 bytes .../animation/reanchorRTL/data/reanchor.11.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.12.png | Bin 0 -> 636 bytes .../animation/reanchorRTL/data/reanchor.2.png | Bin 0 -> 636 bytes .../animation/reanchorRTL/data/reanchor.3.png | Bin 0 -> 647 bytes .../animation/reanchorRTL/data/reanchor.4.png | Bin 0 -> 641 bytes .../animation/reanchorRTL/data/reanchor.5.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.6.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.7.png | Bin 0 -> 636 bytes .../animation/reanchorRTL/data/reanchor.8.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.9.png | Bin 0 -> 637 bytes .../animation/reanchorRTL/data/reanchor.qml | 1499 ++++++++++++++++++++ .../qmlvisual/animation/reanchorRTL/reanchor.qml | 69 + 22 files changed, 1988 insertions(+), 38 deletions(-) create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.0.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.1.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.10.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.11.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.12.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.2.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.3.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.4.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.5.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.6.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.7.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.8.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.9.png create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.qml create mode 100644 tests/auto/declarative/qmlvisual/animation/reanchorRTL/reanchor.qml diff --git a/src/declarative/graphicsitems/qdeclarativeanchors.cpp b/src/declarative/graphicsitems/qdeclarativeanchors.cpp index 444bbd4..a2d6261 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors.cpp +++ b/src/declarative/graphicsitems/qdeclarativeanchors.cpp @@ -181,10 +181,12 @@ void QDeclarativeAnchorsPrivate::fillChanged() if (updatingFill < 2) { ++updatingFill; + qreal horizontalMargin = isMirrored() ? rightMargin : leftMargin; + if (fill == item->parentItem()) { //child-parent - setItemPos(QPointF(leftMargin, topMargin)); + setItemPos(QPointF(horizontalMargin, topMargin)); } else if (fill->parentItem() == item->parentItem()) { //siblings - setItemPos(QPointF(fill->x()+leftMargin, fill->y()+topMargin)); + setItemPos(QPointF(fill->x()+horizontalMargin, fill->y()+topMargin)); } QGraphicsItemPrivate *fillPrivate = QGraphicsItemPrivate::get(fill); setItemSize(QSizeF(fillPrivate->width()-leftMargin-rightMargin, fillPrivate->height()-topMargin-bottomMargin)); @@ -204,13 +206,15 @@ void QDeclarativeAnchorsPrivate::centerInChanged() if (updatingCenterIn < 2) { ++updatingCenterIn; + + qreal effectiveHCenterOffset = isMirrored() ? -hCenterOffset : hCenterOffset; if (centerIn == item->parentItem()) { - QPointF p(hcenter(item->parentItem()) - hcenter(item) + hCenterOffset, + QPointF p(hcenter(item->parentItem()) - hcenter(item) + effectiveHCenterOffset, vcenter(item->parentItem()) - vcenter(item) + vCenterOffset); setItemPos(p); } else if (centerIn->parentItem() == item->parentItem()) { - QPointF p(centerIn->x() + hcenter(centerIn) - hcenter(item) + hCenterOffset, + QPointF p(centerIn->x() + hcenter(centerIn) - hcenter(item) + effectiveHCenterOffset, centerIn->y() + vcenter(centerIn) - vcenter(item) + vCenterOffset); setItemPos(p); } @@ -498,6 +502,11 @@ bool QDeclarativeAnchorsPrivate::calcStretch(const QDeclarativeAnchorLine &edge1 return invalid; } +bool QDeclarativeAnchorsPrivate::isMirrored() const +{ + return layoutDirection == Qt::RightToLeft; +} + void QDeclarativeAnchorsPrivate::updateVerticalAnchors() { if (fill || centerIn || !isItemComplete()) @@ -570,58 +579,93 @@ void QDeclarativeAnchorsPrivate::updateVerticalAnchors() } } +inline QDeclarativeAnchorLine::AnchorLine reverseAnchorLine(QDeclarativeAnchorLine::AnchorLine anchorLine) { + if (anchorLine == QDeclarativeAnchorLine::Left) { + return QDeclarativeAnchorLine::Right; + } else if (anchorLine == QDeclarativeAnchorLine::Right) { + return QDeclarativeAnchorLine::Left; + } else { + return anchorLine; + } +} + void QDeclarativeAnchorsPrivate::updateHorizontalAnchors() { if (fill || centerIn || !isItemComplete()) return; - if (updatingHorizontalAnchor < 2) { + if (updatingHorizontalAnchor < 3) { ++updatingHorizontalAnchor; + qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset; + QDeclarativeAnchorLine effectiveLeft, effectiveRight, effectiveHorizontalCenter; + QDeclarativeAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor; + if (isMirrored()) { + effectiveLeftAnchor = QDeclarativeAnchors::RightAnchor; + effectiveRightAnchor = QDeclarativeAnchors::LeftAnchor; + effectiveLeft.item = right.item; + effectiveLeft.anchorLine = reverseAnchorLine(right.anchorLine); + effectiveRight.item = left.item; + effectiveRight.anchorLine = reverseAnchorLine(left.anchorLine); + effectiveHorizontalCenter.item = hCenter.item; + effectiveHorizontalCenter.anchorLine = reverseAnchorLine(hCenter.anchorLine); + effectiveLeftMargin = rightMargin; + effectiveRightMargin = leftMargin; + effectiveHorizontalCenterOffset = -hCenterOffset; + } else { + effectiveLeftAnchor = QDeclarativeAnchors::LeftAnchor; + effectiveRightAnchor = QDeclarativeAnchors::RightAnchor; + effectiveLeft = left; + effectiveRight = right; + effectiveHorizontalCenter = hCenter; + effectiveLeftMargin = leftMargin; + effectiveRightMargin = rightMargin; + effectiveHorizontalCenterOffset = hCenterOffset; + } + QGraphicsItemPrivate *itemPrivate = QGraphicsItemPrivate::get(item); - if (usedAnchors & QDeclarativeAnchors::LeftAnchor) { + if (usedAnchors & effectiveLeftAnchor) { //Handle stretching bool invalid = true; qreal width = 0.0; - if (usedAnchors & QDeclarativeAnchors::RightAnchor) { - invalid = calcStretch(left, right, leftMargin, -rightMargin, QDeclarativeAnchorLine::Left, width); + if (usedAnchors & effectiveRightAnchor) { + invalid = calcStretch(effectiveLeft, effectiveRight, effectiveLeftMargin, -effectiveRightMargin, QDeclarativeAnchorLine::Left, width); } else if (usedAnchors & QDeclarativeAnchors::HCenterAnchor) { - invalid = calcStretch(left, hCenter, leftMargin, hCenterOffset, QDeclarativeAnchorLine::Left, width); + invalid = calcStretch(effectiveLeft, effectiveHorizontalCenter, effectiveLeftMargin, effectiveHorizontalCenterOffset, QDeclarativeAnchorLine::Left, width); width *= 2; } if (!invalid) setItemWidth(width); //Handle left - if (left.item == item->parentItem()) { - setItemX(adjustedPosition(left.item, left.anchorLine) + leftMargin); - } else if (left.item->parentItem() == item->parentItem()) { - setItemX(position(left.item, left.anchorLine) + leftMargin); + if (effectiveLeft.item == item->parentItem()) { + setItemX(adjustedPosition(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin); + } else if (effectiveLeft.item->parentItem() == item->parentItem()) { + setItemX(position(effectiveLeft.item, effectiveLeft.anchorLine) + effectiveLeftMargin); } - } else if (usedAnchors & QDeclarativeAnchors::RightAnchor) { + } else if (usedAnchors & effectiveRightAnchor) { //Handle stretching (left + right case is handled in updateLeftAnchor) if (usedAnchors & QDeclarativeAnchors::HCenterAnchor) { qreal width = 0.0; - bool invalid = calcStretch(hCenter, right, hCenterOffset, -rightMargin, + bool invalid = calcStretch(effectiveHorizontalCenter, effectiveRight, effectiveHorizontalCenterOffset, -effectiveRightMargin, QDeclarativeAnchorLine::Left, width); if (!invalid) setItemWidth(width*2); } //Handle right - if (right.item == item->parentItem()) { - setItemX(adjustedPosition(right.item, right.anchorLine) - itemPrivate->width() - rightMargin); - } else if (right.item->parentItem() == item->parentItem()) { - setItemX(position(right.item, right.anchorLine) - itemPrivate->width() - rightMargin); + if (effectiveRight.item == item->parentItem()) { + setItemX(adjustedPosition(effectiveRight.item, effectiveRight.anchorLine) - itemPrivate->width() - effectiveRightMargin); + } else if (effectiveRight.item->parentItem() == item->parentItem()) { + setItemX(position(effectiveRight.item, effectiveRight.anchorLine) - itemPrivate->width() - effectiveRightMargin); } } else if (usedAnchors & QDeclarativeAnchors::HCenterAnchor) { //Handle hCenter - if (hCenter.item == item->parentItem()) { - setItemX(adjustedPosition(hCenter.item, hCenter.anchorLine) - hcenter(item) + hCenterOffset); - } else if (hCenter.item->parentItem() == item->parentItem()) { - setItemX(position(hCenter.item, hCenter.anchorLine) - hcenter(item) + hCenterOffset); + if (effectiveHorizontalCenter.item == item->parentItem()) { + setItemX(adjustedPosition(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset); + } else if (effectiveHorizontalCenter.item->parentItem() == item->parentItem()) { + setItemX(position(effectiveHorizontalCenter.item, effectiveHorizontalCenter.anchorLine) - hcenter(item) + effectiveHorizontalCenterOffset); } } - --updatingHorizontalAnchor; } else { // ### Make this certain :) @@ -1042,6 +1086,25 @@ QDeclarativeAnchors::Anchors QDeclarativeAnchors::usedAnchors() const return d->usedAnchors; } + +Qt::LayoutDirection QDeclarativeAnchors::layoutDirection() const +{ + Q_D(const QDeclarativeAnchors); + return d->layoutDirection; +} + +void QDeclarativeAnchors::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + Q_D(QDeclarativeAnchors); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; + d->fillChanged(); + d->centerInChanged(); + d->updateHorizontalAnchors(); + emit layoutDirectionChanged(); + } +} + bool QDeclarativeAnchorsPrivate::checkHValid() const { if (usedAnchors & QDeclarativeAnchors::LeftAnchor && diff --git a/src/declarative/graphicsitems/qdeclarativeanchors_p.h b/src/declarative/graphicsitems/qdeclarativeanchors_p.h index d2c0a89..90a3508 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors_p.h +++ b/src/declarative/graphicsitems/qdeclarativeanchors_p.h @@ -79,6 +79,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeAnchors : public QObject Q_PROPERTY(qreal baselineOffset READ baselineOffset WRITE setBaselineOffset NOTIFY baselineOffsetChanged) Q_PROPERTY(QGraphicsObject *fill READ fill WRITE setFill RESET resetFill NOTIFY fillChanged) Q_PROPERTY(QGraphicsObject *centerIn READ centerIn WRITE setCenterIn RESET resetCenterIn NOTIFY centerInChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) public: QDeclarativeAnchors(QObject *parent=0); @@ -160,6 +161,9 @@ public: Anchors usedAnchors() const; + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection (Qt::LayoutDirection); + void classBegin(); void componentComplete(); @@ -181,6 +185,7 @@ Q_SIGNALS: void verticalCenterOffsetChanged(); void horizontalCenterOffsetChanged(); void baselineOffsetChanged(); + Q_REVISION(1) void layoutDirectionChanged(); private: friend class QDeclarativeItem; diff --git a/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h b/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h index c4508e0..ec96582 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h @@ -94,7 +94,7 @@ public: : componentComplete(true), updatingMe(false), 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) + margins(0), vCenterOffset(0), hCenterOffset(0), baselineOffset(0), layoutDirection(Qt::LeftToRight) { } @@ -133,6 +133,7 @@ public: bool checkVAnchorValid(QDeclarativeAnchorLine anchor) const; bool calcStretch(const QDeclarativeAnchorLine &edge1, const QDeclarativeAnchorLine &edge2, qreal offset1, qreal offset2, QDeclarativeAnchorLine::AnchorLine line, qreal &stretch); + bool isMirrored() const; void updateHorizontalAnchors(); void updateVerticalAnchors(); void fillChanged(); @@ -160,6 +161,8 @@ public: qreal vCenterOffset; qreal hCenterOffset; qreal baselineOffset; + + Qt::LayoutDirection layoutDirection; }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/qdeclarativeanchors/data/fill.qml b/tests/auto/declarative/qdeclarativeanchors/data/fill.qml index 50fbbe0..ff19675 100644 --- a/tests/auto/declarative/qdeclarativeanchors/data/fill.qml +++ b/tests/auto/declarative/qdeclarativeanchors/data/fill.qml @@ -6,9 +6,9 @@ Rectangle { objectName: "filler" width: 50; height: 50; color: "blue" anchors.fill: parent; - anchors.leftMargin: 10; - anchors.rightMargin: 20; - anchors.topMargin: 30; - anchors.bottomMargin: 40; + anchors.leftMargin: 10; + anchors.rightMargin: 20; + anchors.topMargin: 30; + anchors.bottomMargin: 40; } } diff --git a/tests/auto/declarative/qdeclarativeanchors/data/margins.qml b/tests/auto/declarative/qdeclarativeanchors/data/margins.qml index dace9c0..685346a 100644 --- a/tests/auto/declarative/qdeclarativeanchors/data/margins.qml +++ b/tests/auto/declarative/qdeclarativeanchors/data/margins.qml @@ -6,8 +6,8 @@ Rectangle { objectName: "filler" width: 50; height: 50; color: "blue" anchors.fill: parent; - anchors.margins: 10 - anchors.leftMargin: 5 - anchors.topMargin: 6 + anchors.margins: 10 + anchors.leftMargin: 5 + anchors.topMargin: 6 } } diff --git a/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp b/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp index e880857..79e233b 100644 --- a/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp +++ b/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp @@ -65,13 +65,10 @@ class tst_qdeclarativeanchors : public QObject public: tst_qdeclarativeanchors() {} - template - T *findItem(QGraphicsObject *parent, const QString &id); - QGraphicsObject *findObject(QGraphicsObject *parent, const QString &objectName); - private slots: void basicAnchors(); void basicAnchorsQGraphicsWidget(); + void basicAnchorsRTL(); void loops(); void illegalSets(); void illegalSets_data(); @@ -82,16 +79,20 @@ private slots: void nullItem_data(); void crash1(); void centerIn(); + void centerInRTL(); void hvCenter(); + void hvCenterRTL(); void fill(); + void fillRTL(); void margins(); + void marginsRTL(); }; /* Find an item with the specified id. */ template -T *tst_qdeclarativeanchors::findItem(QGraphicsObject *parent, const QString &objectName) +T *findItem(QGraphicsObject *parent, const QString &objectName) { const QMetaObject &mo = T::staticMetaObject; QList children = parent->childItems(); @@ -110,7 +111,7 @@ T *tst_qdeclarativeanchors::findItem(QGraphicsObject *parent, const QString &obj return 0; } -QGraphicsObject *tst_qdeclarativeanchors::findObject(QGraphicsObject *parent, const QString &objectName) +QGraphicsObject *findObject(QGraphicsObject *parent, const QString &objectName) { QList children = parent->childItems(); for (int i = 0; i < children.count(); ++i) { @@ -263,6 +264,95 @@ void tst_qdeclarativeanchors::basicAnchorsQGraphicsWidget() delete view; } +QDeclarativeItem* childItem(QDeclarativeItem *parentItem, const char * itemString) { + return findItem(parentItem, QLatin1String(itemString)); +} + +qreal offsetMasterRTL(QDeclarativeItem *rootItem, const char * itemString) { + QDeclarativeItem* masterItem = findItem(rootItem, QLatin1String("masterRect")); + return masterItem->width()+2*masterItem->x()-findItem(rootItem, QLatin1String(itemString))->width(); +} + +qreal offsetParentRTL(QDeclarativeItem *rootItem, const char * itemString) { + return rootItem->width()+2*rootItem->x()-findItem(rootItem, QLatin1String(itemString))->width(); +} + +void mirrorAnchors(QDeclarativeItem *item) { + QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item); + itemPrivate->anchors()->setLayoutDirection(Qt::RightToLeft); +} + +void tst_qdeclarativeanchors::basicAnchorsRTL() +{ + QDeclarativeView *view = new QDeclarativeView; + view->setSource(QUrl::fromLocalFile(SRCDIR "/data/anchors.qml")); + + qApp->processEvents(); + + QDeclarativeItem* rootItem = qobject_cast(view->rootObject()); + foreach(QObject *child, rootItem->children()) + mirrorAnchors(qobject_cast(child)); + + //sibling horizontal + QCOMPARE(childItem(rootItem, "rect1")->x(), offsetMasterRTL(rootItem, "rect1")-26.0); + QCOMPARE(childItem(rootItem, "rect2")->x(), offsetMasterRTL(rootItem, "rect2")-122.0); + QCOMPARE(childItem(rootItem, "rect3")->x(), offsetMasterRTL(rootItem, "rect3")-74.0); + QCOMPARE(childItem(rootItem, "rect4")->x(), offsetMasterRTL(rootItem, "rect4")-16.0); + QCOMPARE(childItem(rootItem, "rect5")->x(), offsetMasterRTL(rootItem, "rect5")-112.0); + QCOMPARE(childItem(rootItem, "rect6")->x(), offsetMasterRTL(rootItem, "rect6")-64.0); + + //parent horizontal + QCOMPARE(childItem(rootItem, "rect7")->x(), offsetParentRTL(rootItem, "rect7")-0.0); + QCOMPARE(childItem(rootItem, "rect8")->x(), offsetParentRTL(rootItem, "rect8")-240.0); + QCOMPARE(childItem(rootItem, "rect9")->x(), offsetParentRTL(rootItem, "rect9")-120.0); + QCOMPARE(childItem(rootItem, "rect10")->x(), offsetParentRTL(rootItem, "rect10")+10.0); + QCOMPARE(childItem(rootItem, "rect11")->x(), offsetParentRTL(rootItem, "rect11")-230.0); + QCOMPARE(childItem(rootItem, "rect12")->x(), offsetParentRTL(rootItem, "rect12")-110.0); + + //vertical + QCOMPARE(childItem(rootItem, "rect13")->y(), 20.0); + QCOMPARE(childItem(rootItem, "rect14")->y(), 155.0); + + //stretch + QCOMPARE(childItem(rootItem, "rect15")->x(), offsetMasterRTL(rootItem, "rect15")-26.0); + QCOMPARE(childItem(rootItem, "rect15")->width(), 96.0); + QCOMPARE(childItem(rootItem, "rect16")->x(), offsetMasterRTL(rootItem, "rect16")-26.0); + QCOMPARE(childItem(rootItem, "rect16")->width(), 192.0); + QCOMPARE(childItem(rootItem, "rect17")->x(), offsetMasterRTL(rootItem, "rect17")+70.0); + QCOMPARE(childItem(rootItem, "rect17")->width(), 192.0); + + //vertical stretch + QCOMPARE(childItem(rootItem, "rect18")->y(), 20.0); + QCOMPARE(childItem(rootItem, "rect18")->height(), 40.0); + + //more parent horizontal + QCOMPARE(childItem(rootItem, "rect19")->x(), offsetParentRTL(rootItem, "rect19")-115.0); + QCOMPARE(childItem(rootItem, "rect20")->x(), offsetParentRTL(rootItem, "rect20")-235.0); + QCOMPARE(childItem(rootItem, "rect21")->x(), offsetParentRTL(rootItem, "rect21")+5.0); + + //centerIn + QCOMPARE(childItem(rootItem, "rect22")->x(), offsetMasterRTL(rootItem, "rect22")-69.0); + QCOMPARE(childItem(rootItem, "rect22")->y(), 5.0); + + //margins + QCOMPARE(childItem(rootItem, "rect23")->x(), offsetMasterRTL(rootItem, "rect23")-31.0); + QCOMPARE(childItem(rootItem, "rect23")->y(), 5.0); + QCOMPARE(childItem(rootItem, "rect23")->width(), 86.0); + QCOMPARE(childItem(rootItem, "rect23")->height(), 10.0); + + // offsets + QCOMPARE(childItem(rootItem, "rect24")->x(), offsetMasterRTL(rootItem, "rect24")-26.0); + QCOMPARE(childItem(rootItem, "rect25")->y(), 60.0); + QCOMPARE(childItem(rootItem, "rect26")->y(), 5.0); + + //baseline + QDeclarativeText *text1 = findItem(rootItem, QLatin1String("text1")); + QDeclarativeText *text2 = findItem(rootItem, QLatin1String("text2")); + QCOMPARE(text1->y(), text2->y()); + + delete view; +} + // mostly testing that we don't crash void tst_qdeclarativeanchors::loops() { @@ -514,6 +604,31 @@ void tst_qdeclarativeanchors::fill() delete view; } +void tst_qdeclarativeanchors::fillRTL() +{ + QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/fill.qml")); + + qApp->processEvents(); + QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("filler")); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + mirrorAnchors(rect); + + QCOMPARE(rect->x(), 0.0 + 20.0); + QCOMPARE(rect->y(), 0.0 + 30.0); + QCOMPARE(rect->width(), 200.0 - 10.0 - 20.0); + QCOMPARE(rect->height(), 200.0 - 30.0 - 40.0); + //Alter Offsets (tests QTBUG-6631) + rectPrivate->anchors()->setLeftMargin(20.0); + rectPrivate->anchors()->setRightMargin(0.0); + rectPrivate->anchors()->setBottomMargin(0.0); + rectPrivate->anchors()->setTopMargin(10.0); + QCOMPARE(rect->x(), 0.0 + 0.0); + QCOMPARE(rect->y(), 0.0 + 10.0); + QCOMPARE(rect->width(), 200.0 - 20.0); + QCOMPARE(rect->height(), 200.0 - 10.0); + + delete view; +} void tst_qdeclarativeanchors::centerIn() { QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/centerin.qml")); @@ -521,6 +636,7 @@ void tst_qdeclarativeanchors::centerIn() qApp->processEvents(); QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("centered")); QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + QCOMPARE(rect->x(), 75.0 + 10); QCOMPARE(rect->y(), 75.0 + 30); //Alter Offsets (tests QTBUG-6631) @@ -532,6 +648,27 @@ void tst_qdeclarativeanchors::centerIn() delete view; } + +void tst_qdeclarativeanchors::centerInRTL() +{ + QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/centerin.qml")); + + qApp->processEvents(); + QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("centered")); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + mirrorAnchors(rect); + + QCOMPARE(rect->x(), 75.0 - 10); + QCOMPARE(rect->y(), 75.0 + 30); + //Alter Offsets (tests QTBUG-6631) + rectPrivate->anchors()->setHorizontalCenterOffset(-20.0); + rectPrivate->anchors()->setVerticalCenterOffset(-10.0); + QCOMPARE(rect->x(), 75.0 + 20.0); + QCOMPARE(rect->y(), 75.0 - 10.0); + + delete view; +} + void tst_qdeclarativeanchors::hvCenter() { QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/hvCenter.qml")); @@ -539,12 +676,39 @@ void tst_qdeclarativeanchors::hvCenter() qApp->processEvents(); QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("centered")); QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + // test QTBUG-10999 QCOMPARE(rect->x(), 10.0); QCOMPARE(rect->y(), 19.0); + + rectPrivate->anchors()->setHorizontalCenterOffset(-5.0); + rectPrivate->anchors()->setVerticalCenterOffset(5.0); + QCOMPARE(rect->x(), 10.0 - 5.0); + QCOMPARE(rect->y(), 19.0 + 5.0); + delete view; } +void tst_qdeclarativeanchors::hvCenterRTL() +{ + QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/hvCenter.qml")); + + qApp->processEvents(); + QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("centered")); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + mirrorAnchors(rect); + + // test QTBUG-10999 + QCOMPARE(rect->x(), 10.0); + QCOMPARE(rect->y(), 19.0); + + rectPrivate->anchors()->setHorizontalCenterOffset(-5.0); + rectPrivate->anchors()->setVerticalCenterOffset(5.0); + QCOMPARE(rect->x(), 10.0 + 5.0); + QCOMPARE(rect->y(), 19.0 + 5.0); + + delete view; +} void tst_qdeclarativeanchors::margins() { QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); @@ -568,6 +732,31 @@ void tst_qdeclarativeanchors::margins() delete view; } +void tst_qdeclarativeanchors::marginsRTL() +{ + QDeclarativeView *view = new QDeclarativeView(QUrl::fromLocalFile(SRCDIR "/data/margins.qml")); + + QDeclarativeRectangle* rect = findItem(view->rootObject(), QLatin1String("filler")); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + mirrorAnchors(rect); + + QCOMPARE(rect->x(), 10.0); + QCOMPARE(rect->y(), 6.0); + QCOMPARE(rect->width(), 200.0 - 5.0 - 10.0); + QCOMPARE(rect->height(), 200.0 - 6.0 - 10.0); + + rectPrivate->anchors()->setTopMargin(0.0); + rectPrivate->anchors()->setMargins(20.0); + + QCOMPARE(rect->x(), 20.0); + QCOMPARE(rect->y(), 20.0); + QCOMPARE(rect->width(), 200.0 - 5.0 - 20.0); + QCOMPARE(rect->height(), 200.0 - 20.0 - 20.0); + + delete view; +} + + QTEST_MAIN(tst_qdeclarativeanchors) #include "tst_qdeclarativeanchors.moc" diff --git a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp index 56bed30..2220b6d 100644 --- a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp +++ b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp @@ -120,6 +120,9 @@ private slots: void anchorChanges3(); void anchorChanges4(); void anchorChanges5(); + void anchorChangesRTL(); + void anchorChangesRTL2(); + void anchorChangesRTL3(); void anchorChangesCrash(); void anchorRewindBug(); void anchorRewindBug2(); @@ -813,6 +816,125 @@ void tst_qdeclarativestates::anchorChanges5() delete rect; } +void mirrorAnchors(QDeclarativeItem *item) { + QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item); + itemPrivate->anchors()->setLayoutDirection(Qt::RightToLeft); +} + +qreal offsetRTL(QDeclarativeItem *anchorItem, QDeclarativeItem *item) { + return anchorItem->width()+2*anchorItem->x()-item->width(); +} + +void tst_qdeclarativestates::anchorChangesRTL() +{ + QDeclarativeEngine engine; + + QDeclarativeComponent rectComponent(&engine, SRCDIR "/data/anchorChanges1.qml"); + QDeclarativeRectangle *rect = qobject_cast(rectComponent.create()); + QVERIFY(rect != 0); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + + QDeclarativeRectangle *innerRect = qobject_cast(rect->findChild("MyRect")); + QVERIFY(innerRect != 0); + mirrorAnchors(innerRect); + + QDeclarativeListReference list(rect, "states"); + QDeclarativeState *state = qobject_cast(list.at(0)); + QVERIFY(state != 0); + + qmlExecuteDeferred(state); + QDeclarativeAnchorChanges *aChanges = qobject_cast(state->operationAt(0)); + QVERIFY(aChanges != 0); + + rectPrivate->setState("right"); + QCOMPARE(innerRect->x(), offsetRTL(rect, innerRect) - qreal(150)); + QCOMPARE(aChanges->object(), qobject_cast(innerRect)); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->left().anchorLine, QDeclarativeAnchorLine::Invalid); //### was reset (how do we distinguish from not set at all) + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->right().item, rectPrivate->right().item); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->right().anchorLine, rectPrivate->right().anchorLine); + + rectPrivate->setState(""); + QCOMPARE(innerRect->x(), offsetRTL(rect, innerRect) -qreal(5)); + + delete rect; +} + +void tst_qdeclarativestates::anchorChangesRTL2() +{ + QDeclarativeEngine engine; + + QDeclarativeComponent rectComponent(&engine, SRCDIR "/data/anchorChanges2.qml"); + QDeclarativeRectangle *rect = qobject_cast(rectComponent.create()); + QVERIFY(rect != 0); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + + QDeclarativeRectangle *innerRect = qobject_cast(rect->findChild("MyRect")); + QVERIFY(innerRect != 0); + mirrorAnchors(innerRect); + + rectPrivate->setState("right"); + QCOMPARE(innerRect->x(), offsetRTL(rect, innerRect) - qreal(150)); + + rectPrivate->setState(""); + QCOMPARE(innerRect->x(), offsetRTL(rect, innerRect) - qreal(5)); + + delete rect; +} + +void tst_qdeclarativestates::anchorChangesRTL3() +{ + QDeclarativeEngine engine; + + QDeclarativeComponent rectComponent(&engine, SRCDIR "/data/anchorChanges3.qml"); + QDeclarativeRectangle *rect = qobject_cast(rectComponent.create()); + QVERIFY(rect != 0); + QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect); + + QDeclarativeRectangle *innerRect = qobject_cast(rect->findChild("MyRect")); + QVERIFY(innerRect != 0); + mirrorAnchors(innerRect); + + QDeclarativeItem *leftGuideline = qobject_cast(rect->findChild("LeftGuideline")); + QVERIFY(leftGuideline != 0); + + QDeclarativeItem *bottomGuideline = qobject_cast(rect->findChild("BottomGuideline")); + QVERIFY(bottomGuideline != 0); + + QDeclarativeListReference list(rect, "states"); + QDeclarativeState *state = qobject_cast(list.at(0)); + QVERIFY(state != 0); + + qmlExecuteDeferred(state); + QDeclarativeAnchorChanges *aChanges = qobject_cast(state->operationAt(0)); + QVERIFY(aChanges != 0); + + rectPrivate->setState("reanchored"); + QCOMPARE(aChanges->object(), qobject_cast(innerRect)); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->left().item, QDeclarativeItemPrivate::get(leftGuideline)->left().item); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->left().anchorLine, QDeclarativeItemPrivate::get(leftGuideline)->left().anchorLine); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->right().item, rectPrivate->right().item); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->right().anchorLine, rectPrivate->right().anchorLine); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->top().item, rectPrivate->top().item); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->top().anchorLine, rectPrivate->top().anchorLine); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->bottom().item, QDeclarativeItemPrivate::get(bottomGuideline)->bottom().item); + QCOMPARE(QDeclarativeItemPrivate::get(aChanges->object())->anchors()->bottom().anchorLine, QDeclarativeItemPrivate::get(bottomGuideline)->bottom().anchorLine); + + QCOMPARE(innerRect->x(), offsetRTL(leftGuideline, innerRect) - qreal(10)); + QCOMPARE(innerRect->y(), qreal(0)); + // between left side of parent and leftGuideline.x: 10, which has width 0 + QCOMPARE(innerRect->width(), qreal(10)); + QCOMPARE(innerRect->height(), qreal(150)); + + rectPrivate->setState(""); + QCOMPARE(innerRect->x(), offsetRTL(rect, innerRect) - qreal(0)); + QCOMPARE(innerRect->y(), qreal(10)); + // between right side of parent and left side of rightGuideline.x: 150, which has width 0 + QCOMPARE(innerRect->width(), qreal(50)); + QCOMPARE(innerRect->height(), qreal(190)); + + delete rect; +} + //QTBUG-9609 void tst_qdeclarativestates::anchorChangesCrash() { diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.0.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.0.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.0.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.1.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.1.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.1.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.10.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.10.png new file mode 100644 index 0000000..1ccab41 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.10.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.11.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.11.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.11.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.12.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.12.png new file mode 100644 index 0000000..f25bd7c Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.12.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.2.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.2.png new file mode 100644 index 0000000..f25bd7c Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.2.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.3.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.3.png new file mode 100644 index 0000000..dad1de4 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.3.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.4.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.4.png new file mode 100644 index 0000000..cd4f23a Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.4.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.5.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.5.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.5.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.6.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.6.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.6.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.7.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.7.png new file mode 100644 index 0000000..f25bd7c Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.7.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.8.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.8.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.8.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.9.png b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.9.png new file mode 100644 index 0000000..160155e Binary files /dev/null and b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.9.png differ diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.qml b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.qml new file mode 100644 index 0000000..e858c11 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/data/reanchor.qml @@ -0,0 +1,1499 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "reanchor.0.png" + } + Frame { + msec: 32 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 48 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 64 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 80 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 96 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 112 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 128 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 144 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 160 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 176 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 192 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 208 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 224 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 240 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 256 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 272 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 288 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 304 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 320 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 336 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 352 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 368 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 384 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 400 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 416 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 432 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 448 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 464 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 480 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 496 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 512 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 528 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 544 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 560 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 576 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 592 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 608 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 624 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 640 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 656 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 672 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 688 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 704 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 720 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 736 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 752 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 768 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 784 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 800 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 816 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 832 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 848 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 864 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 880 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 896 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 912 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 928 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 944 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 960 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 976 + image: "reanchor.1.png" + } + Frame { + msec: 992 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1008 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1024 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1040 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1056 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Mouse { + type: 2 + button: 1 + buttons: 1 + x: 164; y: 196 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 1072 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1088 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1104 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1120 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1136 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 164; y: 196 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 1152 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 1168 + hash: "f7814217626627ce70ca0e9487354ba9" + } + Frame { + msec: 1184 + hash: "7825b2b77e441ca6f46dbca80c7fe602" + } + Frame { + msec: 1200 + hash: "0ac443a9946b0bcf8db768af7d16d51e" + } + Frame { + msec: 1216 + hash: "c943d5d46f0d527690f38a9c8bd7be51" + } + Frame { + msec: 1232 + hash: "38151db0c9964d33bcb2ff155ebd468c" + } + Frame { + msec: 1248 + hash: "0fb8c53587a95a12cced6d30018edec1" + } + Frame { + msec: 1264 + hash: "2c684a649652270a638aca41a80e327c" + } + Frame { + msec: 1280 + hash: "60dd5c448ef8b97ec13ad3140a584229" + } + Frame { + msec: 1296 + hash: "d564f28f9d528daca729db6fab163b6c" + } + Frame { + msec: 1312 + hash: "4c07b33632ec4f30ee31141099c15a88" + } + Frame { + msec: 1328 + hash: "9facfd27fa16ee9d493e7fb7bcfadbf8" + } + Frame { + msec: 1344 + hash: "fc0fbb8aac8f389841e615be1e7b06de" + } + Frame { + msec: 1360 + hash: "579c18fa201b5609276c761ffd42df33" + } + Frame { + msec: 1376 + hash: "5b3630c37acfc2599a5a8b2e11aaa34c" + } + Frame { + msec: 1392 + hash: "2c1ee8aca06dccf0d39287721bf76aa7" + } + Frame { + msec: 1408 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1424 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1440 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1456 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1472 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1488 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1504 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1520 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1536 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1552 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1568 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1584 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1600 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1616 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1632 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1648 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1664 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1680 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1696 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1712 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1728 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1744 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1760 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1776 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1792 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1808 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1824 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1840 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1856 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1872 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1888 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1904 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1920 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1936 + image: "reanchor.2.png" + } + Frame { + msec: 1952 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1968 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 1984 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2000 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2016 + hash: "c03bb338fff252a100b080366ac907b5" + } + Mouse { + type: 2 + button: 1 + buttons: 1 + x: 170; y: 120 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2032 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2048 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2064 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2080 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2096 + hash: "c03bb338fff252a100b080366ac907b5" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 170; y: 120 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2112 + hash: "c03bb338fff252a100b080366ac907b5" + } + Frame { + msec: 2128 + hash: "e9d7372c17ca1510eb15faff5d0794b2" + } + Frame { + msec: 2144 + hash: "60f897e2b9594c4b5c02ce2fbdf9ae3c" + } + Frame { + msec: 2160 + hash: "c35ead9a8e682e8f3c0a091d232310f7" + } + Frame { + msec: 2176 + hash: "272632b0568391022590edc09ea30e28" + } + Frame { + msec: 2192 + hash: "9d4cdb31b01e86a31627e3ff9bb64100" + } + Frame { + msec: 2208 + hash: "5ee65b0290721fe47508c6435c18554b" + } + Frame { + msec: 2224 + hash: "8dd65e1a9417318d793d2027de4fe6ae" + } + Frame { + msec: 2240 + hash: "bcce6d1fd7d2c1539ad9ac42c0552d5e" + } + Frame { + msec: 2256 + hash: "e01f5850113c178da3383406fe73d6e0" + } + Frame { + msec: 2272 + hash: "968fc6b2bf6b7d43e05254339cf6123f" + } + Frame { + msec: 2288 + hash: "30f25fdde31e13934e328fa1d2655ccb" + } + Frame { + msec: 2304 + hash: "f58a21e96037813c9dd7f933405c9b11" + } + Frame { + msec: 2320 + hash: "1fe42c887f2eaf7696fcf0b8b884d0fd" + } + Frame { + msec: 2336 + hash: "848a27b9e4f4c0bcc1a11d6dba7ce92b" + } + Frame { + msec: 2352 + hash: "ca92736257db83e39f54b04325201942" + } + Frame { + msec: 2368 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2384 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Mouse { + type: 2 + button: 1 + buttons: 1 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2400 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2416 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2432 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2448 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2464 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2480 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 2496 + hash: "9082504eee5e0c3cbef9fd9545f09dcb" + } + Frame { + msec: 2512 + hash: "dbe5169edb4400c74841a8af64e0949f" + } + Frame { + msec: 2528 + hash: "d588405fc5e2423cdb954c5624172209" + } + Frame { + msec: 2544 + hash: "ed2b273ea36fb7d8feaca4d5dae72f81" + } + Frame { + msec: 2560 + hash: "5249e4824eb169b5ee3f7fb52fe09aa7" + } + Frame { + msec: 2576 + hash: "2838eff2a1a299c9e47cf78be99172ca" + } + Frame { + msec: 2592 + hash: "c47f6a937a4a6ef045159d7ba04de8af" + } + Frame { + msec: 2608 + hash: "fd3bc1b9ba2629bccb0fec04deffcdad" + } + Frame { + msec: 2624 + hash: "54c9b8599a32ac95aff324977b34f7e6" + } + Frame { + msec: 2640 + hash: "cc5652a05828146cdc9c9b8430f5f59c" + } + Frame { + msec: 2656 + hash: "ce5815fb51a4bd697a2fde46084e118b" + } + Frame { + msec: 2672 + hash: "01dfd2604263f1fd24382ce876af10f9" + } + Frame { + msec: 2688 + hash: "45ea282d20ee9e345eb2cac8c22c42e0" + } + Frame { + msec: 2704 + hash: "afd26ac9776e57c94e4b52ebfeb7206c" + } + Frame { + msec: 2720 + hash: "97aeed321d4d92cb1ec236d2a98fbe9b" + } + Mouse { + type: 4 + button: 1 + buttons: 1 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2736 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 2752 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 2768 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 2784 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 2800 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2816 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 2832 + hash: "81b8228c6aeefe8072b7704f11e6707e" + } + Frame { + msec: 2848 + hash: "617e416bf117a51b756c90321ebb1449" + } + Frame { + msec: 2864 + hash: "656d8d5d54c9ee137aceb519aff72cce" + } + Frame { + msec: 2880 + hash: "94ba3b6f558c010cdd32f54cce436388" + } + Mouse { + type: 2 + button: 1 + buttons: 1 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 2896 + image: "reanchor.3.png" + } + Frame { + msec: 2912 + hash: "0bc822fdd4caac17aab80e8601d3a523" + } + Frame { + msec: 2928 + hash: "886d0407ac76d7344f7a314f07b3efff" + } + Frame { + msec: 2944 + hash: "eb6c46af5037f24348edbe0dda48fb62" + } + Frame { + msec: 2960 + hash: "1c578a1eeb67c6833241bcb3214f06fb" + } + Frame { + msec: 2976 + hash: "55f1631ef567217a5945b2a23c59b549" + } + Frame { + msec: 2992 + hash: "25fdd4d54ddb035b082dc3a0d0816114" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 134; y: 106 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 3008 + hash: "25fdd4d54ddb035b082dc3a0d0816114" + } + Frame { + msec: 3024 + hash: "efd61e7c1aaffec77bd3d2de6645b2c0" + } + Frame { + msec: 3040 + hash: "02ac5ca0fa7d2ec3903fccd5dc556fa5" + } + Frame { + msec: 3056 + hash: "daf52e45b8fc68f74e424554074678cc" + } + Frame { + msec: 3072 + hash: "9e2def87e83b0c4b9f26684665aa1e51" + } + Frame { + msec: 3088 + hash: "0e72fc762cc9a061e91692376d65d292" + } + Frame { + msec: 3104 + hash: "c5ac37e4a5250b35a4976bcb31505cca" + } + Frame { + msec: 3120 + hash: "eefe6bb7963c580c68198ee6098a36f4" + } + Frame { + msec: 3136 + hash: "7b78d77ac11b72d1fb827ebb66a04c8e" + } + Frame { + msec: 3152 + hash: "ce5815fb51a4bd697a2fde46084e118b" + } + Frame { + msec: 3168 + hash: "94ba3b6f558c010cdd32f54cce436388" + } + Frame { + msec: 3184 + hash: "61a56140e5a6a2bfcee5c6322b37e130" + } + Frame { + msec: 3200 + hash: "a67b22c0a966fe3fbe869497dc00960f" + } + Frame { + msec: 3216 + hash: "4edd212676ac93ae761039e80f989349" + } + Frame { + msec: 3232 + hash: "fea5797441d65625c400238f73d94807" + } + Frame { + msec: 3248 + hash: "23e9209ff0257343016cffdf7ea6571c" + } + Frame { + msec: 3264 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3280 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3296 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3312 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3328 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3344 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3360 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3376 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3392 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3408 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3424 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3440 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3456 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3472 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3488 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3504 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3520 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3536 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3552 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3568 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3584 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3600 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3616 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3632 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3648 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3664 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3680 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3696 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3712 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3728 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3744 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3760 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3776 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3792 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3808 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3824 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3840 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3856 + image: "reanchor.4.png" + } + Frame { + msec: 3872 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3888 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3904 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3920 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3936 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3952 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3968 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 3984 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4000 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4016 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4032 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4048 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4064 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4080 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4096 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4112 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4128 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4144 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4160 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4176 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Mouse { + type: 2 + button: 1 + buttons: 1 + x: 124; y: 113 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 4192 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4208 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4224 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4240 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4256 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Mouse { + type: 3 + button: 1 + buttons: 0 + x: 124; y: 113 + modifiers: 0 + sendToViewport: true + } + Frame { + msec: 4272 + hash: "3a1fc9be558078e35a9828e411847c19" + } + Frame { + msec: 4288 + hash: "81b8228c6aeefe8072b7704f11e6707e" + } + Frame { + msec: 4304 + hash: "617e416bf117a51b756c90321ebb1449" + } + Frame { + msec: 4320 + hash: "656d8d5d54c9ee137aceb519aff72cce" + } + Frame { + msec: 4336 + hash: "94ba3b6f558c010cdd32f54cce436388" + } + Frame { + msec: 4352 + hash: "5b0679ff3730cba4ac026e89c7811fbe" + } + Frame { + msec: 4368 + hash: "0bc822fdd4caac17aab80e8601d3a523" + } + Frame { + msec: 4384 + hash: "886d0407ac76d7344f7a314f07b3efff" + } + Frame { + msec: 4400 + hash: "eb6c46af5037f24348edbe0dda48fb62" + } + Frame { + msec: 4416 + hash: "1c578a1eeb67c6833241bcb3214f06fb" + } + Frame { + msec: 4432 + hash: "55f1631ef567217a5945b2a23c59b549" + } + Frame { + msec: 4448 + hash: "25fdd4d54ddb035b082dc3a0d0816114" + } + Frame { + msec: 4464 + hash: "295ea6ff4d3c2c7de0cfbc29b2bd2c38" + } + Frame { + msec: 4480 + hash: "26b978ab645c04731703bcf15ac34a11" + } + Frame { + msec: 4496 + hash: "0db4c2515b89506df51732c4b9bf75dc" + } + Frame { + msec: 4512 + hash: "3cf30f3a06e325e195a4a7dec1e04c01" + } + Frame { + msec: 4528 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4544 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4560 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4576 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4592 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4608 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4624 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4640 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4656 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4672 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4688 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4704 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4720 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4736 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4752 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4768 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4784 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4800 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4816 + image: "reanchor.5.png" + } + Frame { + msec: 4832 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4848 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4864 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4880 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4896 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4912 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4928 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4944 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4960 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4976 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 4992 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5008 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5024 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5040 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5056 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5072 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5088 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5104 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5120 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5136 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5152 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5168 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5184 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5200 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5216 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5232 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5248 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5264 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5280 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5296 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5312 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5328 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5344 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5360 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5376 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5392 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5408 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5424 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5440 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5456 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5472 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5488 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5504 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5520 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5536 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5552 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5568 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } + Frame { + msec: 5584 + hash: "0009d8bfdfaed2a4f05aacb7a7992234" + } +} diff --git a/tests/auto/declarative/qmlvisual/animation/reanchorRTL/reanchor.qml b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/reanchor.qml new file mode 100644 index 0000000..ba37737 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/animation/reanchorRTL/reanchor.qml @@ -0,0 +1,69 @@ +import QtQuick 1.1 + +Rectangle { + id: container + width: 200; height: 200 + Rectangle { + id: myRect + anchors.layoutDirection: Qt.RightToLeft + objectName: "MyRect" + color: "green"; + anchors.left: parent.left + anchors.right: rightGuideline.left + anchors.top: topGuideline.top + anchors.bottom: container.bottom + } + Item { id: leftGuideline; x: 10 } + Item { id: rightGuideline; x: 150 } + Item { id: topGuideline; y: 10 } + Item { id: bottomGuideline; y: 150 } + Item { id: topGuideline2; y: 50 } + Item { id: bottomGuideline2; y: 175 } + MouseArea { + id: wholeArea + anchors.fill: parent + onClicked: { + if (container.state == "") { + container.state = "reanchored"; + } else if (container.state == "reanchored") { + container.state = "reanchored2"; + } else if (container.state == "reanchored2") + container.state = "reanchored"; + } + } + + states: [ State { + name: "reanchored" + AnchorChanges { + target: myRect; + anchors.left: leftGuideline.left + anchors.right: container.right + anchors.top: container.top + anchors.bottom: bottomGuideline.bottom + } + }, State { + name: "reanchored2" + AnchorChanges { + target: myRect; + anchors.left: undefined + anchors.right: undefined + anchors.top: topGuideline2.top + anchors.bottom: bottomGuideline2.bottom + } + }] + + transitions: Transition { + AnchorAnimation { } + } + + MouseArea { + width: 50; height: 50 + anchors.right: parent.right + anchors.bottom: parent.bottom + onClicked: { + container.state = ""; + } + } + + state: "reanchored" +} -- cgit v0.12 From 0b0358732cafc34d4b6794200752ac9cb99de569 Mon Sep 17 00:00:00 2001 From: Christopher Ham Date: Tue, 15 Feb 2011 16:22:46 +1000 Subject: Righ-to-left support for GridView and ListView GridView and ListView can now be laid out right-to-left. The behaviour should be identical and mirrored to the oridinary behaviour. Change-Id: I8e55c5f88358042caa5201712ef239cd67628172 Task-number: QTBUG-15877 Reviewed-by: Joona Petrell --- .../graphicsitems/qdeclarativegridview.cpp | 535 ++++++++++++++++----- .../graphicsitems/qdeclarativegridview_p.h | 12 +- .../graphicsitems/qdeclarativelistview.cpp | 385 +++++++++++---- .../graphicsitems/qdeclarativelistview_p.h | 11 +- .../data/gridview-enforcerange.qml | 2 + .../qdeclarativegridview/data/gridview1.qml | 3 +- .../tst_qdeclarativegridview.cpp | 324 +++++++++++++ .../tst_qdeclarativelistview.cpp | 42 ++ 8 files changed, 1102 insertions(+), 212 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 7e7889c..39fa8e8 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -68,19 +68,60 @@ public: } ~FxGridItem() {} - qreal rowPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->y() : item->x()); } - qreal colPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->x() : item->y()); } + qreal rowPos() const { + qreal rowPos = 0; + if (view->flow() == QDeclarativeGridView::LeftToRight) { + rowPos = item->y(); + } else { + if (view->layoutDirection() == Qt::LeftToRight) + rowPos = item->x(); + else + rowPos = -view->cellWidth()-item->x(); + } + return rowPos; + } + qreal colPos() const { + qreal colPos = 0; + if (view->flow() == QDeclarativeGridView::LeftToRight) { + if (view->layoutDirection() == Qt::LeftToRight) { + colPos = item->x(); + } else { + int colSize = view->cellWidth(); + int columns = view->width()/colSize; + colPos = colSize * (columns-1) - item->x(); + } + } else { + colPos = item->y(); + } + + return colPos; + } + qreal endRowPos() const { - return view->flow() == QDeclarativeGridView::LeftToRight - ? item->y() + view->cellHeight() - 1 - : item->x() + view->cellWidth() - 1; + if (view->flow() == QDeclarativeGridView::LeftToRight) { + return item->y() + view->cellHeight() - 1; + } else { + if (view->layoutDirection() == Qt::LeftToRight) + return item->x() + view->cellWidth() - 1; + else + return -item->x() - 1; + } } void setPosition(qreal col, qreal row) { - if (view->flow() == QDeclarativeGridView::LeftToRight) { - item->setPos(QPointF(col, row)); + if (view->layoutDirection() == Qt::LeftToRight) { + if (view->flow() == QDeclarativeGridView::LeftToRight) + item->setPos(QPointF(col, row)); + else + item->setPos(QPointF(row, col)); } else { - item->setPos(QPointF(row, col)); + if (view->flow() == QDeclarativeGridView::LeftToRight) { + int columns = view->width()/view->cellWidth(); + item->setPos(QPointF((view->cellWidth() * (columns-1) - col), row)); + } else { + item->setPos(QPointF(-view->cellWidth()-row, col)); + } } + } bool contains(int x, int y) const { return (x >= item->x() && x < item->x() + view->cellWidth() && @@ -101,10 +142,12 @@ class QDeclarativeGridViewPrivate : public QDeclarativeFlickablePrivate public: QDeclarativeGridViewPrivate() - : currentItem(0), flow(QDeclarativeGridView::LeftToRight) + : currentItem(0), layoutDirection(Qt::LeftToRight), flow(QDeclarativeGridView::LeftToRight) , visibleIndex(0) , currentIndex(-1) , cellWidth(100), cellHeight(100), columns(1), requestedIndex(-1), itemCount(0) - , highlightRangeStart(0), highlightRangeEnd(0), highlightRange(QDeclarativeGridView::NoHighlightRange) + , highlightRangeStart(0), highlightRangeEnd(0) + , highlightRangeStartValid(false), highlightRangeEndValid(false) + , highlightRange(QDeclarativeGridView::NoHighlightRange) , highlightComponent(0), highlight(0), trackedItem(0) , moveReason(Other), buffer(0), highlightXAnimator(0), highlightYAnimator(0) , highlightMoveDuration(150) @@ -144,35 +187,54 @@ public: return 0; } + bool isRightToLeftTopToBottom() const { + return flow == QDeclarativeGridView::TopToBottom && layoutDirection == Qt::RightToLeft; + } + qreal position() const { Q_Q(const QDeclarativeGridView); return flow == QDeclarativeGridView::LeftToRight ? q->contentY() : q->contentX(); } void setPosition(qreal pos) { Q_Q(QDeclarativeGridView); - if (flow == QDeclarativeGridView::LeftToRight) + if (flow == QDeclarativeGridView::LeftToRight) { q->QDeclarativeFlickable::setContentY(pos); - else - q->QDeclarativeFlickable::setContentX(pos); + q->QDeclarativeFlickable::setContentX(0); + } else { + if (layoutDirection == Qt::LeftToRight) + q->QDeclarativeFlickable::setContentX(pos); + else + q->QDeclarativeFlickable::setContentX(-pos-size()); + q->QDeclarativeFlickable::setContentY(0); + } } int size() const { Q_Q(const QDeclarativeGridView); return flow == QDeclarativeGridView::LeftToRight ? q->height() : q->width(); } - qreal startPosition() const { + qreal originPosition() const { qreal pos = 0; if (!visibleItems.isEmpty()) pos = visibleItems.first()->rowPos() - visibleIndex / columns * rowSize(); return pos; } - qreal endPosition() const { + qreal lastPosition() const { qreal pos = 0; if (model && model->count()) pos = rowPosAt(model->count() - 1) + rowSize(); return pos; } + qreal startPosition() const { + return isRightToLeftTopToBottom() ? -lastPosition()+1 : originPosition(); + } + + qreal endPosition() const { + return isRightToLeftTopToBottom() ? -originPosition()+1 : lastPosition(); + + } + bool isValid() const { return model && model->count() && model->isValid(); } @@ -227,7 +289,7 @@ public: } FxGridItem *firstVisibleItem() const { - const qreal pos = position(); + const qreal pos = isRightToLeftTopToBottom() ? -position()-size() : position(); for (int i = 0; i < visibleItems.count(); ++i) { FxGridItem *item = visibleItems.at(i); if (item->index != -1 && item->endRowPos() > pos) @@ -237,15 +299,12 @@ public: } int lastVisibleIndex() const { - int lastIndex = -1; - for (int i = visibleItems.count()-1; i >= 0; --i) { - FxGridItem *gridItem = visibleItems.at(i); - if (gridItem->index != -1) { - lastIndex = gridItem->index; - break; - } + for (int i = 0; i < visibleItems.count(); ++i) { + FxGridItem *item = visibleItems.at(i); + if (item->index != -1) + return item->index; } - return lastIndex; + return -1; } // Map a model index to visibleItems list index. @@ -271,8 +330,15 @@ public: pos += rowSize()/2; snapPos = visibleItems.first()->rowPos() - visibleIndex / columns * rowSize(); snapPos = pos - fmodf(pos - snapPos, qreal(rowSize())); - qreal maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); - qreal minExtent = flow == QDeclarativeGridView::LeftToRight ? -q->minYExtent() : -q->minXExtent(); + qreal maxExtent; + qreal minExtent; + if (isRightToLeftTopToBottom()) { + maxExtent = q->minXExtent(); + minExtent = q->maxXExtent(); + } else { + maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); + minExtent = flow == QDeclarativeGridView::LeftToRight ? -q->minYExtent() : -q->minXExtent(); + } if (snapPos > maxExtent) snapPos = maxExtent; if (snapPos < minExtent) @@ -363,6 +429,7 @@ public: QList visibleItems; QHash unrequestedItems; FxGridItem *currentItem; + Qt::LayoutDirection layoutDirection; QDeclarativeGridView::Flow flow; int visibleIndex; int currentIndex; @@ -373,6 +440,8 @@ public: int itemCount; qreal highlightRangeStart; qreal highlightRangeEnd; + bool highlightRangeStartValid; + bool highlightRangeEndValid; QDeclarativeGridView::HighlightRangeMode highlightRange; QDeclarativeComponent *highlightComponent; FxGridItem *highlight; @@ -554,7 +623,7 @@ void QDeclarativeGridViewPrivate::refill(qreal from, qreal to, bool doBuffer) if (!lazyRelease || !changed || deferredRelease) { // avoid destroying items in the same frame that we create while (visibleItems.count() > 1 && (item = visibleItems.first()) - && item->endRowPos() < bufferFrom - rowSize()*(item->colPos()/colSize()+1)/(columns+1)) { + && item->rowPos()+rowSize()-1 < bufferFrom - rowSize()*(item->colPos()/colSize()+1)/(columns+1)) { if (item->attached->delayRemove()) break; // qDebug() << "refill: remove first" << visibleIndex << "top end pos" << item->endRowPos(); @@ -596,12 +665,14 @@ void QDeclarativeGridViewPrivate::refill(qreal from, qreal to, bool doBuffer) void QDeclarativeGridViewPrivate::updateGrid() { Q_Q(QDeclarativeGridView); + columns = (int)qMax((flow == QDeclarativeGridView::LeftToRight ? q->width() : q->height()) / colSize(), qreal(1.)); if (isValid()) { if (flow == QDeclarativeGridView::LeftToRight) q->setContentHeight(endPosition() - startPosition()); else - q->setContentWidth(endPosition() - startPosition()); + q->setContentWidth(lastPosition() - originPosition()); + setPosition(0); } } @@ -626,7 +697,7 @@ void QDeclarativeGridViewPrivate::layout() qreal rowPos = visibleItems.first()->rowPos(); qreal colPos = visibleItems.first()->colPos(); int col = visibleIndex % columns; - if (colPos != col * colSize()) { + if (colPos != col * colSize() || isRightToLeftTopToBottom()) { colPos = col * colSize(); visibleItems.first()->setPosition(colPos, rowPos); } @@ -669,10 +740,14 @@ void QDeclarativeGridViewPrivate::updateUnrequestedPositions() { QHash::const_iterator it; for (it = unrequestedItems.begin(); it != unrequestedItems.end(); ++it) { + QDeclarativeItem *item = it.key(); if (flow == QDeclarativeGridView::LeftToRight) { - it.key()->setPos(QPointF(colPosAt(*it), rowPosAt(*it))); + item->setPos(QPointF(colPosAt(*it), rowPosAt(*it))); } else { - it.key()->setPos(QPointF(rowPosAt(*it), colPosAt(*it))); + if (isRightToLeftTopToBottom()) + item->setPos(QPointF(-rowPosAt(*it)-item->width(), colPosAt(*it))); + else + item->setPos(QPointF(rowPosAt(*it), (columns-1)*colSize()-colPosAt(*it))); } } } @@ -837,23 +912,30 @@ void QDeclarativeGridViewPrivate::updateFooter() } } if (footer) { + qreal colOffset = 0; + qreal rowOffset; + if (isRightToLeftTopToBottom()) { + rowOffset = footer->item->width()-cellWidth; + } else { + rowOffset = 0; + if (layoutDirection == Qt::RightToLeft) + colOffset = footer->item->width()-cellWidth; + } if (visibleItems.count()) { - qreal endPos = endPosition(); + qreal endPos = lastPosition(); if (lastVisibleIndex() == model->count()-1) { - footer->setPosition(0, endPos); + footer->setPosition(colOffset, endPos + rowOffset); } else { - qreal visiblePos = position() + q->height(); - if (endPos <= visiblePos || footer->endRowPos() < endPos) - footer->setPosition(0, endPos); + qreal visiblePos = isRightToLeftTopToBottom() ? -position() : position() + size(); + if (endPos <= visiblePos || footer->endRowPos() < endPos + rowOffset) + footer->setPosition(colOffset, endPos + rowOffset); } } else { qreal endPos = 0; if (header) { - endPos += flow == QDeclarativeGridView::LeftToRight - ? header->item->height() - : header->item->width(); + endPos += flow == QDeclarativeGridView::LeftToRight ? header->item->height() : header->item->width(); } - footer->setPosition(0, endPos); + footer->setPosition(colOffset, endPos); } } } @@ -883,16 +965,27 @@ void QDeclarativeGridViewPrivate::updateHeader() } } if (header) { + qreal colOffset = 0; + qreal rowOffset; + if (isRightToLeftTopToBottom()) { + rowOffset = -cellWidth; + } else { + rowOffset = -headerSize(); + if (layoutDirection == Qt::RightToLeft) + colOffset = header->item->width()-cellWidth; + } if (visibleItems.count()) { - qreal startPos = startPosition(); + qreal startPos = originPosition(); if (visibleIndex == 0) { - header->setPosition(0, startPos - headerSize()); + header->setPosition(colOffset, startPos + rowOffset); } else { - if (position() <= startPos || header->rowPos() > startPos - headerSize()) - header->setPosition(0, startPos - headerSize()); + qreal tempPos = isRightToLeftTopToBottom() ? -position()-size() : position(); + qreal headerPos = isRightToLeftTopToBottom() ? header->rowPos() + cellWidth - headerSize() : header->rowPos(); + if (tempPos <= startPos || headerPos > startPos + rowOffset) + header->setPosition(colOffset, startPos + rowOffset); } } else { - header->setPosition(0, 0); + header->setPosition(colOffset, 0); } } } @@ -915,21 +1008,44 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m int oldDuration = fixupDuration; fixupDuration = moveReason == Mouse ? fixupDuration : 0; + qreal highlightStart; + qreal highlightEnd; + qreal viewPos; + if (isRightToLeftTopToBottom()) { + // Handle Right-To-Left exceptions + viewPos = -position()-size(); + highlightStart = highlightRangeStartValid ? size()-highlightRangeEnd : highlightRangeStart; + highlightEnd = highlightRangeEndValid ? size()-highlightRangeStart : highlightRangeEnd; + } else { + viewPos = position(); + highlightStart = highlightRangeStart; + highlightEnd = highlightRangeEnd; + } + if (snapMode != QDeclarativeGridView::NoSnap) { - FxGridItem *topItem = snapItemAt(position()+highlightRangeStart); - FxGridItem *bottomItem = snapItemAt(position()+highlightRangeEnd); + qreal tempPosition = isRightToLeftTopToBottom() ? -position()-size() : position(); + FxGridItem *topItem = snapItemAt(tempPosition+highlightStart); + FxGridItem *bottomItem = snapItemAt(tempPosition+highlightEnd); qreal pos; if (topItem && bottomItem && haveHighlightRange && highlightRange == QDeclarativeGridView::StrictlyEnforceRange) { - qreal topPos = qMin(topItem->rowPos() - highlightRangeStart, -maxExtent); - qreal bottomPos = qMax(bottomItem->rowPos() - highlightRangeEnd, -minExtent); + qreal topPos = qMin(topItem->rowPos() - highlightStart, -maxExtent); + qreal bottomPos = qMax(bottomItem->rowPos() - highlightEnd, -minExtent); pos = qAbs(data.move + topPos) < qAbs(data.move + bottomPos) ? topPos : bottomPos; } else if (topItem) { - if (topItem->index == 0 && header && position()+highlightRangeStart < header->rowPos()+headerSize()/2) - pos = header->rowPos() - highlightRangeStart; - else - pos = qMax(qMin(topItem->rowPos() - highlightRangeStart, -maxExtent), -minExtent); + qreal headerPos = isRightToLeftTopToBottom() ? header->rowPos() + cellWidth - headerSize() : header->rowPos(); + if (topItem->index == 0 && header && tempPosition+highlightStart < headerPos+headerSize()/2) { + pos = isRightToLeftTopToBottom() ? - headerPos + highlightStart - size() : headerPos - highlightStart; + } else { + if (isRightToLeftTopToBottom()) + pos = qMax(qMin(-topItem->rowPos() + highlightStart - size(), -maxExtent), -minExtent); + else + pos = qMax(qMin(topItem->rowPos() - highlightStart, -maxExtent), -minExtent); + } } else if (bottomItem) { - pos = qMax(qMin(bottomItem->rowPos() - highlightRangeStart, -maxExtent), -minExtent); + if (isRightToLeftTopToBottom()) + pos = qMax(qMin(-bottomItem->rowPos() + highlightStart - size(), -maxExtent), -minExtent); + else + pos = qMax(qMin(bottomItem->rowPos() - highlightStart, -maxExtent), -minExtent); } else { QDeclarativeFlickablePrivate::fixup(data, minExtent, maxExtent); fixupDuration = oldDuration; @@ -938,12 +1054,15 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m if (currentItem && haveHighlightRange && highlightRange == QDeclarativeGridView::StrictlyEnforceRange) { updateHighlight(); qreal currPos = currentItem->rowPos(); - if (pos < currPos + rowSize() - highlightRangeEnd) - pos = currPos + rowSize() - highlightRangeEnd; - if (pos > currPos - highlightRangeStart) - pos = currPos - highlightRangeStart; + if (isRightToLeftTopToBottom()) + pos = -pos-size(); // Transform Pos if required + if (pos < currPos + rowSize() - highlightEnd) + pos = currPos + rowSize() - highlightEnd; + if (pos > currPos - highlightStart) + pos = currPos - highlightStart; + if (isRightToLeftTopToBottom()) + pos = -pos-size(); // Untransform } - qreal dist = qAbs(data.move + pos); if (dist > 0) { timeline.reset(data.move); @@ -957,12 +1076,12 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m if (currentItem) { updateHighlight(); qreal pos = currentItem->rowPos(); - qreal viewPos = position(); - if (viewPos < pos + rowSize() - highlightRangeEnd) - viewPos = pos + rowSize() - highlightRangeEnd; - if (viewPos > pos - highlightRangeStart) - viewPos = pos - highlightRangeStart; - + if (viewPos < pos + rowSize() - highlightEnd) + viewPos = pos + rowSize() - highlightEnd; + if (viewPos > pos - highlightStart) + viewPos = pos - highlightStart; + if (isRightToLeftTopToBottom()) + viewPos = -viewPos-size(); timeline.reset(data.move); if (viewPos != position()) { if (fixupDuration) @@ -989,12 +1108,14 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m return; } qreal maxDistance = 0; - // -ve velocity means list is moving up + qreal dataValue = isRightToLeftTopToBottom() ? -data.move.value()+size() : data.move.value(); + // -ve velocity means list is moving up/left if (velocity > 0) { if (data.move.value() < minExtent) { if (snapMode == QDeclarativeGridView::SnapOneRow) { - if (FxGridItem *item = firstVisibleItem()) - maxDistance = qAbs(item->rowPos() + data.move.value()); + if (FxGridItem *item = firstVisibleItem()) { + maxDistance = qAbs(item->rowPos() + dataValue); + } } else { maxDistance = qAbs(minExtent - data.move.value()); } @@ -1004,8 +1125,8 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m } else { if (data.move.value() > maxExtent) { if (snapMode == QDeclarativeGridView::SnapOneRow) { - qreal pos = snapPosAt(-data.move.value()) + rowSize(); - maxDistance = qAbs(pos + data.move.value()); + qreal pos = snapPosAt(-dataValue) + (isRightToLeftTopToBottom() ? 0 : rowSize()); + maxDistance = qAbs(pos + dataValue); } else { maxDistance = qAbs(maxExtent - data.move.value()); } @@ -1013,7 +1134,10 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m if (snapMode == QDeclarativeGridView::NoSnap && highlightRange != QDeclarativeGridView::StrictlyEnforceRange) data.flickTarget = maxExtent; } + bool overShoot = boundsBehavior == QDeclarativeFlickable::DragAndOvershootBounds; + qreal highlightStart = isRightToLeftTopToBottom() && highlightRangeStartValid ? size()-highlightRangeEnd : highlightRangeStart; + if (maxDistance > 0 || overShoot) { // This mode requires the grid to stop exactly on a row boundary. qreal v = velocity; @@ -1032,7 +1156,9 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m dist = qMin(dist, maxDistance); if (v > 0) dist = -dist; - data.flickTarget = -snapPosAt(-(data.move.value() - highlightRangeStart) + dist) + highlightRangeStart; + qreal distTemp = isRightToLeftTopToBottom() ? -dist : dist; + data.flickTarget = -snapPosAt(-(dataValue - highlightStart) + distTemp) + highlightStart; + data.flickTarget = isRightToLeftTopToBottom() ? -data.flickTarget+size() : data.flickTarget; qreal adjDist = -data.flickTarget + data.move.value(); if (qAbs(adjDist) > qAbs(dist)) { // Prevent painfully slow flicking - adjust velocity to suit flickDeceleration @@ -1224,6 +1350,13 @@ QVariant QDeclarativeGridView::model() const return d->modelVariant; } +// For internal use +int QDeclarativeGridView::modelCount() const +{ + Q_D(const QDeclarativeGridView); + return d->model->count(); +} + void QDeclarativeGridView::setModel(const QVariant &model) { Q_D(QDeclarativeGridView); @@ -1549,6 +1682,7 @@ qreal QDeclarativeGridView::preferredHighlightBegin() const void QDeclarativeGridView::setPreferredHighlightBegin(qreal start) { Q_D(QDeclarativeGridView); + d->highlightRangeStartValid = true; if (d->highlightRangeStart == start) return; d->highlightRangeStart = start; @@ -1556,6 +1690,16 @@ void QDeclarativeGridView::setPreferredHighlightBegin(qreal start) emit preferredHighlightBeginChanged(); } +void QDeclarativeGridView::resetPreferredHighlightBegin() +{ + Q_D(QDeclarativeGridView); + d->highlightRangeStartValid = false; + if (d->highlightRangeStart == 0) + return; + d->highlightRangeStart = 0; + emit preferredHighlightBeginChanged(); +} + qreal QDeclarativeGridView::preferredHighlightEnd() const { Q_D(const QDeclarativeGridView); @@ -1565,6 +1709,7 @@ qreal QDeclarativeGridView::preferredHighlightEnd() const void QDeclarativeGridView::setPreferredHighlightEnd(qreal end) { Q_D(QDeclarativeGridView); + d->highlightRangeEndValid = true; if (d->highlightRangeEnd == end) return; d->highlightRangeEnd = end; @@ -1572,6 +1717,16 @@ void QDeclarativeGridView::setPreferredHighlightEnd(qreal end) emit preferredHighlightEndChanged(); } +void QDeclarativeGridView::resetPreferredHighlightEnd() +{ + Q_D(QDeclarativeGridView); + d->highlightRangeEndValid = false; + if (d->highlightRangeEnd == 0) + return; + d->highlightRangeEnd = 0; + emit preferredHighlightEndChanged(); +} + QDeclarativeGridView::HighlightRangeMode QDeclarativeGridView::highlightRangeMode() const { Q_D(const QDeclarativeGridView); @@ -1588,6 +1743,43 @@ void QDeclarativeGridView::setHighlightRangeMode(HighlightRangeMode mode) emit highlightRangeModeChanged(); } +/*! + \qmlproperty enumeration GridView::layoutDirection + This property holds the layout direction of the grid. + + Possible values: + + \list + \o Qt.LeftToRight (default) - Items will be laid out starting in the top, left corner. The flow is + dependent on the \l GridView::flow property. + \o Qt.RightToLeft - Items will be laid out starting in the top, right corner. The flow is dependent + on the \l GridView:flow property. + \endlist + + \bold Note: If GridView::flow is set to GridView.LeftToRight, this is not to be confused if + GridView::layoutDirection is set to Qt.RightToLeft. The GridView.LeftToRight flow value simply + indicates that the flow is horizontal. + +*/ + +Qt::LayoutDirection QDeclarativeGridView::layoutDirection() const +{ + Q_D(const QDeclarativeGridView); + return d->layoutDirection; +} + +void QDeclarativeGridView::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + Q_D(QDeclarativeGridView); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; + d->clear(); + d->updateGrid(); + refill(); + d->updateCurrent(d->currentIndex); + emit layoutDirectionChanged(); + } +} /*! \qmlproperty enumeration GridView::flow @@ -1890,11 +2082,23 @@ void QDeclarativeGridView::viewportMoved() if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) { // reposition highlight qreal pos = d->highlight->rowPos(); - qreal viewPos = d->position(); - if (pos > viewPos + d->highlightRangeEnd - d->rowSize()) - pos = viewPos + d->highlightRangeEnd - d->rowSize(); - if (pos < viewPos + d->highlightRangeStart) - pos = viewPos + d->highlightRangeStart; + qreal viewPos; + qreal highlightStart; + qreal highlightEnd; + if (d->isRightToLeftTopToBottom()) { + highlightStart = d->highlightRangeStartValid ? d->size()-d->highlightRangeEnd : d->highlightRangeStart; + highlightEnd = d->highlightRangeEndValid ? d->size()-d->highlightRangeStart : d->highlightRangeEnd; + viewPos = -d->position()-d->size(); + } else { + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + viewPos = d->position(); + } + if (pos > viewPos + highlightEnd - d->rowSize()) + pos = viewPos + highlightEnd - d->rowSize(); + if (pos < viewPos + highlightStart) + pos = viewPos + highlightStart; + d->highlight->setPosition(d->highlight->colPos(), qRound(pos)); // update current index @@ -1956,11 +2160,27 @@ qreal QDeclarativeGridView::minXExtent() const if (d->flow == QDeclarativeGridView::LeftToRight) return QDeclarativeFlickable::minXExtent(); qreal extent = -d->startPosition(); - if (d->header && d->visibleItems.count()) - extent += d->header->item->width(); + qreal highlightStart; + qreal highlightEnd; + qreal endPositionFirstItem; + if (d->isRightToLeftTopToBottom()) { + endPositionFirstItem = d->rowPosAt(d->model->count()-1); + highlightStart = d->highlightRangeStartValid + ? d->highlightRangeStart - (d->lastPosition()-endPositionFirstItem) + : d->size() - (d->lastPosition()-endPositionFirstItem); + highlightEnd = d->highlightRangeEndValid ? d->highlightRangeEnd : d->size(); + if (d->footer && d->visibleItems.count()) + extent += d->footer->item->width(); + } else { + endPositionFirstItem = d->rowPosAt(0)+d->rowSize(); + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + if (d->header && d->visibleItems.count()) + extent += d->header->item->width(); + } if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { - extent += d->highlightRangeStart; - extent = qMax(extent, -(d->rowPosAt(0) + d->rowSize() - d->highlightRangeEnd)); + extent += highlightStart; + extent = qMax(extent, -(endPositionFirstItem - highlightEnd)); } return extent; } @@ -1973,15 +2193,36 @@ qreal QDeclarativeGridView::maxXExtent() const qreal extent; if (!d->model || !d->model->count()) { extent = 0; - } if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { - extent = -(d->rowPosAt(d->model->count()-1) - d->highlightRangeStart); - if (d->highlightRangeEnd != d->highlightRangeStart) - extent = qMin(extent, -(d->endPosition() - d->highlightRangeEnd + 1)); + } + qreal highlightStart; + qreal highlightEnd; + qreal lastItemPosition; + if (d->isRightToLeftTopToBottom()){ + highlightStart = d->highlightRangeStartValid ? d->highlightRangeEnd : d->size(); + highlightEnd = d->highlightRangeEndValid ? d->highlightRangeStart : d->size(); + lastItemPosition = d->endPosition(); + } else { + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + lastItemPosition = d->rowPosAt(d->model->count()-1); + } + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { + extent = -(lastItemPosition - highlightStart); + if (highlightEnd != highlightStart) + extent = d->isRightToLeftTopToBottom() + ? qMax(extent, -(d->endPosition() - highlightEnd + 1)) + : qMin(extent, -(d->endPosition() - highlightEnd + 1)); } else { extent = -(d->endPosition() - width()); } - if (d->footer) - extent -= d->footer->item->width(); + if (d->isRightToLeftTopToBottom()) { + if (d->header) + extent -= d->header->item->width(); + } else { + if (d->footer) + extent -= d->footer->item->width(); + } + const qreal minX = minXExtent(); if (extent > minX) extent = minX; @@ -2094,15 +2335,30 @@ void QDeclarativeGridView::moveCurrentIndexLeft() const int count = d->model ? d->model->count() : 0; if (!count) return; - if (d->flow == QDeclarativeGridView::LeftToRight) { - if (currentIndex() > 0 || d->wrap) { - int index = currentIndex() - 1; - setCurrentIndex((index >= 0 && index < count) ? index : count-1); + + if (d->layoutDirection == Qt::LeftToRight) { + if (d->flow == QDeclarativeGridView::LeftToRight) { + if (currentIndex() > 0 || d->wrap) { + int index = currentIndex() - 1; + setCurrentIndex((index >= 0 && index < count) ? index : count-1); + } + } else { + if (currentIndex() >= d->columns || d->wrap) { + int index = currentIndex() - d->columns; + setCurrentIndex((index >= 0 && index < count) ? index : count-1); + } } } else { - if (currentIndex() >= d->columns || d->wrap) { - int index = currentIndex() - d->columns; - setCurrentIndex((index >= 0 && index < count) ? index : count-1); + if (d->flow == QDeclarativeGridView::LeftToRight) { + if (currentIndex() < count - 1 || d->wrap) { + int index = currentIndex() + 1; + setCurrentIndex((index >= 0 && index < count) ? index : 0); + } + } else { + if (currentIndex() < count - d->columns || d->wrap) { + int index = currentIndex() + d->columns; + setCurrentIndex((index >= 0 && index < count) ? index : 0); + } } } } @@ -2122,15 +2378,30 @@ void QDeclarativeGridView::moveCurrentIndexRight() const int count = d->model ? d->model->count() : 0; if (!count) return; - if (d->flow == QDeclarativeGridView::LeftToRight) { - if (currentIndex() < count - 1 || d->wrap) { - int index = currentIndex() + 1; - setCurrentIndex((index >= 0 && index < count) ? index : 0); + + if (d->layoutDirection == Qt::LeftToRight) { + if (d->flow == QDeclarativeGridView::LeftToRight) { + if (currentIndex() < count - 1 || d->wrap) { + int index = currentIndex() + 1; + setCurrentIndex((index >= 0 && index < count) ? index : 0); + } + } else { + if (currentIndex() < count - d->columns || d->wrap) { + int index = currentIndex()+d->columns; + setCurrentIndex((index >= 0 && index < count) ? index : 0); + } } } else { - if (currentIndex() < count - d->columns || d->wrap) { - int index = currentIndex()+d->columns; - setCurrentIndex((index >= 0 && index < count) ? index : 0); + if (d->flow == QDeclarativeGridView::LeftToRight) { + if (currentIndex() > 0 || d->wrap) { + int index = currentIndex() - 1; + setCurrentIndex((index >= 0 && index < count) ? index : count-1); + } + } else { + if (currentIndex() >= d->columns || d->wrap) { + int index = currentIndex() - d->columns; + setCurrentIndex((index >= 0 && index < count) ? index : count-1); + } } } } @@ -2147,16 +2418,24 @@ void QDeclarativeGridViewPrivate::positionViewAtIndex(int index, int mode) if (layoutScheduled) layout(); - qreal pos = position(); + qreal pos = isRightToLeftTopToBottom() ? -position() - size() : position(); FxGridItem *item = visibleItem(idx); - qreal maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); + qreal maxExtent; + if (flow == QDeclarativeGridView::LeftToRight) + maxExtent = -q->maxYExtent(); + else + maxExtent = isRightToLeftTopToBottom() ? q->minXExtent()-size() : -q->maxXExtent(); + if (!item) { int itemPos = rowPosAt(idx); // save the currently visible items in case any of them end up visible again QList oldVisible = visibleItems; visibleItems.clear(); visibleIndex = idx - idx % columns; - maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); + if (flow == QDeclarativeGridView::LeftToRight) + maxExtent = -q->maxYExtent(); + else + maxExtent = isRightToLeftTopToBottom() ? q->minXExtent()-size() : -q->maxXExtent(); setPosition(qMin(qreal(itemPos), maxExtent)); // now release the reference to all the old visible items. for (int i = 0; i < oldVisible.count(); ++i) @@ -2197,8 +2476,13 @@ void QDeclarativeGridViewPrivate::positionViewAtIndex(int index, int mode) if (itemPos < pos) pos = itemPos; } + pos = qMin(pos, maxExtent); - qreal minExtent = flow == QDeclarativeGridView::LeftToRight ? -q->minYExtent() : -q->minXExtent(); + qreal minExtent; + if (flow == QDeclarativeGridView::LeftToRight) + minExtent = -q->minYExtent(); + else + minExtent = isRightToLeftTopToBottom() ? q->maxXExtent()-size() : -q->minXExtent(); pos = qMax(pos, minExtent); moveReason = QDeclarativeGridViewPrivate::Other; q->cancelFlick(); @@ -2337,32 +2621,43 @@ void QDeclarativeGridView::trackedPositionChanged() return; if (d->moveReason == QDeclarativeGridViewPrivate::SetIndex) { const qreal trackedPos = d->trackedItem->rowPos(); - const qreal viewPos = d->position(); + qreal viewPos; + qreal highlightStart; + qreal highlightEnd; + if (d->isRightToLeftTopToBottom()) { + viewPos = -d->position()-d->size(); + highlightStart = d->highlightRangeStartValid ? d->size()-d->highlightRangeEnd : d->highlightRangeStart; + highlightEnd = d->highlightRangeEndValid ? d->size()-d->highlightRangeStart : d->highlightRangeEnd; + } else { + viewPos = d->position(); + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + } qreal pos = viewPos; if (d->haveHighlightRange) { if (d->highlightRange == StrictlyEnforceRange) { - if (trackedPos > pos + d->highlightRangeEnd - d->rowSize()) - pos = trackedPos - d->highlightRangeEnd + d->rowSize(); - if (trackedPos < pos + d->highlightRangeStart) - pos = trackedPos - d->highlightRangeStart; + if (trackedPos > pos + highlightEnd - d->rowSize()) + pos = trackedPos - highlightEnd + d->rowSize(); + if (trackedPos < pos + highlightStart) + pos = trackedPos - highlightStart; } else { - if (trackedPos < d->startPosition() + d->highlightRangeStart) { + if (trackedPos < d->startPosition() + highlightStart) { pos = d->startPosition(); - } else if (d->trackedItem->endRowPos() > d->endPosition() - d->size() + d->highlightRangeEnd) { + } else if (d->trackedItem->endRowPos() > d->endPosition() - d->size() + highlightEnd) { pos = d->endPosition() - d->size() + 1; if (pos < d->startPosition()) pos = d->startPosition(); } else { - if (trackedPos < viewPos + d->highlightRangeStart) { - pos = trackedPos - d->highlightRangeStart; - } else if (trackedPos > viewPos + d->highlightRangeEnd - d->rowSize()) { - pos = trackedPos - d->highlightRangeEnd + d->rowSize(); + if (trackedPos < viewPos + highlightStart) { + pos = trackedPos - highlightStart; + } else if (trackedPos > viewPos + highlightEnd - d->rowSize()) { + pos = trackedPos - highlightEnd + d->rowSize(); } } } } else { if (trackedPos < viewPos && d->currentItem->rowPos() < viewPos) { - pos = d->currentItem->rowPos() < trackedPos ? trackedPos : d->currentItem->rowPos(); + pos = qMax(trackedPos, d->currentItem->rowPos()); } else if (d->trackedItem->endRowPos() >= viewPos + d->size() && d->currentItem->endRowPos() >= viewPos + d->size()) { if (d->trackedItem->endRowPos() <= d->currentItem->endRowPos()) { @@ -2430,7 +2725,8 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) modelIndex = d->visibleIndex; } - int to = d->buffer+d->position()+d->size()-1; + qreal tempPos = d->isRightToLeftTopToBottom() ? -d->position()-d->size() : d->position(); + int to = d->buffer+tempPos+d->size()-1; int colPos = 0; int rowPos = 0; if (d->visibleItems.count()) { @@ -2756,7 +3052,10 @@ void QDeclarativeGridView::animStopped() void QDeclarativeGridView::refill() { Q_D(QDeclarativeGridView); - d->refill(d->position(), d->position()+d->size()-1); + if (d->isRightToLeftTopToBottom()) + d->refill(-d->position()-d->size()+1, -d->position()); + else + d->refill(d->position(), d->position()+d->size()-1); } diff --git a/src/declarative/graphicsitems/qdeclarativegridview_p.h b/src/declarative/graphicsitems/qdeclarativegridview_p.h index 248b9ef..fc9e6b4 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview_p.h +++ b/src/declarative/graphicsitems/qdeclarativegridview_p.h @@ -69,11 +69,12 @@ class Q_AUTOTEST_EXPORT QDeclarativeGridView : public QDeclarativeFlickable Q_PROPERTY(bool highlightFollowsCurrentItem READ highlightFollowsCurrentItem WRITE setHighlightFollowsCurrentItem) Q_PROPERTY(int highlightMoveDuration READ highlightMoveDuration WRITE setHighlightMoveDuration NOTIFY highlightMoveDurationChanged) - Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged) - Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged) + Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged RESET resetPreferredHighlightBegin) + Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged RESET resetPreferredHighlightEnd) Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) //Versioning support? Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged) Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged) Q_PROPERTY(int cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged) @@ -95,6 +96,7 @@ public: ~QDeclarativeGridView(); QVariant model() const; + int modelCount() const; void setModel(const QVariant &); QDeclarativeComponent *delegate() const; @@ -122,9 +124,14 @@ public: qreal preferredHighlightBegin() const; void setPreferredHighlightBegin(qreal); + void resetPreferredHighlightBegin(); qreal preferredHighlightEnd() const; void setPreferredHighlightEnd(qreal); + void resetPreferredHighlightEnd(); + + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection(Qt::LayoutDirection); enum Flow { LeftToRight, TopToBottom }; Flow flow() const; @@ -184,6 +191,7 @@ Q_SIGNALS: void modelChanged(); void delegateChanged(); void flowChanged(); + void layoutDirectionChanged(); void keyNavigationWrapsChanged(); void cacheBufferChanged(); void snapModeChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index a60a4aa..6749657 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -100,13 +100,20 @@ public: } ~FxListItem() {} qreal position() const { - if (section) - return (view->orientation() == QDeclarativeListView::Vertical ? section->y() : section->x()); - else - return (view->orientation() == QDeclarativeListView::Vertical ? item->y() : item->x()); + if (section) { + if (view->orientation() == QDeclarativeListView::Vertical) + return section->y(); + else + return (view->layoutDirection() == Qt::RightToLeft ? -section->width()-section->x() : section->x()); + } else { + return itemPosition(); + } } qreal itemPosition() const { - return (view->orientation() == QDeclarativeListView::Vertical ? item->y() : item->x()); + if (view->orientation() == QDeclarativeListView::Vertical) + return item->y(); + else + return (view->layoutDirection() == Qt::RightToLeft ? -item->width()-item->x() : item->x()); } qreal size() const { if (section) @@ -123,9 +130,13 @@ public: return 0.0; } qreal endPosition() const { - return (view->orientation() == QDeclarativeListView::Vertical - ? item->y() + (item->height() >= 1.0 ? item->height() : 1) - : item->x() + (item->width() >= 1.0 ? item->width() : 1)) - 1; + if (view->orientation() == QDeclarativeListView::Vertical) { + return item->y() + (item->height() >= 1.0 ? item->height() : 1) - 1; + } else { + return (view->layoutDirection() == Qt::RightToLeft + ? -item->width()-item->x() + (item->width() >= 1.0 ? item->width() : 1) + : item->x() + (item->width() >= 1.0 ? item->width() : 1)) - 1; + } } void setPosition(qreal pos) { if (view->orientation() == QDeclarativeListView::Vertical) { @@ -135,11 +146,19 @@ public: } item->setY(pos); } else { - if (section) { - section->setX(pos); - pos += section->width(); + if (view->layoutDirection() == Qt::RightToLeft) { + if (section) { + section->setX(-section->width()-pos); + pos += section->width(); + } + item->setX(-item->width()-pos); + } else { + if (section) { + section->setX(pos); + pos += section->width(); + } + item->setX(pos); } - item->setX(pos); } } void setSize(qreal size) { @@ -168,10 +187,11 @@ class QDeclarativeListViewPrivate : public QDeclarativeFlickablePrivate public: QDeclarativeListViewPrivate() - : currentItem(0), orient(QDeclarativeListView::Vertical) + : currentItem(0), orient(QDeclarativeListView::Vertical), layoutDirection(Qt::LeftToRight) , visiblePos(0), visibleIndex(0) , averageSize(100.0), currentIndex(-1), requestedIndex(-1) , itemCount(0), highlightRangeStart(0), highlightRangeEnd(0) + , highlightRangeStartValid(false), highlightRangeEndValid(false) , highlightComponent(0), highlight(0), trackedItem(0) , moveReason(Other), buffer(0), highlightPosAnimator(0), highlightSizeAnimator(0) , sectionCriteria(0), spacing(0.0) @@ -204,7 +224,7 @@ public: } FxListItem *firstVisibleItem() const { - const qreal pos = position(); + const qreal pos = isRightToLeft() ? -position()-size() : position(); for (int i = 0; i < visibleItems.count(); ++i) { FxListItem *item = visibleItems.at(i); if (item->index != -1 && item->endPosition() > pos) @@ -214,7 +234,7 @@ public: } FxListItem *nextVisibleItem() const { - const qreal pos = position(); + const qreal pos = isRightToLeft() ? -position()-size() : position(); bool foundFirst = false; for (int i = 0; i < visibleItems.count(); ++i) { FxListItem *item = visibleItems.at(i); @@ -228,23 +248,32 @@ public: return 0; } + bool isRightToLeft() const { + return (layoutDirection == Qt::RightToLeft && orient == QDeclarativeListView::Horizontal); + } + qreal position() const { Q_Q(const QDeclarativeListView); return orient == QDeclarativeListView::Vertical ? q->contentY() : q->contentX(); } + void setPosition(qreal pos) { Q_Q(QDeclarativeListView); - if (orient == QDeclarativeListView::Vertical) + if (orient == QDeclarativeListView::Vertical) { q->QDeclarativeFlickable::setContentY(pos); - else - q->QDeclarativeFlickable::setContentX(pos); + } else { + if (layoutDirection == Qt::RightToLeft) + q->QDeclarativeFlickable::setContentX(-pos-size()); + else + q->QDeclarativeFlickable::setContentX(pos); + } } qreal size() const { Q_Q(const QDeclarativeListView); return orient == QDeclarativeListView::Vertical ? q->height() : q->width(); } - qreal startPosition() const { + qreal originPosition() const { qreal pos = 0; if (!visibleItems.isEmpty()) { pos = (*visibleItems.constBegin())->position(); @@ -254,7 +283,7 @@ public: return pos; } - qreal endPosition() const { + qreal lastPosition() const { qreal pos = 0; if (!visibleItems.isEmpty()) { int invisibleCount = visibleItems.count() - visibleIndex; @@ -271,6 +300,14 @@ public: return pos; } + qreal startPosition() const { + return isRightToLeft() ? -lastPosition()-1 : originPosition(); + } + + qreal endPosition() const { + return isRightToLeft() ? -originPosition()-1 : lastPosition(); + } + qreal positionAt(int modelIndex) const { if (FxListItem *item = visibleItem(modelIndex)) return item->position(); @@ -348,7 +385,7 @@ public: } else if (pos > endPos) return endPos + qRound((pos - endPos) / averageSize) * averageSize; } - return qRound((pos - startPosition()) / averageSize) * averageSize + startPosition(); + return qRound((pos - originPosition()) / averageSize) * averageSize + originPosition(); } FxListItem *snapItemAt(qreal pos) { @@ -464,6 +501,7 @@ public: QHash unrequestedItems; FxListItem *currentItem; QDeclarativeListView::Orientation orient; + Qt::LayoutDirection layoutDirection; qreal visiblePos; int visibleIndex; qreal averageSize; @@ -472,6 +510,8 @@ public: int itemCount; qreal highlightRangeStart; qreal highlightRangeEnd; + bool highlightRangeStartValid; + bool highlightRangeEndValid; QDeclarativeComponent *highlightComponent; FxListItem *highlight; FxListItem *trackedItem; @@ -649,7 +689,6 @@ void QDeclarativeListViewPrivate::refill(qreal from, qreal to, bool doBuffer) if (visibleItems.at(i)->index != -1) modelIndex = visibleItems.at(i)->index + 1; } - bool changed = false; FxListItem *item = 0; qreal pos = itemEnd + 1; @@ -795,8 +834,12 @@ void QDeclarativeListViewPrivate::updateUnrequestedPositions() if (item->y() + item->height() > pos && item->y() < pos + q->height()) item->setY(positionAt(*it)); } else { - if (item->x() + item->width() > pos && item->x() < pos + q->width()) - item->setX(positionAt(*it)); + if (item->x() + item->width() > pos && item->x() < pos + q->width()) { + if (isRightToLeft()) + item->setX(-positionAt(*it)-item->width()); + else + item->setX(positionAt(*it)); + } } } } @@ -887,7 +930,9 @@ void QDeclarativeListViewPrivate::updateHighlight() createHighlight(); if (currentItem && autoHighlight && highlight && !movingHorizontally && !movingVertically) { // auto-update highlight - highlightPosAnimator->to = currentItem->itemPosition(); + highlightPosAnimator->to = isRightToLeft() + ? -currentItem->itemPosition()-currentItem->itemSize() + : currentItem->itemPosition(); highlightSizeAnimator->to = currentItem->itemSize(); if (orient == QDeclarativeListView::Vertical) { if (highlight->item->width() == 0) @@ -1104,7 +1149,7 @@ void QDeclarativeListViewPrivate::updateFooter() } if (footer) { if (visibleItems.count()) { - qreal endPos = endPosition() + 1; + qreal endPos = lastPosition() + 1; if (lastVisibleIndex() == model->count()-1) { footer->setPosition(endPos); } else { @@ -1144,7 +1189,7 @@ void QDeclarativeListViewPrivate::updateHeader() } if (header) { if (visibleItems.count()) { - qreal startPos = startPosition(); + qreal startPos = originPosition(); if (visibleIndex == 0) { header->setPosition(startPos - header->size()); } else { @@ -1180,14 +1225,30 @@ void QDeclarativeListViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m int oldDuration = fixupDuration; fixupDuration = moveReason == Mouse ? fixupDuration : 0; - if (currentItem && haveHighlightRange && highlightRange == QDeclarativeListView::StrictlyEnforceRange) { + qreal highlightStart; + qreal highlightEnd; + qreal viewPos; + if (isRightToLeft()) { + // Handle Right-To-Left exceptions + viewPos = -position()-size(); + highlightStart = highlightRangeStartValid ? size() - highlightRangeEnd : highlightRangeStart; + highlightEnd = highlightRangeEndValid ? size() - highlightRangeStart : highlightRangeEnd; + } else { + viewPos = position(); + highlightStart = highlightRangeStart; + highlightEnd = highlightRangeEnd; + } + + if (currentItem && haveHighlightRange && highlightRange == QDeclarativeListView::StrictlyEnforceRange + && moveReason != QDeclarativeListViewPrivate::SetIndex) { updateHighlight(); qreal pos = currentItem->itemPosition(); - qreal viewPos = position(); - if (viewPos < pos + currentItem->itemSize() - highlightRangeEnd) - viewPos = pos + currentItem->itemSize() - highlightRangeEnd; - if (viewPos > pos - highlightRangeStart) - viewPos = pos - highlightRangeStart; + if (viewPos < pos + currentItem->itemSize() - highlightEnd) + viewPos = pos + currentItem->itemSize() - highlightEnd; + if (viewPos > pos - highlightStart) + viewPos = pos - highlightStart; + if (isRightToLeft()) + viewPos = -viewPos-size(); timeline.reset(data.move); if (viewPos != position()) { @@ -1197,17 +1258,26 @@ void QDeclarativeListViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m timeline.set(data.move, -viewPos); } vTime = timeline.time(); - } else if (snapMode != QDeclarativeListView::NoSnap) { - FxListItem *topItem = snapItemAt(position()+highlightRangeStart); - FxListItem *bottomItem = snapItemAt(position()+highlightRangeEnd); + } else if (snapMode != QDeclarativeListView::NoSnap && moveReason != QDeclarativeListViewPrivate::SetIndex) { + qreal tempPosition = isRightToLeft() ? -position()-size() : position(); + FxListItem *topItem = snapItemAt(tempPosition+highlightStart); + FxListItem *bottomItem = snapItemAt(tempPosition+highlightEnd); qreal pos; - if (topItem) { - if (topItem->index == 0 && header && position()+highlightRangeStart < header->position()+header->size()/2) - pos = header->position() - highlightRangeStart; + bool isInBounds = -position() > maxExtent && -position() < minExtent; + if (topItem && isInBounds) { + if (topItem->index == 0 && header && tempPosition+highlightStart < header->position()+header->size()/2) { + pos = isRightToLeft() ? - header->position() + highlightStart - size() : header->position() - highlightStart; + } else { + if (isRightToLeft()) + pos = qMax(qMin(-topItem->position() + highlightStart - size(), -maxExtent), -minExtent); + else + pos = qMax(qMin(topItem->position() - highlightStart, -maxExtent), -minExtent); + } + } else if (bottomItem && isInBounds) { + if (isRightToLeft()) + pos = qMax(qMin(-bottomItem->position() + highlightStart - size(), -maxExtent), -minExtent); else - pos = qMax(qMin(topItem->position() - highlightRangeStart, -maxExtent), -minExtent); - } else if (bottomItem) { - pos = qMax(qMin(bottomItem->position() - highlightRangeStart, -maxExtent), -minExtent); + pos = qMax(qMin(bottomItem->position() - highlightStart, -maxExtent), -minExtent); } else { QDeclarativeFlickablePrivate::fixup(data, minExtent, maxExtent); fixupDuration = oldDuration; @@ -1241,12 +1311,15 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m return; } qreal maxDistance = 0; + qreal dataValue = isRightToLeft() ? -data.move.value()+size() : data.move.value(); // -ve velocity means list is moving up/left if (velocity > 0) { if (data.move.value() < minExtent) { if (snapMode == QDeclarativeListView::SnapOneItem) { - if (FxListItem *item = firstVisibleItem()) - maxDistance = qAbs(item->position() + data.move.value()); + if (FxListItem *item = isRightToLeft() ? nextVisibleItem() : firstVisibleItem()) { + maxDistance = qAbs(item->position() + dataValue); +// qDebug() << "maxDist" << maxDistance << item->position() << dataValue; + } } else { maxDistance = qAbs(minExtent - data.move.value()); } @@ -1256,8 +1329,10 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m } else { if (data.move.value() > maxExtent) { if (snapMode == QDeclarativeListView::SnapOneItem) { - if (FxListItem *item = nextVisibleItem()) - maxDistance = qAbs(item->position() + data.move.value()); + if (FxListItem *item = isRightToLeft() ? firstVisibleItem() : nextVisibleItem()) { + maxDistance = qAbs(item->position() + dataValue); +// qDebug() << "maxDist2" << maxDistance << item->position() << dataValue; + } } else { maxDistance = qAbs(maxExtent - data.move.value()); } @@ -1265,7 +1340,10 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m if (snapMode == QDeclarativeListView::NoSnap && highlightRange != QDeclarativeListView::StrictlyEnforceRange) data.flickTarget = maxExtent; } + bool overShoot = boundsBehavior == QDeclarativeFlickable::DragAndOvershootBounds; + qreal highlightStart = isRightToLeft() && highlightRangeStartValid ? size()-highlightRangeEnd : highlightRangeStart; + if (maxDistance > 0 || overShoot) { // These modes require the list to stop exactly on an item boundary. // The initial flick will estimate the boundary to stop on. @@ -1290,7 +1368,9 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m if (v > 0) dist = -dist; if ((maxDistance > 0.0 && v2 / (2.0f * maxDistance) < accel) || snapMode == QDeclarativeListView::SnapOneItem) { - data.flickTarget = -snapPosAt(-(data.move.value() - highlightRangeStart) + dist) + highlightRangeStart; + qreal distTemp = isRightToLeft() ? -dist : dist; + data.flickTarget = -snapPosAt(-(dataValue - highlightStart) + distTemp) + highlightStart; + data.flickTarget = isRightToLeft() ? -data.flickTarget+size() : data.flickTarget; if (overShoot) { if (data.flickTarget >= minExtent) { overshootDist = overShootDistance(v, vSize); @@ -1323,6 +1403,7 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m data.flickTarget -= overshootDist; } } + timeline.reset(data.move); timeline.accel(data.move, v, accel, maxDistance + overshootDist); timeline.callback(QDeclarativeTimeLineCallback(&data.move, fixupCallback, this)); @@ -1342,8 +1423,11 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m } else { // reevaluate the target boundary. qreal newtarget = data.flickTarget; - if (snapMode != QDeclarativeListView::NoSnap || highlightRange == QDeclarativeListView::StrictlyEnforceRange) - newtarget = -snapPosAt(-(data.flickTarget - highlightRangeStart)) + highlightRangeStart; + if (snapMode != QDeclarativeListView::NoSnap || highlightRange == QDeclarativeListView::StrictlyEnforceRange) { + qreal tempFlickTarget = isRightToLeft() ? -data.flickTarget+size() : data.flickTarget; + newtarget = -snapPosAt(-(tempFlickTarget - highlightStart)) + highlightStart; + newtarget = isRightToLeft() ? -newtarget+size() : newtarget; + } if (velocity < 0 && newtarget <= maxExtent) newtarget = maxExtent - overshootDist; else if (velocity > 0 && newtarget >= minExtent) @@ -1361,6 +1445,7 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m fixup(data, minExtent, maxExtent); return; } + timeline.reset(data.move); timeline.accelDistance(data.move, v, -dist); timeline.callback(QDeclarativeTimeLineCallback(&data.move, fixupCallback, this)); @@ -1859,6 +1944,7 @@ qreal QDeclarativeListView::preferredHighlightBegin() const void QDeclarativeListView::setPreferredHighlightBegin(qreal start) { Q_D(QDeclarativeListView); + d->highlightRangeStartValid = true; if (d->highlightRangeStart == start) return; d->highlightRangeStart = start; @@ -1866,6 +1952,16 @@ void QDeclarativeListView::setPreferredHighlightBegin(qreal start) emit preferredHighlightBeginChanged(); } +void QDeclarativeListView::resetPreferredHighlightBegin() +{ + Q_D(QDeclarativeListView); + d->highlightRangeStartValid = false; + if (d->highlightRangeStart == 0) + return; + d->highlightRangeStart = 0; + emit preferredHighlightBeginChanged(); +} + qreal QDeclarativeListView::preferredHighlightEnd() const { Q_D(const QDeclarativeListView); @@ -1875,6 +1971,7 @@ qreal QDeclarativeListView::preferredHighlightEnd() const void QDeclarativeListView::setPreferredHighlightEnd(qreal end) { Q_D(QDeclarativeListView); + d->highlightRangeEndValid = true; if (d->highlightRangeEnd == end) return; d->highlightRangeEnd = end; @@ -1882,6 +1979,16 @@ void QDeclarativeListView::setPreferredHighlightEnd(qreal end) emit preferredHighlightEndChanged(); } +void QDeclarativeListView::resetPreferredHighlightEnd() +{ + Q_D(QDeclarativeListView); + d->highlightRangeEndValid = false; + if (d->highlightRangeEnd == 0) + return; + d->highlightRangeEnd = 0; + emit preferredHighlightEndChanged(); +} + QDeclarativeListView::HighlightRangeMode QDeclarativeListView::highlightRangeMode() const { Q_D(const QDeclarativeListView); @@ -1968,6 +2075,25 @@ void QDeclarativeListView::setOrientation(QDeclarativeListView::Orientation orie } } +Qt::LayoutDirection QDeclarativeListView::layoutDirection() const +{ + Q_D(const QDeclarativeListView); + return d->layoutDirection; +} + +void QDeclarativeListView::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + Q_D(QDeclarativeListView); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; + d->clear(); + d->setPosition(0); + refill(); + emit layoutDirectionChanged(); + d->updateCurrent(d->currentIndex); + } +} + /*! \qmlproperty bool ListView::keyNavigationWraps This property holds whether the list wraps key navigation. @@ -2339,11 +2465,23 @@ void QDeclarativeListView::viewportMoved() if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) { // reposition highlight qreal pos = d->highlight->position(); - qreal viewPos = d->position(); - if (pos > viewPos + d->highlightRangeEnd - d->highlight->size()) - pos = viewPos + d->highlightRangeEnd - d->highlight->size(); - if (pos < viewPos + d->highlightRangeStart) - pos = viewPos + d->highlightRangeStart; + qreal viewPos; + qreal highlightStart; + qreal highlightEnd; + if (d->isRightToLeft()) { + // Handle Right-To-Left exceptions + viewPos = -d->position()-d->size(); + highlightStart = d->highlightRangeStartValid ? d->size()-d->highlightRangeEnd : d->highlightRangeStart; + highlightEnd = d->highlightRangeEndValid ? d->size()-d->highlightRangeStart : d->highlightRangeEnd; + } else { + viewPos = d->position(); + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + } + if (pos > viewPos + highlightEnd - d->highlight->size()) + pos = viewPos + highlightEnd - d->highlight->size(); + if (pos < viewPos + highlightStart) + pos = viewPos + highlightStart; d->highlightPosAnimator->stop(); d->highlight->setPosition(qRound(pos)); @@ -2381,13 +2519,15 @@ void QDeclarativeListView::viewportMoved() if ((minX - d->hData.move.value() < width()/2 || d->hData.flickTarget - d->hData.move.value() < width()/2) && minX != d->hData.flickTarget) d->flickX(-d->hData.smoothVelocity.value()); - d->bufferMode = QDeclarativeListViewPrivate::BufferBefore; + d->bufferMode = d->isRightToLeft() + ? QDeclarativeListViewPrivate::BufferAfter : QDeclarativeListViewPrivate::BufferBefore; } else if (d->hData.velocity < 0) { const qreal maxX = maxXExtent(); if ((d->hData.move.value() - maxX < width()/2 || d->hData.move.value() - d->hData.flickTarget < width()/2) && maxX != d->hData.flickTarget) d->flickX(-d->hData.smoothVelocity.value()); - d->bufferMode = QDeclarativeListViewPrivate::BufferAfter; + d->bufferMode = d->isRightToLeft() + ? QDeclarativeListViewPrivate::BufferBefore : QDeclarativeListViewPrivate::BufferAfter; } } d->inFlickCorrection = false; @@ -2450,11 +2590,28 @@ qreal QDeclarativeListView::minXExtent() const return QDeclarativeFlickable::minXExtent(); if (d->minExtentDirty) { d->minExtent = -d->startPosition(); - if (d->header) - d->minExtent += d->header->size(); + + qreal highlightStart; + qreal highlightEnd; + qreal endPositionFirstItem; + if (d->isRightToLeft()) { + endPositionFirstItem = d->positionAt(d->model->count()-1); + highlightStart = d->highlightRangeStartValid + ? d->highlightRangeStart - (d->lastPosition()-endPositionFirstItem) + : d->size() - (d->lastPosition()-endPositionFirstItem); + highlightEnd = d->highlightRangeEndValid ? d->highlightRangeEnd : d->size(); + if (d->footer) + d->minExtent += d->footer->size(); + } else { + endPositionFirstItem = d->endPositionAt(0); + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + if (d->header) + d->minExtent += d->header->size(); + } if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { - d->minExtent += d->highlightRangeStart; - d->minExtent = qMax(d->minExtent, -(d->endPositionAt(0) - d->highlightRangeEnd + 1)); + d->minExtent += highlightStart; + d->minExtent = qMax(d->minExtent, -(endPositionFirstItem - highlightEnd + 1)); } d->minExtentDirty = false; } @@ -2468,23 +2625,42 @@ qreal QDeclarativeListView::maxXExtent() const if (d->orient == QDeclarativeListView::Vertical) return width(); if (d->maxExtentDirty) { + qreal highlightStart; + qreal highlightEnd; + qreal lastItemPosition; + if (d->isRightToLeft()) { + highlightStart = d->highlightRangeStartValid ? d->highlightRangeEnd : d->size(); + highlightEnd = d->highlightRangeEndValid ? d->highlightRangeStart : d->size(); + lastItemPosition = d->endPosition(); + } else { + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + lastItemPosition = d->positionAt(d->model->count()-1); + } if (!d->model || !d->model->count()) { d->maxExtent = 0; } else if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { - d->maxExtent = -(d->positionAt(d->model->count()-1) - d->highlightRangeStart); - if (d->highlightRangeEnd != d->highlightRangeStart) - d->maxExtent = qMin(d->maxExtent, -(d->endPosition() - d->highlightRangeEnd + 1)); + d->maxExtent = -(lastItemPosition - highlightStart); + if (highlightEnd != highlightStart) { + d->maxExtent = d->isRightToLeft() + ? qMax(d->maxExtent, -(d->endPosition() - highlightEnd + 1)) + : qMin(d->maxExtent, -(d->endPosition() - highlightEnd + 1)); + } } else { d->maxExtent = -(d->endPosition() - width() + 1); } - if (d->footer) - d->maxExtent -= d->footer->size(); + if (d->isRightToLeft()) { + if (d->header) + d->maxExtent -= d->header->size(); + } else { + if (d->footer) + d->maxExtent -= d->footer->size(); + } qreal minX = minXExtent(); if (d->maxExtent > minX) d->maxExtent = minX; d->maxExtentDirty = false; } - return d->maxExtent; } @@ -2496,7 +2672,8 @@ void QDeclarativeListView::keyPressEvent(QKeyEvent *event) return; if (d->model && d->model->count() && d->interactive) { - if ((d->orient == QDeclarativeListView::Horizontal && event->key() == Qt::Key_Left) + if ((d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::LeftToRight && event->key() == Qt::Key_Left) + || (d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::RightToLeft && event->key() == Qt::Key_Right) || (d->orient == QDeclarativeListView::Vertical && event->key() == Qt::Key_Up)) { if (currentIndex() > 0 || (d->wrap && !event->isAutoRepeat())) { decrementCurrentIndex(); @@ -2506,7 +2683,8 @@ void QDeclarativeListView::keyPressEvent(QKeyEvent *event) event->accept(); return; } - } else if ((d->orient == QDeclarativeListView::Horizontal && event->key() == Qt::Key_Right) + } else if ((d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::LeftToRight && event->key() == Qt::Key_Right) + || (d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::RightToLeft && event->key() == Qt::Key_Left) || (d->orient == QDeclarativeListView::Vertical && event->key() == Qt::Key_Down)) { if (currentIndex() < d->model->count() - 1 || (d->wrap && !event->isAutoRepeat())) { incrementCurrentIndex(); @@ -2583,9 +2761,14 @@ void QDeclarativeListViewPrivate::positionViewAtIndex(int index, int mode) if (layoutScheduled) layout(); - qreal pos = position(); + qreal pos = isRightToLeft() ? -position() - size() : position(); FxListItem *item = visibleItem(idx); - qreal maxExtent = orient == QDeclarativeListView::Vertical ? -q->maxYExtent() : -q->maxXExtent(); + qreal maxExtent; + if (orient == QDeclarativeListView::Vertical) + maxExtent = -q->maxYExtent(); + else + maxExtent = isRightToLeft() ? q->minXExtent()-size(): -q->maxXExtent(); + if (!item) { int itemPos = positionAt(idx); // save the currently visible items in case any of them end up visible again @@ -2628,7 +2811,12 @@ void QDeclarativeListViewPrivate::positionViewAtIndex(int index, int mode) pos = itemPos; } pos = qMin(pos, maxExtent); - qreal minExtent = orient == QDeclarativeListView::Vertical ? -q->minYExtent() : -q->minXExtent(); + qreal minExtent; + if (orient == QDeclarativeListView::Vertical) { + minExtent = -q->minYExtent(); + } else { + minExtent = isRightToLeft() ? q->maxXExtent()-size(): -q->minXExtent(); + } pos = qMax(pos, minExtent); moveReason = QDeclarativeListViewPrivate::Other; q->cancelFlick(); @@ -2783,7 +2971,10 @@ void QDeclarativeListView::updateSections() void QDeclarativeListView::refill() { Q_D(QDeclarativeListView); - d->refill(d->position(), d->position()+d->size()-1); + if (layoutDirection() == Qt::RightToLeft && orientation() == QDeclarativeListView::Horizontal) + d->refill(-d->position()-d->size()+1, -d->position()); + else + d->refill(d->position(), d->position()+d->size()-1); } void QDeclarativeListView::trackedPositionChanged() @@ -2798,26 +2989,37 @@ void QDeclarativeListView::trackedPositionChanged() trackedPos -= d->currentItem->sectionSize(); trackedSize += d->currentItem->sectionSize(); } - const qreal viewPos = d->position(); + qreal viewPos; + qreal highlightStart; + qreal highlightEnd; + if (d->isRightToLeft()) { + viewPos = -d->position()-d->size(); + highlightStart = d->highlightRangeStartValid ? d->size()-d->highlightRangeEnd : d->highlightRangeStart; + highlightEnd = d->highlightRangeEndValid ? d->size()-d->highlightRangeStart : d->highlightRangeEnd; + } else { + viewPos = d->position(); + highlightStart = d->highlightRangeStart; + highlightEnd = d->highlightRangeEnd; + } qreal pos = viewPos; if (d->haveHighlightRange) { if (d->highlightRange == StrictlyEnforceRange) { - if (trackedPos > pos + d->highlightRangeEnd - d->trackedItem->size()) - pos = trackedPos - d->highlightRangeEnd + d->trackedItem->size(); - if (trackedPos < pos + d->highlightRangeStart) - pos = trackedPos - d->highlightRangeStart; + if (trackedPos > pos + highlightEnd - d->trackedItem->size()) + pos = trackedPos - highlightEnd + d->trackedItem->size(); + if (trackedPos < pos + highlightStart) + pos = trackedPos - highlightStart; } else { - if (trackedPos < d->startPosition() + d->highlightRangeStart) { + if (trackedPos < d->startPosition() + highlightStart) { pos = d->startPosition(); - } else if (d->trackedItem->endPosition() > d->endPosition() - d->size() + d->highlightRangeEnd) { + } else if (d->trackedItem->endPosition() > d->endPosition() - d->size() + highlightEnd) { pos = d->endPosition() - d->size() + 1; if (pos < d->startPosition()) pos = d->startPosition(); } else { - if (trackedPos < viewPos + d->highlightRangeStart) { - pos = trackedPos - d->highlightRangeStart; - } else if (trackedPos > viewPos + d->highlightRangeEnd - trackedSize) { - pos = trackedPos - d->highlightRangeEnd + trackedSize; + if (trackedPos < viewPos + highlightStart) { + pos = trackedPos - highlightStart; + } else if (trackedPos > viewPos + highlightEnd - trackedSize) { + pos = trackedPos - highlightEnd + trackedSize; } } } @@ -2854,6 +3056,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count) d->updateUnrequestedIndexes(); d->moveReason = QDeclarativeListViewPrivate::Other; + qreal tempPos = d->isRightToLeft() ? -d->position()-d->size() : d->position(); int index = d->visibleItems.count() ? d->mapFromModel(modelIndex) : 0; if (index < 0) { int i = d->visibleItems.count() - 1; @@ -2863,7 +3066,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count) // there are no visible items except items marked for removal index = d->visibleItems.count(); } else if (d->visibleItems.at(i)->index + 1 == modelIndex - && d->visibleItems.at(i)->endPosition() < d->buffer+d->position()+d->size()-1) { + && d->visibleItems.at(i)->endPosition() < d->buffer+tempPos+d->size()-1) { // Special case of appending an item to the model. index = d->visibleItems.count(); } else { @@ -2908,7 +3111,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count) // Insert items before the visible item. int insertionIdx = index; int i = 0; - int from = d->position() - d->buffer; + int from = tempPos - d->buffer; for (i = count-1; i >= 0 && pos > from; --i) { if (!addedVisible) { d->scheduleLayout(); @@ -2938,7 +3141,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count) } } else { int i = 0; - int to = d->buffer+d->position()+d->size()-1; + int to = d->buffer+tempPos+d->size()-1; for (i = 0; i < count && pos <= to; ++i) { if (!addedVisible) { d->scheduleLayout(); @@ -3233,10 +3436,14 @@ void QDeclarativeListView::createdItem(int index, QDeclarativeItem *item) if (d->requestedIndex != index) { item->setParentItem(contentItem()); d->unrequestedItems.insert(item, index); - if (d->orient == QDeclarativeListView::Vertical) + if (d->orient == QDeclarativeListView::Vertical) { item->setY(d->positionAt(index)); - else - item->setX(d->positionAt(index)); + } else { + if (d->isRightToLeft()) + item->setX(-d->positionAt(index)-item->width()); + else + item->setX(d->positionAt(index)); + } } } diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h index 10fbf10..6b72240 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview_p.h +++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h @@ -107,12 +107,13 @@ class Q_AUTOTEST_EXPORT QDeclarativeListView : public QDeclarativeFlickable Q_PROPERTY(qreal highlightResizeSpeed READ highlightResizeSpeed WRITE setHighlightResizeSpeed NOTIFY highlightResizeSpeedChanged) Q_PROPERTY(int highlightResizeDuration READ highlightResizeDuration WRITE setHighlightResizeDuration NOTIFY highlightResizeDurationChanged) - Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged) - Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged) + Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin NOTIFY preferredHighlightBeginChanged RESET resetPreferredHighlightBegin) + Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd NOTIFY preferredHighlightEndChanged RESET resetPreferredHighlightEnd) Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged) Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged) Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged) Q_PROPERTY(QDeclarativeViewSection *section READ sectionCriteria CONSTANT) @@ -158,9 +159,11 @@ public: qreal preferredHighlightBegin() const; void setPreferredHighlightBegin(qreal); + void resetPreferredHighlightBegin(); qreal preferredHighlightEnd() const; void setPreferredHighlightEnd(qreal); + void resetPreferredHighlightEnd(); qreal spacing() const; void setSpacing(qreal spacing); @@ -169,6 +172,9 @@ public: Orientation orientation() const; void setOrientation(Orientation); + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection(Qt::LayoutDirection); + bool isWrapEnabled() const; void setWrapEnabled(bool); @@ -220,6 +226,7 @@ Q_SIGNALS: void countChanged(); void spacingChanged(); void orientationChanged(); + void layoutDirectionChanged(); void currentIndexChanged(); void currentSectionChanged(); void highlightMoveSpeedChanged(); diff --git a/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml b/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml index 5719f43..164103d 100644 --- a/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml +++ b/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml @@ -48,6 +48,8 @@ Rectangle { model: testModel delegate: myDelegate highlight: myHighlight + flow: (testTopToBottom == true) ? GridView.TopToBottom : GridView.LeftToRight + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight preferredHighlightBegin: 100 preferredHighlightEnd: 100 highlightRangeMode: "StrictlyEnforceRange" diff --git a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml index e4e699c..1f5943d 100644 --- a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml +++ b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml @@ -55,7 +55,8 @@ Rectangle { height: 320 cellWidth: 80 cellHeight: 60 - flow: (testTopToBottom == false) ? "LeftToRight" : "TopToBottom" + flow: (testTopToBottom == false) ? GridView.LeftToRight : GridView.TopToBottom + layoutDirection: (testRightToLeft == true) ? Qt.RightToLeft : Qt.LeftToRight model: testModel delegate: myDelegate header: root.showHeader ? headerFooter : null diff --git a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp index 79189a7..188fd6e 100644 --- a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp +++ b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp @@ -78,9 +78,11 @@ private slots: void componentChanges(); void modelChanges(); void positionViewAtIndex(); + void positionViewAtIndex_rightToLeft(); void snapping(); void resetModel(); void enforceRange(); + void enforceRange_rightToLeft(); void QTBUG_8456(); void manualHighlight(); void footer(); @@ -203,6 +205,7 @@ void tst_QDeclarativeGridView::items() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -251,6 +254,7 @@ void tst_QDeclarativeGridView::changed() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -284,6 +288,7 @@ void tst_QDeclarativeGridView::inserted() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -360,6 +365,7 @@ void tst_QDeclarativeGridView::removed() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -503,6 +509,7 @@ void tst_QDeclarativeGridView::moved() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -726,6 +733,58 @@ void tst_QDeclarativeGridView::currentIndex() QVERIFY(!gridview->highlightItem()); QVERIFY(!gridview->currentItem()); + gridview->setHighlightFollowsCurrentItem(true); + + gridview->setFlow(QDeclarativeGridView::LeftToRight); + gridview->setLayoutDirection(Qt::RightToLeft); + + qApp->setActiveWindow(canvas); +#ifdef Q_WS_X11 + // to be safe and avoid failing setFocus with window managers + qt_x11_wait_for_window_manager(canvas); +#endif + QTRY_VERIFY(canvas->hasFocus()); + QTRY_VERIFY(canvas->scene()->hasFocus()); + qApp->processEvents(); + + gridview->setCurrentIndex(35); + + QTest::keyClick(canvas, Qt::Key_Right); + QCOMPARE(gridview->currentIndex(), 34); + + QTest::keyClick(canvas, Qt::Key_Down); + QCOMPARE(gridview->currentIndex(), 37); + + QTest::keyClick(canvas, Qt::Key_Up); + QCOMPARE(gridview->currentIndex(), 34); + + QTest::keyClick(canvas, Qt::Key_Left); + QCOMPARE(gridview->currentIndex(), 35); + + + // turn off auto highlight + gridview->setHighlightFollowsCurrentItem(false); + QVERIFY(gridview->highlightFollowsCurrentItem() == false); + QVERIFY(gridview->highlightItem()); + hlPosX = gridview->highlightItem()->x(); + hlPosY = gridview->highlightItem()->y(); + + gridview->setCurrentIndex(5); + QTRY_COMPARE(gridview->highlightItem()->x(), hlPosX); + QTRY_COMPARE(gridview->highlightItem()->y(), hlPosY); + + // insert item before currentIndex + gridview->setCurrentIndex(28); + model.insertItem(0, "Foo", "1111"); + QTRY_COMPARE(canvas->rootObject()->property("current").toInt(), 29); + + // check removing highlight by setting currentIndex to -1; + gridview->setCurrentIndex(-1); + + QCOMPARE(gridview->currentIndex(), -1); + QVERIFY(!gridview->highlightItem()); + QVERIFY(!gridview->currentItem()); + delete canvas; } @@ -774,6 +833,7 @@ void tst_QDeclarativeGridView::changeFlow() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -819,6 +879,44 @@ void tst_QDeclarativeGridView::changeFlow() QTRY_COMPARE(number->text(), model.number(i)); } + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + // Confirm items positioned correctly and indexes correct + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80 - item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + QDeclarativeText *name = findItem(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QDeclarativeText *number = findItem(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + gridview->setContentX(100); + QTRY_COMPARE(gridview->contentX(), 100.); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); + QTRY_COMPARE(gridview->contentX(), 0.); + + // Confirm items positioned correctly and indexes correct + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(240 - (i%3+1)*80)); + QTRY_COMPARE(item->y(), qreal((i/3)*60)); + QDeclarativeText *name = findItem(contentItem, "textName", i); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(i)); + QDeclarativeText *number = findItem(contentItem, "textNumber", i); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(i)); + } + delete canvas; } @@ -879,6 +977,7 @@ void tst_QDeclarativeGridView::propertyChanges() QSignalSpy keyNavigationWrapsSpy(gridView, SIGNAL(keyNavigationWrapsChanged())); QSignalSpy cacheBufferSpy(gridView, SIGNAL(cacheBufferChanged())); + QSignalSpy layoutSpy(gridView, SIGNAL(layoutDirectionChanged())); QSignalSpy flowSpy(gridView, SIGNAL(flowChanged())); QTRY_COMPARE(gridView->isWrapEnabled(), true); @@ -905,6 +1004,38 @@ void tst_QDeclarativeGridView::propertyChanges() QTRY_COMPARE(cacheBufferSpy.count(),1); QTRY_COMPARE(flowSpy.count(),1); + gridView->setFlow(QDeclarativeGridView::LeftToRight); + QTRY_COMPARE(gridView->flow(), QDeclarativeGridView::LeftToRight); + + gridView->setWrapEnabled(true); + gridView->setCacheBuffer(5); + gridView->setLayoutDirection(Qt::RightToLeft); + + QTRY_COMPARE(gridView->isWrapEnabled(), true); + QTRY_COMPARE(gridView->cacheBuffer(), 5); + QTRY_COMPARE(gridView->layoutDirection(), Qt::RightToLeft); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),2); + QTRY_COMPARE(cacheBufferSpy.count(),2); + QTRY_COMPARE(layoutSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),2); + + gridView->setWrapEnabled(true); + gridView->setCacheBuffer(5); + gridView->setLayoutDirection(Qt::RightToLeft); + + QTRY_COMPARE(keyNavigationWrapsSpy.count(),2); + QTRY_COMPARE(cacheBufferSpy.count(),2); + QTRY_COMPARE(layoutSpy.count(),1); + QTRY_COMPARE(flowSpy.count(),2); + + gridView->setFlow(QDeclarativeGridView::TopToBottom); + QTRY_COMPARE(gridView->flow(), QDeclarativeGridView::TopToBottom); + QTRY_COMPARE(flowSpy.count(),3); + + gridView->setFlow(QDeclarativeGridView::TopToBottom); + QTRY_COMPARE(flowSpy.count(),3); + delete canvas; } @@ -992,6 +1123,7 @@ void tst_QDeclarativeGridView::positionViewAtIndex() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); @@ -1213,6 +1345,138 @@ void tst_QDeclarativeGridView::snapping() delete canvas; } +void tst_QDeclarativeGridView::positionViewAtIndex_rightToLeft() +{ + QDeclarativeView *canvas = createView(); + + TestModel model; + for (int i = 0; i < 40; i++) + model.addItem("Item" + QString::number(i), ""); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); + qApp->processEvents(); + + QDeclarativeGridView *gridview = findItem(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QDeclarativeItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // Confirm items positioned correctly + int itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on a currently visible item + gridview->positionViewAtIndex(6, QDeclarativeGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -320.); + + // Confirm items positioned correctly + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on an item beyond the visible items + gridview->positionViewAtIndex(21, QDeclarativeGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Confirm items positioned correctly + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position on an item that would leave empty space if positioned at the top + gridview->positionViewAtIndex(31, QDeclarativeGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -639.); + + // Confirm items positioned correctly + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position at the beginning again + gridview->positionViewAtIndex(0, QDeclarativeGridView::Beginning); + QTRY_COMPARE(gridview->contentX(), -240.); + + // Confirm items positioned correctly + itemCount = findItems(contentItem, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount-1; ++i) { + QDeclarativeItem *item = findItem(contentItem, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), qreal(-(i/5)*80-item->width())); + QTRY_COMPARE(item->y(), qreal((i%5)*60)); + } + + // Position at End + gridview->positionViewAtIndex(30, QDeclarativeGridView::End); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Position in Center + gridview->positionViewAtIndex(15, QDeclarativeGridView::Center); + QTRY_COMPARE(gridview->contentX(), -400.); + + // Ensure at least partially visible + gridview->positionViewAtIndex(15, QDeclarativeGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-555.); + gridview->positionViewAtIndex(15, QDeclarativeGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -555.); + + gridview->setContentX(-239); + gridview->positionViewAtIndex(15, QDeclarativeGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -320.); + + gridview->setContentX(-239); + gridview->positionViewAtIndex(20, QDeclarativeGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-640); + gridview->positionViewAtIndex(20, QDeclarativeGridView::Visible); + QTRY_COMPARE(gridview->contentX(), -560.); + + // Ensure completely visible + gridview->setContentX(-400); + gridview->positionViewAtIndex(20, QDeclarativeGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -400.); + + gridview->setContentX(-315); + gridview->positionViewAtIndex(15, QDeclarativeGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -320.); + + gridview->setContentX(-640); + gridview->positionViewAtIndex(20, QDeclarativeGridView::Contain); + QTRY_COMPARE(gridview->contentX(), -560.); + + delete canvas; +} + void tst_QDeclarativeGridView::resetModel() { QDeclarativeView *canvas = createView(); @@ -1264,6 +1528,8 @@ void tst_QDeclarativeGridView::enforceRange() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); + ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml")); qApp->processEvents(); @@ -1307,6 +1573,63 @@ void tst_QDeclarativeGridView::enforceRange() delete canvas; } +void tst_QDeclarativeGridView::enforceRange_rightToLeft() +{ + QDeclarativeView *canvas = createView(); + + TestModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(true)); + ctxt->setContextProperty("testTopToBottom", QVariant(true)); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml")); + qApp->processEvents(); + + QDeclarativeGridView *gridview = findItem(canvas->rootObject(), "grid"); + QTRY_VERIFY(gridview != 0); + + QTRY_COMPARE(gridview->preferredHighlightBegin(), 100.0); + QTRY_COMPARE(gridview->preferredHighlightEnd(), 100.0); + QTRY_COMPARE(gridview->highlightRangeMode(), QDeclarativeGridView::StrictlyEnforceRange); + + QDeclarativeItem *contentItem = gridview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + // view should be positioned at the top of the range. + QDeclarativeItem *item = findItem(contentItem, "wrapper", 0); + QTRY_VERIFY(item); + QTRY_COMPARE(gridview->contentX(), -100.); + QTRY_COMPARE(gridview->contentY(), 0.0); + + QDeclarativeText *name = findItem(contentItem, "textName", 0); + QTRY_VERIFY(name != 0); + QTRY_COMPARE(name->text(), model.name(0)); + QDeclarativeText *number = findItem(contentItem, "textNumber", 0); + QTRY_VERIFY(number != 0); + QTRY_COMPARE(number->text(), model.number(0)); + + // Check currentIndex is updated when contentItem moves + gridview->setContentX(-200); + QTRY_COMPARE(gridview->currentIndex(), 3); + + gridview->setCurrentIndex(7); + QTRY_COMPARE(gridview->contentX(), -300.); + QTRY_COMPARE(gridview->contentY(), 0.0); + + TestModel model2; + for (int i = 0; i < 5; i++) + model2.addItem("Item" + QString::number(i), ""); + + ctxt->setContextProperty("testModel", &model2); + QCOMPARE(gridview->count(), 5); + + delete canvas; +} + void tst_QDeclarativeGridView::QTBUG_8456() { QDeclarativeView *canvas = createView(); @@ -1475,6 +1798,7 @@ void tst_QDeclarativeGridView::indexAt() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); ctxt->setContextProperty("testTopToBottom", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index f358625..26219fe 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -115,6 +115,7 @@ private slots: void onRemove_data(); void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); + void rightToLeft(); private: template void items(); @@ -2299,6 +2300,47 @@ void tst_QDeclarativeListView::testQtQuick11Attributes_data() << ""; } +void tst_QDeclarativeListView::rightToLeft() +{ + QDeclarativeView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml")); + qApp->processEvents(); + + QDeclarativeListView *listview = findItem(canvas->rootObject(), "view"); + QTRY_VERIFY(listview != 0); + + QDeclarativeItem *contentItem = listview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QDeclarativeVisualItemModel *model = canvas->rootObject()->findChild("itemModel"); + QTRY_VERIFY(model != 0); + + QTRY_VERIFY(model->count() == 3); + QTRY_COMPARE(listview->currentIndex(), 0); + + QDeclarativeItem *item = findItem(contentItem, "item1"); + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), -100.0); + QCOMPARE(item->height(), listview->height()); + + QDeclarativeText *text = findItem(contentItem, "text1"); + QTRY_VERIFY(text); + QTRY_COMPARE(text->text(), QLatin1String("index: 0")); + + listview->setCurrentIndex(2); + + item = findItem(contentItem, "item3"); + QTRY_VERIFY(item); + QTRY_COMPARE(item->x(), -600.0); + + text = findItem(contentItem, "text3"); + QTRY_VERIFY(text); + QTRY_COMPARE(text->text(), QLatin1String("index: 2")); + + delete canvas; +} + void tst_QDeclarativeListView::qListModelInterface_items() { items(); -- cgit v0.12 From b5076fb392894e71b44b4762d0567354ef1c8a9e Mon Sep 17 00:00:00 2001 From: Sarah Smith Date: Wed, 16 Feb 2011 14:58:28 +1000 Subject: Add a "note well" to QVectorXD re float precision. Several bug-reports ask for clarifications/warnings in the doc for this class, since it stores in float, but has a qreal interface (as per qt convention). Change-Id: I32ffcde3733866942134e881567eef367a5620f5 Task-number: QTBUG-8216 Reviewed-by: Rhys Weatherley --- src/gui/math3d/qvector2d.cpp | 5 +++++ src/gui/math3d/qvector3d.cpp | 5 +++++ src/gui/math3d/qvector4d.cpp | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/gui/math3d/qvector2d.cpp b/src/gui/math3d/qvector2d.cpp index 7f5a937..1fccfc9 100644 --- a/src/gui/math3d/qvector2d.cpp +++ b/src/gui/math3d/qvector2d.cpp @@ -60,6 +60,11 @@ QT_BEGIN_NAMESPACE The QVector2D class can also be used to represent vertices in 2D space. We therefore do not need to provide a separate vertex class. + \bold{Note:} By design values in the QVector2D instance are stored as \c float. + This means that on platforms where the \c qreal arguments to QVector2D + functions are represented by \c double values, it is possible to + lose precision. + \sa QVector3D, QVector4D, QQuaternion */ diff --git a/src/gui/math3d/qvector3d.cpp b/src/gui/math3d/qvector3d.cpp index 2414b5f..7bf0400 100644 --- a/src/gui/math3d/qvector3d.cpp +++ b/src/gui/math3d/qvector3d.cpp @@ -63,6 +63,11 @@ QT_BEGIN_NAMESPACE The QVector3D class can also be used to represent vertices in 3D space. We therefore do not need to provide a separate vertex class. + \bold{Note:} By design values in the QVector3D instance are stored as \c float. + This means that on platforms where the \c qreal arguments to QVector3D + functions are represented by \c double values, it is possible to + lose precision. + \sa QVector2D, QVector4D, QQuaternion */ diff --git a/src/gui/math3d/qvector4d.cpp b/src/gui/math3d/qvector4d.cpp index 74dedc4..23befc0 100644 --- a/src/gui/math3d/qvector4d.cpp +++ b/src/gui/math3d/qvector4d.cpp @@ -59,6 +59,11 @@ QT_BEGIN_NAMESPACE The QVector4D class can also be used to represent vertices in 4D space. We therefore do not need to provide a separate vertex class. + \bold{Note:} By design values in the QVector4D instance are stored as \c float. + This means that on platforms where the \c qreal arguments to QVector4D + functions are represented by \c double values, it is possible to + lose precision. + \sa QQuaternion, QVector2D, QVector3D */ -- cgit v0.12 From 4e75cb56f37ac2ff22fbc562c85daeb8599753a9 Mon Sep 17 00:00:00 2001 From: Christopher Ham Date: Wed, 16 Feb 2011 11:48:02 +1000 Subject: GridView and ListView bug fixes ListView and GridView check before accessing an empty model. Fixed issue with undefined header. Current index initialised properly when dynamically creating ListModel items with javascript. Change-Id: I121c0626db6eb7ccaab689dfc750e0d03773d90f Task-number: QTBUG-15877 Reviewed-by: Joona Petrell --- .../graphicsitems/qdeclarativegridview.cpp | 57 +++++++++++----------- .../graphicsitems/qdeclarativelistview.cpp | 16 +++--- .../tst_qdeclarativegridview.cpp | 2 + .../tst_qdeclarativelistview.cpp | 2 +- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 39fa8e8..2eeadf4 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -73,22 +73,22 @@ public: if (view->flow() == QDeclarativeGridView::LeftToRight) { rowPos = item->y(); } else { - if (view->layoutDirection() == Qt::LeftToRight) - rowPos = item->x(); - else + if (view->layoutDirection() == Qt::RightToLeft) rowPos = -view->cellWidth()-item->x(); + else + rowPos = item->x(); } return rowPos; } qreal colPos() const { qreal colPos = 0; if (view->flow() == QDeclarativeGridView::LeftToRight) { - if (view->layoutDirection() == Qt::LeftToRight) { - colPos = item->x(); - } else { + if (view->layoutDirection() == Qt::RightToLeft) { int colSize = view->cellWidth(); int columns = view->width()/colSize; colPos = colSize * (columns-1) - item->x(); + } else { + colPos = item->x(); } } else { colPos = item->y(); @@ -101,25 +101,25 @@ public: if (view->flow() == QDeclarativeGridView::LeftToRight) { return item->y() + view->cellHeight() - 1; } else { - if (view->layoutDirection() == Qt::LeftToRight) - return item->x() + view->cellWidth() - 1; - else + if (view->layoutDirection() == Qt::RightToLeft) return -item->x() - 1; + else + return item->x() + view->cellWidth() - 1; } } void setPosition(qreal col, qreal row) { - if (view->layoutDirection() == Qt::LeftToRight) { - if (view->flow() == QDeclarativeGridView::LeftToRight) - item->setPos(QPointF(col, row)); - else - item->setPos(QPointF(row, col)); - } else { + if (view->layoutDirection() == Qt::RightToLeft) { if (view->flow() == QDeclarativeGridView::LeftToRight) { int columns = view->width()/view->cellWidth(); item->setPos(QPointF((view->cellWidth() * (columns-1) - col), row)); } else { item->setPos(QPointF(-view->cellWidth()-row, col)); } + } else { + if (view->flow() == QDeclarativeGridView::LeftToRight) + item->setPos(QPointF(col, row)); + else + item->setPos(QPointF(row, col)); } } @@ -697,7 +697,7 @@ void QDeclarativeGridViewPrivate::layout() qreal rowPos = visibleItems.first()->rowPos(); qreal colPos = visibleItems.first()->colPos(); int col = visibleIndex % columns; - if (colPos != col * colSize() || isRightToLeftTopToBottom()) { + if (colPos != col * colSize()) { colPos = col * colSize(); visibleItems.first()->setPosition(colPos, rowPos); } @@ -747,7 +747,7 @@ void QDeclarativeGridViewPrivate::updateUnrequestedPositions() if (isRightToLeftTopToBottom()) item->setPos(QPointF(-rowPosAt(*it)-item->width(), colPosAt(*it))); else - item->setPos(QPointF(rowPosAt(*it), (columns-1)*colSize()-colPosAt(*it))); + item->setPos(QPointF(rowPosAt(*it), colPosAt(*it))); } } } @@ -1032,7 +1032,9 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m qreal bottomPos = qMax(bottomItem->rowPos() - highlightEnd, -minExtent); pos = qAbs(data.move + topPos) < qAbs(data.move + bottomPos) ? topPos : bottomPos; } else if (topItem) { - qreal headerPos = isRightToLeftTopToBottom() ? header->rowPos() + cellWidth - headerSize() : header->rowPos(); + qreal headerPos = 0; + if (header) + headerPos = isRightToLeftTopToBottom() ? header->rowPos() + cellWidth - headerSize() : header->rowPos(); if (topItem->index == 0 && header && tempPosition+highlightStart < headerPos+headerSize()/2) { pos = isRightToLeftTopToBottom() ? - headerPos + highlightStart - size() : headerPos - highlightStart; } else { @@ -2191,9 +2193,6 @@ qreal QDeclarativeGridView::maxXExtent() const if (d->flow == QDeclarativeGridView::LeftToRight) return QDeclarativeFlickable::maxXExtent(); qreal extent; - if (!d->model || !d->model->count()) { - extent = 0; - } qreal highlightStart; qreal highlightEnd; qreal lastItemPosition; @@ -2204,9 +2203,12 @@ qreal QDeclarativeGridView::maxXExtent() const } else { highlightStart = d->highlightRangeStart; highlightEnd = d->highlightRangeEnd; - lastItemPosition = d->rowPosAt(d->model->count()-1); + if (d->model && d->model->count()) + lastItemPosition = d->rowPosAt(d->model->count()-1); } - if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { + if (!d->model || !d->model->count()) { + extent = 0; + } else if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) { extent = -(lastItemPosition - highlightStart); if (highlightEnd != highlightStart) extent = d->isRightToLeftTopToBottom() @@ -2725,7 +2727,7 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) modelIndex = d->visibleIndex; } - qreal tempPos = d->isRightToLeftTopToBottom() ? -d->position()-d->size() : d->position(); + qreal tempPos = d->isRightToLeftTopToBottom() ? -d->position()-d->size()+d->width()+1 : d->position(); int to = d->buffer+tempPos+d->size()-1; int colPos = 0; int rowPos = 0; @@ -2744,10 +2746,7 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) } } } else if (d->itemCount == 0 && d->header) { - if (d->flow == QDeclarativeGridView::LeftToRight) - rowPos = d->headerSize(); - else - colPos = d->headerSize(); + rowPos = d->headerSize(); } // Update the indexes of the following visible items. @@ -2804,6 +2803,8 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) d->updateCurrent(0); } emit currentIndexChanged(); + } else if (d->itemCount == 0 && d->currentIndex == -1) { + setCurrentIndex(0); } // everything is in order now - emit add() signal diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index 6749657..486cec8 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -329,6 +329,7 @@ public: else idx = visibleItems.at(idx)->index; int count = modelIndex - idx - 1; + return (*(--visibleItems.constEnd()))->endPosition() + spacing + count * (averageSize + spacing) + 1; } } @@ -1316,10 +1317,8 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m if (velocity > 0) { if (data.move.value() < minExtent) { if (snapMode == QDeclarativeListView::SnapOneItem) { - if (FxListItem *item = isRightToLeft() ? nextVisibleItem() : firstVisibleItem()) { + if (FxListItem *item = isRightToLeft() ? nextVisibleItem() : firstVisibleItem()) maxDistance = qAbs(item->position() + dataValue); -// qDebug() << "maxDist" << maxDistance << item->position() << dataValue; - } } else { maxDistance = qAbs(minExtent - data.move.value()); } @@ -1329,10 +1328,8 @@ void QDeclarativeListViewPrivate::flick(AxisData &data, qreal minExtent, qreal m } else { if (data.move.value() > maxExtent) { if (snapMode == QDeclarativeListView::SnapOneItem) { - if (FxListItem *item = isRightToLeft() ? firstVisibleItem() : nextVisibleItem()) { + if (FxListItem *item = isRightToLeft() ? firstVisibleItem() : nextVisibleItem()) maxDistance = qAbs(item->position() + dataValue); -// qDebug() << "maxDist2" << maxDistance << item->position() << dataValue; - } } else { maxDistance = qAbs(maxExtent - data.move.value()); } @@ -2595,7 +2592,8 @@ qreal QDeclarativeListView::minXExtent() const qreal highlightEnd; qreal endPositionFirstItem; if (d->isRightToLeft()) { - endPositionFirstItem = d->positionAt(d->model->count()-1); + if (d->model && d->model->count()) + endPositionFirstItem = d->positionAt(d->model->count()-1); highlightStart = d->highlightRangeStartValid ? d->highlightRangeStart - (d->lastPosition()-endPositionFirstItem) : d->size() - (d->lastPosition()-endPositionFirstItem); @@ -2635,7 +2633,8 @@ qreal QDeclarativeListView::maxXExtent() const } else { highlightStart = d->highlightRangeStart; highlightEnd = d->highlightRangeEnd; - lastItemPosition = d->positionAt(d->model->count()-1); + if (d->model && d->model->count()) + lastItemPosition = d->positionAt(d->model->count()-1); } if (!d->model || !d->model->count()) { d->maxExtent = 0; @@ -3058,6 +3057,7 @@ void QDeclarativeListView::itemsInserted(int modelIndex, int count) qreal tempPos = d->isRightToLeft() ? -d->position()-d->size() : d->position(); int index = d->visibleItems.count() ? d->mapFromModel(modelIndex) : 0; + if (index < 0) { int i = d->visibleItems.count() - 1; while (i > 0 && d->visibleItems.at(i)->index == -1) diff --git a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp index 188fd6e..4fcaed6 100644 --- a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp +++ b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp @@ -1317,6 +1317,7 @@ void tst_QDeclarativeGridView::snapping() QDeclarativeContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testModel", &model); ctxt->setContextProperty("testTopToBottom", QVariant(false)); + ctxt->setContextProperty("testRightToLeft", QVariant(false)); canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview1.qml")); qApp->processEvents(); @@ -1343,6 +1344,7 @@ void tst_QDeclarativeGridView::snapping() QCOMPARE(gridview->contentY(), 120.); delete canvas; + } void tst_QDeclarativeGridView::positionViewAtIndex_rightToLeft() diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index 26219fe..02c8dad 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -2332,7 +2332,7 @@ void tst_QDeclarativeListView::rightToLeft() item = findItem(contentItem, "item3"); QTRY_VERIFY(item); - QTRY_COMPARE(item->x(), -600.0); + QTRY_COMPARE(item->x(), -540.0); text = findItem(contentItem, "text3"); QTRY_VERIFY(text); -- cgit v0.12 From 13ea38369b7f095e6af96c98bed7a3bb9bf795d5 Mon Sep 17 00:00:00 2001 From: Christopher Ham Date: Thu, 17 Feb 2011 16:36:08 +1000 Subject: Adding file required for ListView Autotest The file was missed out in the previous commit. Change-Id: Ic8d055e9797b5da2ba1cb548984efc8c7e205751 Task-number: QTBUG-16010 Reviewed-by: Trust Me --- .../qdeclarativelistview/data/rightToLeft.qml | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml diff --git a/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml b/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml new file mode 100644 index 0000000..e31d923 --- /dev/null +++ b/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml @@ -0,0 +1,42 @@ +// This example demonstrates placing items in a view using +// a VisualItemModel + +import QtQuick 1.0 + +Rectangle { + color: "lightgray" + width: 240 + height: 320 + + VisualItemModel { + id: itemModel + objectName: "itemModel" + Rectangle { + objectName: "item1" + height: view.height; width: 100; color: "#FFFEF0" + Text { objectName: "text1"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item2" + height: view.height; width: 200; color: "#F0FFF7" + Text { objectName: "text2"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item3" + height: view.height; width: 240; color: "#F4F0FF" + Text { objectName: "text3"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + } + + ListView { + id: view + objectName: "view" + anchors.fill: parent + anchors.bottomMargin: 30 + model: itemModel + highlightRangeMode: "StrictlyEnforceRange" + orientation: ListView.Horizontal + flickDeceleration: 2000 + layoutDirection: Qt.RightToLeft + } +} -- cgit v0.12 From 2c7cab4172f1acc86fd49345a2847417e162f2c3 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Thu, 24 Feb 2011 12:33:11 +1000 Subject: Add LayoutMirroring attached property for mirroring QML layouts Task-number: QTBUG-17280 Reviewed-by: Martin Jones Change-Id: I34a623b49ce0fd5c05ae7a7ea5d0437c107b8a9b --- doc/src/snippets/declarative/layoutmirroring.qml | 64 ++++++++ .../layoutdirection/layoutdirection.qml | 22 ++- .../graphicsitems/qdeclarativeanchors.cpp | 40 ++--- .../graphicsitems/qdeclarativeanchors_p.h | 10 +- .../graphicsitems/qdeclarativeanchors_p_p.h | 4 +- .../graphicsitems/qdeclarativegridview.cpp | 69 +++++--- .../graphicsitems/qdeclarativegridview_p.h | 7 +- src/declarative/graphicsitems/qdeclarativeitem.cpp | 154 +++++++++++++++++- src/declarative/graphicsitems/qdeclarativeitem_p.h | 53 +++++- .../graphicsitems/qdeclarativeitemsmodule.cpp | 1 + .../graphicsitems/qdeclarativelistview.cpp | 90 ++++++++--- .../graphicsitems/qdeclarativelistview_p.h | 7 +- .../graphicsitems/qdeclarativepositioners.cpp | 114 +++++++++---- .../graphicsitems/qdeclarativepositioners_p.h | 15 +- .../graphicsitems/qdeclarativepositioners_p_p.h | 30 +++- .../tst_qdeclarativeanchors.cpp | 12 +- .../data/gridview-enforcerange.qml | 2 +- .../qdeclarativegridview/data/gridview1.qml | 2 +- .../qdeclarativegridview/data/mirroring.qml | 43 +++++ .../tst_qdeclarativegridview.cpp | 65 ++++++++ .../qdeclarativeitem/data/layoutmirroring.qml | 54 +++++++ .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 180 +++++++++++++++++---- .../qdeclarativelistview/data/rightToLeft.qml | 8 +- .../tst_qdeclarativelistview.cpp | 69 +++++++- .../data/grid-righttoleft.qml | 41 ----- .../qdeclarativepositioners/data/gridtest.qml | 6 +- .../qdeclarativepositioners/data/horizontal.qml | 1 - .../tst_qdeclarativepositioners.cpp | 78 ++++++++- .../qdeclarativestates/tst_qdeclarativestates.cpp | 2 +- 29 files changed, 1027 insertions(+), 216 deletions(-) create mode 100644 doc/src/snippets/declarative/layoutmirroring.qml create mode 100644 tests/auto/declarative/qdeclarativegridview/data/mirroring.qml create mode 100644 tests/auto/declarative/qdeclarativeitem/data/layoutmirroring.qml delete mode 100644 tests/auto/declarative/qdeclarativepositioners/data/grid-righttoleft.qml diff --git a/doc/src/snippets/declarative/layoutmirroring.qml b/doc/src/snippets/declarative/layoutmirroring.qml new file mode 100644 index 0000000..23eecd6 --- /dev/null +++ b/doc/src/snippets/declarative/layoutmirroring.qml @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import QtQuick 1.1 + +Rectangle { + LayoutMirroring.enabled: true + LayoutMirroring.childrenInherit: true + width: 240; height: 50 + Row { + anchors { left: parent.left; margins: 5 } + y: 5; spacing: 5 + Repeater { + model: 5 + Rectangle { + color: "red" + opacity: (5-index) / 5 + width: 40; height: 40 + Text { + text: index+1 + anchors.centerIn: parent + } + } + } + } +} +//![0] diff --git a/examples/declarative/positioners/layoutdirection/layoutdirection.qml b/examples/declarative/positioners/layoutdirection/layoutdirection.qml index 3e23b15..080010e 100644 --- a/examples/declarative/positioners/layoutdirection/layoutdirection.qml +++ b/examples/declarative/positioners/layoutdirection/layoutdirection.qml @@ -41,10 +41,13 @@ import QtQuick 1.1 Rectangle { + property bool mirror + property int direction: Qt.application.layoutDirection + LayoutMirroring.enabled: mirror + LayoutMirroring.childrenInherit: true width: column.width + 100 height: column.height + 100 - property int direction: Qt.application.layoutDirection Column { id: column @@ -133,6 +136,23 @@ Rectangle { anchors.fill: parent } } + Rectangle { + height: 50; width: parent.width + color: mouseArea2.pressed ? "black" : "gray" + Text { + text: mirror ? "Mirrored" : "Normal" + color: "white" + font.pixelSize: 16 + anchors.centerIn: parent + } + MouseArea { + id: mouseArea2 + onClicked: { + mirror = !mirror; + } + anchors.fill: parent + } + } } Component { diff --git a/src/declarative/graphicsitems/qdeclarativeanchors.cpp b/src/declarative/graphicsitems/qdeclarativeanchors.cpp index a2d6261..5ff6d2c 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors.cpp +++ b/src/declarative/graphicsitems/qdeclarativeanchors.cpp @@ -175,13 +175,14 @@ QDeclarativeAnchors::~QDeclarativeAnchors() void QDeclarativeAnchorsPrivate::fillChanged() { + Q_Q(QDeclarativeAnchors); if (!fill || !isItemComplete()) return; if (updatingFill < 2) { ++updatingFill; - qreal horizontalMargin = isMirrored() ? rightMargin : leftMargin; + qreal horizontalMargin = q->mirrored() ? rightMargin : leftMargin; if (fill == item->parentItem()) { //child-parent setItemPos(QPointF(horizontalMargin, topMargin)); @@ -201,13 +202,14 @@ void QDeclarativeAnchorsPrivate::fillChanged() void QDeclarativeAnchorsPrivate::centerInChanged() { + Q_Q(QDeclarativeAnchors); if (!centerIn || fill || !isItemComplete()) return; if (updatingCenterIn < 2) { ++updatingCenterIn; - qreal effectiveHCenterOffset = isMirrored() ? -hCenterOffset : hCenterOffset; + qreal effectiveHCenterOffset = q->mirrored() ? -hCenterOffset : hCenterOffset; if (centerIn == item->parentItem()) { QPointF p(hcenter(item->parentItem()) - hcenter(item) + effectiveHCenterOffset, vcenter(item->parentItem()) - vcenter(item) + vCenterOffset); @@ -315,6 +317,13 @@ void QDeclarativeAnchors::componentComplete() d->componentComplete = true; } +bool QDeclarativeAnchors::mirrored() +{ + Q_D(QDeclarativeAnchors); + QGraphicsItemPrivate * itemPrivate = QGraphicsItemPrivate::get(d->item); + return itemPrivate->isDeclarativeItem ? static_cast(itemPrivate)->effectiveLayoutMirror : false; +} + void QDeclarativeAnchorsPrivate::setItemHeight(qreal v) { updatingMe = true; @@ -502,11 +511,6 @@ bool QDeclarativeAnchorsPrivate::calcStretch(const QDeclarativeAnchorLine &edge1 return invalid; } -bool QDeclarativeAnchorsPrivate::isMirrored() const -{ - return layoutDirection == Qt::RightToLeft; -} - void QDeclarativeAnchorsPrivate::updateVerticalAnchors() { if (fill || centerIn || !isItemComplete()) @@ -591,6 +595,7 @@ inline QDeclarativeAnchorLine::AnchorLine reverseAnchorLine(QDeclarativeAnchorLi void QDeclarativeAnchorsPrivate::updateHorizontalAnchors() { + Q_Q(QDeclarativeAnchors); if (fill || centerIn || !isItemComplete()) return; @@ -599,7 +604,7 @@ void QDeclarativeAnchorsPrivate::updateHorizontalAnchors() qreal effectiveRightMargin, effectiveLeftMargin, effectiveHorizontalCenterOffset; QDeclarativeAnchorLine effectiveLeft, effectiveRight, effectiveHorizontalCenter; QDeclarativeAnchors::Anchor effectiveLeftAnchor, effectiveRightAnchor; - if (isMirrored()) { + if (q->mirrored()) { effectiveLeftAnchor = QDeclarativeAnchors::RightAnchor; effectiveRightAnchor = QDeclarativeAnchors::LeftAnchor; effectiveLeft.item = right.item; @@ -1086,25 +1091,6 @@ QDeclarativeAnchors::Anchors QDeclarativeAnchors::usedAnchors() const return d->usedAnchors; } - -Qt::LayoutDirection QDeclarativeAnchors::layoutDirection() const -{ - Q_D(const QDeclarativeAnchors); - return d->layoutDirection; -} - -void QDeclarativeAnchors::setLayoutDirection(Qt::LayoutDirection layoutDirection) -{ - Q_D(QDeclarativeAnchors); - if (d->layoutDirection != layoutDirection) { - d->layoutDirection = layoutDirection; - d->fillChanged(); - d->centerInChanged(); - d->updateHorizontalAnchors(); - emit layoutDirectionChanged(); - } -} - bool QDeclarativeAnchorsPrivate::checkHValid() const { if (usedAnchors & QDeclarativeAnchors::LeftAnchor && diff --git a/src/declarative/graphicsitems/qdeclarativeanchors_p.h b/src/declarative/graphicsitems/qdeclarativeanchors_p.h index 90a3508..388d6b9 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors_p.h +++ b/src/declarative/graphicsitems/qdeclarativeanchors_p.h @@ -79,7 +79,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeAnchors : public QObject Q_PROPERTY(qreal baselineOffset READ baselineOffset WRITE setBaselineOffset NOTIFY baselineOffsetChanged) Q_PROPERTY(QGraphicsObject *fill READ fill WRITE setFill RESET resetFill NOTIFY fillChanged) Q_PROPERTY(QGraphicsObject *centerIn READ centerIn WRITE setCenterIn RESET resetCenterIn NOTIFY centerInChanged) - Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) + Q_PROPERTY(bool mirrored READ mirrored NOTIFY mirroredChanged REVISION 1) public: QDeclarativeAnchors(QObject *parent=0); @@ -161,12 +161,11 @@ public: Anchors usedAnchors() const; - Qt::LayoutDirection layoutDirection() const; - void setLayoutDirection (Qt::LayoutDirection); - void classBegin(); void componentComplete(); + bool mirrored(); + Q_SIGNALS: void leftChanged(); void rightChanged(); @@ -185,10 +184,11 @@ Q_SIGNALS: void verticalCenterOffsetChanged(); void horizontalCenterOffsetChanged(); void baselineOffsetChanged(); - Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void mirroredChanged(); private: friend class QDeclarativeItem; + friend class QDeclarativeItemPrivate; friend class QDeclarativeGraphicsWidget; Q_DISABLE_COPY(QDeclarativeAnchors) Q_DECLARE_PRIVATE(QDeclarativeAnchors) diff --git a/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h b/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h index ec96582..d8d2f15 100644 --- a/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeanchors_p_p.h @@ -94,7 +94,7 @@ public: : componentComplete(true), updatingMe(false), 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), layoutDirection(Qt::LeftToRight) + margins(0), vCenterOffset(0), hCenterOffset(0), baselineOffset(0) { } @@ -161,8 +161,6 @@ public: qreal vCenterOffset; qreal hCenterOffset; qreal baselineOffset; - - Qt::LayoutDirection layoutDirection; }; QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 2eeadf4..f6810ed 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -73,7 +73,7 @@ public: if (view->flow() == QDeclarativeGridView::LeftToRight) { rowPos = item->y(); } else { - if (view->layoutDirection() == Qt::RightToLeft) + if (view->effectiveLayoutDirection() == Qt::RightToLeft) rowPos = -view->cellWidth()-item->x(); else rowPos = item->x(); @@ -83,7 +83,7 @@ public: qreal colPos() const { qreal colPos = 0; if (view->flow() == QDeclarativeGridView::LeftToRight) { - if (view->layoutDirection() == Qt::RightToLeft) { + if (view->effectiveLayoutDirection() == Qt::RightToLeft) { int colSize = view->cellWidth(); int columns = view->width()/colSize; colPos = colSize * (columns-1) - item->x(); @@ -101,14 +101,14 @@ public: if (view->flow() == QDeclarativeGridView::LeftToRight) { return item->y() + view->cellHeight() - 1; } else { - if (view->layoutDirection() == Qt::RightToLeft) + if (view->effectiveLayoutDirection() == Qt::RightToLeft) return -item->x() - 1; else return item->x() + view->cellWidth() - 1; } } void setPosition(qreal col, qreal row) { - if (view->layoutDirection() == Qt::RightToLeft) { + if (view->effectiveLayoutDirection() == Qt::RightToLeft) { if (view->flow() == QDeclarativeGridView::LeftToRight) { int columns = view->width()/view->cellWidth(); item->setPos(QPointF((view->cellWidth() * (columns-1) - col), row)); @@ -188,7 +188,24 @@ public: } bool isRightToLeftTopToBottom() const { - return flow == QDeclarativeGridView::TopToBottom && layoutDirection == Qt::RightToLeft; + Q_Q(const QDeclarativeGridView); + return flow == QDeclarativeGridView::TopToBottom && q->effectiveLayoutDirection() == Qt::RightToLeft; + } + + void regenerate() { + Q_Q(QDeclarativeGridView); + if (q->isComponentComplete()) { + clear(); + updateGrid(); + q->refill(); + updateCurrent(currentIndex); + } + } + + void mirrorChange() { + Q_Q(QDeclarativeGridView); + regenerate(); + emit q->effectiveLayoutDirectionChanged(); } qreal position() const { @@ -201,7 +218,7 @@ public: q->QDeclarativeFlickable::setContentY(pos); q->QDeclarativeFlickable::setContentX(0); } else { - if (layoutDirection == Qt::LeftToRight) + if (q->effectiveLayoutDirection() == Qt::LeftToRight) q->QDeclarativeFlickable::setContentX(pos); else q->QDeclarativeFlickable::setContentX(-pos-size()); @@ -918,7 +935,7 @@ void QDeclarativeGridViewPrivate::updateFooter() rowOffset = footer->item->width()-cellWidth; } else { rowOffset = 0; - if (layoutDirection == Qt::RightToLeft) + if (q->effectiveLayoutDirection() == Qt::RightToLeft) colOffset = footer->item->width()-cellWidth; } if (visibleItems.count()) { @@ -971,7 +988,7 @@ void QDeclarativeGridViewPrivate::updateHeader() rowOffset = -cellWidth; } else { rowOffset = -headerSize(); - if (layoutDirection == Qt::RightToLeft) + if (q->effectiveLayoutDirection() == Qt::RightToLeft) colOffset = header->item->width()-cellWidth; } if (visibleItems.count()) { @@ -1761,7 +1778,6 @@ void QDeclarativeGridView::setHighlightRangeMode(HighlightRangeMode mode) \bold Note: If GridView::flow is set to GridView.LeftToRight, this is not to be confused if GridView::layoutDirection is set to Qt.RightToLeft. The GridView.LeftToRight flow value simply indicates that the flow is horizontal. - */ Qt::LayoutDirection QDeclarativeGridView::layoutDirection() const @@ -1775,15 +1791,33 @@ void QDeclarativeGridView::setLayoutDirection(Qt::LayoutDirection layoutDirectio Q_D(QDeclarativeGridView); if (d->layoutDirection != layoutDirection) { d->layoutDirection = layoutDirection; - d->clear(); - d->updateGrid(); - refill(); - d->updateCurrent(d->currentIndex); + d->regenerate(); emit layoutDirectionChanged(); + emit effectiveLayoutDirectionChanged(); } } /*! + \qmlproperty enumeration GridView::effectiveLayoutDirection + This property holds the effective layout direction of the grid. + + When using the attached property \l {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + the visual layout direction of the grid will be mirrored. However, the + property \l {GridView::layoutDirection}{layoutDirection} will remain unchanged. + + \sa GridView::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ + +Qt::LayoutDirection QDeclarativeGridView::effectiveLayoutDirection() const +{ + Q_D(const QDeclarativeGridView); + if (d->effectiveLayoutMirror) + return d->layoutDirection == Qt::RightToLeft ? Qt::LeftToRight : Qt::RightToLeft; + else + return d->layoutDirection; +} + +/*! \qmlproperty enumeration GridView::flow This property holds the flow of the grid. @@ -1814,10 +1848,7 @@ void QDeclarativeGridView::setFlow(Flow flow) } setContentX(0); setContentY(0); - d->clear(); - d->updateGrid(); - refill(); - d->updateCurrent(d->currentIndex); + d->regenerate(); emit flowChanged(); } } @@ -2338,7 +2369,7 @@ void QDeclarativeGridView::moveCurrentIndexLeft() if (!count) return; - if (d->layoutDirection == Qt::LeftToRight) { + if (effectiveLayoutDirection() == Qt::LeftToRight) { if (d->flow == QDeclarativeGridView::LeftToRight) { if (currentIndex() > 0 || d->wrap) { int index = currentIndex() - 1; @@ -2381,7 +2412,7 @@ void QDeclarativeGridView::moveCurrentIndexRight() if (!count) return; - if (d->layoutDirection == Qt::LeftToRight) { + if (effectiveLayoutDirection() == Qt::LeftToRight) { if (d->flow == QDeclarativeGridView::LeftToRight) { if (currentIndex() < count - 1 || d->wrap) { int index = currentIndex() + 1; diff --git a/src/declarative/graphicsitems/qdeclarativegridview_p.h b/src/declarative/graphicsitems/qdeclarativegridview_p.h index fc9e6b4..ad0c609 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview_p.h +++ b/src/declarative/graphicsitems/qdeclarativegridview_p.h @@ -74,7 +74,8 @@ class Q_AUTOTEST_EXPORT QDeclarativeGridView : public QDeclarativeFlickable Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode NOTIFY highlightRangeModeChanged) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) - Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) //Versioning support? + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) + Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged REVISION 1) Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged) Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged) Q_PROPERTY(int cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged) @@ -132,6 +133,7 @@ public: Qt::LayoutDirection layoutDirection() const; void setLayoutDirection(Qt::LayoutDirection); + Qt::LayoutDirection effectiveLayoutDirection() const; enum Flow { LeftToRight, TopToBottom }; Flow flow() const; @@ -191,7 +193,8 @@ Q_SIGNALS: void modelChanged(); void delegateChanged(); void flowChanged(); - void layoutDirectionChanged(); + Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void effectiveLayoutDirectionChanged(); void keyNavigationWrapsChanged(); void cacheBufferChanged(); void snapModeChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index ac5d55c..d16025d 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -39,12 +39,12 @@ ** ****************************************************************************/ -#include "private/qdeclarativeitem_p.h" #include "qdeclarativeitem.h" #include "private/qdeclarativeevents_p_p.h" #include #include +#include #include #include @@ -731,6 +731,152 @@ void QDeclarativeKeyNavigationAttached::setFocusNavigation(QDeclarativeItem *cur } /*! + \qmlclass LayoutMirroring QDeclarativeLayoutMirroringAttached + \since QtQuick 1.1 + \ingroup qml-utility-elements + \brief The LayoutMirroring is used for mirroring the Qt Quick application layouts. + + LayoutMirroring \l enabled property can be used to horizontally mirror \l {anchor-layout}{Item anchors}, + \l{Using QML Positioner and Repeater Items}{Positioner} elements and QML views like \l {GridView}{GridView} + and horizontal \l {ListView}{ListView}. Mirroring is a visual change, left anchors will become + right anchors and left-to-right positioner will instead position child items from right to left. + By default setting the \l enabled property to true only affects the item in question. You can set property + LayoutDirection \l childrenInherit to true if you want the item children also inherit the mirror setting. + If no attached property has been defined, mirroring is disabled. + + The following example shows mirroring in action. When \l enabled is set to true, left anchor + becomes right, and \l {Row}{Row} starts positioning items in a reverse order: + + \snippet doc/src/snippets/declarative/layoutmirroring.qml 0 + + Layout mirroring is useful when you need to support both left-to-right and right-to-left + layout versions of your application that target different language areas. Inheritance saves + you from having to mirror the layouts manually for each layout item in your application. Keep + in mind however that the mirroring does not affect the positioning done by modifying Item's x + co-ordinate directly, so even with the mirroring enabled you will often need to do some layout + fixes to support the other reading direction. Also, there are cases where you need to disable + mirroring of individual child items, either because mirroring is not the wanted behavior or + because the item already implements mirroring in some custom way. +*/ + +/*! + \qmlproperty bool LayoutMirroring::enabled + + Setting this property to true mirrors item's layout horizontally, whether the layout is done + using \l {anchor-layout}{anchors}, \l{Using QML Positioner and Repeater Items}{Positioners} + or as a QML view \l {GridView}{GridView} or \l {ListView}{ListView}. +*/ + +/*! + \qmlproperty bool LayoutMirroring::childrenInherit + + This property can be set to true if you want the item children + to inherit the item's mirror setting. +*/ + +QDeclarativeLayoutMirroringAttached::QDeclarativeLayoutMirroringAttached(QObject *parent) : QObject(parent), itemPrivate(0) +{ + if (QDeclarativeItem *item = qobject_cast(parent)) { + itemPrivate = QDeclarativeItemPrivate::get(item); + itemPrivate->attachedLayoutDirection = this; + } else + qmlInfo(parent) << tr("LayoutDirection attached property only works with Items"); +} + +QDeclarativeLayoutMirroringAttached * QDeclarativeLayoutMirroringAttached::qmlAttachedProperties(QObject *object) +{ + return new QDeclarativeLayoutMirroringAttached(object); +} + +bool QDeclarativeLayoutMirroringAttached::enabled() const +{ + return itemPrivate ? itemPrivate->effectiveLayoutMirror : false; +} + +void QDeclarativeLayoutMirroringAttached::setEnabled(bool enabled) +{ + if (!itemPrivate) + return; + + itemPrivate->isMirrorImplicit = false; + if (enabled != itemPrivate->effectiveLayoutMirror) { + itemPrivate->setLayoutMirror(enabled); + if (itemPrivate->inheritMirrorFromItem) + itemPrivate->resolveLayoutMirror(); + } +} + +void QDeclarativeLayoutMirroringAttached::resetEnabled() +{ + if (itemPrivate && !itemPrivate->isMirrorImplicit) { + itemPrivate->isMirrorImplicit = true; + itemPrivate->resolveLayoutMirror(); + } +} + +bool QDeclarativeLayoutMirroringAttached::childrenInherit() const +{ + return itemPrivate ? itemPrivate->inheritMirrorFromItem : false; +} + +void QDeclarativeLayoutMirroringAttached::setChildrenInherit(bool childrenInherit) { + if (itemPrivate && childrenInherit != itemPrivate->inheritMirrorFromItem) { + itemPrivate->inheritMirrorFromItem = childrenInherit; + itemPrivate->resolveLayoutMirror(); + childrenInheritChanged(); + } +} + +void QDeclarativeItemPrivate::resolveLayoutMirror() +{ + Q_Q(QDeclarativeItem); + if (QDeclarativeItem *parentItem = q->parentItem()) { + QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parentItem); + setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent); + } else { + setImplicitLayoutMirror(isMirrorImplicit ? false : effectiveLayoutMirror, inheritMirrorFromItem); + } +} + +void QDeclarativeItemPrivate::setImplicitLayoutMirror(bool mirror, bool inherit) +{ + inherit = inherit || inheritMirrorFromItem; + if (!isMirrorImplicit && inheritMirrorFromItem) + mirror = effectiveLayoutMirror; + if (mirror == inheritedLayoutMirror && inherit == inheritMirrorFromParent) + return; + + inheritMirrorFromParent = inherit; + inheritedLayoutMirror = inheritMirrorFromParent ? mirror : false; + + if (isMirrorImplicit) + setLayoutMirror(inherit ? inheritedLayoutMirror : false); + for (int i = 0; i < children.count(); ++i) { + if (QDeclarativeItem *child = qobject_cast(children.at(i))) { + QDeclarativeItemPrivate *childPrivate = QDeclarativeItemPrivate::get(child); + childPrivate->setImplicitLayoutMirror(inheritedLayoutMirror, inheritMirrorFromParent); + } + } +} + +void QDeclarativeItemPrivate::setLayoutMirror(bool mirror) +{ + if (mirror != effectiveLayoutMirror) { + effectiveLayoutMirror = mirror; + if (_anchors) { + _anchors->d_func()->fillChanged(); + _anchors->d_func()->centerInChanged(); + _anchors->d_func()->updateHorizontalAnchors(); + emit _anchors->mirroredChanged(); + } + mirrorChange(); + if (attachedLayoutDirection) { + emit attachedLayoutDirection->enabledChanged(); + } + } +} + +/*! \qmlclass Keys QDeclarativeKeysAttached \ingroup qml-basic-interaction-elements \since 4.7 @@ -1389,6 +1535,11 @@ QDeclarativeKeysAttached *QDeclarativeKeysAttached::qmlAttachedProperties(QObjec \endqml See the \l {Keys}{Keys} attached property for detailed documentation. + + \section1 Layout Mirroring + + Item layouts can be mirrored using \l {LayoutMirroring}{LayoutMirroring} attached property. + */ /*! @@ -2782,6 +2933,7 @@ QVariant QDeclarativeItem::itemChange(GraphicsItemChange change, Q_D(QDeclarativeItem); switch (change) { case ItemParentHasChanged: + d->resolveLayoutMirror(); emit parentChanged(parentItem()); d->parentNotifier.notify(); break; diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h index 4303c0a..b204d7f 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem_p.h +++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h @@ -77,6 +77,7 @@ QT_BEGIN_NAMESPACE class QNetworkReply; class QDeclarativeItemKeyFilter; +class QDeclarativeLayoutMirroringAttached; //### merge into private? class QDeclarativeContents : public QObject, public QDeclarativeItemChangeListener @@ -125,8 +126,10 @@ public: _stateGroup(0), origin(QDeclarativeItem::Center), widthValid(false), heightValid(false), componentComplete(true), keepMouse(false), - smooth(false), transformOriginDirty(true), doneEventPreHandler(false), keyHandler(0), - mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), hadSubFocusItem(false) + smooth(false), transformOriginDirty(true), doneEventPreHandler(false), + inheritedLayoutMirror(false), effectiveLayoutMirror(false), isMirrorImplicit(true), + inheritMirrorFromParent(false), inheritMirrorFromItem(false), keyHandler(0), + mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), attachedLayoutDirection(0), hadSubFocusItem(false) { QGraphicsItemPrivate::acceptedMouseButtons = 0; isDeclarativeItem = 1; @@ -134,7 +137,6 @@ public: QGraphicsItem::ItemHasNoContents | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemNegativeZStacksBehindParent); - } void init(QDeclarativeItem *parent) @@ -146,6 +148,11 @@ public: } baselineOffset.invalidate(); mouseSetsFocus = false; + resolveLayoutMirror(); + } + + bool isMirrored() const { + return effectiveLayoutMirror; } // Private Properties @@ -162,6 +169,10 @@ public: virtual void implicitWidthChanged(); virtual void implicitHeightChanged(); + void resolveLayoutMirror(); + void setImplicitLayoutMirror(bool mirror, bool inherit); + void setLayoutMirror(bool mirror); + QDeclarativeListProperty data(); QDeclarativeListProperty resources(); @@ -272,6 +283,11 @@ public: bool smooth:1; bool transformOriginDirty : 1; bool doneEventPreHandler : 1; + bool inheritedLayoutMirror:1; + bool effectiveLayoutMirror:1; + bool isMirrorImplicit:1; + bool inheritMirrorFromParent:1; + bool inheritMirrorFromItem:1; QDeclarativeItemKeyFilter *keyHandler; @@ -280,6 +296,8 @@ public: qreal mImplicitWidth; qreal mImplicitHeight; + QDeclarativeLayoutMirroringAttached* attachedLayoutDirection; + bool hadSubFocusItem; QPointF computeTransformOrigin() const; @@ -326,6 +344,8 @@ public: virtual void focusChanged(bool); + virtual void mirrorChange() {}; + static qint64 consistentTime; static void setConsistentTime(qint64 t); static void start(QElapsedTimer &); @@ -423,6 +443,31 @@ private: void setFocusNavigation(QDeclarativeItem *currentItem, const char *dir); }; +class QDeclarativeLayoutMirroringAttached : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled RESET resetEnabled NOTIFY enabledChanged) + Q_PROPERTY(bool childrenInherit READ childrenInherit WRITE setChildrenInherit NOTIFY childrenInheritChanged) + +public: + explicit QDeclarativeLayoutMirroringAttached(QObject *parent = 0); + + bool enabled() const; + void setEnabled(bool); + void resetEnabled(); + + bool childrenInherit() const; + void setChildrenInherit(bool); + + static QDeclarativeLayoutMirroringAttached *qmlAttachedProperties(QObject *); +Q_SIGNALS: + void enabledChanged(); + void childrenInheritChanged(); +private: + friend class QDeclarativeItemPrivate; + QDeclarativeItemPrivate *itemPrivate; +}; + class QDeclarativeKeysAttachedPrivate : public QObjectPrivate { public: @@ -572,5 +617,7 @@ QML_DECLARE_TYPE(QDeclarativeKeysAttached) QML_DECLARE_TYPEINFO(QDeclarativeKeysAttached, QML_HAS_ATTACHED_PROPERTIES) QML_DECLARE_TYPE(QDeclarativeKeyNavigationAttached) QML_DECLARE_TYPEINFO(QDeclarativeKeyNavigationAttached, QML_HAS_ATTACHED_PROPERTIES) +QML_DECLARE_TYPE(QDeclarativeLayoutMirroringAttached) +QML_DECLARE_TYPEINFO(QDeclarativeLayoutMirroringAttached, QML_HAS_ATTACHED_PROPERTIES) #endif // QDECLARATIVEITEM_P_H diff --git a/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp b/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp index 3c8f64e..c4a9030 100644 --- a/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp @@ -198,6 +198,7 @@ void QDeclarativeItemModule::defineModule() qmlRegisterRevision("QtQuick",1,1); qmlRegisterRevision("QtQuick",1,0); qmlRegisterRevision("QtQuick",1,1); + qmlRegisterUncreatableType("QtQuick",1,1,"LayoutMirroring", QDeclarativeLayoutMirroringAttached::tr("LayoutMirroring is only available via attached properties")); #ifndef QT_NO_IMPORT_QT47_QML #ifdef QT_NO_MOVIE diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index 486cec8..879b99b 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -104,16 +104,17 @@ public: if (view->orientation() == QDeclarativeListView::Vertical) return section->y(); else - return (view->layoutDirection() == Qt::RightToLeft ? -section->width()-section->x() : section->x()); + return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -section->width()-section->x() : section->x()); } else { return itemPosition(); } } + qreal itemPosition() const { if (view->orientation() == QDeclarativeListView::Vertical) return item->y(); else - return (view->layoutDirection() == Qt::RightToLeft ? -item->width()-item->x() : item->x()); + return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -item->width()-item->x() : item->x()); } qreal size() const { if (section) @@ -133,7 +134,7 @@ public: if (view->orientation() == QDeclarativeListView::Vertical) { return item->y() + (item->height() >= 1.0 ? item->height() : 1) - 1; } else { - return (view->layoutDirection() == Qt::RightToLeft + return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -item->width()-item->x() + (item->width() >= 1.0 ? item->width() : 1) : item->x() + (item->width() >= 1.0 ? item->width() : 1)) - 1; } @@ -146,7 +147,7 @@ public: } item->setY(pos); } else { - if (view->layoutDirection() == Qt::RightToLeft) { + if (view->effectiveLayoutDirection() == Qt::RightToLeft) { if (section) { section->setX(-section->width()-pos); pos += section->width(); @@ -248,8 +249,25 @@ public: return 0; } + void regenerate() { + Q_Q(QDeclarativeListView); + if (q->isComponentComplete()) { + clear(); + setPosition(0); + q->refill(); + updateCurrent(currentIndex); + } + } + + void mirrorChange() { + Q_Q(QDeclarativeListView); + regenerate(); + emit q->effectiveLayoutDirectionChanged(); + } + bool isRightToLeft() const { - return (layoutDirection == Qt::RightToLeft && orient == QDeclarativeListView::Horizontal); + Q_Q(const QDeclarativeListView); + return orient == QDeclarativeListView::Horizontal && q->effectiveLayoutDirection() == Qt::RightToLeft; } qreal position() const { @@ -262,7 +280,7 @@ public: if (orient == QDeclarativeListView::Vertical) { q->QDeclarativeFlickable::setContentY(pos); } else { - if (layoutDirection == Qt::RightToLeft) + if (isRightToLeft()) q->QDeclarativeFlickable::setContentX(-pos-size()); else q->QDeclarativeFlickable::setContentX(pos); @@ -2064,14 +2082,25 @@ void QDeclarativeListView::setOrientation(QDeclarativeListView::Orientation orie setContentHeight(-1); setFlickableDirection(HorizontalFlick); } - d->clear(); - d->setPosition(0); - refill(); + d->regenerate(); emit orientationChanged(); - d->updateCurrent(d->currentIndex); } } +/*! + \qmlproperty enumeration ListView::layoutDirection + This property holds the layout direction of the horizontal list. + + Possible values: + + \list + \o Qt.LeftToRight (default) - Items will be laid out from left to right. + \o Qt.RightToLeft - Items will be laid out from right to let. + \endlist + + \sa ListView::effectiveLayoutDirection +*/ + Qt::LayoutDirection QDeclarativeListView::layoutDirection() const { Q_D(const QDeclarativeListView); @@ -2083,15 +2112,33 @@ void QDeclarativeListView::setLayoutDirection(Qt::LayoutDirection layoutDirectio Q_D(QDeclarativeListView); if (d->layoutDirection != layoutDirection) { d->layoutDirection = layoutDirection; - d->clear(); - d->setPosition(0); - refill(); + d->regenerate(); emit layoutDirectionChanged(); - d->updateCurrent(d->currentIndex); + emit effectiveLayoutDirectionChanged(); } } /*! + \qmlproperty enumeration ListView::effectiveLayoutDirection + This property holds the effective layout direction of the horizontal list. + + When using the attached property \l {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + the visual layout direction of the horizontal list will be mirrored. However, the + property \l {ListView::layoutDirection}{layoutDirection} will remain unchanged. + + \sa ListView::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ + +Qt::LayoutDirection QDeclarativeListView::effectiveLayoutDirection() const +{ + Q_D(const QDeclarativeListView); + if (d->effectiveLayoutMirror) + return d->layoutDirection == Qt::RightToLeft ? Qt::LeftToRight : Qt::RightToLeft; + else + return d->layoutDirection; +} + +/*! \qmlproperty bool ListView::keyNavigationWraps This property holds whether the list wraps key navigation. @@ -2671,8 +2718,8 @@ void QDeclarativeListView::keyPressEvent(QKeyEvent *event) return; if (d->model && d->model->count() && d->interactive) { - if ((d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::LeftToRight && event->key() == Qt::Key_Left) - || (d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::RightToLeft && event->key() == Qt::Key_Right) + if ((!d->isRightToLeft() && event->key() == Qt::Key_Left) + || (d->orient == QDeclarativeListView::Horizontal && d->isRightToLeft() && event->key() == Qt::Key_Right) || (d->orient == QDeclarativeListView::Vertical && event->key() == Qt::Key_Up)) { if (currentIndex() > 0 || (d->wrap && !event->isAutoRepeat())) { decrementCurrentIndex(); @@ -2682,8 +2729,8 @@ void QDeclarativeListView::keyPressEvent(QKeyEvent *event) event->accept(); return; } - } else if ((d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::LeftToRight && event->key() == Qt::Key_Right) - || (d->orient == QDeclarativeListView::Horizontal && d->layoutDirection == Qt::RightToLeft && event->key() == Qt::Key_Left) + } else if ((!d->isRightToLeft() && event->key() == Qt::Key_Right) + || (d->orient == QDeclarativeListView::Horizontal && d->isRightToLeft() && event->key() == Qt::Key_Left) || (d->orient == QDeclarativeListView::Vertical && event->key() == Qt::Key_Down)) { if (currentIndex() < d->model->count() - 1 || (d->wrap && !event->isAutoRepeat())) { incrementCurrentIndex(); @@ -2970,7 +3017,7 @@ void QDeclarativeListView::updateSections() void QDeclarativeListView::refill() { Q_D(QDeclarativeListView); - if (layoutDirection() == Qt::RightToLeft && orientation() == QDeclarativeListView::Horizontal) + if (d->isRightToLeft()) d->refill(-d->position()-d->size()+1, -d->position()); else d->refill(d->position(), d->position()+d->size()-1); @@ -3416,11 +3463,8 @@ void QDeclarativeListView::itemsChanged(int, int) void QDeclarativeListView::modelReset() { Q_D(QDeclarativeListView); - d->clear(); - d->setPosition(0); - refill(); d->moveReason = QDeclarativeListViewPrivate::SetIndex; - d->updateCurrent(d->currentIndex); + d->regenerate(); if (d->highlight && d->currentItem) { if (d->autoHighlight) d->highlight->setPosition(d->currentItem->position()); diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h index 6b72240..30171f8 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview_p.h +++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h @@ -113,7 +113,8 @@ class Q_AUTOTEST_EXPORT QDeclarativeListView : public QDeclarativeFlickable Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) Q_PROPERTY(Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged) - Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) + Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged REVISION 1) Q_PROPERTY(bool keyNavigationWraps READ isWrapEnabled WRITE setWrapEnabled NOTIFY keyNavigationWrapsChanged) Q_PROPERTY(int cacheBuffer READ cacheBuffer WRITE setCacheBuffer NOTIFY cacheBufferChanged) Q_PROPERTY(QDeclarativeViewSection *section READ sectionCriteria CONSTANT) @@ -174,6 +175,7 @@ public: Qt::LayoutDirection layoutDirection() const; void setLayoutDirection(Qt::LayoutDirection); + Qt::LayoutDirection effectiveLayoutDirection() const; bool isWrapEnabled() const; void setWrapEnabled(bool); @@ -226,7 +228,8 @@ Q_SIGNALS: void countChanged(); void spacingChanged(); void orientationChanged(); - void layoutDirectionChanged(); + Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void effectiveLayoutDirectionChanged(); void currentIndexChanged(); void currentSectionChanged(); void highlightMoveSpeedChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativepositioners.cpp b/src/declarative/graphicsitems/qdeclarativepositioners.cpp index 4560d32..f57f501 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners.cpp +++ b/src/declarative/graphicsitems/qdeclarativepositioners.cpp @@ -574,7 +574,7 @@ void QDeclarativeColumn::reportConflictingAnchors() \sa Grid::spacing */ QDeclarativeRow::QDeclarativeRow(QDeclarativeItem *parent) -: QDeclarativeBasePositioner(Horizontal, parent), m_layoutDirection(Qt::LeftToRight) +: QDeclarativeBasePositioner(Horizontal, parent) { } @@ -595,20 +595,39 @@ QDeclarativeRow::QDeclarativeRow(QDeclarativeItem *parent) */ Qt::LayoutDirection QDeclarativeRow::layoutDirection() const { - return m_layoutDirection; + return QDeclarativeBasePositionerPrivate::getLayoutDirection(this); } void QDeclarativeRow::setLayoutDirection(Qt::LayoutDirection layoutDirection) { - if (m_layoutDirection != layoutDirection) { - m_layoutDirection = layoutDirection; + QDeclarativeBasePositionerPrivate *d = static_cast(QDeclarativeBasePositionerPrivate::get(this)); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; prePositioning(); emit layoutDirectionChanged(); + emit effectiveLayoutDirectionChanged(); } } +/*! + \qmlproperty enumeration Row::effectiveLayoutDirection + This property holds the effective layout direction of the row positioner. + + When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + the visual layout direction of the row positioner will be mirrored. However, the + property \l {Row::layoutDirection}{layoutDirection} will remain unchanged. + + \sa Row::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ + +Qt::LayoutDirection QDeclarativeRow::effectiveLayoutDirection() const +{ + return QDeclarativeBasePositionerPrivate::getEffectiveLayoutDirection(this); +} + void QDeclarativeRow::doPositioning(QSizeF *contentSize) { + QDeclarativeBasePositionerPrivate *d = static_cast(QDeclarativeBasePositionerPrivate::get(this)); int hoffset = 0; QList hoffsets; @@ -617,7 +636,7 @@ void QDeclarativeRow::doPositioning(QSizeF *contentSize) if (!child.item || !child.isVisible) continue; - if(m_layoutDirection == Qt::LeftToRight){ + if(d->isLeftToRight()){ if(child.item->x() != hoffset) positionX(hoffset, child); }else{ @@ -632,7 +651,7 @@ void QDeclarativeRow::doPositioning(QSizeF *contentSize) contentSize->setWidth(hoffset - spacing()); - if(m_layoutDirection == Qt::LeftToRight) + if(d->isLeftToRight()) return; //Right to Left layout @@ -786,7 +805,7 @@ void QDeclarativeRow::reportConflictingAnchors() \sa rows, columns */ QDeclarativeGrid::QDeclarativeGrid(QDeclarativeItem *parent) : - QDeclarativeBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight), m_layoutDirection(Qt::LeftToRight) + QDeclarativeBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight) { } @@ -860,11 +879,11 @@ void QDeclarativeGrid::setFlow(Flow flow) Possible values are: \list - \o Qt.LeftToRight (default) - Items are positioned beginning - from the top, left anchor. The flow direction is dependent - on the \l Grid::flow property. - \o Qt.RightToLeft - Items are positioned beginning from the - top, right anchor. The flow direction is dependent on the + \o Qt.LeftToRight (default) - Items are positioned from the top to bottom, + and left to right. The flow direction is dependent on the + \l Grid::flow property. + \o Qt.RightToLeft - Items are positioned from the top to bottom, + and right to left. The flow direction is dependent on the \l Grid::flow property. \endlist @@ -872,21 +891,39 @@ void QDeclarativeGrid::setFlow(Flow flow) */ Qt::LayoutDirection QDeclarativeGrid::layoutDirection() const { - return m_layoutDirection; + return QDeclarativeBasePositionerPrivate::getLayoutDirection(this); } void QDeclarativeGrid::setLayoutDirection(Qt::LayoutDirection layoutDirection) { - if (m_layoutDirection != layoutDirection) { - m_layoutDirection = layoutDirection; + QDeclarativeBasePositionerPrivate *d = static_cast(QDeclarativeBasePositionerPrivate::get(this)); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; prePositioning(); emit layoutDirectionChanged(); + emit effectiveLayoutDirectionChanged(); } } -void QDeclarativeGrid::doPositioning(QSizeF *contentSize) +/*! + \qmlproperty enumeration Grid::effectiveLayoutDirection + This property holds the effective layout direction of the grid positioner. + + When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + the visual layout direction of the grid positioner will be mirrored. However, the + property \l {Grid::layoutDirection}{layoutDirection} will remain unchanged. + + \sa Grid::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ + +Qt::LayoutDirection QDeclarativeGrid::effectiveLayoutDirection() const { + return QDeclarativeBasePositionerPrivate::getEffectiveLayoutDirection(this); +} +void QDeclarativeGrid::doPositioning(QSizeF *contentSize) +{ + QDeclarativeBasePositionerPrivate *d = static_cast(QDeclarativeBasePositionerPrivate::get(this)); int c = m_columns; int r = m_rows; //Is allocating the extra QPODVector too much overhead? @@ -976,7 +1013,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) end = widthSum; int xoffset=0; - if(m_layoutDirection == Qt::RightToLeft) + if(!d->isLeftToRight()) xoffset=end; int yoffset=0; int curRow =0; @@ -984,7 +1021,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) for (int i = 0; i < visibleItems.count(); ++i) { const PositionedItem &child = visibleItems.at(i); int childXOffset = xoffset; - if(m_layoutDirection == Qt::RightToLeft) + if(!d->isLeftToRight()) childXOffset -= QGraphicsItemPrivate::get(child.item)->width(); if((child.item->x()!=childXOffset)||(child.item->y()!=yoffset)){ positionX(childXOffset, child); @@ -992,7 +1029,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) } if (m_flow == LeftToRight) { - if(m_layoutDirection == Qt::LeftToRight) + if(d->isLeftToRight()) xoffset+=maxColWidth[curCol]+spacing(); else xoffset-=maxColWidth[curCol]+spacing(); @@ -1000,7 +1037,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) curCol%=c; if (!curCol){ yoffset+=maxRowHeight[curRow]+spacing(); - if(m_layoutDirection == Qt::LeftToRight) + if(d->isLeftToRight()) xoffset=0; else xoffset=end; @@ -1013,7 +1050,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) curRow++; curRow%=r; if (!curRow){ - if(m_layoutDirection == Qt::LeftToRight) + if(d->isLeftToRight()) xoffset+=maxColWidth[curCol]+spacing(); else xoffset-=maxColWidth[curCol]+spacing(); @@ -1148,12 +1185,10 @@ class QDeclarativeFlowPrivate : public QDeclarativeBasePositionerPrivate public: QDeclarativeFlowPrivate() - : QDeclarativeBasePositionerPrivate(), flow(QDeclarativeFlow::LeftToRight), - layoutDirection(Qt::LeftToRight) + : QDeclarativeBasePositionerPrivate(), flow(QDeclarativeFlow::LeftToRight) {} QDeclarativeFlow::Flow flow; - Qt::LayoutDirection layoutDirection; }; QDeclarativeFlow::QDeclarativeFlow(QDeclarativeItem *parent) @@ -1202,11 +1237,11 @@ void QDeclarativeFlow::setFlow(Flow flow) Possible values are: \list - \o Qt.LeftToRight (default) - Items are positioned beginning - from the top, left anchor. The flow direction is dependent - on the \l Flow::flow property. - \o Qt.RightToLeft - Items are positioned beginning from the - top, right anchor. The flow direction is dependent on the + \o Qt.LeftToRight (default) - Items are positioned from the top to bottom, + and left to right. The flow direction is dependent on the + \l Flow::flow property. + \o Qt.RightToLeft - Items are positioned from the top to bottom, + and right to left. The flow direction is dependent on the \l Flow::flow property. \endlist @@ -1226,9 +1261,26 @@ void QDeclarativeFlow::setLayoutDirection(Qt::LayoutDirection layoutDirection) d->layoutDirection = layoutDirection; prePositioning(); emit layoutDirectionChanged(); + emit effectiveLayoutDirectionChanged(); } } +/*! + \qmlproperty enumeration Flow::effectiveLayoutDirection + This property holds the effective layout direction of the flow positioner. + + When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + the visual layout direction of the grid positioner will be mirrored. However, the + property \l {Flow::layoutDirection}{layoutDirection} will remain unchanged. + + \sa Flow::layoutDirection, {LayoutMirroring}{LayoutMirroring} +*/ + +Qt::LayoutDirection QDeclarativeFlow::effectiveLayoutDirection() const +{ + return QDeclarativeBasePositionerPrivate::getEffectiveLayoutDirection(this); +} + void QDeclarativeFlow::doPositioning(QSizeF *contentSize) { Q_D(QDeclarativeFlow); @@ -1258,7 +1310,7 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) } } - if(d->layoutDirection == Qt::LeftToRight){ + if(d->isLeftToRight()){ if(child.item->x() != hoffset) positionX(hoffset, child); }else{ @@ -1281,7 +1333,7 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) } } - if(d->layoutDirection == Qt::LeftToRight) + if(d->isLeftToRight()) return; int end; diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p.h index 55d8fa1..214c04f 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p.h @@ -130,20 +130,22 @@ class Q_AUTOTEST_EXPORT QDeclarativeRow: public QDeclarativeBasePositioner { Q_OBJECT Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) + Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged REVISION 1) public: QDeclarativeRow(QDeclarativeItem *parent=0); Qt::LayoutDirection layoutDirection() const; void setLayoutDirection (Qt::LayoutDirection); + Qt::LayoutDirection effectiveLayoutDirection() const; Q_SIGNALS: Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void effectiveLayoutDirectionChanged(); protected: virtual void doPositioning(QSizeF *contentSize); virtual void reportConflictingAnchors(); private: - Qt::LayoutDirection m_layoutDirection; Q_DISABLE_COPY(QDeclarativeRow) }; @@ -154,7 +156,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeGrid : public QDeclarativeBasePositioner Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) - + Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged REVISION 1) public: QDeclarativeGrid(QDeclarativeItem *parent=0); @@ -171,12 +173,14 @@ public: Qt::LayoutDirection layoutDirection() const; void setLayoutDirection (Qt::LayoutDirection); + Qt::LayoutDirection effectiveLayoutDirection() const; Q_SIGNALS: void rowsChanged(); void columnsChanged(); void flowChanged(); Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void effectiveLayoutDirectionChanged(); protected: virtual void doPositioning(QSizeF *contentSize); @@ -186,7 +190,6 @@ private: int m_rows; int m_columns; Flow m_flow; - Qt::LayoutDirection m_layoutDirection; Q_DISABLE_COPY(QDeclarativeGrid) }; @@ -194,8 +197,9 @@ class QDeclarativeFlowPrivate; class Q_AUTOTEST_EXPORT QDeclarativeFlow: public QDeclarativeBasePositioner { Q_OBJECT - Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) + Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged REVISION 1) public: QDeclarativeFlow(QDeclarativeItem *parent=0); @@ -206,10 +210,11 @@ public: Qt::LayoutDirection layoutDirection() const; void setLayoutDirection (Qt::LayoutDirection); - + Qt::LayoutDirection effectiveLayoutDirection() const; Q_SIGNALS: void flowChanged(); Q_REVISION(1) void layoutDirectionChanged(); + Q_REVISION(1) void effectiveLayoutDirectionChanged(); protected: virtual void doPositioning(QSizeF *contentSize); diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h index df105c6..e80129d 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h @@ -75,7 +75,7 @@ public: QDeclarativeBasePositionerPrivate() : spacing(0), type(QDeclarativeBasePositioner::None) , moveTransition(0), addTransition(0), queuedPositioning(false) - , doingPositioning(false), anchorConflict(false) + , doingPositioning(false), anchorConflict(false), layoutDirection(Qt::LeftToRight) { } @@ -100,6 +100,9 @@ public: bool doingPositioning : 1; bool anchorConflict : 1; + Qt::LayoutDirection layoutDirection; + + void schedulePositioning() { Q_Q(QDeclarativeBasePositioner); @@ -109,6 +112,18 @@ public: } } + void mirrorChange() { + Q_Q(QDeclarativeBasePositioner); + if (type != QDeclarativeBasePositioner::Vertical) + q->prePositioning(); + } + bool isLeftToRight() const { + if (type == QDeclarativeBasePositioner::Vertical) + return true; + else + return effectiveLayoutMirror ? layoutDirection == Qt::RightToLeft : layoutDirection == Qt::LeftToRight; + } + virtual void itemSiblingOrderChanged(QDeclarativeItem* other) { Q_UNUSED(other); @@ -139,6 +154,19 @@ public: Q_Q(QDeclarativeBasePositioner); q->positionedItems.removeOne(QDeclarativeBasePositioner::PositionedItem(item)); } + + static Qt::LayoutDirection getLayoutDirection(const QDeclarativeBasePositioner *positioner) + { + return positioner->d_func()->layoutDirection; + } + + static Qt::LayoutDirection getEffectiveLayoutDirection(const QDeclarativeBasePositioner *positioner) + { + if (positioner->d_func()->effectiveLayoutMirror) + return positioner->d_func()->layoutDirection == Qt::RightToLeft ? Qt::LeftToRight : Qt::RightToLeft; + else + return positioner->d_func()->layoutDirection; + } }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp b/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp index 79e233b..0442350 100644 --- a/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp +++ b/tests/auto/declarative/qdeclarativeanchors/tst_qdeclarativeanchors.cpp @@ -279,7 +279,7 @@ qreal offsetParentRTL(QDeclarativeItem *rootItem, const char * itemString) { void mirrorAnchors(QDeclarativeItem *item) { QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item); - itemPrivate->anchors()->setLayoutDirection(Qt::RightToLeft); + itemPrivate->setLayoutMirror(true); } void tst_qdeclarativeanchors::basicAnchorsRTL() @@ -290,9 +290,19 @@ void tst_qdeclarativeanchors::basicAnchorsRTL() qApp->processEvents(); QDeclarativeItem* rootItem = qobject_cast(view->rootObject()); + foreach(QObject *child, rootItem->children()) { + bool mirrored = QDeclarativeItemPrivate::get(qobject_cast(child))->anchors()->property("mirrored").toBool(); + QCOMPARE(mirrored, false); + } + foreach(QObject *child, rootItem->children()) mirrorAnchors(qobject_cast(child)); + foreach(QObject *child, rootItem->children()) { + bool mirrored = QDeclarativeItemPrivate::get(qobject_cast(child))->anchors()->property("mirrored").toBool(); + QCOMPARE(mirrored, true); + } + //sibling horizontal QCOMPARE(childItem(rootItem, "rect1")->x(), offsetMasterRTL(rootItem, "rect1")-26.0); QCOMPARE(childItem(rootItem, "rect2")->x(), offsetMasterRTL(rootItem, "rect2")-122.0); diff --git a/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml b/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml index 164103d..69eaa47 100644 --- a/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml +++ b/tests/auto/declarative/qdeclarativegridview/data/gridview-enforcerange.qml @@ -1,4 +1,4 @@ -import QtQuick 1.0 +import QtQuick 1.1 Rectangle { width: 240 diff --git a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml index 1f5943d..caa28d6 100644 --- a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml +++ b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml @@ -1,4 +1,4 @@ -import QtQuick 1.0 +import QtQuick 1.1 Rectangle { id: root diff --git a/tests/auto/declarative/qdeclarativegridview/data/mirroring.qml b/tests/auto/declarative/qdeclarativegridview/data/mirroring.qml new file mode 100644 index 0000000..54de16b --- /dev/null +++ b/tests/auto/declarative/qdeclarativegridview/data/mirroring.qml @@ -0,0 +1,43 @@ +// This example demonstrates how item positioning +// changes in right-to-left layout direction + +import QtQuick 1.1 + +Rectangle { + color: "lightgray" + width: 340 + height: 370 + + VisualItemModel { + id: itemModel + objectName: "itemModel" + Rectangle { + objectName: "item1" + height: 110; width: 120; color: "#FFFEF0" + Text { objectName: "text1"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item2" + height: 130; width: 150; color: "#F0FFF7" + Text { objectName: "text2"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + Rectangle { + objectName: "item3" + height: 170; width: 190; color: "#F4F0FF" + Text { objectName: "text3"; text: "index: " + parent.VisualItemModel.index; font.bold: true; anchors.centerIn: parent } + } + } + + GridView { + id: view + objectName: "view" + cellWidth: 190 + cellHeight: 170 + anchors.fill: parent + anchors.bottomMargin: 30 + model: itemModel + highlightRangeMode: "StrictlyEnforceRange" + flow: GridView.TopToBottom + flickDeceleration: 2000 + } +} diff --git a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp index 4fcaed6..5ced02b 100644 --- a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp +++ b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +80,7 @@ private slots: void modelChanges(); void positionViewAtIndex(); void positionViewAtIndex_rightToLeft(); + void mirroring(); void snapping(); void resetModel(); void enforceRange(); @@ -1347,6 +1349,67 @@ void tst_QDeclarativeGridView::snapping() } +void tst_QDeclarativeGridView::mirroring() +{ + QDeclarativeView *canvasA = createView(); + canvasA->setSource(QUrl::fromLocalFile(SRCDIR "/data/mirroring.qml")); + QDeclarativeGridView *gridviewA = findItem(canvasA->rootObject(), "view"); + QTRY_VERIFY(gridviewA != 0); + + QDeclarativeView *canvasB = createView(); + canvasB->setSource(QUrl::fromLocalFile(SRCDIR "/data/mirroring.qml")); + QDeclarativeGridView *gridviewB = findItem(canvasB->rootObject(), "view"); + QTRY_VERIFY(gridviewA != 0); + qApp->processEvents(); + + QList objectNames; + objectNames << "item1" << "item2"; // << "item3" + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + gridviewB->setProperty("layoutDirection", Qt::RightToLeft); + QCOMPARE(gridviewA->layoutDirection(), gridviewA->effectiveLayoutDirection()); + + // LTR != RTL + foreach(const QString objectName, objectNames) + QVERIFY(findItem(gridviewA, objectName)->x() != findItem(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + gridviewB->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == LTR + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(gridviewA, objectName)->x(), findItem(gridviewB, objectName)->x()); + + QVERIFY(gridviewB->layoutDirection() == gridviewB->effectiveLayoutDirection()); + QDeclarativeItemPrivate::get(gridviewB)->setLayoutMirror(true); + QVERIFY(gridviewB->layoutDirection() != gridviewB->effectiveLayoutDirection()); + + // LTR != LTR+mirror + foreach(const QString objectName, objectNames) + QVERIFY(findItem(gridviewA, objectName)->x() != findItem(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL == LTR+mirror + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(gridviewA, objectName)->x(), findItem(gridviewB, objectName)->x()); + + gridviewB->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL != RTL+mirror + foreach(const QString objectName, objectNames) + QVERIFY(findItem(gridviewA, objectName)->x() != findItem(gridviewB, objectName)->x()); + + gridviewA->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == RTL+mirror + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(gridviewA, objectName)->x(), findItem(gridviewB, objectName)->x()); + + delete canvasA; + delete canvasB; +} + void tst_QDeclarativeGridView::positionViewAtIndex_rightToLeft() { QDeclarativeView *canvas = createView(); @@ -1535,6 +1598,7 @@ void tst_QDeclarativeGridView::enforceRange() canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml")); qApp->processEvents(); + QVERIFY(canvas->rootObject() != 0); QDeclarativeGridView *gridview = findItem(canvas->rootObject(), "grid"); QTRY_VERIFY(gridview != 0); @@ -1590,6 +1654,7 @@ void tst_QDeclarativeGridView::enforceRange_rightToLeft() canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview-enforcerange.qml")); qApp->processEvents(); + QVERIFY(canvas->rootObject() != 0); QDeclarativeGridView *gridview = findItem(canvas->rootObject(), "grid"); QTRY_VERIFY(gridview != 0); diff --git a/tests/auto/declarative/qdeclarativeitem/data/layoutmirroring.qml b/tests/auto/declarative/qdeclarativeitem/data/layoutmirroring.qml new file mode 100644 index 0000000..866b615 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/layoutmirroring.qml @@ -0,0 +1,54 @@ +import QtQuick 1.1 + +Item { + property bool childrenInherit: true + Item { + objectName: "mirrored1" + LayoutMirroring.enabled: true + LayoutMirroring.childrenInherit: parent.childrenInherit + Item { + Item { + objectName: "notMirrored1" + LayoutMirroring.enabled: false + Item { + objectName: "inheritedMirror1" + } + } + Item { + objectName: "inheritedMirror2" + } + } + } + Item { + objectName: "mirrored2" + LayoutMirroring.enabled: true + LayoutMirroring.childrenInherit: false + Item { + objectName: "notMirrored2" + } + } + Item { + LayoutMirroring.enabled: true + LayoutMirroring.childrenInherit: true + Loader { + id: loader + } + } + states: State { + name: "newContent" + PropertyChanges { + target: loader + sourceComponent: component + } + } + Component { + id: component + Item { + objectName: "notMirrored3" + LayoutMirroring.enabled: false + Item { + objectName: "inheritedMirror3" + } + } + } +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index 137522d..f83207c 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -66,6 +66,8 @@ private slots: void keysProcessingOrder(); void keyNavigation(); void keyNavigation_skipNotVisible(); + void layoutMirroring(); + void layoutMirroringIllegalParent(); void smooth(); void clip(); void mapCoordinates(); @@ -87,13 +89,33 @@ private slots: void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); void qtbug_16871(); - private: - template - T *findItem(QGraphicsObject *parent, const QString &objectName); QDeclarativeEngine engine; }; +template +T *findItem(QGraphicsObject *parent, const QString &objectName) +{ + if (!parent) + return 0; + + const QMetaObject &mo = T::staticMetaObject; + //qDebug() << parent->QGraphicsObject::children().count() << "children"; + for (int i = 0; i < parent->childItems().count(); ++i) { + QDeclarativeItem *item = qobject_cast(parent->childItems().at(i)); + if(!item) + continue; + //qDebug() << "try" << item; + if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) + return static_cast(item); + item = findItem(item, objectName); + if (item) + return static_cast(item); + } + + return 0; +} + class KeysTestObject : public QObject { Q_OBJECT @@ -380,6 +402,132 @@ void tst_QDeclarativeItem::keysProcessingOrder() delete testObject; } +QDeclarativeItemPrivate *childPrivate(QGraphicsObject *rootItem, const char * itemString) +{ + QDeclarativeItem *item = findItem(rootItem, QString(QLatin1String(itemString))); + QDeclarativeItemPrivate* itemPrivate = QDeclarativeItemPrivate::get(item); + return itemPrivate; +} + +QVariant childProperty(QGraphicsObject *rootItem, const char * itemString, const char * property) +{ + QDeclarativeItem *item = findItem(rootItem, QString(QLatin1String(itemString))); + return item->property(property); +} + +bool anchorsMirrored(QGraphicsObject *rootItem, const char * itemString) +{ + QDeclarativeItem *item = findItem(rootItem, QString(QLatin1String(itemString))); + QDeclarativeItemPrivate* itemPrivate = QDeclarativeItemPrivate::get(item); + return itemPrivate->anchors()->mirrored(); +} + +void tst_QDeclarativeItem::layoutMirroring() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/layoutmirroring.qml")); + canvas->show(); + + QDeclarativeItem *rootItem = qobject_cast(canvas->rootObject()); + QVERIFY(rootItem); + QDeclarativeItemPrivate *rootPrivate = QDeclarativeItemPrivate::get(rootItem); + QVERIFY(rootPrivate); + + QCOMPARE(childPrivate(rootItem, "mirrored1")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "mirrored2")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->effectiveLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "notMirrored2")->effectiveLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->effectiveLayoutMirror, true); + + QCOMPARE(anchorsMirrored(rootItem, "mirrored1"), true); + QCOMPARE(anchorsMirrored(rootItem, "mirrored2"), true); + QCOMPARE(anchorsMirrored(rootItem, "notMirrored1"), false); + QCOMPARE(anchorsMirrored(rootItem, "notMirrored2"), false); + QCOMPARE(anchorsMirrored(rootItem, "inheritedMirror1"), true); + QCOMPARE(anchorsMirrored(rootItem, "inheritedMirror2"), true); + + QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "mirrored2")->inheritedLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "notMirrored2")->inheritedLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritedLayoutMirror, true); + + QCOMPARE(childPrivate(rootItem, "mirrored1")->isMirrorImplicit, false); + QCOMPARE(childPrivate(rootItem, "mirrored2")->isMirrorImplicit, false); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->isMirrorImplicit, false); + QCOMPARE(childPrivate(rootItem, "notMirrored2")->isMirrorImplicit, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->isMirrorImplicit, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->isMirrorImplicit, true); + + QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritMirrorFromParent, true); + QCOMPARE(childPrivate(rootItem, "mirrored2")->inheritMirrorFromParent, false); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritMirrorFromParent, true); + QCOMPARE(childPrivate(rootItem, "notMirrored2")->inheritMirrorFromParent, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->inheritMirrorFromParent, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritMirrorFromParent, true); + + QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritMirrorFromItem, true); + QCOMPARE(childPrivate(rootItem, "mirrored2")->inheritMirrorFromItem, false); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritMirrorFromItem, false); + QCOMPARE(childPrivate(rootItem, "notMirrored2")->inheritMirrorFromItem, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->inheritMirrorFromItem, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritMirrorFromItem, false); + + // load dynamic content using Loader that needs to inherit mirroring + rootItem->setProperty("state", "newContent"); + QCOMPARE(childPrivate(rootItem, "notMirrored3")->effectiveLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror3")->effectiveLayoutMirror, true); + + QCOMPARE(childPrivate(rootItem, "notMirrored3")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror3")->inheritedLayoutMirror, true); + + QCOMPARE(childPrivate(rootItem, "notMirrored3")->isMirrorImplicit, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror3")->isMirrorImplicit, true); + + QCOMPARE(childPrivate(rootItem, "notMirrored3")->inheritMirrorFromParent, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror3")->inheritMirrorFromParent, true); + + QCOMPARE(childPrivate(rootItem, "notMirrored3")->inheritMirrorFromItem, false); + QCOMPARE(childPrivate(rootItem, "notMirrored3")->inheritMirrorFromItem, false); + + // disable inheritance + rootItem->setProperty("childrenInherit", false); + + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->effectiveLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->effectiveLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "mirrored1")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->effectiveLayoutMirror, false); + + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->inheritedLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritedLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritedLayoutMirror, false); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritedLayoutMirror, false); + + // re-enable inheritance + rootItem->setProperty("childrenInherit", true); + + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "mirrored1")->effectiveLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->effectiveLayoutMirror, false); + + QCOMPARE(childPrivate(rootItem, "inheritedMirror1")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritedLayoutMirror, true); + QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritedLayoutMirror, true); +} + +void tst_QDeclarativeItem::layoutMirroringIllegalParent() +{ + QDeclarativeComponent component(&engine); + component.setData("import QtQuick 1.1; QtObject { LayoutMirroring.enabled: true; LayoutMirroring.childrenInherit: true }", QUrl::fromLocalFile("")); + QTest::ignoreMessage(QtWarningMsg, "file::1:21: QML QtObject: LayoutDirection attached property only works with Items"); + QObject *object = component.create(); + QVERIFY(object != 0); +} + void tst_QDeclarativeItem::keyNavigation() { QDeclarativeView *canvas = new QDeclarativeView(0); @@ -1003,32 +1151,6 @@ void tst_QDeclarativeItem::qtbug_16871() delete o; } - -template -T *tst_QDeclarativeItem::findItem(QGraphicsObject *parent, const QString &objectName) -{ - if (!parent) - return 0; - - const QMetaObject &mo = T::staticMetaObject; - //qDebug() << parent->QGraphicsObject::children().count() << "children"; - for (int i = 0; i < parent->childItems().count(); ++i) { - QDeclarativeItem *item = qobject_cast(parent->childItems().at(i)); - if(!item) - continue; - //qDebug() << "try" << item; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) - return static_cast(item); - item = findItem(item, objectName); - if (item) - return static_cast(item); - } - - return 0; -} - - - QTEST_MAIN(tst_QDeclarativeItem) #include "tst_qdeclarativeitem.moc" diff --git a/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml b/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml index e31d923..1e92bb3 100644 --- a/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml +++ b/tests/auto/declarative/qdeclarativelistview/data/rightToLeft.qml @@ -1,11 +1,11 @@ -// This example demonstrates placing items in a view using -// a VisualItemModel +// This example demonstrates how item positioning +// changes in right-to-left layout direction -import QtQuick 1.0 +import QtQuick 1.1 Rectangle { color: "lightgray" - width: 240 + width: 640 height: 320 VisualItemModel { diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index 02c8dad..bba86c3 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,7 @@ private slots: void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); void rightToLeft(); + void test_mirroring(); private: template void items(); @@ -1697,8 +1699,6 @@ void tst_QDeclarativeListView::manualHighlight() QDeclarativeView *canvas = new QDeclarativeView(0); canvas->setFixedSize(240,320); - QDeclarativeContext *ctxt = canvas->rootContext(); - QString filename(SRCDIR "/data/manual-highlight.qml"); canvas->setSource(QUrl::fromLocalFile(filename)); @@ -1829,8 +1829,6 @@ void tst_QDeclarativeListView::header() TestModel model; - QDeclarativeContext *ctxt = canvas->rootContext(); - canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/header1.qml")); qApp->processEvents(); @@ -2303,10 +2301,10 @@ void tst_QDeclarativeListView::testQtQuick11Attributes_data() void tst_QDeclarativeListView::rightToLeft() { QDeclarativeView *canvas = createView(); - canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml")); qApp->processEvents(); + QVERIFY(canvas->rootObject() != 0); QDeclarativeListView *listview = findItem(canvas->rootObject(), "view"); QTRY_VERIFY(listview != 0); @@ -2341,6 +2339,67 @@ void tst_QDeclarativeListView::rightToLeft() delete canvas; } +void tst_QDeclarativeListView::test_mirroring() +{ + QDeclarativeView *canvasA = createView(); + canvasA->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml")); + QDeclarativeListView *listviewA = findItem(canvasA->rootObject(), "view"); + QTRY_VERIFY(listviewA != 0); + + QDeclarativeView *canvasB = createView(); + canvasB->setSource(QUrl::fromLocalFile(SRCDIR "/data/rightToLeft.qml")); + QDeclarativeListView *listviewB = findItem(canvasB->rootObject(), "view"); + QTRY_VERIFY(listviewA != 0); + qApp->processEvents(); + + QList objectNames; + objectNames << "item1" << "item2"; // << "item3" + + listviewA->setProperty("layoutDirection", Qt::LeftToRight); + listviewB->setProperty("layoutDirection", Qt::RightToLeft); + QCOMPARE(listviewA->layoutDirection(), listviewA->effectiveLayoutDirection()); + + // LTR != RTL + foreach(const QString objectName, objectNames) + QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); + + listviewA->setProperty("layoutDirection", Qt::LeftToRight); + listviewB->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == LTR + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); + + QVERIFY(listviewB->layoutDirection() == listviewB->effectiveLayoutDirection()); + QDeclarativeItemPrivate::get(listviewB)->setLayoutMirror(true); + QVERIFY(listviewB->layoutDirection() != listviewB->effectiveLayoutDirection()); + + // LTR != LTR+mirror + foreach(const QString objectName, objectNames) + QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); + + listviewA->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL == LTR+mirror + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); + + listviewB->setProperty("layoutDirection", Qt::RightToLeft); + + // RTL != RTL+mirror + foreach(const QString objectName, objectNames) + QVERIFY(findItem(listviewA, objectName)->x() != findItem(listviewB, objectName)->x()); + + listviewA->setProperty("layoutDirection", Qt::LeftToRight); + + // LTR == RTL+mirror + foreach(const QString objectName, objectNames) + QCOMPARE(findItem(listviewA, objectName)->x(), findItem(listviewB, objectName)->x()); + + delete canvasA; + delete canvasB; +} + void tst_QDeclarativeListView::qListModelInterface_items() { items(); diff --git a/tests/auto/declarative/qdeclarativepositioners/data/grid-righttoleft.qml b/tests/auto/declarative/qdeclarativepositioners/data/grid-righttoleft.qml deleted file mode 100644 index 0ec1f37..0000000 --- a/tests/auto/declarative/qdeclarativepositioners/data/grid-righttoleft.qml +++ /dev/null @@ -1,41 +0,0 @@ -import QtQuick 1.1 - -Item { - width: 640 - height: 480 - Grid { - objectName: "grid" - columns: 3 - layoutDirection: Qt.RightToLeft - Rectangle { - objectName: "one" - color: "red" - width: 50 - height: 50 - } - Rectangle { - objectName: "two" - color: "green" - width: 20 - height: 50 - } - Rectangle { - objectName: "three" - color: "blue" - width: 50 - height: 20 - } - Rectangle { - objectName: "four" - color: "cyan" - width: 50 - height: 50 - } - Rectangle { - objectName: "five" - color: "magenta" - width: 10 - height: 10 - } - } -} diff --git a/tests/auto/declarative/qdeclarativepositioners/data/gridtest.qml b/tests/auto/declarative/qdeclarativepositioners/data/gridtest.qml index f3b17dd..929b726 100644 --- a/tests/auto/declarative/qdeclarativepositioners/data/gridtest.qml +++ b/tests/auto/declarative/qdeclarativepositioners/data/gridtest.qml @@ -1,9 +1,11 @@ -import QtQuick 1.0 +import QtQuick 1.1 Item { width: 640 height: 480 + property bool testRightToLeft: false Grid { + layoutDirection: testRightToLeft ? Qt.RightToLeft : Qt.LeftToRight objectName: "grid" columns: 3 Rectangle { @@ -21,7 +23,7 @@ Item { Rectangle { objectName: "three" color: "blue" - width: 50 + width: 30 height: 20 } Rectangle { diff --git a/tests/auto/declarative/qdeclarativepositioners/data/horizontal.qml b/tests/auto/declarative/qdeclarativepositioners/data/horizontal.qml index e1a9652..d35c02d 100644 --- a/tests/auto/declarative/qdeclarativepositioners/data/horizontal.qml +++ b/tests/auto/declarative/qdeclarativepositioners/data/horizontal.qml @@ -4,7 +4,6 @@ Item { width: 640 height: 480 property bool testRightToLeft: false - Row { objectName: "row" layoutDirection: testRightToLeft ? Qt.RightToLeft : Qt.LeftToRight diff --git a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp index 40e533d..92ab722 100644 --- a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp +++ b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include "../../../shared/util.h" @@ -87,6 +88,7 @@ private slots: void test_flow_implicit_resize(); void test_conflictinganchors(); void test_vertical_qgraphicswidget(); + void test_mirroring(); void testQtQuick11Attributes(); void testQtQuick11Attributes_data(); private: @@ -452,7 +454,7 @@ void tst_QDeclarativePositioners::test_grid() QDeclarativeGrid *grid = canvas->rootObject()->findChild("grid"); QCOMPARE(grid->flow(), QDeclarativeGrid::LeftToRight); - QCOMPARE(grid->width(), 120.0); + QCOMPARE(grid->width(), 100.0); QCOMPARE(grid->height(), 100.0); delete canvas; @@ -494,7 +496,9 @@ void tst_QDeclarativePositioners::test_grid_topToBottom() void tst_QDeclarativePositioners::test_grid_rightToLeft() { - QDeclarativeView *canvas = createView(SRCDIR "/data/grid-righttoleft.qml"); + QDeclarativeView *canvas = createView(SRCDIR "/data/gridtest.qml"); + + canvas->rootObject()->setProperty("testRightToLeft", true); QDeclarativeRectangle *one = canvas->rootObject()->findChild("one"); QVERIFY(one != 0); @@ -507,20 +511,20 @@ void tst_QDeclarativePositioners::test_grid_rightToLeft() QDeclarativeRectangle *five = canvas->rootObject()->findChild("five"); QVERIFY(five != 0); - QCOMPARE(one->x(), 70.0); + QCOMPARE(one->x(), 50.0); QCOMPARE(one->y(), 0.0); - QCOMPARE(two->x(), 50.0); + QCOMPARE(two->x(), 30.0); QCOMPARE(two->y(), 0.0); QCOMPARE(three->x(), 0.0); QCOMPARE(three->y(), 0.0); - QCOMPARE(four->x(), 70.0); + QCOMPARE(four->x(), 50.0); QCOMPARE(four->y(), 50.0); - QCOMPARE(five->x(), 60.0); + QCOMPARE(five->x(), 40.0); QCOMPARE(five->y(), 50.0); QDeclarativeGrid *grid = canvas->rootObject()->findChild("grid"); QCOMPARE(grid->layoutDirection(), Qt::RightToLeft); - QCOMPARE(grid->width(), 120.0); + QCOMPARE(grid->width(), 100.0); QCOMPARE(grid->height(), 100.0); delete canvas; @@ -1198,6 +1202,66 @@ void tst_QDeclarativePositioners::test_vertical_qgraphicswidget() delete canvas; } +void tst_QDeclarativePositioners::test_mirroring() +{ + QList qmlFiles; + qmlFiles << "horizontal.qml" << "gridtest.qml" << "flowtest.qml"; + QList objectNames; + objectNames << "one" << "two" << "three" << "four" << "five"; + + foreach(const QString qmlFile, qmlFiles) { + QDeclarativeView *canvasA = createView(QString(SRCDIR) + "/data/" + qmlFile); + QDeclarativeItem *rootA = qobject_cast(canvasA->rootObject()); + + QDeclarativeView *canvasB = createView(QString(SRCDIR) + "/data/" + qmlFile); + QDeclarativeItem *rootB = qobject_cast(canvasB->rootObject()); + + rootA->setProperty("testRightToLeft", true); // layoutDirection: Qt.RightToLeft + + // LTR != RTL + foreach(const QString objectName, objectNames) { + // horizontal.qml only has three items + if (qmlFile == QString("horizontal.qml") && objectName == QString("four")) + break; + QDeclarativeItem *itemA = rootA->findChild(objectName); + QDeclarativeItem *itemB = rootB->findChild(objectName); + QVERIFY(itemA->x() != itemB->x()); + } + + QDeclarativeItemPrivate* rootPrivateB = QDeclarativeItemPrivate::get(rootB); + + rootPrivateB->effectiveLayoutMirror = true; // LayoutMirroring.enabled: true + rootPrivateB->isMirrorImplicit = false; + rootPrivateB->inheritMirrorFromItem = true; // LayoutMirroring.childrenInherit: true + rootPrivateB->resolveLayoutMirror(); + + // RTL == mirror + foreach(const QString objectName, objectNames) { + // horizontal.qml only has three items + if (qmlFile == QString("horizontal.qml") && objectName == QString("four")) + break; + QDeclarativeItem *itemA = rootA->findChild(objectName); + QDeclarativeItem *itemB = rootB->findChild(objectName); + QCOMPARE(itemA->x(), itemB->x()); + } + + rootA->setProperty("testRightToLeft", false); // layoutDirection: Qt.LeftToRight + rootB->setProperty("testRightToLeft", true); // layoutDirection: Qt.RightToLeft + + // LTR == RTL + mirror + foreach(const QString objectName, objectNames) { + // horizontal.qml only has three items + if (qmlFile == QString("horizontal.qml") && objectName == QString("four")) + break; + QDeclarativeItem *itemA = rootA->findChild(objectName); + QDeclarativeItem *itemB = rootB->findChild(objectName); + QCOMPARE(itemA->x(), itemB->x()); + } + delete canvasA; + delete canvasB; + } +} + void tst_QDeclarativePositioners::testQtQuick11Attributes() { QFETCH(QString, code); diff --git a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp index 2220b6d..20e2640 100644 --- a/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp +++ b/tests/auto/declarative/qdeclarativestates/tst_qdeclarativestates.cpp @@ -818,7 +818,7 @@ void tst_qdeclarativestates::anchorChanges5() void mirrorAnchors(QDeclarativeItem *item) { QDeclarativeItemPrivate *itemPrivate = QDeclarativeItemPrivate::get(item); - itemPrivate->anchors()->setLayoutDirection(Qt::RightToLeft); + itemPrivate->setLayoutMirror(true); } qreal offsetRTL(QDeclarativeItem *anchorItem, QDeclarativeItem *item) { -- cgit v0.12 From d5c72c6fb75357061c5f9e0d0d2efdaff9140741 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 1 Mar 2011 12:57:14 +1000 Subject: Reverse KeyNavigation left and right properties when the layout mirroring is enabled Task-number: QTBUG-15882 Reviewed-by: Martin Jones Change-Id: I4c9f0b48e089b30ced5e7fefa5d6e97b3155f3b2 --- src/declarative/graphicsitems/qdeclarativeitem.cpp | 32 +++++++++---- .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 54 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index d16025d..867a16d 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -615,19 +615,28 @@ void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post) return; } + bool mirror = false; switch(event->key()) { - case Qt::Key_Left: - if (d->left) { - setFocusNavigation(d->left, "left"); + case Qt::Key_Left: { + if (QDeclarativeItem *parentItem = qobject_cast(parent())) + mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror; + QDeclarativeItem* leftItem = mirror ? d->right : d->left; + if (leftItem) { + setFocusNavigation(leftItem, mirror ? "right" : "left"); event->accept(); } break; - case Qt::Key_Right: - if (d->right) { - setFocusNavigation(d->right, "right"); + } + case Qt::Key_Right: { + if (QDeclarativeItem *parentItem = qobject_cast(parent())) + mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror; + QDeclarativeItem* rightItem = mirror ? d->left : d->right; + if (rightItem) { + setFocusNavigation(rightItem, mirror ? "left" : "right"); event->accept(); } break; + } case Qt::Key_Up: if (d->up) { setFocusNavigation(d->up, "up"); @@ -669,16 +678,19 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post) return; } + bool mirror = false; switch(event->key()) { case Qt::Key_Left: - if (d->left) { + if (QDeclarativeItem *parentItem = qobject_cast(parent())) + mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror; + if (mirror ? d->right : d->left) event->accept(); - } break; case Qt::Key_Right: - if (d->right) { + if (QDeclarativeItem *parentItem = qobject_cast(parent())) + mirror = QDeclarativeItemPrivate::get(parentItem)->effectiveLayoutMirror; + if (mirror ? d->left : d->right) event->accept(); - } break; case Qt::Key_Up: if (d->up) { diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index f83207c..2d14e66 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -65,6 +65,7 @@ private slots: void keys(); void keysProcessingOrder(); void keyNavigation(); + void keyNavigation_RightToLeft(); void keyNavigation_skipNotVisible(); void layoutMirroring(); void layoutMirroringIllegalParent(); @@ -608,6 +609,59 @@ void tst_QDeclarativeItem::keyNavigation() delete canvas; } +void tst_QDeclarativeItem::keyNavigation_RightToLeft() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setFixedSize(240,320); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/keynavigationtest.qml")); + canvas->show(); + qApp->processEvents(); + + QDeclarativeItem *rootItem = qobject_cast(canvas->rootObject()); + QVERIFY(rootItem); + QDeclarativeItemPrivate* rootItemPrivate = QDeclarativeItemPrivate::get(rootItem); + + rootItemPrivate->effectiveLayoutMirror = true; // LayoutMirroring.mirror: true + rootItemPrivate->isMirrorImplicit = false; + rootItemPrivate->inheritMirrorFromItem = true; // LayoutMirroring.inherit: true + rootItemPrivate->resolveLayoutMirror(); + + QEvent wa(QEvent::WindowActivate); + QApplication::sendEvent(canvas, &wa); + QFocusEvent fe(QEvent::FocusIn); + QApplication::sendEvent(canvas, &fe); + + QDeclarativeItem *item = findItem(canvas->rootObject(), "item1"); + QVERIFY(item); + QVERIFY(item->hasActiveFocus()); + + QVariant result; + QVERIFY(QMetaObject::invokeMethod(canvas->rootObject(), "verify", + Q_RETURN_ARG(QVariant, result))); + QVERIFY(result.toBool()); + + // right + QKeyEvent key(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier, "", false, 1); + QApplication::sendEvent(canvas, &key); + QVERIFY(key.isAccepted()); + + item = findItem(canvas->rootObject(), "item2"); + QVERIFY(item); + QVERIFY(item->hasActiveFocus()); + + // left + key = QKeyEvent(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier, "", false, 1); + QApplication::sendEvent(canvas, &key); + QVERIFY(key.isAccepted()); + + item = findItem(canvas->rootObject(), "item1"); + QVERIFY(item); + QVERIFY(item->hasActiveFocus()); + + delete canvas; +} + void tst_QDeclarativeItem::keyNavigation_skipNotVisible() { QDeclarativeView *canvas = new QDeclarativeView(0); -- cgit v0.12 From 88253db8a7d7910e1393b1948fb3747117538c92 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 1 Mar 2011 12:51:27 +1000 Subject: Make sure horizontal QML editor text aligment always returns the actual alignment Also, implicit empty text alignment now follows the Application's default layout direction traditionally set by the locale. Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: I88340513d489290bafd393072786a19731097b77 --- src/declarative/graphicsitems/qdeclarativetext.cpp | 67 +++++++++++++------- src/declarative/graphicsitems/qdeclarativetext_p.h | 3 +- .../graphicsitems/qdeclarativetext_p_p.h | 2 + .../graphicsitems/qdeclarativetextedit.cpp | 52 ++++++++++++++- .../graphicsitems/qdeclarativetextedit_p.h | 3 +- .../graphicsitems/qdeclarativetextedit_p_p.h | 7 ++- .../graphicsitems/qdeclarativetextinput.cpp | 73 +++++++++++++++++----- .../graphicsitems/qdeclarativetextinput_p.h | 3 +- .../graphicsitems/qdeclarativetextinput_p_p.h | 24 +++---- .../qdeclarativetext/tst_qdeclarativetext.cpp | 34 ++++++++-- .../tst_qdeclarativetextedit.cpp | 37 +++++++++-- .../tst_qdeclarativetextinput.cpp | 37 ++++++++++- 12 files changed, 274 insertions(+), 68 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 4c6c34f..af7e4a1 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -97,17 +97,18 @@ DEFINE_BOOL_CONFIG_OPTION(enableImageCache, QML_ENABLE_TEXT_IMAGE_CACHE); QString QDeclarativeTextPrivate::elideChar = QString(0x2026); QDeclarativeTextPrivate::QDeclarativeTextPrivate() -: color((QRgb)0), style(QDeclarativeText::Normal), hAlign(QDeclarativeText::AlignLeft), +: color((QRgb)0), style(QDeclarativeText::Normal), hAlign(QDeclarativeText::AlignLeft), vAlign(QDeclarativeText::AlignTop), elideMode(QDeclarativeText::ElideNone), format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), lineHeight(1), - lineHeightMode(QDeclarativeText::ProportionalHeight), - lineCount(1), truncated(false), maximumLineCount(INT_MAX), + lineHeightMode(QDeclarativeText::ProportionalHeight), lineCount(1), truncated(false), maximumLineCount(INT_MAX), maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false), - cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), naturalWidth(0), doc(0) + cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), naturalWidth(0), doc(0) { cacheAllTextAsImage = enableImageCache(); QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton; QGraphicsItemPrivate::flags = QGraphicsItemPrivate::flags & ~QGraphicsItem::ItemHasNoContents; + if (QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeText::AlignRight; } QTextDocumentWithImageResources::QTextDocumentWithImageResources(QDeclarativeText *parent) @@ -211,6 +212,21 @@ qreal QDeclarativeTextPrivate::implicitWidth() const return mImplicitWidth; } +void QDeclarativeTextPrivate::determineHorizontalAlignment() +{ + Q_Q(QDeclarativeText); + if (hAlignImplicit && q->isComponentComplete()) { + // if no explicit alignment has been set, follow the natural layout direction of the text + QDeclarativeText::HAlignment previousAlign = hAlign; + if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeText::AlignRight; + else + hAlign = text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft; + if (previousAlign != hAlign) + emit q->horizontalAlignmentChanged(hAlign); + } +} + void QDeclarativeTextPrivate::updateLayout() { Q_Q(QDeclarativeText); @@ -367,17 +383,6 @@ QSize QDeclarativeTextPrivate::setupTextLayout() textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); layout.setTextOption(textOption); - QDeclarativeText::HAlignment hAlignment = hAlign; - if(text.isRightToLeft()) { - if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { - hAlignment = QDeclarativeText::AlignRight; - } else if (hAlign == QDeclarativeText::AlignRight) { - hAlignment = QDeclarativeText::AlignLeft; - } else { - hAlignment = hAlign; - } - } - bool elideText = false; bool truncate = false; @@ -423,10 +428,10 @@ QSize QDeclarativeTextPrivate::setupTextLayout() // Need to correct for alignment line.setLineWidth(lineWidth-elideWidth); int x = line.naturalTextWidth(); - if (hAlignment == QDeclarativeText::AlignRight) { + if (hAlign == QDeclarativeText::AlignRight) { x = q->width()-elideWidth; - } else if (hAlignment == QDeclarativeText::AlignHCenter) { - x = (q->width()+line.naturalTextWidth()-elideWidth)/2; + } else if (hAlign == QDeclarativeText::AlignHCenter) { + x = (q->width()+line.naturalTextWidth() - elideWidth)/2; } elidePos = QPointF(x, y + fm.ascent()); elideText = true; @@ -472,13 +477,13 @@ QSize QDeclarativeTextPrivate::setupTextLayout() height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight; if (!cacheAllTextAsImage) { - if ((hAlignment == QDeclarativeText::AlignLeft) || (hAlignment == QDeclarativeText::AlignJustify)) { + if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { x = 0; - } else if (hAlignment == QDeclarativeText::AlignRight) { + } else if (hAlign == QDeclarativeText::AlignRight) { x = layoutWidth - line.naturalTextWidth(); if (elideText && i == layout.lineCount()-1) x -= elideWidth; // Correct for when eliding multilines - } else if (hAlignment == QDeclarativeText::AlignHCenter) { + } else if (hAlign == QDeclarativeText::AlignHCenter) { x = (layoutWidth - line.naturalTextWidth()) / 2; if (elideText && i == layout.lineCount()-1) x -= elideWidth/2; // Correct for when eliding multilines @@ -946,6 +951,7 @@ void QDeclarativeText::setText(const QString &n) } d->text = n; + d->determineHorizontalAlignment(); d->updateLayout(); emit textChanged(d->text); @@ -1064,7 +1070,9 @@ void QDeclarativeText::setStyleColor(const QColor &color) \qmlproperty enumeration Text::verticalAlignment Sets the horizontal and vertical alignment of the text within the Text items - width and height. By default, the text is top-left aligned. + width and height. By default, the text is vertically aligned to the top. Horizontal + alignment follows the natural alignment of the text, for example text that is read + from left to right will be aligned to the left. The valid values for \c horizontalAlignment are \c Text.AlignLeft, \c Text.AlignRight, \c Text.AlignHCenter and \c Text.AlignJustify. The valid values for \c verticalAlignment are \c Text.AlignTop, \c Text.AlignBottom @@ -1084,6 +1092,7 @@ QDeclarativeText::HAlignment QDeclarativeText::hAlign() const void QDeclarativeText::setHAlign(HAlignment align) { Q_D(QDeclarativeText); + d->hAlignImplicit = false; if (d->hAlign == align) return; @@ -1096,6 +1105,19 @@ void QDeclarativeText::setHAlign(HAlignment align) emit horizontalAlignmentChanged(align); } +void QDeclarativeText::resetHAlign() +{ + Q_D(QDeclarativeText); + d->hAlignImplicit = true; + QDeclarativeText::HAlignment oldAlignment = d->hAlign; + d->determineHorizontalAlignment(); + if (oldAlignment != d->hAlign) { + prepareGeometryChange(); + d->updateLayout(); + emit horizontalAlignmentChanged(d->hAlign); + } +} + QDeclarativeText::VAlignment QDeclarativeText::vAlign() const { Q_D(const QDeclarativeText); @@ -1556,6 +1578,7 @@ void QDeclarativeText::componentComplete() QDeclarativeItem::componentComplete(); if (d->updateOnComponentComplete) { d->updateOnComponentComplete = false; + d->determineHorizontalAlignment(); if (d->richText) { d->ensureDoc(); d->doc->setText(d->text); diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h index b8835d1..92c2eab 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p.h @@ -69,7 +69,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeImplici Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(TextStyle style READ style WRITE setStyle NOTIFY styleChanged) Q_PROPERTY(QColor styleColor READ styleColor WRITE setStyleColor NOTIFY styleColorChanged) - Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged) + Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) @@ -133,6 +133,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); + void resetHAlign(); VAlignment vAlign() const; void setVAlign(VAlignment align); diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index 36ae123..6886d91 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -76,6 +76,7 @@ public: void updateSize(); void updateLayout(); + void determineHorizontalAlignment(); QString text; QFont font; @@ -110,6 +111,7 @@ public: bool cacheAllTextAsImage:1; bool internalWidthUpdate:1; bool requireImplicitWidth:1; + bool hAlignImplicit:1; QSize layedOutTextSize; QSize paintedSize; diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 87a49bd..35716d0 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -249,6 +249,7 @@ void QDeclarativeTextEdit::setText(const QString &text) Q_D(QDeclarativeTextEdit); if (QDeclarativeTextEdit::text() == text) return; + d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text)); if (d->richText) { #ifndef QT_NO_TEXTHTMLPARSER @@ -457,7 +458,9 @@ void QDeclarativeTextEdit::setSelectedTextColor(const QColor &color) \qmlproperty enumeration TextEdit::verticalAlignment Sets the horizontal and vertical alignment of the text within the TextEdit item's - width and height. By default, the text is top-left aligned. + width and height. By default, the text alignment follows the natural alignment + of the text, for example text that is read from left to right will be aligned to + the left. Valid values for \c horizontalAlignment are: \list @@ -483,6 +486,7 @@ QDeclarativeTextEdit::HAlignment QDeclarativeTextEdit::hAlign() const void QDeclarativeTextEdit::setHAlign(QDeclarativeTextEdit::HAlignment alignment) { Q_D(QDeclarativeTextEdit); + d->hAlignImplicit = false; if (alignment == d->hAlign) return; d->hAlign = alignment; @@ -491,6 +495,19 @@ void QDeclarativeTextEdit::setHAlign(QDeclarativeTextEdit::HAlignment alignment) emit horizontalAlignmentChanged(d->hAlign); } +void QDeclarativeTextEdit::resetHAlign() +{ + Q_D(QDeclarativeTextEdit); + d->hAlignImplicit = true; + QDeclarativeTextEdit::HAlignment oldAlignment = d->hAlign; + d->determineHorizontalAlignment(); + if (oldAlignment != d->hAlign) { + d->updateDefaultTextOption(); + updateSize(); + + } +} + QDeclarativeTextEdit::VAlignment QDeclarativeTextEdit::vAlign() const { Q_D(const QDeclarativeTextEdit); @@ -948,6 +965,8 @@ void QDeclarativeTextEdit::componentComplete() Q_D(QDeclarativeTextEdit); QDeclarativePaintedItem::componentComplete(); if (d->dirty) { + d->determineHorizontalAlignment(); + d->updateDefaultTextOption(); updateSize(); d->dirty = false; } @@ -1438,6 +1457,9 @@ void QDeclarativeTextEditPrivate::init() document->setDocumentMargin(textMargin); document->setUndoRedoEnabled(false); // flush undo buffer. document->setUndoRedoEnabled(true); + + if (QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeTextEdit::AlignRight; updateDefaultTextOption(); } @@ -1445,6 +1467,9 @@ void QDeclarativeTextEdit::q_textChanged() { Q_D(QDeclarativeTextEdit); d->text = text(); + d->rightToLeftText = d->text.isRightToLeft(); + d->determineHorizontalAlignment(); + d->updateDefaultTextOption(); updateSize(); updateTotalLines(); updateMicroFocus(); @@ -1461,6 +1486,21 @@ void QDeclarativeTextEdit::moveCursorDelegate() d->cursor->setY(cursorRect.y()); } +void QDeclarativeTextEditPrivate::determineHorizontalAlignment() +{ + Q_Q(QDeclarativeTextEdit); + if (hAlignImplicit && q->isComponentComplete()) { + // if no explicit alignment has been set, follow the natural layout direction of the text + QDeclarativeTextEdit::HAlignment previousAlign = hAlign; + if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeTextEdit::AlignRight; + else + hAlign = rightToLeftText ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft; + if (previousAlign != hAlign) + emit q->horizontalAlignmentChanged(hAlign); + } +} + void QDeclarativeTextEditPrivate::updateSelection() { Q_Q(QDeclarativeTextEdit); @@ -1611,7 +1651,15 @@ void QDeclarativeTextEditPrivate::updateDefaultTextOption() { QTextOption opt = document->defaultTextOption(); int oldAlignment = opt.alignment(); - opt.setAlignment((Qt::Alignment)(int)(hAlign | vAlign)); + + QDeclarativeTextEdit::HAlignment horizontalAlignment = hAlign; + if (rightToLeftText) { + if (hAlign == QDeclarativeTextEdit::AlignLeft) + horizontalAlignment = QDeclarativeTextEdit::AlignRight; + else if (hAlign == QDeclarativeTextEdit::AlignRight) + horizontalAlignment = QDeclarativeTextEdit::AlignLeft; + } + opt.setAlignment((Qt::Alignment)(int)(horizontalAlignment | vAlign)); QTextOption::WrapMode oldWrapMode = opt.wrapMode(); opt.setWrapMode(QTextOption::WrapMode(wrapMode)); diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index 7785a7a..c7dc2f6 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -72,7 +72,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativeImplicitSizePa Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged) Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) - Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged) + Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) @@ -152,6 +152,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); + void resetHAlign(); VAlignment vAlign() const; void setVAlign(VAlignment align); diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h index 111cc02..f4a6c0e 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h @@ -71,8 +71,8 @@ public: : color("black"), hAlign(QDeclarativeTextEdit::AlignLeft), vAlign(QDeclarativeTextEdit::AlignTop), imgDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true), showInputPanelOnFocus(true), clickCausedFocus(false), persistentSelection(true), requireImplicitWidth(false), - textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0), - format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap), + hAlignImplicit(true), rightToLeftText(false), textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), + cursorComponent(0), cursor(0), format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap), mouseSelectionMode(QDeclarativeTextEdit::SelectCharacters), selectByMouse(false), canPaste(false), yoff(0) { @@ -88,6 +88,7 @@ public: void updateDefaultTextOption(); void relayoutDocument(); void updateSelection(); + void determineHorizontalAlignment(); qreal implicitWidth() const; void focusChanged(bool); @@ -112,6 +113,8 @@ public: bool clickCausedFocus : 1; bool persistentSelection : 1; bool requireImplicitWidth:1; + bool hAlignImplicit:1; + bool rightToLeftText:1; qreal textMargin; int lastSelectionStart; int lastSelectionEnd; diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index e7c2ac7..adc2860 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -327,7 +327,9 @@ void QDeclarativeTextInput::setSelectedTextColor(const QColor &color) \qmlproperty enumeration TextInput::horizontalAlignment Sets the horizontal alignment of the text within the TextInput item's - width and height. By default, the text is left aligned. + width and height. By default, the text alignment follows the natural alignment + of the text, for example text that is read from left to right will be aligned to + the left. TextInput does not have vertical alignment, as the natural height is exactly the height of the single line of text. If you set the height @@ -347,7 +349,8 @@ QDeclarativeTextInput::HAlignment QDeclarativeTextInput::hAlign() const void QDeclarativeTextInput::setHAlign(HAlignment align) { Q_D(QDeclarativeTextInput); - if(align == d->hAlign) + d->hAlignImplicit = false; + if(align == d->hAlign || align > QDeclarativeTextInput::AlignHCenter) // justify not supported return; d->hAlign = align; updateRect(); @@ -355,6 +358,19 @@ void QDeclarativeTextInput::setHAlign(HAlignment align) emit horizontalAlignmentChanged(d->hAlign); } +void QDeclarativeTextInput::resetHAlign() +{ + Q_D(QDeclarativeTextInput); + d->hAlignImplicit = true; + QDeclarativeTextInput::HAlignment oldAlignment = d->hAlign; + d->determineHorizontalAlignment(); + if (oldAlignment != d->hAlign) { + updateRect(); + d->updateHorizontalScroll(); + emit horizontalAlignmentChanged(d->hAlign); + } +} + /*! \qmlproperty bool TextInput::readOnly @@ -961,16 +977,21 @@ void QDeclarativeTextInput::keyPressEvent(QKeyEvent* ev) keyPressPreHandler(ev); if (ev->isAccepted()) return; - if (((ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier) // Don't allow MacOSX up/down support, and we don't allow a completer. - || (((d->control->cursor() == 0 && ev->key() == Qt::Key_Left) - || (d->control->cursor() == d->control->text().length() - && ev->key() == Qt::Key_Right)) - && (d->lastSelectionStart == d->lastSelectionEnd))) - { - //ignore when moving off the end - //unless there is a selection, because then moving will do something (deselect) + + // Don't allow MacOSX up/down support, and we don't allow a completer. + bool ignore = (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier; + if (!ignore && (d->lastSelectionStart == d->lastSelectionEnd) && (ev->key() == Qt::Key_Right || ev->key() == Qt::Key_Left)) { + // Ignore when moving off the end unless there is a selection, + // because then moving will do something (deselect). + int cursorPosition = d->control->cursor(); + if (cursorPosition == 0) + ignore = ev->key() == (d->control->text().mid(cursorPosition,1).isRightToLeft() ? Qt::Key_Right : Qt::Key_Left); + if (cursorPosition == d->control->text().length()) + ignore = ev->key() == (d->control->text().mid(cursorPosition-1,1).isRightToLeft() ? Qt::Key_Left : Qt::Key_Right); + } + if (ignore) { ev->ignore(); - }else{ + } else { d->control->processKeyEvent(ev); } if (!ev->isAccepted()) @@ -1129,11 +1150,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() int cix = qRound(control->cursorToX(control->cursor() + preeditLength)); QRect br(q->boundingRect().toRect()); int widthUsed = calculateTextWidth(); - Qt::Alignment va = QStyle::visualAlignment(control->layoutDirection(), QFlag(Qt::Alignment(hAlign))); + if (autoScroll) { if (widthUsed <= br.width()) { // text fits in br; use hscroll for alignment - switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { + switch (hAlign & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { case Qt::AlignRight: hscroll = widthUsed - br.width() - 1; break; @@ -1165,11 +1186,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() hscroll = cix; } } else { - switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { - case Qt::AlignRight: + switch (hAlign) { + case QDeclarativeTextInput::AlignRight: hscroll = q->width() - widthUsed; break; - case Qt::AlignHCenter: + case QDeclarativeTextInput::AlignHCenter: hscroll = (q->width() - widthUsed) / 2; break; default: @@ -1180,6 +1201,22 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() } } +void QDeclarativeTextInputPrivate::determineHorizontalAlignment() +{ + Q_Q(QDeclarativeTextInput); + if (hAlignImplicit) { + QString text = control->text(); + // if no explicit alignment has been set, follow the natural layout direction of the text + QDeclarativeTextInput::HAlignment previousAlign = hAlign; + if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeTextInput::AlignRight; + else + hAlign = text.isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft; + if (previousAlign != hAlign) + emit q->horizontalAlignmentChanged(hAlign); + } +} + void QDeclarativeTextInput::drawContents(QPainter *p, const QRect &r) { Q_D(QDeclarativeTextInput); @@ -1671,6 +1708,9 @@ void QDeclarativeTextInputPrivate::init() QPalette p = control->palette(); selectedTextColor = p.color(QPalette::HighlightedText); selectionColor = p.color(QPalette::Highlight); + + if (QApplication::layoutDirection() == Qt::RightToLeft) + hAlign = QDeclarativeTextInput::AlignRight; } void QDeclarativeTextInput::cursorPosChanged() @@ -1719,6 +1759,7 @@ void QDeclarativeTextInput::q_textChanged() Q_D(QDeclarativeTextInput); updateSize(); d->updateHorizontalScroll(); + d->determineHorizontalAlignment(); updateMicroFocus(); emit textChanged(); emit displayTextChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index e1e66a9..a3e8d29 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -70,7 +70,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativeImplicitSizeP Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged) Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) - Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged) + Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged) Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged) @@ -146,6 +146,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); + void resetHAlign(); bool isReadOnly() const; void setReadOnly(bool); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h index f7446b4..321d124 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h @@ -74,9 +74,9 @@ public: color((QRgb)0), style(QDeclarativeText::Normal), styleColor((QRgb)0), hAlign(QDeclarativeTextInput::AlignLeft), mouseSelectionMode(QDeclarativeTextInput::SelectCharacters), - hscroll(0), oldScroll(0), focused(false), focusOnPress(true), + hscroll(0), oldScroll(0), oldValidity(false), focused(false), focusOnPress(true), showInputPanelOnFocus(true), clickCausedFocus(false), cursorVisible(false), - autoScroll(true), selectByMouse(false), canPaste(false) + autoScroll(true), selectByMouse(false), canPaste(false), hAlignImplicit(true) { #ifdef Q_OS_SYMBIAN if (QSysInfo::symbianVersion() == QSysInfo::SV_SF_1 || QSysInfo::symbianVersion() == QSysInfo::SV_SF_3) { @@ -103,6 +103,7 @@ public: void startCreatingCursor(); void focusChanged(bool hasFocus); void updateHorizontalScroll(); + void determineHorizontalAlignment(); int calculateTextWidth(); QLineControl* control; @@ -124,17 +125,18 @@ public: int lastSelectionEnd; int oldHeight; int oldWidth; - bool oldValidity; int hscroll; int oldScroll; - bool focused; - bool focusOnPress; - bool showInputPanelOnFocus; - bool clickCausedFocus; - bool cursorVisible; - bool autoScroll; - bool selectByMouse; - bool canPaste; + bool oldValidity:1; + bool focused:1; + bool focusOnPress:1; + bool showInputPanelOnFocus:1; + bool clickCausedFocus:1; + bool cursorVisible:1; + bool autoScroll:1; + bool selectByMouse:1; + bool canPaste:1; + bool hAlignImplicit:1; static inline QDeclarativeTextInputPrivate *get(QDeclarativeTextInput *t) { return t->d_func(); diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 05546cb..33c8b89 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -517,18 +517,44 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() QDeclarativeTextPrivate *textPrivate = QDeclarativeTextPrivate::get(text); QVERIFY(textPrivate != 0); + // implicit alignment should follow the reading direction of RTL text + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); - // "Right" aligned + // explicitly left aligned + text->setHAlign(QDeclarativeText::AlignLeft); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + + // explicitly right aligned text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); - // Center aligned + // explicitly center aligned text->setHAlign(QDeclarativeText::AlignHCenter); QCOMPARE(text->hAlign(), QDeclarativeText::AlignHCenter); QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); - QVERIFY(textPrivate->layout.lineAt(0).x() + textPrivate->layout.lineAt(0).width() > canvas->width()/2); + + // reseted alignment should go back to following the text reading direction + text->resetHAlign(); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); + + // English text should be implicitly left aligned + text->setText("Hello world!"); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + + // empty text should implicitly follow the layout direction + QApplication::setLayoutDirection(Qt::RightToLeft); + text->setText(""); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + text->setHAlign(QDeclarativeText::AlignLeft); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + + // set layout direction back to LTR to avoid affecting other autotests + QApplication::setLayoutDirection(Qt::LeftToRight); delete canvas; } diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index 87c2c60..eb02936 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -438,20 +438,47 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QVERIFY(textEdit != 0); canvas->show(); + // implicit alignment should follow the reading direction of RTL text + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); - // "Right" align + // explicitly left aligned + textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + + // explicitly right aligned textEdit->setHAlign(QDeclarativeTextEdit::AlignRight); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); - QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); - // Center align - // Note that position 0 is on the right-hand side + // explicitly center aligned textEdit->setHAlign(QDeclarativeTextEdit::AlignHCenter); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignHCenter); - QVERIFY(textEdit->positionToRectangle(0).x() - textEdit->width() < canvas->width()/2); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + // reseted alignment should go back to following the text reading direction + textEdit->resetHAlign(); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + + // English text should be implicitly left aligned + textEdit->setText("Hello world!"); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + + // empty text should implicitly follow the layout direction + QApplication::setLayoutDirection(Qt::RightToLeft); + textEdit->setText(""); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + + // set layout direction back to LTR to avoid affecting other autotests + QApplication::setLayoutDirection(Qt::LeftToRight); + delete canvas; } diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 7753f11..45f2cf7 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -914,17 +914,48 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() QVERIFY(textInputPrivate != 0); QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); - // "Right" Align - textInput->setHAlign(QDeclarativeTextInput::AlignRight); + // implicit alignment should follow the reading direction of RTL text QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + + // explicitly left aligned + textInput->setHAlign(QDeclarativeTextInput::AlignLeft); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); - // Center Align + // explicitly right aligned + textInput->setHAlign(QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + + // explicitly center aligned textInput->setHAlign(QDeclarativeTextInput::AlignHCenter); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignHCenter); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); QVERIFY(-textInputPrivate->hscroll + textInputPrivate->width() > canvas->width()/2); + // reseted alignment should go back to following the text reading direction + textInput->resetHAlign(); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + + // English text should be implicitly left aligned + textInput->setText("Hello world!"); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); + QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); + + // empty text should implicitly follow the layout direction + QApplication::setLayoutDirection(Qt::RightToLeft); + textInput->setText(""); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + textInput->setHAlign(QDeclarativeTextInput::AlignLeft); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); + QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); + + // set layout direction back to LTR to avoid affecting other autotests + QApplication::setLayoutDirection(Qt::LeftToRight); + delete canvas; } -- cgit v0.12 From 6aaf7b3c93687b857ff7cac0f2ad2ee510b2813b Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Thu, 3 Mar 2011 14:43:42 +1000 Subject: Add a way to query the reading direction of QML editor text Task-number: QTBUG-17490 Reviewed-by: Martin Jones Change-Id: I3dd3854f820860d32e822605ed547150d5f17eb2 --- .../graphicsitems/qdeclarativetextedit.cpp | 17 +++++++ .../graphicsitems/qdeclarativetextedit_p.h | 1 + .../graphicsitems/qdeclarativetextinput.cpp | 17 +++++++ .../graphicsitems/qdeclarativetextinput_p.h | 1 + .../tst_qdeclarativetextedit.cpp | 59 ++++++++++++++++++++++ .../tst_qdeclarativetextinput.cpp | 59 ++++++++++++++++++++++ 6 files changed, 154 insertions(+) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 3c2684c..5e1459c 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -1235,6 +1235,23 @@ void QDeclarativeTextEdit::select(int start, int end) updateSelectionMarkers(); } +/*! + \qmlmethod void TextEdit::isRightToLeft(int start, int end) + + Returns true if the natural reading direction of the editor text + found between positions \a start and \a end is right to left. +*/ +bool QDeclarativeTextEdit::isRightToLeft(int start, int end) +{ + Q_D(QDeclarativeTextEdit); + if (start > end) { + qmlInfo(this) << "isRightToLeft(start, end) called with the end property being smaller than the start."; + return false; + } else { + return d->text.mid(start, end - start).isRightToLeft(); + } +} + #ifndef QT_NO_CLIPBOARD /*! \qmlmethod TextEdit::cut() diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index a8d9fe2..471b201 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -253,6 +253,7 @@ public Q_SLOTS: void selectWord(); void select(int start, int end); Q_REVISION(1) void deselect(); + Q_REVISION(1) bool isRightToLeft(int start, int end); #ifndef QT_NO_CLIPBOARD void cut(); void copy(); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index cb7f739..09404ce 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -1370,6 +1370,23 @@ void QDeclarativeTextInput::selectAll() d->control->setSelection(0, d->control->text().length()); } +/*! + \qmlmethod void TextInput::isRightToLeft(int start, int end) + + Returns true if the natural reading direction of the editor text + found between positions \a start and \a end is right to left. +*/ +bool QDeclarativeTextInput::isRightToLeft(int start, int end) +{ + Q_D(QDeclarativeTextInput); + if (start > end) { + qmlInfo(this) << "isRightToLeft(start, end) called with the end property being smaller than the start."; + return false; + } else { + return d->control->text().mid(start, end - start).isRightToLeft(); + } +} + #ifndef QT_NO_CLIPBOARD /*! \qmlmethod TextInput::cut() diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index c057f1f..ce5f267 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -262,6 +262,7 @@ public Q_SLOTS: void selectWord(); void select(int start, int end); Q_REVISION(1) void deselect(); + Q_REVISION(1) bool isRightToLeft(int start, int end); #ifndef QT_NO_CLIPBOARD void cut(); void copy(); diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index c5e2a11..6d5750f 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -110,6 +110,8 @@ private slots: void persistentSelection(); void focusOnPress(); void selection(); + void isRightToLeft_data(); + void isRightToLeft(); void moveCursorSelection_data(); void moveCursorSelection(); void moveCursorSelectionSequence_data(); @@ -787,6 +789,63 @@ void tst_qdeclarativetextedit::selection() QVERIFY(textEditObject->selectedText().isNull()); } +void tst_qdeclarativetextedit::isRightToLeft_data() +{ + QTest::addColumn("text"); + QTest::addColumn("emptyString"); + QTest::addColumn("firstCharacter"); + QTest::addColumn("lastCharacter"); + QTest::addColumn("middleCharacter"); + QTest::addColumn("startString"); + QTest::addColumn("midString"); + QTest::addColumn("endString"); + + const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647}; + QTest::newRow("Empty") << "" << false << false << false << false << false << false << false; + QTest::newRow("Neutral") << "23244242" << false << false << false << false << false << false << false; + QTest::newRow("LTR") << "Hello world" << false << false << false << false << false << false << false; + QTest::newRow("RTL") << QString::fromUtf16(arabic_str, 11) << false << true << true << true << true << true << true; + QTest::newRow("Bidi RTL + LTR + RTL") << QString::fromUtf16(arabic_str, 11) + QString("Hello world") + QString::fromUtf16(arabic_str, 11) << false << true << true << false << true << true << true; + QTest::newRow("Bidi LTR + RTL + LTR") << QString("Hello world") + QString::fromUtf16(arabic_str, 11) + QString("Hello world") << false << false << false << true << false << false << false; +} + +void tst_qdeclarativetextedit::isRightToLeft() +{ + QFETCH(QString, text); + QFETCH(bool, emptyString); + QFETCH(bool, firstCharacter); + QFETCH(bool, lastCharacter); + QFETCH(bool, middleCharacter); + QFETCH(bool, startString); + QFETCH(bool, midString); + QFETCH(bool, endString); + + QDeclarativeTextEdit textEdit; + textEdit.setText(text); + + // first test that the right string is delivered to the QString::isRightToLeft() + QCOMPARE(textEdit.isRightToLeft(0,0), text.mid(0,0).isRightToLeft()); + QCOMPARE(textEdit.isRightToLeft(0,1), text.mid(0,1).isRightToLeft()); + QCOMPARE(textEdit.isRightToLeft(text.count()-2, text.count()-1), text.mid(text.count()-2, text.count()-1).isRightToLeft()); + QCOMPARE(textEdit.isRightToLeft(text.count()/2, text.count()/2 + 1), text.mid(text.count()/2, text.count()/2 + 1).isRightToLeft()); + QCOMPARE(textEdit.isRightToLeft(0,text.count()/4), text.mid(0,text.count()/4).isRightToLeft()); + QCOMPARE(textEdit.isRightToLeft(text.count()/4,3*text.count()/4), text.mid(text.count()/4,3*text.count()/4).isRightToLeft()); + if (text.isEmpty()) + QTest::ignoreMessage(QtWarningMsg, ": QML TextEdit: isRightToLeft(start, end) called with the end property being smaller than the start."); + QCOMPARE(textEdit.isRightToLeft(3*text.count()/4,text.count()-1), text.mid(3*text.count()/4,text.count()-1).isRightToLeft()); + + // then test that the feature actually works + QCOMPARE(textEdit.isRightToLeft(0,0), emptyString); + QCOMPARE(textEdit.isRightToLeft(0,1), firstCharacter); + QCOMPARE(textEdit.isRightToLeft(text.count()-2, text.count()-1), lastCharacter); + QCOMPARE(textEdit.isRightToLeft(text.count()/2, text.count()/2 + 1), middleCharacter); + QCOMPARE(textEdit.isRightToLeft(0,text.count()/4), startString); + QCOMPARE(textEdit.isRightToLeft(text.count()/4,3*text.count()/4), midString); + if (text.isEmpty()) + QTest::ignoreMessage(QtWarningMsg, ": QML TextEdit: isRightToLeft(start, end) called with the end property being smaller than the start."); + QCOMPARE(textEdit.isRightToLeft(3*text.count()/4,text.count()-1), endString); +} + void tst_qdeclarativetextedit::moveCursorSelection_data() { QTest::addColumn("testStr"); diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 65be327..585479c 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -89,6 +89,8 @@ private slots: void font(); void color(); void selection(); + void isRightToLeft_data(); + void isRightToLeft(); void moveCursorSelection_data(); void moveCursorSelection(); void moveCursorSelectionSequence_data(); @@ -435,6 +437,63 @@ void tst_qdeclarativetextinput::selection() delete textinputObject; } +void tst_qdeclarativetextinput::isRightToLeft_data() +{ + QTest::addColumn("text"); + QTest::addColumn("emptyString"); + QTest::addColumn("firstCharacter"); + QTest::addColumn("lastCharacter"); + QTest::addColumn("middleCharacter"); + QTest::addColumn("startString"); + QTest::addColumn("midString"); + QTest::addColumn("endString"); + + const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647}; + QTest::newRow("Empty") << "" << false << false << false << false << false << false << false; + QTest::newRow("Neutral") << "23244242" << false << false << false << false << false << false << false; + QTest::newRow("LTR") << "Hello world" << false << false << false << false << false << false << false; + QTest::newRow("RTL") << QString::fromUtf16(arabic_str, 11) << false << true << true << true << true << true << true; + QTest::newRow("Bidi RTL + LTR + RTL") << QString::fromUtf16(arabic_str, 11) + QString("Hello world") + QString::fromUtf16(arabic_str, 11) << false << true << true << false << true << true << true; + QTest::newRow("Bidi LTR + RTL + LTR") << QString("Hello world") + QString::fromUtf16(arabic_str, 11) + QString("Hello world") << false << false << false << true << false << false << false; +} + +void tst_qdeclarativetextinput::isRightToLeft() +{ + QFETCH(QString, text); + QFETCH(bool, emptyString); + QFETCH(bool, firstCharacter); + QFETCH(bool, lastCharacter); + QFETCH(bool, middleCharacter); + QFETCH(bool, startString); + QFETCH(bool, midString); + QFETCH(bool, endString); + + QDeclarativeTextInput textInput; + textInput.setText(text); + + // first test that the right string is delivered to the QString::isRightToLeft() + QCOMPARE(textInput.isRightToLeft(0,0), text.mid(0,0).isRightToLeft()); + QCOMPARE(textInput.isRightToLeft(0,1), text.mid(0,1).isRightToLeft()); + QCOMPARE(textInput.isRightToLeft(text.count()-2, text.count()-1), text.mid(text.count()-2, text.count()-1).isRightToLeft()); + QCOMPARE(textInput.isRightToLeft(text.count()/2, text.count()/2 + 1), text.mid(text.count()/2, text.count()/2 + 1).isRightToLeft()); + QCOMPARE(textInput.isRightToLeft(0,text.count()/4), text.mid(0,text.count()/4).isRightToLeft()); + QCOMPARE(textInput.isRightToLeft(text.count()/4,3*text.count()/4), text.mid(text.count()/4,3*text.count()/4).isRightToLeft()); + if (text.isEmpty()) + QTest::ignoreMessage(QtWarningMsg, ": QML TextInput: isRightToLeft(start, end) called with the end property being smaller than the start."); + QCOMPARE(textInput.isRightToLeft(3*text.count()/4,text.count()-1), text.mid(3*text.count()/4,text.count()-1).isRightToLeft()); + + // then test that the feature actually works + QCOMPARE(textInput.isRightToLeft(0,0), emptyString); + QCOMPARE(textInput.isRightToLeft(0,1), firstCharacter); + QCOMPARE(textInput.isRightToLeft(text.count()-2, text.count()-1), lastCharacter); + QCOMPARE(textInput.isRightToLeft(text.count()/2, text.count()/2 + 1), middleCharacter); + QCOMPARE(textInput.isRightToLeft(0,text.count()/4), startString); + QCOMPARE(textInput.isRightToLeft(text.count()/4,3*text.count()/4), midString); + if (text.isEmpty()) + QTest::ignoreMessage(QtWarningMsg, ": QML TextInput: isRightToLeft(start, end) called with the end property being smaller than the start."); + QCOMPARE(textInput.isRightToLeft(3*text.count()/4,text.count()-1), endString); +} + void tst_qdeclarativetextinput::moveCursorSelection_data() { QTest::addColumn("testStr"); -- cgit v0.12 From 9f674617daf03026d78f8ce18328cbe6c31b689d Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Fri, 4 Mar 2011 13:26:12 +1000 Subject: Remove text alignment of empty QML editors following the layout direction Task-number: QTBUG-15880 Reviewed-by: Martin Jones Removed implicit text alignment of empty text in QML editor following the application's default layout direction. Change was originally made few days ago in the commit 88253db8a7d7910e1393b1948fb3747117538c92. Aligning empty and neutral text to the right for RTL locales requires much more comprehensive changes to the Qt's internal text classes than initially thought. Change-Id: I93a26df259b87dff47d57423949270656746c9a7 --- src/declarative/graphicsitems/qdeclarativetext.cpp | 5 +---- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 5 +---- src/declarative/graphicsitems/qdeclarativetextinput.cpp | 5 +---- .../declarative/qdeclarativetext/tst_qdeclarativetext.cpp | 10 +++------- .../qdeclarativetextedit/tst_qdeclarativetextedit.cpp | 12 ++++-------- .../qdeclarativetextinput/tst_qdeclarativetextinput.cpp | 12 ++++-------- 6 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 66a2355..3988f7f 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -218,10 +218,7 @@ void QDeclarativeTextPrivate::determineHorizontalAlignment() if (hAlignImplicit && q->isComponentComplete()) { // if no explicit alignment has been set, follow the natural layout direction of the text QDeclarativeText::HAlignment previousAlign = hAlign; - if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeText::AlignRight; - else - hAlign = text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft; + hAlign = text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft; if (previousAlign != hAlign) emit q->horizontalAlignmentChanged(hAlign); } diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 5e1459c..78729aa 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -1547,10 +1547,7 @@ void QDeclarativeTextEditPrivate::determineHorizontalAlignment() if (hAlignImplicit && q->isComponentComplete()) { // if no explicit alignment has been set, follow the natural layout direction of the text QDeclarativeTextEdit::HAlignment previousAlign = hAlign; - if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeTextEdit::AlignRight; - else - hAlign = rightToLeftText ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft; + hAlign = rightToLeftText ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft; if (previousAlign != hAlign) emit q->horizontalAlignmentChanged(hAlign); } diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index d9d9335..670a6ea 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -1283,10 +1283,7 @@ void QDeclarativeTextInputPrivate::determineHorizontalAlignment() QString text = control->text(); // if no explicit alignment has been set, follow the natural layout direction of the text QDeclarativeTextInput::HAlignment previousAlign = hAlign; - if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeTextInput::AlignRight; - else - hAlign = text.isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft; + hAlign = text.isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft; if (previousAlign != hAlign) emit q->horizontalAlignmentChanged(hAlign); } diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 6c9998e..2aeb425 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -547,15 +547,11 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); - // empty text should implicitly follow the layout direction - QApplication::setLayoutDirection(Qt::RightToLeft); + // empty text is also implicitly left aligned text->setText(""); - QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - text->setHAlign(QDeclarativeText::AlignLeft); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); - - // set layout direction back to LTR to avoid affecting other autotests - QApplication::setLayoutDirection(Qt::LeftToRight); + text->setHAlign(QDeclarativeText::AlignRight); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); delete canvas; } diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index 6d5750f..ff52167 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -474,17 +474,13 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); - // empty text should implicitly follow the layout direction - QApplication::setLayoutDirection(Qt::RightToLeft); + // empty text is also implicitly left aligned textEdit->setText(""); - QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); - QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); - textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); - - // set layout direction back to LTR to avoid affecting other autotests - QApplication::setLayoutDirection(Qt::LeftToRight); + textEdit->setHAlign(QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); delete canvas; } diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 72e8b11..666bbc8 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -1078,17 +1078,13 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); - // empty text should implicitly follow the layout direction - QApplication::setLayoutDirection(Qt::RightToLeft); + // empty text is also implicitly left aligned textInput->setText(""); - QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); - QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); - textInput->setHAlign(QDeclarativeTextInput::AlignLeft); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); - - // set layout direction back to LTR to avoid affecting other autotests - QApplication::setLayoutDirection(Qt::LeftToRight); + textInput->setHAlign(QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); delete canvas; } -- cgit v0.12 From 7cdd849b50f0314d0eb227e1d0c92627f2be555b Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Fri, 4 Mar 2011 14:33:30 +1000 Subject: Fix RTL multiline Text drawing We handled horizontal alignment ourselves, but this neglected some RTL layouting issues (e.g. trailing spaces). Allow QTextLayout to do the aligning for us. Also fix eliding of multiline text for RTL language - in this case the eliding should be to the left. Change-Id: I487137b123ae66c1f5fc358a8d8a013049d05818 Reviewed-by: Joona Petrell --- src/declarative/graphicsitems/qdeclarativetext.cpp | 75 ++++++---------------- .../qdeclarativetext/tst_qdeclarativetext.cpp | 12 ++-- 2 files changed, 26 insertions(+), 61 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 3988f7f..890e481 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -364,19 +364,16 @@ QSize QDeclarativeTextPrivate::setupTextLayout() Q_Q(QDeclarativeText); layout.setCacheEnabled(true); - qreal height = 0; qreal widthUsed = 0; qreal lineWidth = 0; - int visibleTextLength = 0; int visibleCount = 0; //set manual width - if ((wrapMode != QDeclarativeText::NoWrap || elideMode != QDeclarativeText::ElideNone) && q->widthValid()) + if (q->widthValid()) lineWidth = q->width(); QTextOption textOption = layout.textOption(); - if (hAlign == QDeclarativeText::AlignJustify) - textOption.setAlignment(Qt::Alignment(hAlign)); + textOption.setAlignment(Qt::Alignment(hAlign)); textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); layout.setTextOption(textOption); @@ -384,7 +381,6 @@ QSize QDeclarativeTextPrivate::setupTextLayout() bool truncate = false; QFontMetrics fm(layout.font()); - qreal elideWidth = fm.width(elideChar); elidePos = QPointF(); if (requireImplicitWidth && q->widthValid()) { @@ -407,36 +403,35 @@ QSize QDeclarativeTextPrivate::setupTextLayout() layout.beginLayout(); if (!lineWidth) lineWidth = INT_MAX; - int y = 0; int linesLeft = maximumLineCount; + int visibleTextLength = 0; while (linesLeft > 0) { QTextLine line = layout.createLine(); if (!line.isValid()) break; visibleCount++; - line.setLineWidth(lineWidth); + if (lineWidth) + line.setLineWidth(lineWidth); visibleTextLength += line.textLength(); if (--linesLeft == 0) { if (visibleTextLength < text.length()) { truncate = true; if (elideMode==QDeclarativeText::ElideRight && q->widthValid()) { + qreal elideWidth = fm.width(elideChar); // Need to correct for alignment line.setLineWidth(lineWidth-elideWidth); - int x = line.naturalTextWidth(); - if (hAlign == QDeclarativeText::AlignRight) { - x = q->width()-elideWidth; - } else if (hAlign == QDeclarativeText::AlignHCenter) { - x = (q->width()+line.naturalTextWidth() - elideWidth)/2; + if (layout.text().mid(line.textStart(), line.textLength()).isRightToLeft()) { + line.setPosition(QPointF(line.position().x() + elideWidth, line.position().y())); + elidePos.setX(line.naturalTextRect().left() - elideWidth); + } else { + elidePos.setX(line.naturalTextRect().right()); } - elidePos = QPointF(x, y + fm.ascent()); elideText = true; } } } - - y += line.height(); } layout.endLayout(); @@ -458,36 +453,20 @@ QSize QDeclarativeTextPrivate::setupTextLayout() layout.endLayout(); } + qreal height = 0; for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); - widthUsed = qMax(widthUsed, line.naturalTextWidth()); + // calc width + widthUsed = qMax(widthUsed, line.naturalTextRect().right()); + // set line spacing + line.setPosition(QPointF(line.position().x(), height)); + if (elideText && i == layout.lineCount()-1) + elidePos.setY(height + fm.ascent()); + height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight; } - qreal layoutWidth = q->widthValid() ? q->width() : widthUsed; if (!q->widthValid()) - naturalWidth = layoutWidth; - - qreal x = 0; - for (int i = 0; i < layout.lineCount(); ++i) { - QTextLine line = layout.lineAt(i); - line.setPosition(QPointF(0, height)); - height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight; - - if (!cacheAllTextAsImage) { - if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { - x = 0; - } else if (hAlign == QDeclarativeText::AlignRight) { - x = layoutWidth - line.naturalTextWidth(); - if (elideText && i == layout.lineCount()-1) - x -= elideWidth; // Correct for when eliding multilines - } else if (hAlign == QDeclarativeText::AlignHCenter) { - x = (layoutWidth - line.naturalTextWidth()) / 2; - if (elideText && i == layout.lineCount()-1) - x -= elideWidth/2; // Correct for when eliding multilines - } - line.setPosition(QPointF(x, line.y())); - } - } + naturalWidth = widthUsed; //Update the number of visible lines if (lineCount != visibleCount) { @@ -506,20 +485,6 @@ QPixmap QDeclarativeTextPrivate::textLayoutImage(bool drawStyle) { //do layout QSize size = layedOutTextSize; - - qreal x = 0; - for (int i = 0; i < layout.lineCount(); ++i) { - QTextLine line = layout.lineAt(i); - if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { - x = 0; - } else if (hAlign == QDeclarativeText::AlignRight) { - x = size.width() - line.naturalTextWidth(); - } else if (hAlign == QDeclarativeText::AlignHCenter) { - x = (size.width() - line.naturalTextWidth()) / 2; - } - line.setPosition(QPointF(x, line.y())); - } - //paint text QPixmap img(size); if (!size.isEmpty()) { diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 2aeb425..f27fc07 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -520,32 +520,32 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() // implicit alignment should follow the reading direction of RTL text QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); // explicitly left aligned text->setHAlign(QDeclarativeText::AlignLeft); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); - QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); // explicitly right aligned text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); // explicitly center aligned text->setHAlign(QDeclarativeText::AlignHCenter); QCOMPARE(text->hAlign(), QDeclarativeText::AlignHCenter); - QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); // reseted alignment should go back to following the text reading direction text->resetHAlign(); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); // English text should be implicitly left aligned text->setText("Hello world!"); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); - QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); // empty text is also implicitly left aligned text->setText(""); -- cgit v0.12 From 86a0b3749a336181c241dd239c863f4ddbbd4ad2 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Fri, 4 Mar 2011 16:50:02 +1000 Subject: Removal of text alignment layout direction dependency was missing couple of changes Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: I1ea87b05c483e5c9339fc47cf719c22d001ef52b --- src/declarative/graphicsitems/qdeclarativetext.cpp | 2 -- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 3 --- src/declarative/graphicsitems/qdeclarativetextinput.cpp | 3 --- 3 files changed, 8 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 890e481..e66a83e 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -107,8 +107,6 @@ QDeclarativeTextPrivate::QDeclarativeTextPrivate() cacheAllTextAsImage = enableImageCache(); QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton; QGraphicsItemPrivate::flags = QGraphicsItemPrivate::flags & ~QGraphicsItem::ItemHasNoContents; - if (QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeText::AlignRight; } QTextDocumentWithImageResources::QTextDocumentWithImageResources(QDeclarativeText *parent) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 78729aa..e4cd66c 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -1512,9 +1512,6 @@ void QDeclarativeTextEditPrivate::init() document->setDocumentMargin(textMargin); document->setUndoRedoEnabled(false); // flush undo buffer. document->setUndoRedoEnabled(true); - - if (QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeTextEdit::AlignRight; updateDefaultTextOption(); } diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 670a6ea..0c021db 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -1827,9 +1827,6 @@ void QDeclarativeTextInputPrivate::init() QPalette p = control->palette(); selectedTextColor = p.color(QPalette::HighlightedText); selectionColor = p.color(QPalette::Highlight); - - if (QApplication::layoutDirection() == Qt::RightToLeft) - hAlign = QDeclarativeTextInput::AlignRight; } void QDeclarativeTextInput::cursorPosChanged() -- cgit v0.12 From 7872985cc2eb21ae9283aa8e6779f490b500dff0 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Thu, 3 Mar 2011 11:06:13 +1000 Subject: Update QtDeclarative def files Symbols introduced in Qt Quick 1.1 layout mirroring support. Task-number: QTBUG-17280 Reviewed-by: Martin Jones --- src/s60installs/bwins/QtDeclarativeu.def | 5 +++++ src/s60installs/eabi/QtDeclarativeu.def | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/s60installs/bwins/QtDeclarativeu.def b/src/s60installs/bwins/QtDeclarativeu.def index 8b23b63..5490f0d 100644 --- a/src/s60installs/bwins/QtDeclarativeu.def +++ b/src/s60installs/bwins/QtDeclarativeu.def @@ -1888,4 +1888,9 @@ EXPORTS ??1QDeclarativeRefCount@@UAE@XZ @ 1887 NONAME ; QDeclarativeRefCount::~QDeclarativeRefCount(void) ?addref@QDeclarativeRefCount@@QAEXXZ @ 1888 NONAME ; void QDeclarativeRefCount::addref(void) ?release@QDeclarativeRefCount@@QAEXXZ @ 1889 NONAME ; void QDeclarativeRefCount::release(void) + ?resolveLayoutMirror@QDeclarativeItemPrivate@@QAEXXZ @ 1890 NONAME ; void QDeclarativeItemPrivate::resolveLayoutMirror(void) + ?mirrorChange@QDeclarativeItemPrivate@@UAEXXZ @ 1891 NONAME ; void QDeclarativeItemPrivate::mirrorChange(void) + ?setLayoutMirror@QDeclarativeItemPrivate@@QAEX_N@Z @ 1892 NONAME ; void QDeclarativeItemPrivate::setLayoutMirror(bool) + ?setImplicitLayoutMirror@QDeclarativeItemPrivate@@QAEX_N0@Z @ 1893 NONAME ; void QDeclarativeItemPrivate::setImplicitLayoutMirror(bool, bool) + ?isMirrored@QDeclarativeItemPrivate@@QBE_NXZ @ 1894 NONAME ; bool QDeclarativeItemPrivate::isMirrored(void) const diff --git a/src/s60installs/eabi/QtDeclarativeu.def b/src/s60installs/eabi/QtDeclarativeu.def index 130e2d5..2849068 100644 --- a/src/s60installs/eabi/QtDeclarativeu.def +++ b/src/s60installs/eabi/QtDeclarativeu.def @@ -1930,4 +1930,7 @@ EXPORTS _ZN20QDeclarativeRefCountD2Ev @ 1929 NONAME _ZTI20QDeclarativeRefCount @ 1930 NONAME _ZTV20QDeclarativeRefCount @ 1931 NONAME + _ZN23QDeclarativeItemPrivate15setLayoutMirrorEb @ 1932 NONAME + _ZN23QDeclarativeItemPrivate19resolveLayoutMirrorEv @ 1933 NONAME + _ZN23QDeclarativeItemPrivate23setImplicitLayoutMirrorEbb @ 1934 NONAME -- cgit v0.12 From 27e4302b7f45f22180693d26747f419177c81e27 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Mon, 7 Mar 2011 13:10:08 +1000 Subject: Reverse horizontal alignment of QML editors when the layout mirroring is enabled Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: Ie9cebd7bc6d40f5f555bfd83ddc3a24a55c6cb4d --- src/declarative/graphicsitems/qdeclarativetext.cpp | 95 ++++++++++++++------- src/declarative/graphicsitems/qdeclarativetext_p.h | 3 + .../graphicsitems/qdeclarativetext_p_p.h | 4 +- .../graphicsitems/qdeclarativetextedit.cpp | 98 ++++++++++++++++------ .../graphicsitems/qdeclarativetextedit_p.h | 3 + .../graphicsitems/qdeclarativetextedit_p_p.h | 4 +- .../graphicsitems/qdeclarativetextinput.cpp | 96 +++++++++++++++------ .../graphicsitems/qdeclarativetextinput_p.h | 4 +- .../graphicsitems/qdeclarativetextinput_p_p.h | 4 +- .../qdeclarativetext/tst_qdeclarativetext.cpp | 29 +++++++ .../tst_qdeclarativetextedit.cpp | 24 ++++++ .../tst_qdeclarativetextinput.cpp | 30 +++++++ 12 files changed, 309 insertions(+), 85 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index e66a83e..4a931fd 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -210,18 +210,6 @@ qreal QDeclarativeTextPrivate::implicitWidth() const return mImplicitWidth; } -void QDeclarativeTextPrivate::determineHorizontalAlignment() -{ - Q_Q(QDeclarativeText); - if (hAlignImplicit && q->isComponentComplete()) { - // if no explicit alignment has been set, follow the natural layout direction of the text - QDeclarativeText::HAlignment previousAlign = hAlign; - hAlign = text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft; - if (previousAlign != hAlign) - emit q->horizontalAlignmentChanged(hAlign); - } -} - void QDeclarativeTextPrivate::updateLayout() { Q_Q(QDeclarativeText); @@ -305,7 +293,7 @@ void QDeclarativeTextPrivate::updateSize() ensureDoc(); doc->setDefaultFont(font); QTextOption option; - option.setAlignment((Qt::Alignment)int(hAlign | vAlign)); + option.setAlignment((Qt::Alignment)int(q->effectiveHAlign() | vAlign)); option.setWrapMode(QTextOption::WrapMode(wrapMode)); doc->setDefaultTextOption(option); if (requireImplicitWidth && q->widthValid()) { @@ -371,7 +359,7 @@ QSize QDeclarativeTextPrivate::setupTextLayout() lineWidth = q->width(); QTextOption textOption = layout.textOption(); - textOption.setAlignment(Qt::Alignment(hAlign)); + textOption.setAlignment(Qt::Alignment(q->effectiveHAlign())); textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); layout.setTextOption(textOption); @@ -1049,6 +1037,7 @@ void QDeclarativeText::setStyleColor(const QColor &color) /*! \qmlproperty enumeration Text::horizontalAlignment \qmlproperty enumeration Text::verticalAlignment + \qmlproperty enumeration Text::effectiveHorizontalAlignment Sets the horizontal and vertical alignment of the text within the Text items width and height. By default, the text is vertically aligned to the top. Horizontal @@ -1063,6 +1052,11 @@ void QDeclarativeText::setStyleColor(const QColor &color) all alignments are equivalent. If you want the text to be, say, centered in its parent, then you will need to either modify the Item::anchors, or set horizontalAlignment to Text.AlignHCenter and bind the width to that of the parent. + + When using the attached property LayoutMirroring::enabled to mirror application + layouts, the horizontal alignment of text will also be mirrored. However, the property + \c horizontalAlignment will remain unchanged. To query the effective horizontal alignment + of Text, use the read-only property \c effectiveHorizontalAlignment. */ QDeclarativeText::HAlignment QDeclarativeText::hAlign() const { @@ -1073,29 +1067,72 @@ QDeclarativeText::HAlignment QDeclarativeText::hAlign() const void QDeclarativeText::setHAlign(HAlignment align) { Q_D(QDeclarativeText); + bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror; d->hAlignImplicit = false; - if (d->hAlign == align) - return; - - if (isComponentComplete()) - prepareGeometryChange(); - - d->hAlign = align; - d->updateLayout(); - - emit horizontalAlignmentChanged(align); + if (d->setHAlign(align, forceAlign) && isComponentComplete()) + d->updateLayout(); } void QDeclarativeText::resetHAlign() { Q_D(QDeclarativeText); d->hAlignImplicit = true; - QDeclarativeText::HAlignment oldAlignment = d->hAlign; - d->determineHorizontalAlignment(); - if (oldAlignment != d->hAlign) { - prepareGeometryChange(); + if (d->determineHorizontalAlignment() && isComponentComplete()) d->updateLayout(); - emit horizontalAlignmentChanged(d->hAlign); +} + +QDeclarativeText::HAlignment QDeclarativeText::effectiveHAlign() const +{ + Q_D(const QDeclarativeText); + QDeclarativeText::HAlignment effectiveAlignment = d->hAlign; + if (!d->hAlignImplicit && d->effectiveLayoutMirror) { + switch (d->hAlign) { + case QDeclarativeText::AlignLeft: + effectiveAlignment = QDeclarativeText::AlignRight; + break; + case QDeclarativeText::AlignRight: + effectiveAlignment = QDeclarativeText::AlignLeft; + break; + default: + break; + } + } + return effectiveAlignment; +} + +bool QDeclarativeTextPrivate::setHAlign(QDeclarativeText::HAlignment alignment, bool forceAlign) +{ + Q_Q(QDeclarativeText); + if (hAlign != alignment || forceAlign) { + QDeclarativeText::HAlignment oldEffectiveHAlign = q->effectiveHAlign(); + hAlign = alignment; + + emit q->horizontalAlignmentChanged(hAlign); + if (oldEffectiveHAlign != q->effectiveHAlign()) + emit q->effectiveHorizontalAlignmentChanged(); + return true; + } + return false; +} + +bool QDeclarativeTextPrivate::determineHorizontalAlignment() +{ + Q_Q(QDeclarativeText); + if (hAlignImplicit && q->isComponentComplete()) { + // if no explicit alignment has been set, follow the natural layout direction of the text + return setHAlign(text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft); + } + return false; +} + +void QDeclarativeTextPrivate::mirrorChange() +{ + Q_Q(QDeclarativeText); + if (q->isComponentComplete()) { + if (!hAlignImplicit && (hAlign == QDeclarativeText::AlignRight || hAlign == QDeclarativeText::AlignLeft)) { + updateLayout(); + emit q->effectiveHorizontalAlignmentChanged(); + } } } diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h index 92c2eab..a1153c2 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p.h @@ -70,6 +70,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeImplici Q_PROPERTY(TextStyle style READ style WRITE setStyle NOTIFY styleChanged) Q_PROPERTY(QColor styleColor READ styleColor WRITE setStyleColor NOTIFY styleColorChanged) Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) + Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION 1) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) @@ -134,6 +135,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); void resetHAlign(); + HAlignment effectiveHAlign() const; VAlignment vAlign() const; void setVAlign(VAlignment align); @@ -189,6 +191,7 @@ Q_SIGNALS: void paintedSizeChanged(); Q_REVISION(1) void lineHeightChanged(qreal lineHeight); Q_REVISION(1) void lineHeightModeChanged(LineHeightMode mode); + Q_REVISION(1) void effectiveHorizontalAlignmentChanged(); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index 6886d91..0864c8f 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -76,7 +76,9 @@ public: void updateSize(); void updateLayout(); - void determineHorizontalAlignment(); + bool determineHorizontalAlignment(); + bool setHAlign(QDeclarativeText::HAlignment, bool forceAlign = false); + void mirrorChange(); QString text; QFont font; diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index e4cd66c..7a75ece 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -458,6 +458,7 @@ void QDeclarativeTextEdit::setSelectedTextColor(const QColor &color) /*! \qmlproperty enumeration TextEdit::horizontalAlignment \qmlproperty enumeration TextEdit::verticalAlignment + \qmlproperty enumeration TextEdit::effectiveHorizontalAlignment Sets the horizontal and vertical alignment of the text within the TextEdit item's width and height. By default, the text alignment follows the natural alignment @@ -476,8 +477,13 @@ void QDeclarativeTextEdit::setSelectedTextColor(const QColor &color) \list \o TextEdit.AlignTop (default) \o TextEdit.AlignBottom - \c TextEdit.AlignVCenter + \o TextEdit.AlignVCenter \endlist + + When using the attached property LayoutMirroring::enabled to mirror application + layouts, the horizontal alignment of text will also be mirrored. However, the property + \c horizontalAlignment will remain unchanged. To query the effective horizontal alignment + of TextEdit, use the read-only property \c effectiveHorizontalAlignment. */ QDeclarativeTextEdit::HAlignment QDeclarativeTextEdit::hAlign() const { @@ -485,28 +491,79 @@ QDeclarativeTextEdit::HAlignment QDeclarativeTextEdit::hAlign() const return d->hAlign; } -void QDeclarativeTextEdit::setHAlign(QDeclarativeTextEdit::HAlignment alignment) +void QDeclarativeTextEdit::setHAlign(HAlignment align) { Q_D(QDeclarativeTextEdit); + bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror; d->hAlignImplicit = false; - if (alignment == d->hAlign) - return; - d->hAlign = alignment; - d->updateDefaultTextOption(); - updateSize(); - emit horizontalAlignmentChanged(d->hAlign); + if (d->setHAlign(align, forceAlign) && isComponentComplete()) { + d->updateDefaultTextOption(); + updateSize(); + } } void QDeclarativeTextEdit::resetHAlign() { Q_D(QDeclarativeTextEdit); d->hAlignImplicit = true; - QDeclarativeTextEdit::HAlignment oldAlignment = d->hAlign; - d->determineHorizontalAlignment(); - if (oldAlignment != d->hAlign) { + if (d->determineHorizontalAlignment() && isComponentComplete()) { d->updateDefaultTextOption(); updateSize(); + } +} +QDeclarativeTextEdit::HAlignment QDeclarativeTextEdit::effectiveHAlign() const +{ + Q_D(const QDeclarativeTextEdit); + QDeclarativeTextEdit::HAlignment effectiveAlignment = d->hAlign; + if (!d->hAlignImplicit && d->effectiveLayoutMirror) { + switch (d->hAlign) { + case QDeclarativeTextEdit::AlignLeft: + effectiveAlignment = QDeclarativeTextEdit::AlignRight; + break; + case QDeclarativeTextEdit::AlignRight: + effectiveAlignment = QDeclarativeTextEdit::AlignLeft; + break; + default: + break; + } + } + return effectiveAlignment; +} + +bool QDeclarativeTextEditPrivate::setHAlign(QDeclarativeTextEdit::HAlignment alignment, bool forceAlign) +{ + Q_Q(QDeclarativeTextEdit); + if (hAlign != alignment || forceAlign) { + QDeclarativeTextEdit::HAlignment oldEffectiveHAlign = q->effectiveHAlign(); + hAlign = alignment; + emit q->horizontalAlignmentChanged(alignment); + if (oldEffectiveHAlign != q->effectiveHAlign()) + emit q->effectiveHorizontalAlignmentChanged(); + return true; + } + return false; +} + +bool QDeclarativeTextEditPrivate::determineHorizontalAlignment() +{ + Q_Q(QDeclarativeTextEdit); + if (hAlignImplicit && q->isComponentComplete()) { + // if no explicit alignment has been set, follow the natural layout direction of the text + return setHAlign(text.isRightToLeft() ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft); + } + return false; +} + +void QDeclarativeTextEditPrivate::mirrorChange() +{ + Q_Q(QDeclarativeTextEdit); + if (q->isComponentComplete()) { + if (!hAlignImplicit && (hAlign == QDeclarativeTextEdit::AlignRight || hAlign == QDeclarativeTextEdit::AlignLeft)) { + updateDefaultTextOption(); + q->updateSize(); + emit q->effectiveHorizontalAlignmentChanged(); + } } } @@ -1538,18 +1595,6 @@ void QDeclarativeTextEdit::moveCursorDelegate() d->cursor->setY(cursorRect.y()); } -void QDeclarativeTextEditPrivate::determineHorizontalAlignment() -{ - Q_Q(QDeclarativeTextEdit); - if (hAlignImplicit && q->isComponentComplete()) { - // if no explicit alignment has been set, follow the natural layout direction of the text - QDeclarativeTextEdit::HAlignment previousAlign = hAlign; - hAlign = rightToLeftText ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft; - if (previousAlign != hAlign) - emit q->horizontalAlignmentChanged(hAlign); - } -} - void QDeclarativeTextEditPrivate::updateSelection() { Q_Q(QDeclarativeTextEdit); @@ -1698,14 +1743,15 @@ void QDeclarativeTextEdit::updateTotalLines() void QDeclarativeTextEditPrivate::updateDefaultTextOption() { + Q_Q(QDeclarativeTextEdit); QTextOption opt = document->defaultTextOption(); int oldAlignment = opt.alignment(); - QDeclarativeTextEdit::HAlignment horizontalAlignment = hAlign; + QDeclarativeTextEdit::HAlignment horizontalAlignment = q->effectiveHAlign(); if (rightToLeftText) { - if (hAlign == QDeclarativeTextEdit::AlignLeft) + if (horizontalAlignment == QDeclarativeTextEdit::AlignLeft) horizontalAlignment = QDeclarativeTextEdit::AlignRight; - else if (hAlign == QDeclarativeTextEdit::AlignRight) + else if (horizontalAlignment == QDeclarativeTextEdit::AlignRight) horizontalAlignment = QDeclarativeTextEdit::AlignLeft; } opt.setAlignment((Qt::Alignment)(int)(horizontalAlignment | vAlign)); diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index 471b201..25ca1e7 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -73,6 +73,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativeImplicitSizePa Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) + Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION 1) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) @@ -154,6 +155,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); void resetHAlign(); + HAlignment effectiveHAlign() const; VAlignment vAlign() const; void setVAlign(VAlignment align); @@ -247,6 +249,7 @@ Q_SIGNALS: Q_REVISION(1) void linkActivated(const QString &link); Q_REVISION(1) void canPasteChanged(); Q_REVISION(1) void inputMethodComposingChanged(); + Q_REVISION(1) void effectiveHorizontalAlignmentChanged(); public Q_SLOTS: void selectAll(); diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h index f4a6c0e..36e1b51 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h @@ -88,7 +88,9 @@ public: void updateDefaultTextOption(); void relayoutDocument(); void updateSelection(); - void determineHorizontalAlignment(); + bool determineHorizontalAlignment(); + bool setHAlign(QDeclarativeTextEdit::HAlignment, bool forceAlign = false); + void mirrorChange(); qreal implicitWidth() const; void focusChanged(bool); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 0c021db..0deffe9 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -326,6 +326,7 @@ void QDeclarativeTextInput::setSelectedTextColor(const QColor &color) /*! \qmlproperty enumeration TextInput::horizontalAlignment + \qmlproperty enumeration TextInput::effectiveHorizontalAlignment Sets the horizontal alignment of the text within the TextInput item's width and height. By default, the text alignment follows the natural alignment @@ -340,6 +341,11 @@ void QDeclarativeTextInput::setSelectedTextColor(const QColor &color) The valid values for \c horizontalAlignment are \c TextInput.AlignLeft, \c TextInput.AlignRight and \c TextInput.AlignHCenter. + + When using the attached property LayoutMirroring::enabled to mirror application + layouts, the horizontal alignment of text will also be mirrored. However, the property + \c horizontalAlignment will remain unchanged. To query the effective horizontal alignment + of TextInput, use the read-only property \c effectiveHorizontalAlignment. */ QDeclarativeTextInput::HAlignment QDeclarativeTextInput::hAlign() const { @@ -350,25 +356,75 @@ QDeclarativeTextInput::HAlignment QDeclarativeTextInput::hAlign() const void QDeclarativeTextInput::setHAlign(HAlignment align) { Q_D(QDeclarativeTextInput); + bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror; d->hAlignImplicit = false; - if(align == d->hAlign || align > QDeclarativeTextInput::AlignHCenter) // justify not supported - return; - d->hAlign = align; - updateRect(); - d->updateHorizontalScroll(); - emit horizontalAlignmentChanged(d->hAlign); + if (d->setHAlign(align, forceAlign) && isComponentComplete()) { + updateRect(); + d->updateHorizontalScroll(); + } } void QDeclarativeTextInput::resetHAlign() { Q_D(QDeclarativeTextInput); d->hAlignImplicit = true; - QDeclarativeTextInput::HAlignment oldAlignment = d->hAlign; - d->determineHorizontalAlignment(); - if (oldAlignment != d->hAlign) { + if (d->determineHorizontalAlignment() && isComponentComplete()) { updateRect(); d->updateHorizontalScroll(); - emit horizontalAlignmentChanged(d->hAlign); + } +} + +QDeclarativeTextInput::HAlignment QDeclarativeTextInput::effectiveHAlign() const +{ + Q_D(const QDeclarativeTextInput); + QDeclarativeTextInput::HAlignment effectiveAlignment = d->hAlign; + if (!d->hAlignImplicit && d->effectiveLayoutMirror) { + switch (d->hAlign) { + case QDeclarativeTextInput::AlignLeft: + effectiveAlignment = QDeclarativeTextInput::AlignRight; + break; + case QDeclarativeTextInput::AlignRight: + effectiveAlignment = QDeclarativeTextInput::AlignLeft; + break; + default: + break; + } + } + return effectiveAlignment; +} + +bool QDeclarativeTextInputPrivate::setHAlign(QDeclarativeTextInput::HAlignment alignment, bool forceAlign) +{ + Q_Q(QDeclarativeTextInput); + if ((hAlign != alignment || forceAlign) && alignment <= QDeclarativeTextInput::AlignHCenter) { // justify not supported + QDeclarativeTextInput::HAlignment oldEffectiveHAlign = q->effectiveHAlign(); + hAlign = alignment; + return true; + emit q->horizontalAlignmentChanged(alignment); + if (oldEffectiveHAlign != q->effectiveHAlign()) + emit q->effectiveHorizontalAlignmentChanged(); + } + return false; +} + +bool QDeclarativeTextInputPrivate::determineHorizontalAlignment() +{ + if (hAlignImplicit) { + // if no explicit alignment has been set, follow the natural layout direction of the text + return setHAlign(control->text().isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft); + } + return false; +} + +void QDeclarativeTextInputPrivate::mirrorChange() +{ + Q_Q(QDeclarativeTextInput); + if (q->isComponentComplete()) { + if (!hAlignImplicit && (hAlign == QDeclarativeTextInput::AlignRight || hAlign == QDeclarativeTextInput::AlignLeft)) { + q->updateRect(); + updateHorizontalScroll(); + emit q->effectiveHorizontalAlignmentChanged(); + } } } @@ -1226,10 +1282,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() QRect br(q->boundingRect().toRect()); int widthUsed = calculateTextWidth(); + QDeclarativeTextInput::HAlignment effectiveHAlign = q->effectiveHAlign(); if (autoScroll) { if (widthUsed <= br.width()) { // text fits in br; use hscroll for alignment - switch (hAlign & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { + switch (effectiveHAlign & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { case Qt::AlignRight: hscroll = widthUsed - br.width() - 1; break; @@ -1261,7 +1318,7 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() hscroll = cix; } } else { - switch (hAlign) { + switch (effectiveHAlign) { case QDeclarativeTextInput::AlignRight: hscroll = q->width() - widthUsed; break; @@ -1276,19 +1333,6 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() } } -void QDeclarativeTextInputPrivate::determineHorizontalAlignment() -{ - Q_Q(QDeclarativeTextInput); - if (hAlignImplicit) { - QString text = control->text(); - // if no explicit alignment has been set, follow the natural layout direction of the text - QDeclarativeTextInput::HAlignment previousAlign = hAlign; - hAlign = text.isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft; - if (previousAlign != hAlign) - emit q->horizontalAlignmentChanged(hAlign); - } -} - void QDeclarativeTextInput::drawContents(QPainter *p, const QRect &r) { Q_D(QDeclarativeTextInput); @@ -1874,8 +1918,8 @@ void QDeclarativeTextInput::q_textChanged() { Q_D(QDeclarativeTextInput); updateSize(); - d->updateHorizontalScroll(); d->determineHorizontalAlignment(); + d->updateHorizontalScroll(); updateMicroFocus(); emit textChanged(); emit displayTextChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index ce5f267..8c873b3 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -71,7 +71,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativeImplicitSizeP Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) - + Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged REVISION 1) Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged) Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged) Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) @@ -154,6 +154,7 @@ public: HAlignment hAlign() const; void setHAlign(HAlignment align); void resetHAlign(); + HAlignment effectiveHAlign() const; bool isReadOnly() const; void setReadOnly(bool); @@ -242,6 +243,7 @@ Q_SIGNALS: Q_REVISION(1) void mouseSelectionModeChanged(SelectionMode mode); Q_REVISION(1) void canPasteChanged(); Q_REVISION(1) void inputMethodComposingChanged(); + Q_REVISION(1) void effectiveHorizontalAlignmentChanged(); protected: virtual void geometryChanged(const QRectF &newGeometry, diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h index 60eeb76..fd4da2e 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h @@ -103,7 +103,9 @@ public: void startCreatingCursor(); void focusChanged(bool hasFocus); void updateHorizontalScroll(); - void determineHorizontalAlignment(); + bool determineHorizontalAlignment(); + bool setHAlign(QDeclarativeTextInput::HAlignment, bool forceAlign = false); + void mirrorChange(); int calculateTextWidth(); bool sendMouseEventToInputContext(QGraphicsSceneMouseEvent *event, QEvent::Type eventType); diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index f27fc07..261dc51 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -520,28 +520,57 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() // implicit alignment should follow the reading direction of RTL text QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); // explicitly left aligned text->setHAlign(QDeclarativeText::AlignLeft); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); // explicitly right aligned text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); // explicitly center aligned text->setHAlign(QDeclarativeText::AlignHCenter); QCOMPARE(text->hAlign(), QDeclarativeText::AlignHCenter); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2); // reseted alignment should go back to following the text reading direction text->resetHAlign(); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); + // mirror the text item + QDeclarativeItemPrivate::get(text)->setLayoutMirror(true); + + // mirrored implicit alignment should continue to follow the reading direction of the text + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), QDeclarativeText::AlignRight); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); + + // mirrored explicitly right aligned behaves as left aligned + text->setHAlign(QDeclarativeText::AlignRight); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), QDeclarativeText::AlignLeft); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); + + // mirrored explicitly left aligned behaves as right aligned + text->setHAlign(QDeclarativeText::AlignLeft); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QCOMPARE(text->effectiveHAlign(), QDeclarativeText::AlignRight); + QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); + + // disable mirroring + QDeclarativeItemPrivate::get(text)->setLayoutMirror(false); + text->resetHAlign(); + // English text should be implicitly left aligned text->setText("Hello world!"); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index ff52167..973128d 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -469,6 +469,30 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + // mirror the text item + QDeclarativeItemPrivate::get(textEdit)->setLayoutMirror(true); + + // mirrored implicit alignment should continue to follow the reading direction of the text + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->effectiveHAlign(), QDeclarativeTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + + // mirrored explicitly right aligned behaves as left aligned + textEdit->setHAlign(QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->effectiveHAlign(), QDeclarativeTextEdit::AlignLeft); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + + // mirrored explicitly left aligned behaves as right aligned + textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->effectiveHAlign(), QDeclarativeTextEdit::AlignRight); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + + // disable mirroring + QDeclarativeItemPrivate::get(textEdit)->setLayoutMirror(false); + textEdit->resetHAlign(); + // English text should be implicitly left aligned textEdit->setText("Hello world!"); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 666bbc8..bcae3fc 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -1050,20 +1050,24 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() // implicit alignment should follow the reading direction of RTL text QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); // explicitly left aligned textInput->setHAlign(QDeclarativeTextInput::AlignLeft); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); // explicitly right aligned textInput->setHAlign(QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); // explicitly center aligned textInput->setHAlign(QDeclarativeTextInput::AlignHCenter); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignHCenter); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); QVERIFY(-textInputPrivate->hscroll + textInputPrivate->width() > canvas->width()/2); @@ -1071,8 +1075,34 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() // reseted alignment should go back to following the text reading direction textInput->resetHAlign(); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + // mirror the text item + QDeclarativeItemPrivate::get(textInput)->setLayoutMirror(true); + + // mirrored implicit alignment should continue to follow the reading direction of the text + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + + // explicitly right aligned behaves as left aligned + textInput->setHAlign(QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); + QCOMPARE(textInput->effectiveHAlign(), QDeclarativeTextInput::AlignLeft); + QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); + + // mirrored explicitly left aligned behaves as right aligned + textInput->setHAlign(QDeclarativeTextInput::AlignLeft); + QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); + QCOMPARE(textInput->effectiveHAlign(), QDeclarativeTextInput::AlignRight); + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); + + // disable mirroring + QDeclarativeItemPrivate::get(textInput)->setLayoutMirror(false); + QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign()); + textInput->resetHAlign(); + // English text should be implicitly left aligned textInput->setText("Hello world!"); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); -- cgit v0.12 From cc6408ccd5453d1bed9f98b9caa14861cea5742b Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Mon, 7 Mar 2011 15:02:01 +1000 Subject: Fix documentation talking about old property LayoutMirror::mirror Task-number: QTBUG-11042 Reviewed-by: Bea Lam Change-Id: I3f842b7672ee57dadbd1ed9216249c36aa527d6a --- src/declarative/graphicsitems/qdeclarativegridview.cpp | 2 +- src/declarative/graphicsitems/qdeclarativelistview.cpp | 2 +- src/declarative/graphicsitems/qdeclarativepositioners.cpp | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index e3ec7e2..5c2f781 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -1804,7 +1804,7 @@ void QDeclarativeGridView::setLayoutDirection(Qt::LayoutDirection layoutDirectio \qmlproperty enumeration GridView::effectiveLayoutDirection This property holds the effective layout direction of the grid. - When using the attached property \l {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts, the visual layout direction of the grid will be mirrored. However, the property \l {GridView::layoutDirection}{layoutDirection} will remain unchanged. diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index 0a54c92..f9f1a48 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -2146,7 +2146,7 @@ void QDeclarativeListView::setLayoutDirection(Qt::LayoutDirection layoutDirectio \qmlproperty enumeration ListView::effectiveLayoutDirection This property holds the effective layout direction of the horizontal list. - When using the attached property \l {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts, the visual layout direction of the horizontal list will be mirrored. However, the property \l {ListView::layoutDirection}{layoutDirection} will remain unchanged. diff --git a/src/declarative/graphicsitems/qdeclarativepositioners.cpp b/src/declarative/graphicsitems/qdeclarativepositioners.cpp index c2d0aed..84dcec6 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners.cpp +++ b/src/declarative/graphicsitems/qdeclarativepositioners.cpp @@ -619,7 +619,7 @@ void QDeclarativeRow::setLayoutDirection(Qt::LayoutDirection layoutDirection) \qmlproperty enumeration Row::effectiveLayoutDirection This property holds the effective layout direction of the row positioner. - When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts, the visual layout direction of the row positioner will be mirrored. However, the property \l {Row::layoutDirection}{layoutDirection} will remain unchanged. @@ -917,7 +917,7 @@ void QDeclarativeGrid::setLayoutDirection(Qt::LayoutDirection layoutDirection) \qmlproperty enumeration Grid::effectiveLayoutDirection This property holds the effective layout direction of the grid positioner. - When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts, the visual layout direction of the grid positioner will be mirrored. However, the property \l {Grid::layoutDirection}{layoutDirection} will remain unchanged. @@ -1279,7 +1279,7 @@ void QDeclarativeFlow::setLayoutDirection(Qt::LayoutDirection layoutDirection) \qmlproperty enumeration Flow::effectiveLayoutDirection This property holds the effective layout direction of the flow positioner. - When using the attached property {LayoutMirroring::mirror}{LayoutMirroring::mirror} for locale layouts, + When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts, the visual layout direction of the grid positioner will be mirrored. However, the property \l {Flow::layoutDirection}{layoutDirection} will remain unchanged. -- cgit v0.12 From 02cde1e6d991d10acd96492d5c5757cf6717ec98 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 7 Mar 2011 15:32:07 +1000 Subject: PinchArea example produced incorrect scaling. The maths was dodgy - producing far greater scaling than that provided by PinchArea. Change-Id: I4a1ee1b0d65eed623ec9ee92c22c9740116430c5 Task-number: QTBUG-17828 Reviewed-by: Michael Brasser --- .../declarative/touchinteraction/pincharea/flickresize.qml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/declarative/touchinteraction/pincharea/flickresize.qml b/examples/declarative/touchinteraction/pincharea/flickresize.qml index a2f81ff..9439ace 100644 --- a/examples/declarative/touchinteraction/pincharea/flickresize.qml +++ b/examples/declarative/touchinteraction/pincharea/flickresize.qml @@ -54,14 +54,21 @@ Rectangle { PinchArea { width: Math.max(flick.contentWidth, flick.width) height: Math.max(flick.contentHeight, flick.height) + + property real initialWidth + property real initialHeight + onPinchStarted: { + initialWidth = flick.contentWidth + initialHeight = flick.contentHeight + } + onPinchUpdated: { // adjust content pos due to drag flick.contentX += pinch.previousCenter.x - pinch.center.x flick.contentY += pinch.previousCenter.y - pinch.center.y // resize content - var scale = 1.0 + pinch.scale - pinch.previousScale - flick.resizeContent(flick.contentWidth * scale, flick.contentHeight * scale, pinch.center) + flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center) } onPinchFinished: { -- cgit v0.12 From 38a3d51593bb5591c7c4f6d51d7d9fa203b4d56e Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Mon, 7 Mar 2011 17:25:44 +1000 Subject: Include dynamic parenting use cases in layout mirroring autotests Task-number: QTBUG-17280 Reviewed-by: Martin Jones Change-Id: Ibbbd2da44d5826b6e499b731eda66b2016bade85 --- src/declarative/graphicsitems/qdeclarativeitem_p.h | 3 +- .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 33 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h index b204d7f..dae581c 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem_p.h +++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h @@ -145,10 +145,11 @@ public: if (parent) { QDeclarative_setParent_noEvent(q, parent); q->setParentItem(parent); + QDeclarativeItemPrivate *parentPrivate = QDeclarativeItemPrivate::get(parent); + setImplicitLayoutMirror(parentPrivate->inheritedLayoutMirror, parentPrivate->inheritMirrorFromParent); } baselineOffset.invalidate(); mouseSetsFocus = false; - resolveLayoutMirror(); } bool isMirrored() const { diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index 2d14e66..52c9a72 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -518,6 +518,39 @@ void tst_QDeclarativeItem::layoutMirroring() QCOMPARE(childPrivate(rootItem, "inheritedMirror2")->inheritedLayoutMirror, true); QCOMPARE(childPrivate(rootItem, "mirrored1")->inheritedLayoutMirror, true); QCOMPARE(childPrivate(rootItem, "notMirrored1")->inheritedLayoutMirror, true); + + // + // dynamic parenting + // + QDeclarativeItem *parentItem1 = new QDeclarativeItem(); + QDeclarativeItemPrivate::get(parentItem1)->effectiveLayoutMirror = true; // LayoutMirroring.enabled: true + QDeclarativeItemPrivate::get(parentItem1)->isMirrorImplicit = false; + QDeclarativeItemPrivate::get(parentItem1)->inheritMirrorFromItem = true; // LayoutMirroring.childrenInherit: true + QDeclarativeItemPrivate::get(parentItem1)->resolveLayoutMirror(); + + // inherit in constructor + QDeclarativeItem *childItem1 = new QDeclarativeItem(parentItem1); + QCOMPARE(QDeclarativeItemPrivate::get(childItem1)->effectiveLayoutMirror, true); + QCOMPARE(QDeclarativeItemPrivate::get(childItem1)->inheritMirrorFromParent, true); + + // inherit through a parent change + QDeclarativeItem *childItem2 = new QDeclarativeItem(); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->effectiveLayoutMirror, false); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->inheritMirrorFromParent, false); + childItem2->setParentItem(parentItem1); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->effectiveLayoutMirror, true); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->inheritMirrorFromParent, true); + + // stop inherting through a parent change + QDeclarativeItem *parentItem2 = new QDeclarativeItem(); + QDeclarativeItemPrivate::get(parentItem2)->effectiveLayoutMirror = true; // LayoutMirroring.enabled: true + QDeclarativeItemPrivate::get(parentItem2)->resolveLayoutMirror(); + childItem2->setParentItem(parentItem2); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->effectiveLayoutMirror, false); + QCOMPARE(QDeclarativeItemPrivate::get(childItem2)->inheritMirrorFromParent, false); + + delete parentItem1; + delete parentItem2; } void tst_QDeclarativeItem::layoutMirroringIllegalParent() -- cgit v0.12 From 54de06c6f3bd7ef74a10bfab9bff5416078edd6f Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 8 Mar 2011 14:33:29 +1000 Subject: Base empty QML editor horizontal alignment on QApplication::keyboardInputDirection() Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: I240d53c8572fd3d1222b555e93812a3ee38e2558 --- src/declarative/graphicsitems/qdeclarativetext.cpp | 3 ++- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 3 ++- src/declarative/graphicsitems/qdeclarativetextinput.cpp | 5 ++++- .../declarative/qdeclarativetext/tst_qdeclarativetext.cpp | 6 ++++-- .../qdeclarativetextedit/tst_qdeclarativetextedit.cpp | 11 ++++++++--- .../qdeclarativetextinput/tst_qdeclarativetextinput.cpp | 11 ++++++++--- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 4a931fd..f0d0221 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -1120,7 +1120,8 @@ bool QDeclarativeTextPrivate::determineHorizontalAlignment() Q_Q(QDeclarativeText); if (hAlignImplicit && q->isComponentComplete()) { // if no explicit alignment has been set, follow the natural layout direction of the text - return setHAlign(text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft); + bool isRightToLeft = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : text.isRightToLeft(); + return setHAlign(isRightToLeft ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft); } return false; } diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 75a22d1..d1d2351 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -550,7 +550,8 @@ bool QDeclarativeTextEditPrivate::determineHorizontalAlignment() Q_Q(QDeclarativeTextEdit); if (hAlignImplicit && q->isComponentComplete()) { // if no explicit alignment has been set, follow the natural layout direction of the text - return setHAlign(text.isRightToLeft() ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft); + bool isRightToLeft = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : text.isRightToLeft(); + return setHAlign(isRightToLeft ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft); } return false; } diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 6305eaa..850c212 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -411,7 +411,9 @@ bool QDeclarativeTextInputPrivate::determineHorizontalAlignment() { if (hAlignImplicit) { // if no explicit alignment has been set, follow the natural layout direction of the text - return setHAlign(control->text().isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft); + QString text = control->text(); + bool isRightToLeft = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : text.isRightToLeft(); + return setHAlign(isRightToLeft ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft); } return false; } @@ -1872,6 +1874,7 @@ void QDeclarativeTextInputPrivate::init() QPalette p = control->palette(); selectedTextColor = p.color(QPalette::HighlightedText); selectionColor = p.color(QPalette::Highlight); + determineHorizontalAlignment(); } void QDeclarativeTextInput::cursorPosChanged() diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 261dc51..c854f86 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -576,9 +576,11 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); - // empty text is also implicitly left aligned + // empty text with implicit alignment follows the system locale-based + // keyboard input direction from QApplication::keyboardInputDirection text->setText(""); - QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QCOMPARE(text->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeText::AlignLeft : QDeclarativeText::AlignRight); text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index 7a0ca12..4d94dae 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -500,10 +500,15 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); - // empty text is also implicitly left aligned + // empty text with implicit alignment follows the system locale-based + // keyboard input direction from QApplication::keyboardInputDirection textEdit->setText(""); - QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); - QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + QCOMPARE(textEdit->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeTextEdit::AlignLeft : QDeclarativeTextEdit::AlignRight); + if (QApplication::keyboardInputDirection() == Qt::LeftToRight) + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + else + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); textEdit->setHAlign(QDeclarativeTextEdit::AlignRight); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index 5c8b59c..caada13 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -1110,10 +1110,15 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); - // empty text is also implicitly left aligned + // empty text with implicit alignment follows the system locale-based + // keyboard input direction from QApplication::keyboardInputDirection textInput->setText(""); - QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft); - QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); + QCOMPARE(textInput->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeTextInput::AlignLeft : QDeclarativeTextInput::AlignRight); + if (QApplication::keyboardInputDirection() == Qt::LeftToRight) + QVERIFY(-textInputPrivate->hscroll < canvas->width()/2); + else + QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); textInput->setHAlign(QDeclarativeTextInput::AlignRight); QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight); QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); -- cgit v0.12 From 0b2983f9339f1003159f3a746491928f74b593ba Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 8 Mar 2011 15:30:36 +1000 Subject: Fix TextInput key navigation for RTL text Task-number: QTBUG-15882 Reviewed-by: Martin Jones Change-Id: I77c02de3bcd1a1d05dfcdd71327da45182050071 --- .../graphicsitems/qdeclarativetextinput.cpp | 4 +-- .../tst_qdeclarativetextinput.cpp | 40 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 850c212..6c26fd3 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -1068,9 +1068,9 @@ void QDeclarativeTextInput::keyPressEvent(QKeyEvent* ev) // because then moving will do something (deselect). int cursorPosition = d->control->cursor(); if (cursorPosition == 0) - ignore = ev->key() == (d->control->text().mid(cursorPosition,1).isRightToLeft() ? Qt::Key_Right : Qt::Key_Left); + ignore = ev->key() == (d->control->layoutDirection() == Qt::LeftToRight ? Qt::Key_Left : Qt::Key_Right); if (cursorPosition == d->control->text().length()) - ignore = ev->key() == (d->control->text().mid(cursorPosition-1,1).isRightToLeft() ? Qt::Key_Left : Qt::Key_Right); + ignore = ev->key() == (d->control->layoutDirection() == Qt::LeftToRight ? Qt::Key_Right : Qt::Key_Left); } if (ignore) { ev->ignore(); diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index caada13..fc19c94 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -115,6 +115,7 @@ private slots: void cursorVisible(); void cursorRectangle(); void navigation(); + void navigation_RTL(); void copyAndPaste(); void canPasteEmpty(); void canPaste(); @@ -1435,6 +1436,45 @@ void tst_qdeclarativetextinput::navigation() delete canvas; } +void tst_qdeclarativetextinput::navigation_RTL() +{ + QDeclarativeView *canvas = createView(SRCDIR "/data/navigation.qml"); + canvas->show(); + canvas->setFocus(); + + QVERIFY(canvas->rootObject() != 0); + + QDeclarativeTextInput *input = qobject_cast(qvariant_cast(canvas->rootObject()->property("myInput"))); + + QVERIFY(input != 0); + const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647}; + input->setText(QString::fromUtf16(arabic_str, 11)); + + input->setCursorPosition(0); + QTRY_VERIFY(input->hasActiveFocus() == true); + + // move off + simulateKey(canvas, Qt::Key_Right); + QVERIFY(input->hasActiveFocus() == false); + + // move back + simulateKey(canvas, Qt::Key_Left); + QVERIFY(input->hasActiveFocus() == true); + + input->setCursorPosition(input->text().length()); + QVERIFY(input->hasActiveFocus() == true); + + // move off + simulateKey(canvas, Qt::Key_Left); + QVERIFY(input->hasActiveFocus() == false); + + // move back + simulateKey(canvas, Qt::Key_Right); + QVERIFY(input->hasActiveFocus() == true); + + delete canvas; +} + void tst_qdeclarativetextinput::copyAndPaste() { #ifndef QT_NO_CLIPBOARD -- cgit v0.12 From f47f01fd34d08f6152c9053c0d6928fc359aa0f9 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 8 Mar 2011 14:55:56 +1000 Subject: Use the text layout bounds calculated by QLayout. We used to calculate the size and position of the cache image ourselves. Rather use the line positions and bounding rect calculated by QLayout. Change-Id: I601688ab7e310b0015a1994adf52b108f39504d8 Reviewed-by: Joona Petrell --- src/declarative/graphicsitems/qdeclarativetext.cpp | 73 +++++++++------------- .../graphicsitems/qdeclarativetext_p_p.h | 4 +- .../qdeclarativetext/tst_qdeclarativetext.cpp | 6 +- 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index f0d0221..6bcd2fa 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -282,11 +282,11 @@ void QDeclarativeTextPrivate::updateSize() //setup instance of QTextLayout for all cases other than richtext if (!richText) { - size = setupTextLayout(); - if (layedOutTextSize != size) { + QRect textRect = setupTextLayout(); + if (layedOutTextRect.size() != textRect.size()) q->prepareGeometryChange(); - layedOutTextSize = size; - } + layedOutTextRect = textRect; + size = textRect.size(); dy -= size.height(); } else { singleline = false; // richtext can't elide or be optimized for single-line case @@ -306,9 +306,9 @@ void QDeclarativeTextPrivate::updateSize() doc->setTextWidth(doc->idealWidth()); // ### Text does not align if width is not set (QTextDoc bug) dy -= (int)doc->size().height(); QSize dsize = doc->size().toSize(); - if (dsize != layedOutTextSize) { + if (dsize != layedOutTextRect.size()) { q->prepareGeometryChange(); - layedOutTextSize = dsize; + layedOutTextRect = QRect(QPoint(0,0), dsize); } size = QSize(int(doc->idealWidth()),dsize.height()); } @@ -343,14 +343,13 @@ void QDeclarativeTextPrivate::updateSize() Returns the size of the final text. This can be used to position the text vertically (the text is already absolutely positioned horizontally). */ -QSize QDeclarativeTextPrivate::setupTextLayout() +QRect QDeclarativeTextPrivate::setupTextLayout() { // ### text layout handling should be profiled and optimized as needed // what about QStackTextEngine engine(tmp, d->font.font()); QTextLayout textLayout(&engine); Q_Q(QDeclarativeText); layout.setCacheEnabled(true); - qreal widthUsed = 0; qreal lineWidth = 0; int visibleCount = 0; @@ -378,11 +377,12 @@ QSize QDeclarativeTextPrivate::setupTextLayout() break; } layout.endLayout(); - naturalWidth = 0; + QRectF br; for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); - naturalWidth = qMax(naturalWidth, line.naturalTextWidth()); + br = br.united(line.naturalTextRect()); } + naturalWidth = br.width(); } if (maximumLineCountValid) { @@ -440,19 +440,21 @@ QSize QDeclarativeTextPrivate::setupTextLayout() } qreal height = 0; + QRectF br; for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); - // calc width - widthUsed = qMax(widthUsed, line.naturalTextRect().right()); // set line spacing line.setPosition(QPointF(line.position().x(), height)); - if (elideText && i == layout.lineCount()-1) + if (elideText && i == layout.lineCount()-1) { elidePos.setY(height + fm.ascent()); + br = br.united(QRectF(elidePos, QSizeF(fm.width(elideChar), fm.ascent()))); + } + br = br.united(line.naturalTextRect()); height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight; } if (!q->widthValid()) - naturalWidth = widthUsed; + naturalWidth = br.width(); //Update the number of visible lines if (lineCount != visibleCount) { @@ -460,7 +462,7 @@ QSize QDeclarativeTextPrivate::setupTextLayout() emit q->lineCountChanged(); } - return QSize(qCeil(widthUsed), qCeil(height)); + return QRect(qRound(br.x()), qRound(br.y()), qCeil(br.width()), qCeil(br.height())); } /*! @@ -470,7 +472,7 @@ QSize QDeclarativeTextPrivate::setupTextLayout() QPixmap QDeclarativeTextPrivate::textLayoutImage(bool drawStyle) { //do layout - QSize size = layedOutTextSize; + QSize size = layedOutTextRect.size(); //paint text QPixmap img(size); if (!size.isEmpty()) { @@ -483,7 +485,7 @@ QPixmap QDeclarativeTextPrivate::textLayoutImage(bool drawStyle) #ifdef Q_WS_MAC qt_applefontsmoothing_enabled = oldSmooth; #endif - drawTextLayout(&p, QPointF(0,0), drawStyle); + drawTextLayout(&p, QPointF(-layedOutTextRect.x(),0), drawStyle); } return img; } @@ -501,7 +503,7 @@ void QDeclarativeTextPrivate::drawTextLayout(QPainter *painter, const QPointF &p painter->setFont(font); layout.draw(painter, pos); if (!elidePos.isNull()) - painter->drawText(elidePos, elideChar); + painter->drawText(pos + elidePos, elideChar); } /*! @@ -1388,44 +1390,25 @@ QRectF QDeclarativeText::boundingRect() const { Q_D(const QDeclarativeText); - int w = width(); - int h = height(); - - int x = 0; - int y = 0; - - QSize size = d->layedOutTextSize; + QRect rect = d->layedOutTextRect; if (d->style != Normal) - size += QSize(2,2); + rect.adjust(-1, 0, 1, 2); // Could include font max left/right bearings to either side of rectangle. - switch (d->hAlign) { - case AlignLeft: - case AlignJustify: - x = 0; - break; - case AlignRight: - x = w - size.width(); - break; - case AlignHCenter: - x = (w - size.width()) / 2; - break; - } - + int h = height(); switch (d->vAlign) { case AlignTop: - y = 0; break; case AlignBottom: - y = h - size.height(); + rect.setY(h - rect.height()); break; case AlignVCenter: - y = (h - size.height()) / 2; + rect.setY((h - rect.height()) / 2); break; } - return QRectF(x,y,size.width(),size.height()); + return QRectF(rect); } /*! \internal */ @@ -1571,8 +1554,8 @@ void QDeclarativeText::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWid } else { QRectF bounds = boundingRect(); - bool needClip = clip() && (d->layedOutTextSize.width() > width() || - d->layedOutTextSize.height() > height()); + bool needClip = clip() && (d->layedOutTextRect.width() > width() || + d->layedOutTextRect.height() > height()); if (needClip) { p->save(); diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index 0864c8f..2e154a0 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -115,7 +115,7 @@ public: bool requireImplicitWidth:1; bool hAlignImplicit:1; - QSize layedOutTextSize; + QRect layedOutTextRect; QSize paintedSize; qreal naturalWidth; virtual qreal implicitWidth() const; @@ -123,7 +123,7 @@ public: QPixmap textDocumentImage(bool drawStyle); QTextDocumentWithImageResources *doc; - QSize setupTextLayout(); + QRect setupTextLayout(); QPixmap textLayoutImage(bool drawStyle); void drawTextLayout(QPainter *p, const QPointF &pos, bool drawStyle); QDeclarativeTextLayout layout; diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index c854f86..9c12a90 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -1156,12 +1156,14 @@ void tst_qdeclarativetext::lineHeight() QVERIFY(myText->lineHeightMode() == QDeclarativeText::ProportionalHeight); qreal h = myText->height(); + int lc = myText->lineCount(); + qreal lh = myText->height() / lc; myText->setLineHeight(1.5); - QVERIFY(myText->height() == h * 1.5); + QVERIFY(qAbs(myText->height() - (((lc-1) * lh * 1.5) + lh)) < 1.0); myText->setLineHeightMode(QDeclarativeText::FixedHeight); myText->setLineHeight(20); - QCOMPARE(myText->height(), myText->lineCount() * 20.0); + QCOMPARE(myText->height(), ((lc-1) * 20.0) + lh); myText->setText("Lorem ipsum sit amet, consectetur adipiscing elit. Integer felis nisl, varius in pretium nec, venenatis non erat. Proin lobortis interdum dictum."); myText->setLineHeightMode(QDeclarativeText::ProportionalHeight); -- cgit v0.12 From 0d6ce63ea84b076efbebfae0f6f39f492d8d7bcf Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 9 Mar 2011 11:52:41 +1000 Subject: Revert to previous lineHeight behavior. f47f01fd34d08f6152c9053c0d6928fc359aa0f9 changed the height of the Text to not include the extra spacing at the end of the block. This is different to what QDextDocument does. Revert back to the old behavior. Change-Id: Ie43dea4dd5a0e9ba179f44c246fb834322db11e3 Reviewed-by: Joona Petrell --- src/declarative/graphicsitems/qdeclarativetext.cpp | 1 + tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 6bcd2fa..3b5be9e 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -452,6 +452,7 @@ QRect QDeclarativeTextPrivate::setupTextLayout() br = br.united(line.naturalTextRect()); height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight; } + br.setHeight(height); if (!q->widthValid()) naturalWidth = br.width(); diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index 9c12a90..c854f86 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -1156,14 +1156,12 @@ void tst_qdeclarativetext::lineHeight() QVERIFY(myText->lineHeightMode() == QDeclarativeText::ProportionalHeight); qreal h = myText->height(); - int lc = myText->lineCount(); - qreal lh = myText->height() / lc; myText->setLineHeight(1.5); - QVERIFY(qAbs(myText->height() - (((lc-1) * lh * 1.5) + lh)) < 1.0); + QVERIFY(myText->height() == h * 1.5); myText->setLineHeightMode(QDeclarativeText::FixedHeight); myText->setLineHeight(20); - QCOMPARE(myText->height(), ((lc-1) * 20.0) + lh); + QCOMPARE(myText->height(), myText->lineCount() * 20.0); myText->setText("Lorem ipsum sit amet, consectetur adipiscing elit. Integer felis nisl, varius in pretium nec, venenatis non erat. Proin lobortis interdum dictum."); myText->setLineHeightMode(QDeclarativeText::ProportionalHeight); -- cgit v0.12 From aeb330e3999ef3d7ae8d94b9330471f2a2a13554 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Tue, 8 Mar 2011 18:05:25 +1000 Subject: Fix horizontal alignment of QTextDocument-based RTL text Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: If537d7c795dec46eedee62511e75bab862676ef1 --- src/declarative/graphicsitems/qdeclarativetext.cpp | 45 +++++++++++++++------- .../graphicsitems/qdeclarativetext_p_p.h | 2 + .../graphicsitems/qdeclarativetextedit.cpp | 7 ++-- .../qdeclarativetext/tst_qdeclarativetext.cpp | 40 +++++++++++++++++-- .../tst_qdeclarativetextedit.cpp | 34 +++++++++++++++- .../tst_qdeclarativetextinput.cpp | 9 +++++ 6 files changed, 115 insertions(+), 22 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 3b5be9e..fdc1a71 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #include @@ -102,7 +101,7 @@ QDeclarativeTextPrivate::QDeclarativeTextPrivate() format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), lineHeight(1), lineHeightMode(QDeclarativeText::ProportionalHeight), lineCount(1), truncated(false), maximumLineCount(INT_MAX), maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false), - cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), naturalWidth(0), doc(0) + cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), rightToLeftText(false), naturalWidth(0), doc(0) { cacheAllTextAsImage = enableImageCache(); QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton; @@ -292,8 +291,16 @@ void QDeclarativeTextPrivate::updateSize() singleline = false; // richtext can't elide or be optimized for single-line case ensureDoc(); doc->setDefaultFont(font); + + QDeclarativeText::HAlignment horizontalAlignment = q->effectiveHAlign(); + if (rightToLeftText) { + if (horizontalAlignment == QDeclarativeText::AlignLeft) + horizontalAlignment = QDeclarativeText::AlignRight; + else if (horizontalAlignment == QDeclarativeText::AlignRight) + horizontalAlignment = QDeclarativeText::AlignLeft; + } QTextOption option; - option.setAlignment((Qt::Alignment)int(q->effectiveHAlign() | vAlign)); + option.setAlignment((Qt::Alignment)int(horizontalAlignment | vAlign)); option.setWrapMode(QTextOption::WrapMode(wrapMode)); doc->setDefaultTextOption(option); if (requireImplicitWidth && q->widthValid()) { @@ -909,15 +916,18 @@ void QDeclarativeText::setText(const QString &n) return; d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(n)); - if (d->richText && isComponentComplete()) { - d->ensureDoc(); - d->doc->setText(n); - } - d->text = n; - d->determineHorizontalAlignment(); + if (isComponentComplete()) { + if (d->richText) { + d->ensureDoc(); + d->doc->setText(n); + d->rightToLeftText = d->doc->toPlainText().isRightToLeft(); + } else { + d->rightToLeftText = d->text.isRightToLeft(); + } + d->determineHorizontalAlignment(); + } d->updateLayout(); - emit textChanged(d->text); } @@ -1122,9 +1132,8 @@ bool QDeclarativeTextPrivate::determineHorizontalAlignment() { Q_Q(QDeclarativeText); if (hAlignImplicit && q->isComponentComplete()) { - // if no explicit alignment has been set, follow the natural layout direction of the text - bool isRightToLeft = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : text.isRightToLeft(); - return setHAlign(isRightToLeft ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft); + bool alignToRight = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : rightToLeftText; + return setHAlign(alignToRight ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft); } return false; } @@ -1140,6 +1149,11 @@ void QDeclarativeTextPrivate::mirrorChange() } } +QTextDocument *QDeclarativeTextPrivate::textDocument() +{ + return doc; +} + QDeclarativeText::VAlignment QDeclarativeText::vAlign() const { Q_D(const QDeclarativeText); @@ -1585,11 +1599,14 @@ void QDeclarativeText::componentComplete() QDeclarativeItem::componentComplete(); if (d->updateOnComponentComplete) { d->updateOnComponentComplete = false; - d->determineHorizontalAlignment(); if (d->richText) { d->ensureDoc(); d->doc->setText(d->text); + d->rightToLeftText = d->doc->toPlainText().isRightToLeft(); + } else { + d->rightToLeftText = d->text.isRightToLeft(); } + d->determineHorizontalAlignment(); d->updateLayout(); } } diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index 2e154a0..e3ab62a 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -79,6 +79,7 @@ public: bool determineHorizontalAlignment(); bool setHAlign(QDeclarativeText::HAlignment, bool forceAlign = false); void mirrorChange(); + QTextDocument *textDocument(); QString text; QFont font; @@ -114,6 +115,7 @@ public: bool internalWidthUpdate:1; bool requireImplicitWidth:1; bool hAlignImplicit:1; + bool rightToLeftText:1; QRect layedOutTextRect; QSize paintedSize; diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index d1d2351..4006d54 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -549,9 +549,8 @@ bool QDeclarativeTextEditPrivate::determineHorizontalAlignment() { Q_Q(QDeclarativeTextEdit); if (hAlignImplicit && q->isComponentComplete()) { - // if no explicit alignment has been set, follow the natural layout direction of the text - bool isRightToLeft = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : text.isRightToLeft(); - return setHAlign(isRightToLeft ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft); + bool alignToRight = text.isEmpty() ? QApplication::keyboardInputDirection() == Qt::RightToLeft : rightToLeftText; + return setHAlign(alignToRight ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft); } return false; } @@ -1578,7 +1577,7 @@ void QDeclarativeTextEdit::q_textChanged() { Q_D(QDeclarativeTextEdit); d->text = text(); - d->rightToLeftText = d->text.isRightToLeft(); + d->rightToLeftText = d->document->begin().layout()->engine()->isRightToLeft(); d->determineHorizontalAlignment(); d->updateDefaultTextOption(); updateSize(); diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index c854f86..b5dfba8 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -523,18 +523,44 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); - // explicitly left aligned + // explicitly left aligned text text->setHAlign(QDeclarativeText::AlignLeft); QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2); - // explicitly right aligned + // explicitly right aligned text text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); QCOMPARE(text->effectiveHAlign(), text->hAlign()); QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2); + // change to rich text + QString textString = text->text(); + text->setText(QString("") + textString + QString("")); + text->setTextFormat(QDeclarativeText::RichText); + text->resetHAlign(); + + // implicitly aligned rich text should follow the reading direction of text + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); + QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft); + + // explicitly left aligned rich text + text->setHAlign(QDeclarativeText::AlignLeft); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); + QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight); + + // explicitly right aligned rich text + text->setHAlign(QDeclarativeText::AlignRight); + QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); + QCOMPARE(text->effectiveHAlign(), text->hAlign()); + QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft); + + text->setText(textString); + text->setTextFormat(QDeclarativeText::PlainText); + // explicitly center aligned text->setHAlign(QDeclarativeText::AlignHCenter); QCOMPARE(text->hAlign(), QDeclarativeText::AlignHCenter); @@ -583,8 +609,16 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft() QDeclarativeText::AlignLeft : QDeclarativeText::AlignRight); text->setHAlign(QDeclarativeText::AlignRight); QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight); - delete canvas; + + // alignment of Text with no text set to it + QString componentStr = "import QtQuick 1.0\nText {}"; + QDeclarativeComponent textComponent(&engine); + textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QDeclarativeText *textObject = qobject_cast(textComponent.create()); + QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeText::AlignLeft : QDeclarativeText::AlignRight); + delete textObject; } void tst_qdeclarativetext::verticalAlignment() diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp index db36220..402c6cd 100644 --- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp +++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp @@ -448,7 +448,7 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QVERIFY(textEdit != 0); canvas->show(); - // implicit alignment should follow the reading direction of RTL text + // implicit alignment should follow the reading direction of text QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); @@ -462,6 +462,29 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + QString textString = textEdit->text(); + textEdit->setText(QString("") + textString + QString("")); + textEdit->resetHAlign(); + + // implicitly aligned rich text should follow the reading direction of RTL text + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign()); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + + // explicitly left aligned rich text + textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft); + QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign()); + QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2); + + // explicitly right aligned rich text + textEdit->setHAlign(QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight); + QCOMPARE(textEdit->effectiveHAlign(), textEdit->hAlign()); + QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); + + textEdit->setText(textString); + // explicitly center aligned textEdit->setHAlign(QDeclarativeTextEdit::AlignHCenter); QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignHCenter); @@ -515,6 +538,15 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft() QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2); delete canvas; + + // alignment of TextEdit with no text set to it + QString componentStr = "import QtQuick 1.0\nTextEdit {}"; + QDeclarativeComponent textComponent(&engine); + textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QDeclarativeTextEdit *textObject = qobject_cast(textComponent.create()); + QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeTextEdit::AlignLeft : QDeclarativeTextEdit::AlignRight); + delete textObject; } void tst_qdeclarativetextedit::vAlign() diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index fc19c94..734f91f 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -1125,6 +1125,15 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft() QVERIFY(-textInputPrivate->hscroll > canvas->width()/2); delete canvas; + + // alignment of TextInput with no text set to it + QString componentStr = "import QtQuick 1.0\nTextInput {}"; + QDeclarativeComponent textComponent(&engine); + textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QDeclarativeTextInput *textObject = qobject_cast(textComponent.create()); + QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ? + QDeclarativeTextInput::AlignLeft : QDeclarativeTextInput::AlignRight); + delete textObject; } void tst_qdeclarativetextinput::positionAt() -- cgit v0.12 From c7e7856f710fe09b1fd715390e74578166c0f709 Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Wed, 9 Mar 2011 17:48:12 +1000 Subject: Add missing header include missing from the previous commit Change-Id: I7993c13c7fe43027caaa28ef3866df5f0d21f5b9 --- src/declarative/graphicsitems/qdeclarativetextedit.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 4006d54..babc020 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -48,6 +48,7 @@ #include +#include #include #include #include -- cgit v0.12