Skip to content

Commit 2a10dc2

Browse files
milianwlievenhey
authored andcommitted
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 d5bdab3 commit 2a10dc2

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
@@ -188,6 +188,8 @@ QVariant EventModel::data(const QModelIndex& index, int role) const
188188
}
189189
} else if (role == SortRole) {
190190
return index.row();
191+
} else if (role == IsFavoritesSectionRole) {
192+
return index.row() == OverviewRow::FavoriteRow;
191193
}
192194
return {};
193195
} 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;
@@ -32,3 +33,18 @@ bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source
3233

3334
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
3435
}
36+
37+
bool EventModelProxy::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
38+
{
39+
const auto lhsIsFavoritesSection = source_left.data(EventModel::IsFavoritesSectionRole).toBool();
40+
const auto rhsIsFavoritesSection = source_right.data(EventModel::IsFavoritesSectionRole).toBool();
41+
if (lhsIsFavoritesSection != rhsIsFavoritesSection) {
42+
// always put the favorites section on the top
43+
if (sortOrder() == Qt::AscendingOrder)
44+
return lhsIsFavoritesSection > rhsIsFavoritesSection;
45+
else
46+
return lhsIsFavoritesSection < rhsIsFavoritesSection;
47+
}
48+
49+
return QSortFilterProxyModel::lessThan(source_left, source_right);
50+
}

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
@@ -614,7 +614,26 @@ private slots:
614614
model.addToFavorites(model.index(0, 0, processesIndex));
615615
QCOMPARE(proxy.rowCount(), 3);
616616

617+
{
618+
// verify that favorites remain at the top
619+
QCOMPARE(proxy.sortOrder(), Qt::AscendingOrder);
620+
QCOMPARE(proxy.sortColumn(), 0);
621+
622+
// favorites on top
623+
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
624+
// followed by CPUs
625+
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::CpuIdRole).value<quint32>(), 1);
626+
627+
proxy.sort(0, Qt::DescendingOrder);
628+
629+
// favorites are still on top
630+
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
631+
// followed by processes
632+
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::ProcessIdRole).value<quint32>(), 5678);
633+
}
634+
617635
model.removeFromFavorites(model.index(0, 0, favoritesIndex));
636+
618637
QCOMPARE(proxy.rowCount(), 2);
619638
}
620639

@@ -865,9 +884,9 @@ private slots:
865884
{
866885
Data::EventResults events;
867886
events.cpus.resize(3);
868-
events.cpus[0].cpuId = 0;
869-
events.cpus[1].cpuId = 1; // empty
870-
events.cpus[2].cpuId = 2;
887+
events.cpus[0].cpuId = 1;
888+
events.cpus[1].cpuId = 2; // empty
889+
events.cpus[2].cpuId = 3;
871890

872891
const quint64 endTime = 1000;
873892
const quint64 deltaTime = 10;
@@ -910,13 +929,13 @@ private slots:
910929
event.time = time;
911930
++costSummary.sampleCount;
912931
costSummary.totalPeriod += event.cost;
913-
events.cpus[cpuId].events << event;
932+
events.cpus[cpuId - 1].events << event;
914933
return event;
915934
};
916935
for (quint64 time = 0; time < endTime; time += deltaTime) {
917-
thread1.events << generateEvent(time, 0);
936+
thread1.events << generateEvent(time, 1);
918937
if (thread2.time.contains(time)) {
919-
thread2.events << generateEvent(time, 2);
938+
thread2.events << generateEvent(time, 3);
920939
}
921940
}
922941
events.totalCosts = {costSummary};

0 commit comments

Comments
 (0)