+void DolphinDetailsView::resizeColumns()
+{
+ QHeaderView* headerView = header();
+ const int rowCount = model()->rowCount();
+ if (rowCount <= 0) {
+ headerView->resizeSection(KDirModel::Name, viewport()->width());
+ return;
+ }
+
+ // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
+ // around 3 seconds for each (!) resize operation when having > 10000 items).
+ // This gets a problem especially when opening large directories, where several
+ // resize operations are received for showing the currently available items during
+ // loading (the application hangs around 20 seconds when loading > 10000 items).
+
+ QFontMetrics fontMetrics(viewport()->font());
+
+ // Define the maximum number of rows, where an exact (but expensive) calculation
+ // of the widths is done.
+ const int maxRowCount = 200;
+
+ // Calculate the required with for each column and store it in columnWidth[]
+ int columnWidth[DolphinModel::ExtraColumnCount];
+
+ const QAbstractProxyModel* proxyModel = qobject_cast<const QAbstractProxyModel*>(model());
+ const KDirModel* dirModel = qobject_cast<const KDirModel*>(proxyModel->sourceModel());
+ for (int column = 0; column < DolphinModel::ExtraColumnCount; ++column) {
+ columnWidth[column] = 0;
+ if (!isColumnHidden(column)) {
+ // Calculate the required width for the current column and consider only
+ // up to maxRowCount columns for performance reasons
+ const int count = qMin(rowCount, maxRowCount);
+ const QStyleOptionViewItem option = viewOptions();
+ for (int row = 0; row < count; ++row) {
+ const QModelIndex index = dirModel->index(row, column);
+ const int width = itemDelegate()->sizeHint(option, index).width();
+ if (width > columnWidth[column]) {
+ columnWidth[column] = width;
+ }
+ }
+
+ // Assure that the required width is sufficient for the header too
+ const int logicalIndex = headerView->logicalIndex(column);
+ const QString headline = model()->headerData(logicalIndex, Qt::Horizontal).toString();
+ const int headlineWidth = fontMetrics.width(headline);
+
+ columnWidth[column] = qMax(columnWidth[column], headlineWidth);
+ }
+ }
+
+ // Resize all columns except of the name column
+ int requiredWidth = 0;
+ for (int column = KDirModel::Size; column < DolphinModel::ExtraColumnCount; ++column) {
+ if (!isColumnHidden(column)) {
+ requiredWidth += columnWidth[column];
+ headerView->resizeSection(column, columnWidth[column]);
+ }
+ }
+
+ // Resize the name column in a way that the whole available width is used
+ columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
+
+ const int minNameWidth = 300;
+ if (columnWidth[KDirModel::Name] < minNameWidth) {
+ columnWidth[KDirModel::Name] = minNameWidth;
+
+ if ((rowCount > 0) && (rowCount < maxRowCount)) {
+ // Try to decrease the name column width without clipping any text
+ const int nameWidth = sizeHintForColumn(DolphinModel::Name);
+ if (nameWidth + requiredWidth <= viewport()->width()) {
+ columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
+ } else if (nameWidth < minNameWidth) {
+ columnWidth[KDirModel::Name] = nameWidth;
+ }
+ }
+ }
+
+ headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
+}
+