diff options
author | Thorbjørn Martsum <tmartsum@gmail.com> | 2013-04-13 09:25:09 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-05-09 06:45:11 +0200 |
commit | 7e6fbb9af15066a098b22017652d8256c8245b8b (patch) | |
tree | d22e8d1b139e358748a59714582fb57bb4b0b993 /src | |
parent | 03c761287f1a007c7177d90ed81341d0147eae05 (diff) |
resizeToContents - QTreeView - improve sizeHintForColumn
QTreeView actually had a better model than QTableView, but after
previous patches QTableView now has a better one.
This patch makes sizeHintForColumn similar to what QTableView has.
Change-Id: I2f2d35e7aa66fc8990f54e2f4a12d97f490840e5
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/widgets/itemviews/qtreeview.cpp | 65 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreeview_p.h | 1 |
2 files changed, 58 insertions, 8 deletions
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index c4f2236b93..a47432334a 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -2815,20 +2815,54 @@ int QTreeView::sizeHintForColumn(int column) const QStyleOptionViewItem option = d->viewOptions(); const QVector<QTreeViewItem> viewItems = d->viewItems; - int start = 0; - int end = viewItems.count(); - if(end > 1000) { //if we have too many item this function would be too slow. - //we get a good approximation by only iterate over 1000 items. - start = qMax(0, d->firstVisibleItem() - 100); - end = qMin(end, start + 900); - } + const int maximumProcessRows = 1000; // To avoid this to take forever. + + int offset = 0; + int start = d->firstVisibleItem(&offset); + int end = d->lastVisibleItem(start, offset); + + int rowsProcessed = 0; - for (int i = start; i < end; ++i) { + for (int i = start; i <= end; ++i) { if (viewItems.at(i).spanning) continue; // we have no good size hint QModelIndex index = viewItems.at(i).index; index = index.sibling(index.row(), column); w = d->widthHintForIndex(index, w, option, i); + ++rowsProcessed; + if (rowsProcessed == maximumProcessRows) + break; + } + + --end; + int actualBottom = viewItems.size() - 1; + while (rowsProcessed != maximumProcessRows && (start > 0 || end < actualBottom)) { + int idx = -1; + + if ((rowsProcessed % 2 && start > 0) || end == actualBottom) { + while (start > 0) { + --start; + if (viewItems.at(start).spanning) + continue; + idx = start; + break; + } + } else { + while (end < actualBottom) { + ++end; + if (viewItems.at(end).spanning) + continue; + idx = end; + break; + } + } + if (idx < 0) + continue; + + QModelIndex index = viewItems.at(idx).index; + index = index.sibling(index.row(), column); + w = d->widthHintForIndex(index, w, option, idx); + ++rowsProcessed; } return w; } @@ -3527,6 +3561,21 @@ int QTreeViewPrivate::firstVisibleItem(int *offset) const return -1; } +int QTreeViewPrivate::lastVisibleItem(int firstVisual, int offset) const +{ + if (firstVisual < 0 || offset < 0) + firstVisual = firstVisibleItem(&offset); + int y = - offset; + int value = viewport->height(); + + for (int i = firstVisual; i < viewItems.count(); ++i) { + y += itemHeight(i); // the height value is cached + if (y > value) + return i; + } + return viewItems.size() - 1; +} + int QTreeViewPrivate::columnAt(int x) const { return header->logicalIndexAt(x); diff --git a/src/widgets/itemviews/qtreeview_p.h b/src/widgets/itemviews/qtreeview_p.h index 4212e5bce0..8be9c568d3 100644 --- a/src/widgets/itemviews/qtreeview_p.h +++ b/src/widgets/itemviews/qtreeview_p.h @@ -148,6 +148,7 @@ public: #endif int firstVisibleItem(int *offset = 0) const; + int lastVisibleItem(int firstVisual = -1, int offset = -1) const; int columnAt(int x) const; bool hasVisibleChildren( const QModelIndex& parent) const; |