fix: order date groups chronologically in EmailTreeModel
- Groups now sorted by representative date (newest first) instead of alphabetically - 'Hoy', 'Ayer', 'Esta semana', 'Semana pasada', 'Hace dos semanas', 'Este mes', meses, años — en orden cronológico correcto
This commit is contained in:
@@ -251,31 +251,55 @@ void EmailTreeModel::buildGroups()
|
|||||||
return dateA > dateB;
|
return dateA > dateB;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Group by date
|
// Group by date - store both groupName and representative date for sorting
|
||||||
QMap<QString, QVector<int>> groups; // groupName -> mail indices
|
struct GroupData {
|
||||||
|
QString name;
|
||||||
|
QDateTime representativeDate; // earliest date in group for sorting
|
||||||
|
QVector<int> mailIndices;
|
||||||
|
};
|
||||||
|
QMap<QString, GroupData> groupsMap;
|
||||||
|
|
||||||
for (int idx : indices) {
|
for (int idx : indices) {
|
||||||
if (idx < 0 || idx >= m_emails.size()) continue;
|
if (idx < 0 || idx >= m_emails.size()) continue;
|
||||||
const QDateTime& dt = m_emails[idx].date();
|
const QDateTime& dt = m_emails[idx].date();
|
||||||
if (!dt.isValid()) continue;
|
if (!dt.isValid()) continue;
|
||||||
|
|
||||||
QString groupName = groupNameForDate(dt);
|
QString groupName = groupNameForDate(dt);
|
||||||
groups[groupName].append(idx);
|
auto& g = groupsMap[groupName];
|
||||||
|
g.name = groupName;
|
||||||
|
g.mailIndices.append(idx);
|
||||||
|
// Keep the earliest (most recent) date as representative for sorting groups chronologically
|
||||||
|
if (!g.representativeDate.isValid() || dt > g.representativeDate) {
|
||||||
|
g.representativeDate = dt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create tree items in group order
|
// Sort groups by representative date descending (newest group first)
|
||||||
int groupIdx = 0;
|
QVector<GroupData> sortedGroups;
|
||||||
for (auto it = groups.constBegin(); it != groups.constEnd(); ++it) {
|
sortedGroups.reserve(groupsMap.size());
|
||||||
const QString& groupName = it.key();
|
for (auto it = groupsMap.constBegin(); it != groupsMap.constEnd(); ++it) {
|
||||||
const QVector<int>& mailIndices = it.value();
|
if (!it.value().mailIndices.isEmpty()) {
|
||||||
if (mailIndices.isEmpty()) continue;
|
sortedGroups.append(it.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::sort(sortedGroups.begin(), sortedGroups.end(), [](const GroupData& a, const GroupData& b) {
|
||||||
|
// Newest group first (descending by representative date)
|
||||||
|
if (!a.representativeDate.isValid() && !b.representativeDate.isValid()) return false;
|
||||||
|
if (!a.representativeDate.isValid()) return false;
|
||||||
|
if (!b.representativeDate.isValid()) return true;
|
||||||
|
return a.representativeDate > b.representativeDate;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create tree items in chronological group order
|
||||||
|
int groupIdx = 0;
|
||||||
|
for (const GroupData& g : sortedGroups) {
|
||||||
TreeItem* groupItem = new TreeItem(TreeItem::Group, m_rootItem);
|
TreeItem* groupItem = new TreeItem(TreeItem::Group, m_rootItem);
|
||||||
groupItem->groupName = groupName;
|
groupItem->groupName = g.name;
|
||||||
groupItem->groupIndex = groupIdx++;
|
groupItem->groupIndex = groupIdx++;
|
||||||
groupItem->row = m_rootItem->children.size();
|
groupItem->row = m_rootItem->children.size();
|
||||||
m_rootItem->children.append(groupItem);
|
m_rootItem->children.append(groupItem);
|
||||||
|
|
||||||
for (int mailIdx : mailIndices) {
|
for (int mailIdx : g.mailIndices) {
|
||||||
if (mailIdx < 0 || mailIdx >= m_emails.size()) continue;
|
if (mailIdx < 0 || mailIdx >= m_emails.size()) continue;
|
||||||
TreeItem* mailItem = new TreeItem(TreeItem::Mail, groupItem);
|
TreeItem* mailItem = new TreeItem(TreeItem::Mail, groupItem);
|
||||||
mailItem->mailIndex = mailIdx;
|
mailItem->mailIndex = mailIdx;
|
||||||
|
|||||||
Reference in New Issue
Block a user