diff options
author | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-05-22 15:33:42 +0200 |
---|---|---|
committer | Marius Bugge Monsen <mmonsen@trolltech.com> | 2009-05-22 15:33:42 +0200 |
commit | 1caabfe8a8d8c4c80e8795806585fe40e335715f (patch) | |
tree | e50f64c9f54be1cdf4637d0b6594000c57a37277 | |
parent | d945b1a31672bb5ab7ed8f4b8d215fe92cbbc7b7 (diff) |
Refactor the doLayout() function; unify vertical and horizontal layout.
-rw-r--r-- | src/qgraphicslistview.cpp | 181 |
1 files changed, 72 insertions, 109 deletions
diff --git a/src/qgraphicslistview.cpp b/src/qgraphicslistview.cpp index 3988bc8..f57956b 100644 --- a/src/qgraphicslistview.cpp +++ b/src/qgraphicslistview.cpp @@ -659,127 +659,90 @@ void QtGraphicsListView::doLayout() const QRectF area = boundingRect(); const QSizeF constraint = area.size(); + const int count = d->model ? d->model->count() : 0; + const bool vertical = (d->orientation == Qt::Vertical); + const qreal areaStart = (vertical ? area.y() : area.x()); + const qreal areaEnd = (vertical ? area.bottom() : area.right()); + const qreal offset = (vertical ? d->verticalOffset : d->horizontalOffset); + const qreal perpendicularOffset = (vertical ? d->horizontalOffset : d->verticalOffset); + + qreal coordinate = -offset; int index = d->firstIndex; - int count = d->model ? d->model->count() : 0; - qreal x = -d->horizontalOffset; - qreal y = -d->verticalOffset; // find the visible items; caching helps us skip this most of the time - if (d->orientation == Qt::Vertical) { - if (y <= 0 && !d->items.isEmpty()) { // ### - // optimization: use the cached index and offset as starting points - // The cached values are offsets from the start of the _contents_ - // and the to index found at that offset. + if (coordinate <= 0 && !d->items.isEmpty()) { + // optimization: use the cached index and offset as starting points + // The cached values are offsets from the start of the _contents_ + // and the to index found at that offset. #if 0 // enable or disable the caching - index += d->cachedIndexOffset; - y += d->cachedCoordinateOffset; + index += d->cachedIndexOffset; + coordinate += d->cachedCoordinateOffset; #endif - // the visible area starts at y == 0 - if (y < 0) { // the cached offset was above the visible area - while (index < count) { - initStyleOption(&option, index); - const qreal height = d->itemSizeHint(&option, index, constraint).height(); - if (y + height > area.y()) - break; - y += height; - ++index; - } - } else if (y > 0) { // the cached offset was below - while (index >= 0 && y > 0) { - initStyleOption(&option, index); - const qreal height = d->itemSizeHint(&option, index, constraint).height(); - y -= height; - --index; - } + // the visible area starts at coordinate == 0 + if (coordinate < 0) { // the cached offset was above or to the left of the visible area + while (index < count) { + initStyleOption(&option, index); + const QSizeF hint = d->itemSizeHint(&option, index, constraint); + const qreal size = vertical ? hint.height() : hint.width(); + if (coordinate + size > areaStart) + break; + coordinate += size; + ++index; } -#if 0 // enable or disable the caching - d->cachedIndexOffset = index - d->firstIndex; - d->cachedCoordinateOffset = y + d->verticalOffset; -#endif - } - // we are now at the visible items - const int firstVisibleIndex = index; - d->scrollItems(firstVisibleIndex); - // set existing item positions - for (int i = 0; i < d->items.count() && index < count && y < area.bottom(); ++i) { - QtGraphicsListViewItem *item = d->items.at(i); - const QSizeF size = item->size(); - item->setPos(d->horizontalOffset, y); - y += size.height(); - ++index; - } - // add more items and set geometry - while (index < count && y < area.bottom()) { - QtGraphicsListViewItem *item = d->creator->create(index, this); - d->items.append(item); - initStyleOption(&option, index); - const QSizeF size = item->sizeHint(index, &option, Qt::PreferredSize, constraint); - item->setGeometry(d->horizontalOffset, y, area.width(), size.height()); - y += size.height(); - ++index; - } - // remove unused items - const int visibleCount = index - firstVisibleIndex; - while (visibleCount < d->items.count()) - d->creator->destroy(d->items.takeLast()); - } else { // Horizontal - if (x <= 0 && !d->items.isEmpty()) { - // optimization: use the cached index and offset as starting points -#if 0 // enable or disable the caching - index += d->cachedIndexOffset; - x += d->cachedCoordinateOffset; -#endif - // the visible area starts at x == 0 - if (x < 0) { // the cached offset was left of the visible area - while (index < count) { - initStyleOption(&option, index); - const qreal width = d->itemSizeHint(&option, index, constraint).width(); - if (x + width > area.x()) - break; - x += width; - ++index; - } - } else if (x > 0) { // the cached offset was to the right - while (index >= 0 && x > 0) { - initStyleOption(&option, index); - const qreal width = d->itemSizeHint(&option, index, constraint).width(); - x -= width; - --index; - } + } else if (coordinate > 0) { // the cached offset was below or to the right + while (index >= 0 && coordinate > 0) { + initStyleOption(&option, index); + const QSizeF hint = d->itemSizeHint(&option, index, constraint); + const qreal size = vertical ? hint.height() : hint.width(); + coordinate -= size; + --index; } + } #if 0 // enable or disable the caching - d->cachedIndexOffset = index - d->firstIndex; - d->cachedCoordinateOffset = x + d->horizontalOffset; + d->cachedIndexOffset = index - d->firstIndex; + d->cachedCoordinateOffset = coordinate + offset; #endif + } + + // shuffle the existing items around + const int firstVisibleIndex = index; + d->scrollItems(firstVisibleIndex); + + // set existing item positions + for (int i = 0; i < d->items.count() && index < count && coordinate < areaEnd; ++i) { + QtGraphicsListViewItem *item = d->items.at(i); + const QSizeF size = item->size(); + if (vertical) { + item->setPos(perpendicularOffset, coordinate); + coordinate += size.height(); + } else { + item->setPos(coordinate, perpendicularOffset); + coordinate += size.width(); } - // we are now at the visible items - const int firstVisibleIndex = index; - d->scrollItems(firstVisibleIndex); - // set existing item positions - for (int i = 0; i < d->items.count() && index < count && x < area.right(); ++i) { - QtGraphicsListViewItem *item = d->items.at(i); - const QSizeF size = item->size(); - item->setPos(x, d->verticalOffset); - x += size.width(); - ++index; - } - // add more items and set geometry - while (index < count && x < area.right()) { - QtGraphicsListViewItem *item = d->creator->create(index, this); - d->items.append(item); - initStyleOption(&option, index); - const QSizeF size = item->sizeHint(index, &option, Qt::PreferredSize, constraint); - item->setGeometry(x, d->verticalOffset, size.width(), area.height()); - x += size.width(); - ++index; + ++index; + } + + // add more items and set geometry + while (index < count && coordinate < areaEnd) { + QtGraphicsListViewItem *item = d->creator->create(index, this); + d->items.append(item); + initStyleOption(&option, index); + const QSizeF size = item->sizeHint(index, &option, Qt::PreferredSize, constraint); + if (vertical) { + item->setGeometry(perpendicularOffset, coordinate, area.width(), size.height()); + coordinate += size.height(); + } else { + item->setGeometry(coordinate, perpendicularOffset, size.width(), area.height()); + coordinate += size.width(); } - // remove unused items - const int visibleCount = index - firstVisibleIndex; - while (visibleCount < d->items.count()) - d->creator->destroy(d->items.takeLast()); + ++index; } - // done - emit layoutChanged(); + + // remove unused items + const int visibleCount = index - firstVisibleIndex; + while (visibleCount < d->items.count()) + d->creator->destroy(d->items.takeLast()); + } /*! |