]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/private/kbaloorolesprovider.cpp
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / kitemviews / private / kbaloorolesprovider.cpp
index 920ce0671d57d832ecfcee960dd3964d403bc699..0f7100faae197d6217bf700d90d82ca149e4bf1c 100644 (file)
 
 #include "kbaloorolesprovider.h"
 
-#include <QDebug>
-#include <KLocalizedString>
-
 #include <Baloo/File>
 #include <KFileMetaData/PropertyInfo>
 #include <KFileMetaData/UserMetaData>
+#include <KFormat>
+#include <KLocalizedString>
 
+#include <QCollator>
+#include <QDebug>
 #include <QTime>
-#include <QMap>
 
 struct KBalooRolesProviderSingleton
 {
@@ -56,47 +56,39 @@ QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& f
 {
     QHash<QByteArray, QVariant> values;
 
-    int width = -1;
-    int height = -1;
+    using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;
+
+    const auto& propMap = file.properties();
+    auto rangeBegin = propMap.constKeyValueBegin();
 
-    QMapIterator<KFileMetaData::Property::Property, QVariant> it(file.properties());
-    while (it.hasNext()) {
-        it.next();
+    while (rangeBegin != propMap.constKeyValueEnd()) {
+        auto key = (*rangeBegin).first;
+        const KFileMetaData::PropertyInfo propertyInfo(key);
+        const QByteArray role = roleForProperty(propertyInfo.name());
+
+        auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(),
+            [key](const entry& e) { return e.first != key; });
 
-        const KFileMetaData::PropertyInfo pi(it.key());
-        const QString property = pi.name();
-        const QByteArray role = roleForProperty(property);
         if (role.isEmpty() || !roles.contains(role)) {
+            rangeBegin = rangeEnd;
             continue;
         }
 
-        const QVariant value = it.value();
-
-        if (role == "imageSize") {
-            // Merge the two properties for width and height
-            // as one string into the "imageSize" role
-            if (property == QLatin1String("width")) {
-                width = value.toInt();
-            }
-            else if (property == QLatin1String("height")) {
-                height = value.toInt();
-            }
-
-            if (width >= 0 && height >= 0) {
-                QString widthAndHeight = QString::number(width);
-                widthAndHeight += QLatin1String(" x ");
-                widthAndHeight += QString::number(height);
-                values.insert(role, widthAndHeight);
-            }
-        } else if (role == "orientation") {
-            const QString orientation = orientationFromValue(value.toInt());
-            values.insert(role, orientation);
-        } else if (role == "duration") {
-            const QString duration = durationFromValue(value.toInt());
-            values.insert(role, duration);
+        auto distance = std::distance(rangeBegin, rangeEnd);
+        if (distance > 1) {
+            QVariantList list;
+            list.reserve(static_cast<int>(distance));
+            std::for_each(rangeBegin, rangeEnd, [&list](const entry& s) { list.append(s.second); });
+            values.insert(role, propertyInfo.formatAsDisplayString(list));
         } else {
-            values.insert(role, value.toString());
+            if (propertyInfo.valueType() == QVariant::DateTime) {
+                // Let dolphin format later Dates
+                values.insert(role, (*rangeBegin).second);
+            } else {
+                values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
+            }
         }
+        rangeBegin = rangeEnd;
     }
 
     KFileMetaData::UserMetaData md(file.path());
@@ -109,6 +101,9 @@ QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& f
     if (roles.contains("comment")) {
         values.insert("comment", md.userComment());
     }
+    if (roles.contains("originUrl")) {
+        values.insert("originUrl", md.originUrl());
+    }
 
     return values;
 }
@@ -129,22 +124,28 @@ KBalooRolesProvider::KBalooRolesProvider() :
     };
 
     // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
-    // a 1:1 mapping: One role may contain several URI-values (e.g. the URIs for height and
-    // width of an image are mapped to the role "imageSize")
+    // a 1:1 mapping: One role may contain several URI-values
     static const PropertyInfo propertyInfoList[] = {
         { "rating", "rating" },
         { "tag",        "tags" },
         { "comment",   "comment" },
+        { "title",         "title" },
         { "wordCount",     "wordCount" },
         { "lineCount",     "lineCount" },
-        { "width",         "imageSize" },
-        { "height",        "imageSize" },
-        { "nexif.orientation", "orientation", },
+        { "width",         "width" },
+        { "height",        "height" },
+        { "imageDateTime",   "imageDateTime"},
+        { "imageOrientation", "orientation", },
         { "artist",     "artist" },
+        { "genre",     "genre"  },
         { "album",    "album" },
         { "duration",      "duration" },
-        { "trackNumber",   "track" }
-        // { "http://www.semanticdesktop.org/ontologies/2010/04/30/ndo#copiedFrom",    "copiedFrom" }
+        { "bitRate", "bitrate" },
+        { "aspectRatio", "aspectRatio" },
+        { "frameRate", "frameRate" },
+        { "releaseYear",    "releaseYear" },
+        { "trackNumber",   "track" },
+        { "originUrl", "originUrl" }
     };
 
     for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
@@ -155,31 +156,9 @@ KBalooRolesProvider::KBalooRolesProvider() :
 
 QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
 {
-    return values.join(", ");
+    QStringList alphabeticalOrderTags = values;
+    QCollator coll;
+    coll.setNumericMode(true);
+    std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
+    return alphabeticalOrderTags.join(QLatin1String(", "));
 }
-
-QString KBalooRolesProvider::orientationFromValue(int value) const
-{
-    QString string;
-    switch (value) {
-    case 1: string = i18nc("@item:intable Image orientation", "Unchanged"); break;
-    case 2: string = i18nc("@item:intable Image orientation", "Horizontally flipped"); break;
-    case 3: string = i18nc("@item:intable image orientation", "180° rotated"); break;
-    case 4: string = i18nc("@item:intable image orientation", "Vertically flipped"); break;
-    case 5: string = i18nc("@item:intable image orientation", "Transposed"); break;
-    case 6: string = i18nc("@item:intable image orientation", "90° rotated"); break;
-    case 7: string = i18nc("@item:intable image orientation", "Transversed"); break;
-    case 8: string = i18nc("@item:intable image orientation", "270° rotated"); break;
-    default:
-        break;
-    }
-    return string;
-}
-
-QString KBalooRolesProvider::durationFromValue(int value) const
-{
-    QTime duration;
-    duration = duration.addSecs(value);
-    return duration.toString("hh:mm:ss");
-}
-