| 157 | } |
| 158 | |
| 159 | void ProblemTreeView::resizeColumns() |
| 160 | { |
| 161 | // Don't simply call QTreeView::resizeColumnToContents() for each column here, |
| 162 | // because it is not useful enough to justify significant performance cost. |
| 163 | // Instead, set column widths to heuristic values independent on the contents (the problem list). |
| 164 | |
| 165 | const int averageCharWidth = fontMetrics().averageCharWidth(); |
| 166 | const int headerWidth = header()->width(); |
| 167 | if (averageCharWidth == m_averageCharWidth && headerWidth == m_headerWidth) { |
| 168 | // No reason to change column widths. This early return is not just an optimization: KDevelop should not |
| 169 | // gratuitously reapply unchanged heuristic column widths, because the user may have fine-tuned them manually. |
| 170 | return; |
| 171 | } |
| 172 | m_averageCharWidth = averageCharWidth; |
| 173 | m_headerWidth = headerWidth; |
| 174 | |
| 175 | struct ColumnSizePolicy |
| 176 | { |
| 177 | int minWidthInCharacters; |
| 178 | int stretchFactor; |
| 179 | }; |
| 180 | static constexpr std::array<ColumnSizePolicy, 5> sizePolicy{ |
| 181 | ColumnSizePolicy{40, 20}, // Error |
| 182 | ColumnSizePolicy{25, 1}, // Source |
| 183 | ColumnSizePolicy{30, 10}, // File |
| 184 | ColumnSizePolicy{10, 1}, // Line |
| 185 | ColumnSizePolicy{10, 1}, // Column |
| 186 | }; |
| 187 | static_assert(sizePolicy.size() == ProblemModel::LastColumn); |
| 188 | |
| 189 | // Cannot use std::accumulate() here, because it is not constexpr in C++17. |
| 190 | static constexpr ColumnSizePolicy totalAllColumns = [] { |
| 191 | ColumnSizePolicy sum{}; |
| 192 | for (auto p : sizePolicy) { |
| 193 | sum.minWidthInCharacters += p.minWidthInCharacters; |
| 194 | sum.stretchFactor += p.stretchFactor; |
| 195 | } |
| 196 | return sum; |
| 197 | }(); |
| 198 | |
| 199 | ColumnSizePolicy total = totalAllColumns; |
| 200 | if (!model()->features().testFlag(ProblemModel::ShowSource)) { |
| 201 | // Disregard the size policy of the hidden Source column. |
| 202 | static constexpr auto hiddenColumn = sizePolicy[ProblemModel::Source]; |
| 203 | total.minWidthInCharacters -= hiddenColumn.minWidthInCharacters; |
| 204 | total.stretchFactor -= hiddenColumn.stretchFactor; |
| 205 | } |
| 206 | Q_ASSERT(total.stretchFactor > 0); |
| 207 | |
| 208 | const int remainingPixels = std::max(0, headerWidth - total.minWidthInCharacters * averageCharWidth); |
| 209 | |
| 210 | // Give each column its minimum needed width. If there is any horizontal space left, |
| 211 | // distribute it among columns in proportion to their stretch factors. |
| 212 | for (std::size_t i = 0; i < sizePolicy.size(); ++i) { |
| 213 | int width = sizePolicy[i].minWidthInCharacters * averageCharWidth; |
| 214 | width += remainingPixels * sizePolicy[i].stretchFactor / total.stretchFactor; |
| 215 | setColumnWidth(i, width); |
| 216 | } |