Skip to content

Commit 28e0037

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 ca13e89 commit 28e0037

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
@@ -611,7 +611,26 @@ private slots:
611611
model.addToFavorites(model.index(0, 0, processesIndex));
612612
QCOMPARE(proxy.rowCount(), 3);
613613

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

@@ -862,9 +881,9 @@ private slots:
862881
{
863882
Data::EventResults events;
864883
events.cpus.resize(3);
865-
events.cpus[0].cpuId = 0;
866-
events.cpus[1].cpuId = 1; // empty
867-
events.cpus[2].cpuId = 2;
884+
events.cpus[0].cpuId = 1;
885+
events.cpus[1].cpuId = 2; // empty
886+
events.cpus[2].cpuId = 3;
868887

869888
const quint64 endTime = 1000;
870889
const quint64 deltaTime = 10;
@@ -907,13 +926,13 @@ private slots:
907926
event.time = time;
908927
++costSummary.sampleCount;
909928
costSummary.totalPeriod += event.cost;
910-
events.cpus[cpuId].events << event;
929+
events.cpus[cpuId - 1].events << event;
911930
return event;
912931
};
913932
for (quint64 time = 0; time < endTime; time += deltaTime) {
914-
thread1.events << generateEvent(time, 0);
933+
thread1.events << generateEvent(time, 1);
915934
if (thread2.time.contains(time)) {
916-
thread2.events << generateEvent(time, 2);
935+
thread2.events << generateEvent(time, 3);
917936
}
918937
}
919938
events.totalCosts = {costSummary};

0 commit comments

Comments
 (0)