forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -152,15 +152,22 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void considerItems(int column, QModelIndex start, int *minimum, bool single) const
|
||||
int suggestedColumnSize(int column) const
|
||||
{
|
||||
QModelIndex a = start;
|
||||
a = a.sibling(a.row(), column);
|
||||
QFontMetrics fm = q->fontMetrics();
|
||||
const QHeaderView *h = q->header();
|
||||
QTC_ASSERT(h, return -1);
|
||||
const QAbstractItemModel *m = q->model();
|
||||
QTC_ASSERT(m, return -1);
|
||||
|
||||
const QFontMetrics fm = q->fontMetrics();
|
||||
const int ind = q->indentation();
|
||||
const int avg = fm.averageCharWidth();
|
||||
QAbstractItemModel *m = q->model();
|
||||
int minimum = fm.horizontalAdvance(m->headerData(column, Qt::Horizontal).toString())
|
||||
+ 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;
|
||||
@@ -168,30 +175,20 @@ public:
|
||||
for (QModelIndex b = a.parent(); b.isValid(); b = b.parent())
|
||||
w += ind;
|
||||
}
|
||||
if (w > *minimum)
|
||||
*minimum = w;
|
||||
if (w > minimum)
|
||||
minimum = w;
|
||||
if (single)
|
||||
break;
|
||||
a = q->indexBelow(a);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int suggestedColumnSize(int column) const
|
||||
{
|
||||
QHeaderView *h = q->header();
|
||||
QTC_ASSERT(h, return -1);
|
||||
QAbstractItemModel *m = q->model();
|
||||
QTC_ASSERT(m, return -1);
|
||||
|
||||
QFontMetrics fm = q->fontMetrics();
|
||||
int minimum = fm.horizontalAdvance(m->headerData(column, Qt::Horizontal).toString())
|
||||
+ 2 * fm.horizontalAdvance(QLatin1Char('m'));
|
||||
considerItems(column, q->indexAt(QPoint(1, 1)), &minimum, false);
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user