]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Don't use mixed units in size-column of details-view
authorPeter Penz <peter.penz19@gmail.com>
Fri, 4 Nov 2011 20:54:01 +0000 (21:54 +0100)
committerPeter Penz <peter.penz19@gmail.com>
Fri, 4 Nov 2011 20:58:32 +0000 (21:58 +0100)
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
src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemmodel.cpp
src/views/dolphinview.cpp
src/views/dolphinview.h

index f3c536b52141393b8d25e4c565b215f78779f7dc..47195bbca2a7b4f4b032788037fa8f149212890a 100644 (file)
@@ -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);
     }
 }
 
index 5f659a1f618c877d977f539508219db6c466369d..d6b8926583318b04f3a7024af222971acdab719f 100644 (file)
@@ -192,13 +192,18 @@ QString KFileItemListWidget::roleText(const QByteArray& role, const QHash<QByteA
         if (values.value("isDir").toBool()) {
             // The item represents a directory. Show the number of sub directories
             // instead of the file size of the directory.
-            if (!roleValue.isNull()) {
+            if (roleValue.isNull()) {
+                text = i18nc("@item:intable", "Unknown");
+            } else {
                 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
                 text = i18ncp("@item:intable", "%1 item", "%1 items", size);
             }
         } else {
-            const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
-            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<KIO::filesize_t>() + roundInc;
+            text = locale->formatByteSize(size, 0, KLocale::DefaultBinaryDialect, KLocale::UnitKiloByte);
         }
         break;
     }
index 4b026fea3d5a3859c4203778420c8bc4a96e9803..afc6decee81309d32d0fec60ef8fb21cd094a067 100644 (file)
@@ -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<KIO::filesize_t>() - valueB.value<KIO::filesize_t>();
+            }
+        } else {
+            Q_ASSERT(!itemB.isDir()); // see "if (m_sortFoldersFirst || m_sortRole == SizeRole)" above
+            result = itemA.size() - itemB.size();
         }
         break;
     }
index 961651b698594263c237ba9710e985ad099d34cd..6312d27cf53d8a173fc04a447cb13363a77b5b4d 100644 (file)
@@ -362,6 +362,11 @@ void DolphinView::markUrlsAsSelected(const QList<KUrl>& 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", "<filename>%1</filename> selected", name) :
-                                        i18nc("@info:status", "<filename>%1</filename> selected (%2)",
-                                              name, KIO::convertSize(totalFileSize));
+            // If only one item is selected, show the filename
+            filesText = i18nc("@info:status", "<filename>%1</filename> 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<QAction*> 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"
index c681737c6bf81c5791334e02e67f9f336e1bd161..2bbdf2b711c5454800a18674ad6da1426478235b 100644 (file)
@@ -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;