From eadbf920b5dffdf2d1548240c25ec2eaf835aad5 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 4 Nov 2011 21:54:01 +0100 Subject: [PATCH] Don't use mixed units in size-column of details-view This makes it tricky to compare the filesizes without adjusting the sort-order, so now all sizes in the size-column are shown in KiB or KB (dependent on the KLocale setting). BUG: 219932 FIXED-IN: 4.8.0 Related fixes: - Stay consistent with the rounding when using the KiB/KB unit in the statusbar. - Fix sorting-by-size issue for folders - Show "Unknown" in the size-column when the number of items cannot be determined. --- src/dolphinviewcontainer.cpp | 8 ++- src/kitemviews/kfileitemlistwidget.cpp | 11 +++- src/kitemviews/kfileitemmodel.cpp | 22 +++++-- src/views/dolphinview.cpp | 84 +++++++++++++++----------- src/views/dolphinview.h | 6 ++ 5 files changed, 85 insertions(+), 46 deletions(-) diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index f3c536b52..47195bbca 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -427,7 +427,13 @@ void DolphinViewContainer::showItemInfo(const KFileItem& item) m_statusBar->clear(); } } else { - m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default); + QString message; + if (item.isDir()) { + message = item.name(); + } else { + message = i18nc("@info:status filename (type)", "%1 (%2)", item.name(), item.mimeComment()); + } + m_statusBar->setMessage(message, DolphinStatusBar::Default); } } diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index 5f659a1f6..d6b892658 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -192,13 +192,18 @@ QString KFileItemListWidget::roleText(const QByteArray& role, const QHash(); text = i18ncp("@item:intable", "%1 item", "%1 items", size); } } else { - const KIO::filesize_t size = roleValue.value(); - text = KIO::convertSize(size); + // Show the size in kilobytes (always round up) + const KLocale* locale = KGlobal::locale(); + const int roundInc = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect) ? 499 : 511; + const KIO::filesize_t size = roleValue.value() + roundInc; + text = locale->formatByteSize(size, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte); } break; } diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index 4b026fea3..afc6decee 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -1032,12 +1032,22 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const } case SizeRole: { - const KIO::filesize_t sizeA = itemA.size(); - const KIO::filesize_t sizeB = itemB.size(); - if (sizeA < sizeB) { - result = -1; - } else if (sizeA > sizeB) { - result = +1; + if (itemA.isDir()) { + Q_ASSERT(itemB.isDir()); // see "if (m_sortFoldersFirst || m_sortRole == SizeRole)" above + + const QVariant valueA = a->values.value("size"); + const QVariant valueB = b->values.value("size"); + + if (valueA.isNull()) { + result = -1; + } else if (valueB.isNull()) { + result = +1; + } else { + result = valueA.value() - valueB.value(); + } + } else { + Q_ASSERT(!itemB.isDir()); // see "if (m_sortFoldersFirst || m_sortRole == SizeRole)" above + result = itemA.size() - itemB.size(); } break; } diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 961651b69..6312d27cf 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -362,6 +362,11 @@ void DolphinView::markUrlsAsSelected(const QList& urls) m_selectedUrls = urls; } +void DolphinView::markUrlAsCurrent(const KUrl& url) +{ + m_currentItemUrl = url; +} + void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled) { const KItemListSelectionManager::SelectionMode mode = enabled @@ -515,61 +520,50 @@ void DolphinView::calculateItemCount(int& fileCount, QString DolphinView::statusBarText() const { - QString text; + QString summary; + QString foldersText; + QString filesText; + int folderCount = 0; int fileCount = 0; KIO::filesize_t totalFileSize = 0; if (hasSelection()) { - // give a summary of the status of the selected files + // Give a summary of the status of the selected files const KFileItemList list = selectedItems(); - if (list.isEmpty()) { - // when an item is triggered, it is temporary selected but selectedItems() - // will return an empty list - return text; - } - - KFileItemList::const_iterator it = list.begin(); - const KFileItemList::const_iterator end = list.end(); - while (it != end) { - const KFileItem& item = *it; + foreach (const KFileItem& item, list) { if (item.isDir()) { ++folderCount; } else { ++fileCount; totalFileSize += item.size(); } - ++it; } if (folderCount + fileCount == 1) { - // if only one item is selected, show the filename - const QString name = list.first().text(); - text = (folderCount == 1) ? i18nc("@info:status", "%1 selected", name) : - i18nc("@info:status", "%1 selected (%2)", - name, KIO::convertSize(totalFileSize)); + // If only one item is selected, show the filename + filesText = i18nc("@info:status", "%1 selected", list.first().text()); } else { - // at least 2 items are selected - const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); - const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount); - if ((folderCount > 0) && (fileCount > 0)) { - text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)", - foldersText, filesText, KIO::convertSize(totalFileSize)); - } else if (fileCount > 0) { - text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize)); - } else { - Q_ASSERT(folderCount > 0); - text = foldersText; - } + // At least 2 items are selected + foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount); + filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount); } } else { calculateItemCount(fileCount, folderCount, totalFileSize); - text = KIO::itemsSummaryString(fileCount + folderCount, - fileCount, folderCount, - totalFileSize, true); + foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount); + filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount); } - return text; + if (fileCount > 0 && folderCount > 0) { + summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)", + foldersText, filesText, fileSizeText(totalFileSize)); + } else if (fileCount > 0) { + summary = i18nc("@info:status files (size)", "%1 (%2)", filesText, fileSizeText(totalFileSize)); + } else if (folderCount > 0) { + summary = foldersText; + } + + return summary; } QList DolphinView::versionControlActions(const KFileItemList& items) const @@ -1365,9 +1359,27 @@ DolphinView::Sorting DolphinView::sortingForSortRole(const QByteArray& sortRole) return sortHash.value(sortRole); } -void DolphinView::markUrlAsCurrent(const KUrl& url) +QString DolphinView::fileSizeText(KIO::filesize_t fileSize) { - m_currentItemUrl = url; + const KLocale* locale = KGlobal::locale(); + const unsigned int multiplier = (locale->binaryUnitDialect() == KLocale::MetricBinaryDialect) + ? 1000 : 1024; + + QString text; + if (fileSize < multiplier) { + // Show the size in bytes + text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitByte); + } else if (fileSize < multiplier * multiplier) { + // Show the size in kilobytes and always round up. This is done + // for consistency with the values shown e.g. in the "Size" column + // of the details-view. + fileSize += (multiplier / 2) - 1; + text = locale->formatByteSize(fileSize, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte); + } else { + // Show the size in the best fitting unit having one decimal + text = locale->formatByteSize(fileSize, 1); + } + return text; } #include "dolphinview.moc" diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index c681737c6..2bbdf2b71 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -732,6 +732,12 @@ private: QByteArray sortRoleForSorting(Sorting sorting) const; Sorting sortingForSortRole(const QByteArray& sortRole) const; + /** + * Returns the text for the filesize by converting it to the best fitting + * unit. + */ + static QString fileSizeText(KIO::filesize_t fileSize); + private: bool m_active : 1; bool m_tabsForFiles : 1; -- 2.47.3