Skip to content

Commit

Permalink
Always put the favorite contents on the top of the view
Browse files Browse the repository at this point in the history
This way we can more easily find them and changing the sort order
doesn't move them to the bottom.
  • Loading branch information
milianw committed Oct 4, 2023
1 parent 0fb1122 commit 52dc89b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/models/eventmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ QVariant EventModel::data(const QModelIndex& index, int role) const
}
} else if (role == SortRole) {
return index.row();
} else if (role == IsFavoritesSectionRole) {
return index.row() == OverviewRow::FavoriteRow;
}
return {};
} else if (tag == Tag::Processes) {
Expand Down
1 change: 1 addition & 0 deletions src/models/eventmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class EventModel : public QAbstractItemModel
TotalCostsRole,
EventResultsRole,
IsFavoriteRole,
IsFavoritesSectionRole,
};

int rowCount(const QModelIndex& parent = {}) const override;
Expand Down
16 changes: 16 additions & 0 deletions src/models/eventmodelproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EventModelProxy::EventModelProxy(QObject* parent)
setSortRole(EventModel::SortRole);
setFilterKeyColumn(EventModel::ThreadColumn);
setFilterRole(Qt::DisplayRole);
sort(0);
}

EventModelProxy::~EventModelProxy() = default;
Expand All @@ -32,3 +33,18 @@ bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source

return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}

bool EventModelProxy::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
{
const auto lhsIsFavoritesSection = source_left.data(EventModel::IsFavoritesSectionRole).toBool();
const auto rhsIsFavoritesSection = source_right.data(EventModel::IsFavoritesSectionRole).toBool();
if (lhsIsFavoritesSection != rhsIsFavoritesSection) {
// always put the favorites section on the top
if (sortOrder() == Qt::AscendingOrder)
return lhsIsFavoritesSection > rhsIsFavoritesSection;
else
return lhsIsFavoritesSection < rhsIsFavoritesSection;
}

return QSortFilterProxyModel::lessThan(source_left, source_right);
}
1 change: 1 addition & 0 deletions src/models/eventmodelproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class EventModelProxy : public QSortFilterProxyModel

protected:
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;
};
31 changes: 25 additions & 6 deletions tests/modeltests/tst_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,26 @@ private slots:
model.addToFavorites(model.index(0, 0, processesIndex));
QCOMPARE(proxy.rowCount(), 3);

{
// verify that favorites remain at the top
QCOMPARE(proxy.sortOrder(), Qt::AscendingOrder);
QCOMPARE(proxy.sortColumn(), 0);

// favorites on top
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
// followed by CPUs
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::CpuIdRole).value<quint32>(), 1);

proxy.sort(0, Qt::DescendingOrder);

// favorites are still on top
QVERIFY(proxy.index(0, 0, proxy.index(0, 0)).data(EventModel::IsFavoriteRole).toBool());
// followed by processes
QCOMPARE(proxy.index(0, 0, proxy.index(1, 0)).data(EventModel::ProcessIdRole).value<quint32>(), 5678);
}

model.removeFromFavorites(model.index(0, 0, favoritesIndex));

QCOMPARE(proxy.rowCount(), 2);
}

Expand Down Expand Up @@ -862,9 +881,9 @@ private slots:
{
Data::EventResults events;
events.cpus.resize(3);
events.cpus[0].cpuId = 0;
events.cpus[1].cpuId = 1; // empty
events.cpus[2].cpuId = 2;
events.cpus[0].cpuId = 1;
events.cpus[1].cpuId = 2; // empty
events.cpus[2].cpuId = 3;

const quint64 endTime = 1000;
const quint64 deltaTime = 10;
Expand Down Expand Up @@ -907,13 +926,13 @@ private slots:
event.time = time;
++costSummary.sampleCount;
costSummary.totalPeriod += event.cost;
events.cpus[cpuId].events << event;
events.cpus[cpuId - 1].events << event;
return event;
};
for (quint64 time = 0; time < endTime; time += deltaTime) {
thread1.events << generateEvent(time, 0);
thread1.events << generateEvent(time, 1);
if (thread2.time.contains(time)) {
thread2.events << generateEvent(time, 2);
thread2.events << generateEvent(time, 3);
}
}
events.totalCosts = {costSummary};
Expand Down

0 comments on commit 52dc89b

Please sign in to comment.