summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativegridview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativegridview.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativegridview.cpp605
1 files changed, 470 insertions, 135 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp
index 694130b..5c2f781 100644
--- a/src/declarative/graphicsitems/qdeclarativegridview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp
@@ -68,21 +68,62 @@ 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->effectiveLayoutDirection() == 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->effectiveLayoutDirection() == 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();
+ }
+
+ 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->effectiveLayoutDirection() == Qt::RightToLeft)
+ return -item->x() - 1;
+ else
+ return item->x() + view->cellWidth() - 1;
+ }
}
void setPosition(qreal col, qreal row) {
- if (view->flow() == QDeclarativeGridView::LeftToRight) {
- item->setPos(QPointF(col, row));
+ 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));
+ } else {
+ item->setPos(QPointF(-view->cellWidth()-row, col));
+ }
} else {
- item->setPos(QPointF(row, col));
+ if (view->flow() == QDeclarativeGridView::LeftToRight)
+ item->setPos(QPointF(col, row));
+ else
+ item->setPos(QPointF(row, col));
}
+
}
- bool contains(int x, int y) const {
+ bool contains(qreal x, qreal y) const {
return (x >= item->x() && x < item->x() + view->cellWidth() &&
y >= item->y() && y < item->y() + view->cellHeight());
}
@@ -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,71 @@ public:
return 0;
}
+ bool isRightToLeftTopToBottom() const {
+ 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 {
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 (q->effectiveLayoutDirection() == 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 +306,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 +316,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 +347,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 +446,7 @@ public:
QList<FxGridItem*> visibleItems;
QHash<QDeclarativeItem*,int> unrequestedItems;
FxGridItem *currentItem;
+ Qt::LayoutDirection layoutDirection;
QDeclarativeGridView::Flow flow;
int visibleIndex;
int currentIndex;
@@ -373,6 +457,8 @@ public:
int itemCount;
qreal highlightRangeStart;
qreal highlightRangeEnd;
+ bool highlightRangeStartValid;
+ bool highlightRangeEndValid;
QDeclarativeGridView::HighlightRangeMode highlightRange;
QDeclarativeComponent *highlightComponent;
FxGridItem *highlight;
@@ -554,7 +640,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 +682,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);
}
}
@@ -669,10 +757,14 @@ void QDeclarativeGridViewPrivate::updateUnrequestedPositions()
{
QHash<QDeclarativeItem*,int>::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), colPosAt(*it)));
}
}
}
@@ -837,23 +929,30 @@ void QDeclarativeGridViewPrivate::updateFooter()
}
}
if (footer) {
+ qreal colOffset = 0;
+ qreal rowOffset;
+ if (isRightToLeftTopToBottom()) {
+ rowOffset = footer->item->width()-cellWidth;
+ } else {
+ rowOffset = 0;
+ if (q->effectiveLayoutDirection() == 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 +982,27 @@ void QDeclarativeGridViewPrivate::updateHeader()
}
}
if (header) {
+ qreal colOffset = 0;
+ qreal rowOffset;
+ if (isRightToLeftTopToBottom()) {
+ rowOffset = -cellWidth;
+ } else {
+ rowOffset = -headerSize();
+ if (q->effectiveLayoutDirection() == 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);
}
}
}
@@ -912,76 +1022,107 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m
|| (flow == QDeclarativeGridView::LeftToRight && &data == &hData))
return;
- int oldDuration = fixupDuration;
- fixupDuration = moveReason == Mouse ? fixupDuration : 0;
+ fixupMode = moveReason == Mouse ? fixupMode : Immediate;
+
+ 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 = 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 {
+ 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;
return;
}
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);
- if (fixupDuration)
+ if (fixupMode != Immediate) {
timeline.move(data.move, -pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2);
- else
+ data.fixingUp = true;
+ } else {
timeline.set(data.move, -pos);
+ }
vTime = timeline.time();
}
} else if (haveHighlightRange && highlightRange == QDeclarativeGridView::StrictlyEnforceRange) {
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)
+ if (fixupMode != Immediate) {
timeline.move(data.move, -viewPos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2);
- else
+ data.fixingUp = true;
+ } else {
timeline.set(data.move, -viewPos);
+ }
}
vTime = timeline.time();
}
} else {
QDeclarativeFlickablePrivate::fixup(data, minExtent, maxExtent);
}
- fixupDuration = oldDuration;
+ fixupMode = Normal;
}
void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity)
{
Q_Q(QDeclarativeGridView);
+ data.fixingUp = false;
moveReason = Mouse;
if ((!haveHighlightRange || highlightRange != QDeclarativeGridView::StrictlyEnforceRange)
&& snapMode == QDeclarativeGridView::NoSnap) {
@@ -989,12 +1130,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 +1147,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 +1156,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 +1178,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 +1372,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 +1704,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 +1712,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 +1731,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 +1739,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 +1765,60 @@ 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->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::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.
+
+ \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
@@ -1620,10 +1851,7 @@ void QDeclarativeGridView::setFlow(Flow flow)
}
setContentX(0);
setContentY(0);
- d->clear();
- d->updateGrid();
- refill();
- d->updateCurrent(d->currentIndex);
+ d->regenerate();
emit flowChanged();
}
}
@@ -1890,11 +2118,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 +2196,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;
}
@@ -1971,17 +2227,38 @@ qreal QDeclarativeGridView::maxXExtent() const
if (d->flow == QDeclarativeGridView::LeftToRight)
return QDeclarativeFlickable::maxXExtent();
qreal extent;
+ 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;
+ if (d->model && d->model->count())
+ lastItemPosition = d->rowPosAt(d->model->count()-1);
+ }
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));
+ } else 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 +2371,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 (effectiveLayoutDirection() == 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 +2414,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 (effectiveLayoutDirection() == 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 +2454,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<FxGridItem*> 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 +2512,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();
@@ -2250,6 +2570,7 @@ void QDeclarativeGridView::positionViewAtIndex(int index, int mode)
/*!
\qmlmethod GridView::positionViewAtBeginning()
\qmlmethod GridView::positionViewAtEnd()
+ \since Quick 1.1
Positions the view at the beginning or end, taking into account any header or footer.
@@ -2294,7 +2615,7 @@ void QDeclarativeGridView::positionViewAtEnd()
\bold Note: methods should only be called after the Component has completed.
*/
-int QDeclarativeGridView::indexAt(int x, int y) const
+int QDeclarativeGridView::indexAt(qreal x, qreal y) const
{
Q_D(const QDeclarativeGridView);
for (int i = 0; i < d->visibleItems.count(); ++i) {
@@ -2337,32 +2658,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 +2762,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->width()+1 : d->position();
+ int to = d->buffer+tempPos+d->size()-1;
int colPos = 0;
int rowPos = 0;
if (d->visibleItems.count()) {
@@ -2448,10 +2781,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.
@@ -2508,6 +2838,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
@@ -2756,7 +3088,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);
}