]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Implemented the possibility for sorting/grouping behaviors that are not tied to roles...
authorZakhar Afonin <aza22u419@student.bmstu.ru>
Sun, 16 Jun 2024 15:49:09 +0000 (18:49 +0300)
committerZakhar Afonin <aza22u419@student.bmstu.ru>
Sun, 16 Jun 2024 15:49:09 +0000 (18:49 +0300)
src/kitemviews/kfileitemmodel.cpp
src/kitemviews/kfileitemmodel.h
src/kitemviews/kitemlistview.cpp
src/views/dolphinviewactionhandler.cpp
src/views/viewproperties.cpp

index 5620460591206d5e43fb5323da6721dffb67f322..a2e7c00758a868cea4db6334dacf8d1c0dce97a1 100644 (file)
@@ -34,7 +34,7 @@ Q_GLOBAL_STATIC(QRecursiveMutex, s_collatorMutex)
 // #define KFILEITEMMODEL_DEBUG
 
 KFileItemModel::KFileItemModel(QObject *parent)
-    : KItemModelBase("text", "text", parent)
+    : KItemModelBase("text", "none", parent)
     , m_dirLister(nullptr)
     , m_sortDirsFirst(true)
     , m_sortHiddenLast(false)
@@ -387,7 +387,14 @@ QList<QPair<int, QVariant>> KFileItemModel::groups() const
         QElapsedTimer timer;
         timer.start();
 #endif
-        switch (typeForRole(groupRole())) {
+        QByteArray role = groupRole();
+        if (typeForRole(role) == NoRole) {
+            // Handle extra grouping information
+            if (m_groupExtraInfo == "followSort") {
+                role = sortRole();
+            }
+        }
+        switch (typeForRole(role)) {
         case NoRole:
             m_groups.clear();
             break;
@@ -424,7 +431,7 @@ QList<QPair<int, QVariant>> KFileItemModel::groups() const
             m_groups = ratingRoleGroups();
             break;
         default:
-            m_groups = genericStringRoleGroups(groupRole());
+            m_groups = genericStringRoleGroups(role);
             break;
         }
 
@@ -949,6 +956,13 @@ void KFileItemModel::onSortRoleChanged(const QByteArray &current, const QByteArr
 {
     Q_UNUSED(previous)
     m_sortRole = typeForRole(current);
+    if (m_sortRole == NoRole) {
+        // Requested role not in list of roles. This could
+        // be used for indicating non-trivial sorting behavior
+        m_sortExtraInfo = current;
+    } else {
+        m_sortExtraInfo.clear();
+    }
 
     if (!m_requestRole[m_sortRole]) {
         QSet<QByteArray> newRoles = m_roles;
@@ -975,8 +989,15 @@ void KFileItemModel::onGroupRoleChanged(const QByteArray &current, const QByteAr
 {
     Q_UNUSED(previous)
     m_groupRole = typeForRole(current);
+    if (m_groupRole == NoRole) {
+        // Requested role not in list of roles. This could
+        // be used for indicating non-trivial grouping behavior
+        m_groupExtraInfo = current;
+    } else {
+        m_groupExtraInfo.clear();
+    }
 
-    if (!m_requestRole[m_sortRole]) {
+    if (!m_requestRole[m_groupRole]) {
         QSet<QByteArray> newRoles = m_roles;
         newRoles << current;
         setRoles(newRoles);
@@ -2293,7 +2314,8 @@ int KFileItemModel::groupRoleCompare(const ItemData *a, const ItemData *b, const
     int groupA, groupB;
     switch (m_groupRole) {
     case NoRole:
-        break;
+        // Non-trivial grouping behavior might be handled there in the future.
+        return 0;
     case NameRole:
         groupA = nameRoleGroup(a, false).comparable;
         groupB = nameRoleGroup(b, false).comparable;
index 9001e7bd5657501c4daae84b298e8c347dc9c469..ea72a48a4660f3de930bf82c62f65a676d087a41 100644 (file)
@@ -575,6 +575,9 @@ private:
 
     RoleType m_sortRole;
     RoleType m_groupRole;
+    QByteArray m_sortExtraInfo;
+    QByteArray m_groupExtraInfo;
+
     int m_sortingProgressPercent; // Value of directorySortingProgress() signal
     QSet<QByteArray> m_roles;
 
index afc392810edf51edd24585a8976dde0d927bd822..52cd49bfbadba9802cb5bf542bbbfb8a87688065 100644 (file)
@@ -2185,7 +2185,7 @@ void KItemListView::updateGroupHeaderForWidget(KItemListWidget *widget)
     const int groupIndex = groupIndexForItem(index);
     Q_ASSERT(groupIndex >= 0);
     groupHeader->setData(groups.at(groupIndex).second);
-    groupHeader->setRole(model()->sortRole());
+    groupHeader->setRole(model()->groupRole());
     groupHeader->setStyleOption(m_styleOption);
     groupHeader->setScrollOrientation(scrollOrientation());
     groupHeader->setItemIndex(index);
index 7454ed0b947f2979483d052da5a7a4a122c1ae08..c215f1bf7547429551e196d15a46baa8f6c7eda0 100644 (file)
@@ -302,6 +302,18 @@ void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper *ac
     // View -> Group By
     QActionGroup *groupByActionGroup = createFileItemRolesActionGroup(QStringLiteral("group_by_"));
 
+    KToggleAction *groupAsNone = m_actionCollection->add<KToggleAction>(QStringLiteral("group_none"));
+    groupAsNone->setData("none");
+    groupAsNone->setActionGroup(groupByActionGroup);
+    groupAsNone->setText(i18nc("@label", "No grouping"));
+    m_groupByActions.insert("none", groupAsNone);
+
+    KToggleAction *groupAsFollowSort = m_actionCollection->add<KToggleAction>(QStringLiteral("group_followSort"));
+    groupAsFollowSort->setData("followSort");
+    groupAsFollowSort->setActionGroup(groupByActionGroup);
+    groupAsFollowSort->setText(i18nc("@label", "Follow sorting"));
+    m_groupByActions.insert("followSort", groupAsFollowSort);
+
     KActionMenu *groupByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("group"));
     groupByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
     groupByActionMenu->setText(i18nc("@action:inmenu View", "Group By"));
@@ -404,9 +416,6 @@ QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QSt
 #endif
 
     QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
-    // Unlike sorting, grouping is optional. If creating for group_by_, include a None role.
-    if (isGroupGroup)
-        rolesInfo.append(KFileItemModel::roleInformation(nullptr));
 
     for (const KFileItemModel::RoleInfo &info : rolesInfo) {
         if (!isSortGroup && !isGroupGroup && info.role == "text") {
index ea89dc4f5c5f42298bea94bb2e44447b21c83587..c5afe663d6fd4ab1a0fee13491a0e8fbcb4eb0a2 100644 (file)
@@ -410,6 +410,8 @@ void ViewProperties::setDirProperties(const ViewProperties &props)
     setGroupedSorting(props.groupedSorting());
     setSortRole(props.sortRole());
     setSortOrder(props.sortOrder());
+    setGroupRole(props.groupRole());
+    setGroupOrder(props.groupOrder());
     setSortFoldersFirst(props.sortFoldersFirst());
     setSortHiddenLast(props.sortHiddenLast());
     setVisibleRoles(props.visibleRoles());