Commit 10e175a7 authored by Sven Brauch's avatar Sven Brauch
Browse files

problem view: faster implementation of resizeColumnsToContents

For large documents with lots of problems, switching the file
has a noticeable delay, caused by computing lots of size hints.
We can avoid that by using a heuristics instead.
parent 1952386c
Pipeline #20967 passed with stage
in 42 minutes and 47 seconds
......@@ -168,8 +168,39 @@ void ProblemTreeView::itemActivated(const QModelIndex& index)
void ProblemTreeView::resizeColumns()
{
for (int i = 0; i < model()->columnCount(); ++i)
resizeColumnToContents(i);
// Returns the approximate amount of characters in a column and
// a stretch factor.
auto sizeHint = [](int column) -> std::pair<int, int> {
switch (column) {
case ProblemModel::Error: return {40, 20};
case ProblemModel::Source: return {30, 1};
case ProblemModel::File: return {30, 10};
case ProblemModel::Line: return {15, 1};
case ProblemModel::Column: return {15, 1};
default: return {20, 0};
}
};
auto charWidth = fontMetrics().averageCharWidth();
// Set each column to its minimum needed width, and figure out how much space is left.
int widths[ProblemModel::LastColumn];
int remainPixels = header()->size().width();
int totalWeight = 0;
for (int i = 0; i < ProblemModel::LastColumn; ++i) {
auto const [minWidth, weight] = sizeHint(i);
widths[i] = minWidth * charWidth;
remainPixels -= minWidth * charWidth;
totalWeight += weight;
}
// Distribute according to stretch factor if there is any unoccupied space.
remainPixels = std::max(0, remainPixels);
for (int i = 0; i < ProblemModel::LastColumn; ++i) {
auto const [minWidth, weight] = sizeHint(i);
widths[i] += (remainPixels * weight) / totalWeight;
setColumnWidth(i, widths[i]);
}
}
void ProblemTreeView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment