From 94262a1c02606279b03e40f2cb3ebc985ff69a08 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Tue, 25 Jan 2022 13:06:17 +0100 Subject: [PATCH] Add "Dimensions" (width x height) role It is more convenient to use than individual width and height properties --- src/kitemviews/kfileitemlistwidget.cpp | 5 +++++ src/kitemviews/kfileitemmodel.cpp | 14 ++++++++++++++ src/kitemviews/kfileitemmodel.h | 2 +- src/kitemviews/private/kbaloorolesprovider.cpp | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index fc4e4bdac..4198df027 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -94,6 +94,11 @@ QString KFileItemListWidgetInformant::roleText(const QByteArray& role, if (dateTime.isValid()) { text = formatDate(dateTime); } + } else if (role == "dimensions") { + const auto dimensions = roleValue.toSize(); + if (dimensions.isValid()) { + text = i18nc("width x height", "%1 x %2", dimensions.width(), dimensions.height()); + } } else { text = KStandardItemListWidgetInformant::roleText(role, values); } diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index d4d0be0e8..23afb087a 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -2089,6 +2089,19 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b, const break; } + case DimensionsRole: { + const QByteArray role = roleForType(m_sortRole); + const QSize dimensionsA = a->values.value(role).toSize(); + const QSize dimensionsB = b->values.value(role).toSize(); + + if (dimensionsA.width() == dimensionsB.width()) { + result = dimensionsA.height() - dimensionsB.height(); + } else { + result = dimensionsA.width() - dimensionsB.width(); + } + break; + } + default: { const QByteArray role = roleForType(m_sortRole); const QString roleValueA = a->values.value(role).toString(); @@ -2596,6 +2609,7 @@ const KFileItemModel::RoleInfoMap* KFileItemModel::rolesInfoMap(int& count) { "wordCount", WordCountRole, kli18nc("@label", "Word Count"), kli18nc("@label", "Document"), true, true }, { "lineCount", LineCountRole, kli18nc("@label", "Line Count"), kli18nc("@label", "Document"), true, true }, { "imageDateTime", ImageDateTimeRole, kli18nc("@label", "Date Photographed"), kli18nc("@label", "Image"), true, true }, + { "dimensions", DimensionsRole, kli18nc("@label width x height", "Dimensions"), kli18nc("@label", "Image"), true, true }, { "width", WidthRole, kli18nc("@label", "Width"), kli18nc("@label", "Image"), true, true }, { "height", HeightRole, kli18nc("@label", "Height"), kli18nc("@label", "Image"), true, true }, { "orientation", OrientationRole, kli18nc("@label", "Orientation"), kli18nc("@label", "Image"), true, true }, diff --git a/src/kitemviews/kfileitemmodel.h b/src/kitemviews/kfileitemmodel.h index f88a8830a..cc39a0084 100644 --- a/src/kitemviews/kfileitemmodel.h +++ b/src/kitemviews/kfileitemmodel.h @@ -292,7 +292,7 @@ private: NoRole, NameRole, SizeRole, ModificationTimeRole, CreationTimeRole, AccessTimeRole, PermissionsRole, OwnerRole, GroupRole, TypeRole, DestinationRole, PathRole, DeletionTimeRole, // User visible roles available with Baloo: - CommentRole, TagsRole, RatingRole, WidthRole, HeightRole, ImageDateTimeRole, OrientationRole, + CommentRole, TagsRole, RatingRole, DimensionsRole, WidthRole, HeightRole, ImageDateTimeRole, OrientationRole, WordCountRole, TitleRole, LineCountRole, ArtistRole, GenreRole, AlbumRole, DurationRole, TrackRole, ReleaseYearRole, BitrateRole, OriginUrlRole, AspectRatioRole, FrameRateRole, // Non-visible roles: diff --git a/src/kitemviews/private/kbaloorolesprovider.cpp b/src/kitemviews/private/kbaloorolesprovider.cpp index 5c87de712..4c231e2ff 100644 --- a/src/kitemviews/private/kbaloorolesprovider.cpp +++ b/src/kitemviews/private/kbaloorolesprovider.cpp @@ -13,6 +13,7 @@ #include #include +#include #include namespace { @@ -117,6 +118,18 @@ QHash KBalooRolesProvider::roleValues(const Baloo::File& f rangeBegin = rangeEnd; } + if (roles.contains("dimensions")) { + bool widthOk = false; + bool heightOk = false; + + const int width = propMap.value(KFileMetaData::Property::Width).toInt(&widthOk); + const int height = propMap.value(KFileMetaData::Property::Height).toInt(&heightOk); + + if (widthOk && heightOk && width >= 0 && height >= 0) { + values.insert("dimensions", QSize(width, height)); + } + } + KFileMetaData::UserMetaData::Attributes attributes; if (roles.contains("tags")) { attributes |= KFileMetaData::UserMetaData::Tags; @@ -160,6 +173,7 @@ KBalooRolesProvider::KBalooRolesProvider() for (const auto& role : propertyRoleMap()) { m_roles.insert(role); } + m_roles.insert("dimensions"); // Display roles provided by UserMetaData m_roles.insert(QByteArrayLiteral("tags"));