diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index e7afa5288d..5b7b781a8f 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -39,6 +39,7 @@ KFileItemModel::KFileItemModel(QObject *parent) , m_sortDirsFirst(true) , m_sortHiddenLast(false) , m_sortRole(NameRole) + , m_groupRole(NoRole) , m_sortingProgressPercent(-1) , m_roles() , m_itemData() @@ -2439,6 +2440,7 @@ int KFileItemModel::stringCompare(const QString &a, const QString &b, const QCol KFileItemModel::ItemGroupInfo KFileItemModel::nameRoleGroup(const ItemData *itemData, bool withString) const { + static bool oldWithString; static ItemGroupInfo oldGroupInfo; static QChar oldFirstChar; ItemGroupInfo groupInfo; @@ -2450,8 +2452,8 @@ KFileItemModel::ItemGroupInfo KFileItemModel::nameRoleGroup(const ItemData *item // Use the first character of the name as group indication firstChar = name.at(0).toUpper(); - - if (firstChar == oldFirstChar) { + + if (firstChar == oldFirstChar && withString == oldWithString) { return oldGroupInfo; } if (firstChar == QLatin1Char('~') && name.length() > 1) { @@ -2506,6 +2508,7 @@ KFileItemModel::ItemGroupInfo KFileItemModel::nameRoleGroup(const ItemData *item } groupInfo.comparable = (int)'.'; } + oldWithString = withString; oldFirstChar = firstChar; oldGroupInfo = groupInfo; return groupInfo; @@ -2547,6 +2550,7 @@ KFileItemModel::ItemGroupInfo KFileItemModel::sizeRoleGroup(const ItemData *item KFileItemModel::ItemGroupInfo KFileItemModel::timeRoleGroup(const std::function &fileTimeCb, const ItemData *itemData, bool withString) const { + static bool oldWithString; static ItemGroupInfo oldGroupInfo; static QDate oldFileDate; ItemGroupInfo groupInfo; @@ -2556,6 +2560,9 @@ KFileItemModel::timeRoleGroup(const std::function & const QDate fileDate = fileTime.date(); const int daysDistance = fileDate.daysTo(currentDate); + if (fileDate == oldFileDate && withString == oldWithString) { + return oldGroupInfo; + } // Simplified grouping algorithm, preserving dates // but not taking "pretty printing" into account if (currentDate.year() == fileDate.year() && currentDate.month() == fileDate.month()) { @@ -2759,6 +2766,7 @@ KFileItemModel::timeRoleGroup(const std::function & } } } + oldWithString = withString; oldFileDate = fileDate; oldGroupInfo = groupInfo; return groupInfo; @@ -2766,13 +2774,14 @@ KFileItemModel::timeRoleGroup(const std::function & KFileItemModel::ItemGroupInfo KFileItemModel::permissionRoleGroup(const ItemData *itemData, bool withString) const { + static bool oldWithString; static ItemGroupInfo oldGroupInfo; static QFileDevice::Permissions oldPermissions; ItemGroupInfo groupInfo; const QFileInfo info(itemData->item.url().toLocalFile()); const QFileDevice::Permissions permissions = info.permissions(); - if (permissions == oldPermissions) { + if (permissions == oldPermissions && withString == oldWithString) { return oldGroupInfo; } groupInfo.comparable = (int)permissions; @@ -2818,6 +2827,7 @@ KFileItemModel::ItemGroupInfo KFileItemModel::permissionRoleGroup(const ItemData others = others.isEmpty() ? i18nc("@item:intext Access permission, concatenated", "Forbidden") : others.mid(0, others.length() - 2); groupInfo.text = i18nc("@title:group Files and folders by permissions", "User: %1 | Group: %2 | Others: %3", user, group, others); } + oldWithString = withString; oldPermissions = permissions; oldGroupInfo = groupInfo; return groupInfo; diff --git a/src/kitemviews/kitemmodelbase.cpp b/src/kitemviews/kitemmodelbase.cpp index 2f8e1e4a30..17716f57f5 100644 --- a/src/kitemviews/kitemmodelbase.cpp +++ b/src/kitemviews/kitemmodelbase.cpp @@ -10,7 +10,7 @@ KItemModelBase::KItemModelBase(QObject *parent) : QObject(parent) - , m_groupedSorting(false) + , m_groupedSorting(true) , m_sortRole() , m_sortOrder(Qt::AscendingOrder) , m_groupRole() @@ -20,7 +20,7 @@ KItemModelBase::KItemModelBase(QObject *parent) KItemModelBase::KItemModelBase(const QByteArray &sortRole, const QByteArray &groupRole, QObject *parent) : QObject(parent) - , m_groupedSorting(false) + , m_groupedSorting(true) , m_sortRole(sortRole) , m_sortOrder(Qt::AscendingOrder) , m_groupRole(groupRole) diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp index fad825dc01..ea05f840b7 100644 --- a/src/tests/kfileitemmodeltest.cpp +++ b/src/tests/kfileitemmodeltest.cpp @@ -164,7 +164,7 @@ void KFileItemModelTest::testDefaultSortRole() void KFileItemModelTest::testDefaultGroupedSorting() { - QCOMPARE(m_model->groupedSorting(), false); + QCOMPARE(m_model->groupedSorting(), true); } void KFileItemModelTest::testNewItems() @@ -2007,6 +2007,7 @@ void KFileItemModelTest::testNameRoleGroups() m_testDir->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"}); m_model->setGroupedSorting(true); + m_model->setGroupRole("text"); m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); QCOMPARE(itemsInModel(), @@ -2093,6 +2094,8 @@ void KFileItemModelTest::testNameRoleGroupsWithExpandedItems() m_testDir->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"}); m_model->setGroupedSorting(true); + m_model->setGroupRole("text"); + m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); QCOMPARE(itemsInModel(), @@ -2102,6 +2105,7 @@ void KFileItemModelTest::testNameRoleGroupsWithExpandedItems() QList> expectedGroups; expectedGroups << QPair(0, QLatin1String("A")); expectedGroups << QPair(1, QLatin1String("D")); + QCOMPARE(m_model->groups(), expectedGroups); // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").