From 14bae32f371663d172e1dd39d98080fd8695d991 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 15 Apr 2011 20:35:25 +0200 Subject: [PATCH] Details view: Fix jumping column-widths If the view has a width where a horizontal scrollbar is required to show all columns, an endless loop might get triggered that results in a periodic jumping of the column-widths. BUG: 270954 FIXED-IN: 4.7.0 --- src/views/dolphindetailsview.cpp | 44 +++++++++++++++++++++++++------- src/views/dolphindetailsview.h | 6 +++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/views/dolphindetailsview.cpp b/src/views/dolphindetailsview.cpp index 5c2e9576f..0ce26df33 100644 --- a/src/views/dolphindetailsview.cpp +++ b/src/views/dolphindetailsview.cpp @@ -135,7 +135,6 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, connect(view, SIGNAL(showPreviewChanged()), this, SLOT(slotShowPreviewChanged())); - viewport()->installEventFilter(this); connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), @@ -458,6 +457,7 @@ void DolphinDetailsView::resizeColumns() QHeaderView* headerView = header(); const int rowCount = model()->rowCount(); QFontMetrics fontMetrics(viewport()->font()); + const int horizontalGap = fontMetrics.height(); // Define the maximum number of rows, where an exact (but expensive) calculation // of the widths is done. @@ -472,14 +472,21 @@ void DolphinDetailsView::resizeColumns() // Calculate the required width for the current column and consider only // up to maxRowCount columns for performance reasons if (rowCount > 0) { - const QAbstractProxyModel* proxyModel = qobject_cast(model()); - const KDirModel* dirModel = qobject_cast(proxyModel->sourceModel()); - 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(); + const QModelIndex index = model()->index(row, column); + QString text; + if (column == DolphinModel::Size) { + // This is a workaround as KFileItemDelegate::sizeHint() does not + // work in a way that is required for calculating the size. + const QAbstractProxyModel* proxyModel = qobject_cast(model()); + const KDirModel* dirModel = qobject_cast(proxyModel->sourceModel()); + const QModelIndex dirIndex = proxyModel->mapToSource(index); + text = itemSizeString(dirIndex, dirModel->itemForIndex(dirIndex)); + } else { + text = model()->data(index).toString(); + } + const int width = fontMetrics.width(text) + horizontalGap; if (width > columnWidth[column]) { columnWidth[column] = width; } @@ -489,8 +496,7 @@ void DolphinDetailsView::resizeColumns() // 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(); - // TODO: check Qt-sources which left/right-gap is used for the headlines - const int headlineWidth = fontMetrics.width(headline) + 20; + const int headlineWidth = fontMetrics.width(headline) + horizontalGap; columnWidth[column] = qMax(columnWidth[column], headlineWidth); } @@ -677,4 +683,24 @@ void DolphinDetailsView::adjustMaximumSizeForEditing(const QModelIndex& index) } } +QString DolphinDetailsView::itemSizeString(const QModelIndex& index, const KFileItem& item) const +{ + // The following code has been copied from KFileItemDelegate::Private::itemSize() + // Copyright (c) 2006-2007, 2008 Fredrik Höglund + // Ideally this should be handled by KFileItemDelegate::sizeHint(). + if (item.isFile()) { + return KGlobal::locale()->formatByteSize(item.size()); + } + + // Return the number of items in the directory + const QVariant value = index.data(KDirModel::ChildCountRole); + const int count = value.type() == QVariant::Int ? value.toInt() : KDirModel::ChildCountUnknown; + + if (count == KDirModel::ChildCountUnknown) { + return QString(); + } + + return i18ncp("Items in a folder", "1 item", "%1 items", count); +} + #include "dolphindetailsview.moc" diff --git a/src/views/dolphindetailsview.h b/src/views/dolphindetailsview.h index 640181274..305035dd5 100644 --- a/src/views/dolphindetailsview.h +++ b/src/views/dolphindetailsview.h @@ -195,6 +195,12 @@ private: */ void adjustMaximumSizeForEditing(const QModelIndex& index); + /** + * Helper method for DolphinDetailsView::resizeColumns(): Returns the + * string representation of the size-value for the given index. + */ + QString itemSizeString(const QModelIndex& index, const KFileItem& item) const; + private: bool m_autoResize; // if true, the columns are resized automatically to the available width -- 2.47.3