Skip to content

Commit

Permalink
allow event source combobox to select multiple costs
Browse files Browse the repository at this point in the history
This patch allows the combobox in the timelinedelegate to select
multiple costs as source.
  • Loading branch information
lievenhey committed Jan 15, 2024
1 parent 84d7904 commit 48c9f49
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/models/eventmodelproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ EventModelProxy::EventModelProxy(QObject* parent)

EventModelProxy::~EventModelProxy() = default;

void EventModelProxy::showCostId(qint32 costId)
{
m_hiddenCostIds.remove(costId);
invalidate();
}

void EventModelProxy::hideCostId(qint32 costId)
{
m_hiddenCostIds.insert(costId);
invalidate();
}

bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
// index is invalid -> we are at the root node
Expand All @@ -31,6 +43,15 @@ bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source
return false;
}

auto data = sourceModel()
->index(source_row, EventModel::EventsColumn, source_parent)
.data(EventModel::EventsRole)
.value<Data::Events>();

if (!data.empty() && m_hiddenCostIds.contains(data[0].type)) {
return false;
}

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

Expand Down
7 changes: 7 additions & 0 deletions src/models/eventmodelproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <QSet>
#include <QSortFilterProxyModel>

class EventModelProxy : public QSortFilterProxyModel
Expand All @@ -16,7 +17,13 @@ class EventModelProxy : public QSortFilterProxyModel
explicit EventModelProxy(QObject* parent = nullptr);
~EventModelProxy() override;

void showCostId(qint32 costId);
void hideCostId(qint32 costId);

protected:
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const override;

private:
QSet<qint32> m_hiddenCostIds;
};
32 changes: 32 additions & 0 deletions src/resultsutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <QTimer>
#include <QTreeView>

#include <QStandardItemModel>

#include "models/costdelegate.h"
#include "models/data.h"
#include "models/filterandzoomstack.h"
Expand Down Expand Up @@ -206,6 +208,36 @@ void fillEventSourceComboBox(QComboBox* combo, const Data::Costs& costs, const Q
}
}

void fillEventSourceComboBoxMultiSelect(QComboBox* combo, const Data::Costs& costs, const QString& /*tooltipTemplate*/)
{
// restore selection if possible
const auto oldData = combo->currentData();

combo->clear();

auto model = new QStandardItemModel(costs.numTypes(), 1, combo);
int itemCounter = 0;
for (int costId = 0, c = costs.numTypes(); costId < c; costId++) {
if (!costs.totalCost(costId)) {
continue;
}

auto item = new QStandardItem(costs.typeName(costId));
item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item->setData(Qt::Checked, Qt::CheckStateRole);
item->setData(costId, Qt::UserRole + 1);
model->setItem(itemCounter, item);
itemCounter++;
}
model->setRowCount(itemCounter);
combo->setModel(model);

const auto index = combo->findData(oldData);
if (index != -1) {
combo->setCurrentIndex(index);
}
}

void setupResultsAggregation(QComboBox* costAggregationComboBox)
{
struct AggregationType
Expand Down
1 change: 1 addition & 0 deletions src/resultsutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void hideEmptyColumns(const Data::Costs& costs, QTreeView* view, int numBaseColu
void hideTracepointColumns(const Data::Costs& costs, QTreeView* view, int numBaseColumns);

void fillEventSourceComboBox(QComboBox* combo, const Data::Costs& costs, const QString& tooltipTemplate);
void fillEventSourceComboBoxMultiSelect(QComboBox* combo, const Data::Costs& costs, const QString& tooltipTemplate);

void setupResultsAggregation(QComboBox* costAggregationComboBox);
}
23 changes: 20 additions & 3 deletions src/timelinewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include "parsers/perf/perfparser.h"

#include <QLabel>
#include <QListView>
#include <QPointer>
#include <QProgressBar>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QVBoxLayout>

#include <KLocalizedString>
Expand Down Expand Up @@ -82,9 +84,24 @@ TimeLineWidget::TimeLineWidget(PerfParser* parser, QMenu* filterMenu, FilterAndZ
connect(timeLineProxy, &QAbstractItemModel::rowsInserted, this, [this]() { ui->timeLineView->expandToDepth(1); });
connect(timeLineProxy, &QAbstractItemModel::modelReset, this, [this]() { ui->timeLineView->expandToDepth(1); });

connect(m_parser, &PerfParser::bottomUpDataAvailable, this, [this](const Data::BottomUpResults& data) {
ResultsUtil::fillEventSourceComboBox(ui->timeLineEventSource, data.costs, tr("Show timeline for %1 events."));
});
connect(m_parser, &PerfParser::bottomUpDataAvailable, this,
[this, timeLineProxy](const Data::BottomUpResults& data) {
ResultsUtil::fillEventSourceComboBoxMultiSelect(ui->timeLineEventSource, data.costs,
tr("Show timeline for %1 events."));

auto model = qobject_cast<QStandardItemModel*>(ui->timeLineEventSource->model());
connect(ui->timeLineEventSource->model(), &QStandardItemModel::dataChanged, model,
[timeLineProxy](const QModelIndex& topLeft, const QModelIndex& /*bottomRight*/,
const QVector<int>& /*roles*/) {
auto checkState = topLeft.data(Qt::CheckStateRole).value<Qt::CheckState>();

if (checkState == Qt::CheckState::Checked) {
timeLineProxy->showCostId(topLeft.data(Qt::UserRole + 1).toUInt());
} else {
timeLineProxy->hideCostId(topLeft.data(Qt::UserRole + 1).toUInt());
}
});
});

connect(m_parser, &PerfParser::eventsAvailable, this, [this, eventModel](const Data::EventResults& data) {
eventModel->setData(data);
Expand Down

0 comments on commit 48c9f49

Please sign in to comment.