// Unlike sortRoleCompare, this function can and often will return 0.
int result = 0;
- int groupA, groupB;
+ ItemGroupInfo groupA, groupB;
switch (m_groupRole) {
case NoRole:
// 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;
+ groupA = nameRoleGroup(a, false);
+ groupB = nameRoleGroup(b, false);
break;
case SizeRole:
- groupA = sizeRoleGroup(a, false).comparable;
- groupB = sizeRoleGroup(b, false).comparable;
+ groupA = sizeRoleGroup(a, false);
+ groupB = sizeRoleGroup(b, false);
break;
case ModificationTimeRole:
groupA = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::ModificationTime);
- },
- a,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::ModificationTime);
+ },
+ a,
+ false);
groupB = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::ModificationTime);
- },
- b,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::ModificationTime);
+ },
+ b,
+ false);
break;
case CreationTimeRole:
groupA = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::CreationTime);
- },
- a,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::CreationTime);
+ },
+ a,
+ false);
groupB = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::CreationTime);
- },
- b,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::CreationTime);
+ },
+ b,
+ false);
break;
case AccessTimeRole:
groupA = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::AccessTime);
- },
- a,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::AccessTime);
+ },
+ a,
+ false);
groupB = timeRoleGroup(
- [](const ItemData *item) {
- return item->item.time(KFileItem::AccessTime);
- },
- b,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->item.time(KFileItem::AccessTime);
+ },
+ b,
+ false);
break;
case DeletionTimeRole:
groupA = timeRoleGroup(
- [](const ItemData *item) {
- return item->values.value("deletiontime").toDateTime();
- },
- a,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->values.value("deletiontime").toDateTime();
+ },
+ a,
+ false);
groupB = timeRoleGroup(
- [](const ItemData *item) {
- return item->values.value("deletiontime").toDateTime();
- },
- b,
- false)
- .comparable;
+ [](const ItemData *item) {
+ return item->values.value("deletiontime").toDateTime();
+ },
+ b,
+ false);
break;
case PermissionsRole:
- groupA = permissionRoleGroup(a, false).comparable;
- groupB = permissionRoleGroup(b, false).comparable;
+ groupA = permissionRoleGroup(a, false);
+ groupB = permissionRoleGroup(b, false);
break;
case RatingRole:
- groupA = ratingRoleGroup(a, false).comparable;
- groupB = ratingRoleGroup(b, false).comparable;
+ groupA = ratingRoleGroup(a, false);
+ groupB = ratingRoleGroup(b, false);
+ break;
+ case TypeRole:
+ groupA = typeRoleGroup(a);
+ groupB = typeRoleGroup(b);
break;
default: {
- QString strGroupA = genericStringRoleGroup(groupRole(), a);
- QString strGroupB = genericStringRoleGroup(groupRole(), b);
- result = stringCompare(strGroupA, strGroupB, collator);
+ groupA = genericStringRoleGroup(groupRole(), a);
+ groupB = genericStringRoleGroup(groupRole(), b);
break;
}
}
- if (result == 0) {
- if (groupA < groupB) {
- result = -1;
- } else if (groupA > groupB) {
- result = 1;
- }
+ if (groupA.comparable < groupB.comparable) {
+ result = -1;
+ } else if (groupA.comparable > groupB.comparable) {
+ result = 1;
+ } else {
+ result = stringCompare(groupA.text, groupB.text, collator);
}
return result;
}
return groupInfo;
}
-QString KFileItemModel::genericStringRoleGroup(const QByteArray &role, const ItemData *itemData) const
+KFileItemModel::ItemGroupInfo KFileItemModel::genericStringRoleGroup(const QByteArray &role, const ItemData *itemData) const
{
- return itemData->values.value(role).toString();
+ return {0, itemData->values.value(role).toString()};
}
QList<QPair<int, QVariant>> KFileItemModel::nameRoleGroups() const
return groups;
}
+KFileItemModel::ItemGroupInfo KFileItemModel::typeRoleGroup(const ItemData *itemData) const
+{
+ int priority = 0;
+ if (itemData->item.isDir() && m_sortDirsFirst) {
+ // Ensure folders stay first regardless of grouping order
+ if (groupOrder() == Qt::AscendingOrder) {
+ priority = -1;
+ } else {
+ priority = 1;
+ }
+ }
+ return {priority, itemData->values.value("type").toString()};
+}
+
QList<QPair<int, QVariant>> KFileItemModel::timeRoleGroups(const std::function<QDateTime(const ItemData *)> &fileTimeCb) const
{
Q_ASSERT(!m_itemData.isEmpty());
const int maxIndex = count() - 1;
QList<QPair<int, QVariant>> groups;
- QString groupText;
+ ItemGroupInfo groupInfo;
for (int i = 0; i <= maxIndex; ++i) {
if (isChildItem(i)) {
continue;
}
- QString newGroupText = genericStringRoleGroup(role, m_itemData.at(i));
+ ItemGroupInfo newGroupInfo = genericStringRoleGroup(role, m_itemData.at(i));
- if (newGroupText != groupText) {
- groupText = newGroupText;
- groups.append(QPair<int, QVariant>(i, newGroupText));
+ if (newGroupInfo != groupInfo) {
+ groupInfo = newGroupInfo;
+ groups.append(QPair<int, QVariant>(i, newGroupInfo.text));
}
}
return groups;