Skip to content

Commit b2ebd1f

Browse files
milianwlievenhey
authored andcommitted
feat: Always put the favorite contents on the top of the view
This way we can more easily find them and changing the sort order doesn't move them to the bottom.
1 parent fe4ff35 commit b2ebd1f

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

src/models/eventmodel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ QVariant EventModel::data(const QModelIndex& index, int role) const
187187
}
188188
} else if (role == SortRole) {
189189
return index.row();
190+
} else if (role == IsFavoritesSectionRole) {
191+
return index.row() == OverviewRow::FavoriteRow;
190192
}
191193
return {};
192194
} else if (tag == Tag::Processes) {

src/models/eventmodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class EventModel : public QAbstractItemModel
4343
TotalCostsRole,
4444
EventResultsRole,
4545
IsFavoriteRole,
46+
IsFavoritesSectionRole,
4647
};
4748

4849
int rowCount(const QModelIndex& parent = {}) const override;

src/models/eventmodelproxy.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ EventModelProxy::EventModelProxy(QObject* parent)
1616
setSortRole(EventModel::SortRole);
1717
setFilterKeyColumn(EventModel::ThreadColumn);
1818
setFilterRole(Qt::DisplayRole);
19+
sort(0);
1920
}
2021

2122
EventModelProxy::~EventModelProxy() = default;
@@ -41,3 +42,18 @@ bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source
4142

4243
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
4344
}
45+
46+
bool EventModelProxy::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
47+
{
48+
const auto lhsIsFavoritesSection = source_left.data(EventModel::IsFavoritesSectionRole).toBool();
49+
const auto rhsIsFavoritesSection = source_right.data(EventModel::IsFavoritesSectionRole).toBool();
50+
if (lhsIsFavoritesSection != rhsIsFavoritesSection) {
51+
// always put the favorites section on the top
52+
if (sortOrder() == Qt::AscendingOrder)
53+
return lhsIsFavoritesSection > rhsIsFavoritesSection;
54+
else
55+
return lhsIsFavoritesSection < rhsIsFavoritesSection;
56+
}
57+
58+
return QSortFilterProxyModel::lessThan(source_left, source_right);
59+
}

src/models/eventmodelproxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ class EventModelProxy : public QSortFilterProxyModel
1818

1919
protected:
2020
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
21+
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;
2122
};

tests/modeltests/tst_models.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,26 @@ private slots:
701701

702702
QCOMPARE(proxy.rowCount(), 3);
703703

704+
{
705+
// verify that favorites remain at the top
706+
QCOMPARE(proxy.sortOrder(), Qt::AscendingOrder);
707+
QCOMPARE(proxy.sortColumn(), 0);
708+
709+
// favorites on top
710+
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
711+
// followed by CPUs
712+
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::CpuIdRole).value<quint32>(), 1);
713+
714+
proxy.sort(0, Qt::DescendingOrder);
715+
716+
// favorites are still on top
717+
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
718+
// followed by processes
719+
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::ProcessIdRole).value<quint32>(), 5678);
720+
}
721+
704722
model.removeFromFavorites(model.index(0, 0, favoritesIndex));
723+
705724
QCOMPARE(proxy.rowCount(), 2);
706725
}
707726

@@ -952,9 +971,9 @@ private slots:
952971
{
953972
Data::EventResults events;
954973
events.cpus.resize(3);
955-
events.cpus[0].cpuId = 0;
956-
events.cpus[1].cpuId = 1; // empty
957-
events.cpus[2].cpuId = 2;
974+
events.cpus[0].cpuId = 1;
975+
events.cpus[1].cpuId = 2; // empty
976+
events.cpus[2].cpuId = 3;
958977

959978
const quint64 endTime = 1000;
960979
const quint64 deltaTime = 10;
@@ -997,13 +1016,13 @@ private slots:
9971016
event.time = time;
9981017
++costSummary.sampleCount;
9991018
costSummary.totalPeriod += event.cost;
1000-
events.cpus[cpuId].events << event;
1019+
events.cpus[cpuId - 1].events << event;
10011020
return event;
10021021
};
10031022
for (quint64 time = 0; time < endTime; time += deltaTime) {
1004-
thread1.events << generateEvent(time, 0);
1023+
thread1.events << generateEvent(time, 1);
10051024
if (thread2.time.contains(time)) {
1006-
thread2.events << generateEvent(time, 2);
1025+
thread2.events << generateEvent(time, 3);
10071026
}
10081027
}
10091028
events.totalCosts = {costSummary};

0 commit comments

Comments
 (0)