summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhjk <hjk@qt.io>2023-02-24 12:49:39 +0100
committerhjk <hjk@qt.io>2023-02-24 15:07:23 +0000
commitf2f5e4d030cf7da58a308363093ed4fda60017ff (patch)
tree8c350e52d9ab1b0ed0ef8f3e1769663125f274d3 /src
parentf53f006301c1a4e241bfb9867615f087adc3b32e (diff)
downloadqt-creator-f2f5e4d030cf7da58a308363093ed4fda60017ff.tar.gz
Utils: Save a few cycles in BaseTreeView column width computation
Change-Id: I5e97cae77db58a396424f5081838d2b2f3769ba7 Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src')
-rw-r--r--src/libs/utils/basetreeview.cpp57
1 files changed, 27 insertions, 30 deletions
diff --git a/src/libs/utils/basetreeview.cpp b/src/libs/utils/basetreeview.cpp
index 6473a7830d..3ccbf1f86e 100644
--- a/src/libs/utils/basetreeview.cpp
+++ b/src/libs/utils/basetreeview.cpp
@@ -152,46 +152,43 @@ public:
}
}
-
- void considerItems(int column, QModelIndex start, int *minimum, bool single) const
- {
- QModelIndex a = start;
- a = a.sibling(a.row(), column);
- QFontMetrics fm = q->fontMetrics();
- const int ind = q->indentation();
- const int avg = fm.averageCharWidth();
- QAbstractItemModel *m = q->model();
- for (int i = 0; i < 100 && a.isValid(); ++i) {
- const QString s = m->data(a).toString();
- int w = avg * s.size() + 20;
- if (column == 0) {
- for (QModelIndex b = a.parent(); b.isValid(); b = b.parent())
- w += ind;
- }
- if (w > *minimum)
- *minimum = w;
- if (single)
- break;
- a = q->indexBelow(a);
- }
- }
-
int suggestedColumnSize(int column) const
{
- QHeaderView *h = q->header();
+ const QHeaderView *h = q->header();
QTC_ASSERT(h, return -1);
- QAbstractItemModel *m = q->model();
+ const QAbstractItemModel *m = q->model();
QTC_ASSERT(m, return -1);
- QFontMetrics fm = q->fontMetrics();
+ const QFontMetrics fm = q->fontMetrics();
+ const int ind = q->indentation();
+ const int avg = fm.averageCharWidth();
int minimum = fm.horizontalAdvance(m->headerData(column, Qt::Horizontal).toString())
- + 2 * fm.horizontalAdvance(QLatin1Char('m'));
- considerItems(column, q->indexAt(QPoint(1, 1)), &minimum, false);
+ + 2 * avg;
+
+ auto considerItems = [&](const QModelIndex &start, bool single) {
+ QModelIndex a = start;
+ a = a.sibling(a.row(), column);
+ for (int i = 0; i < 100 && a.isValid(); ++i) {
+ const QString s = m->data(a).toString();
+ int w = avg * s.size() + 20;
+ if (column == 0) {
+ for (QModelIndex b = a.parent(); b.isValid(); b = b.parent())
+ w += ind;
+ }
+ if (w > minimum)
+ minimum = w;
+ if (single)
+ break;
+ a = q->indexBelow(a);
+ }
+ };
+
+ considerItems(q->indexAt(QPoint(1, 1)), false);
const QVariant extraIndices = m->data(QModelIndex(), BaseTreeView::ExtraIndicesForColumnWidth);
const QList<QModelIndex> values = extraIndices.value<QModelIndexList>();
for (const QModelIndex &a : values)
- considerItems(column, a, &minimum, true);
+ considerItems(a, true);
return minimum;
}