Skip to content

Commit

Permalink
feat: add QSFP to hide empty rows in eventmodel
Browse files Browse the repository at this point in the history
The favourites and tracepoint patches include some rows in the model
that may be empty. To keep the code simple an readable all rows will be
shown. Then a proxy model is put ontop to remove empty rows.
  • Loading branch information
lievenhey committed May 29, 2024
1 parent 34df1ad commit c67c7a3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/models/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(
disassemblymodel.cpp
disassemblyoutput.cpp
eventmodel.cpp
eventmodelproxy.cpp
filterandzoomstack.cpp
formattingutils.cpp
frequencymodel.cpp
Expand Down
34 changes: 34 additions & 0 deletions src/models/eventmodelproxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: Lieven Hey <[email protected]>
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "eventmodelproxy.h"
#include "eventmodel.h"

EventModelProxy::EventModelProxy(QObject* parent)
: QSortFilterProxyModel(parent)
{
setDynamicSortFilter(true);
setRecursiveFilteringEnabled(true);
setSortRole(EventModel::SortRole);
setFilterKeyColumn(EventModel::ThreadColumn);
setFilterRole(Qt::DisplayRole);
}

EventModelProxy::~EventModelProxy() = default;

bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
// index is invalid -> we are at the root node
// hide categories that have no children (e.g. favorites, tracepoints)
if (!source_parent.isValid()) {
const auto model = sourceModel();
if (!model->hasChildren(model->index(source_row, 0)))
return false;
}

return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
21 changes: 21 additions & 0 deletions src/models/eventmodelproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
SPDX-FileCopyrightText: Lieven Hey <[email protected]>
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
SPDX-License-Identifier: GPL-2.0-or-later
*/

#pragma once

#include <QSortFilterProxyModel>

class EventModelProxy : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit EventModelProxy(QObject* parent = nullptr);
~EventModelProxy() override;

protected:
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
};
7 changes: 2 additions & 5 deletions src/timelinewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "filterandzoomstack.h"
#include "models/eventmodel.h"
#include "models/eventmodelproxy.h"
#include "resultsutil.h"
#include "timelinedelegate.h"

Expand Down Expand Up @@ -61,12 +62,8 @@ TimeLineWidget::TimeLineWidget(PerfParser* parser, QMenu* filterMenu, FilterAndZ
ui->setupUi(this);

auto* eventModel = new EventModel(this);
auto* timeLineProxy = new QSortFilterProxyModel(this);
timeLineProxy->setRecursiveFilteringEnabled(true);
auto* timeLineProxy = new EventModelProxy(this);
timeLineProxy->setSourceModel(eventModel);
timeLineProxy->setSortRole(EventModel::SortRole);
timeLineProxy->setFilterKeyColumn(EventModel::ThreadColumn);
timeLineProxy->setFilterRole(Qt::DisplayRole);
ResultsUtil::connectFilter(ui->timeLineSearch, timeLineProxy);
ui->timeLineView->setModel(timeLineProxy);
ui->timeLineView->setSortingEnabled(true);
Expand Down
29 changes: 29 additions & 0 deletions tests/modeltests/tst_models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <models/disassemblymodel.h>
#include <models/eventmodel.h>
#include <models/eventmodelproxy.h>
#include <models/sourcecodemodel.h>

namespace {
Expand Down Expand Up @@ -673,6 +674,34 @@ private slots:
QCOMPARE(model.rowCount(favoritesIndex), 0);
}

void testEventModelProxy()
{
const auto events = createEventModelTestData();
EventModel model;
QAbstractItemModelTester tester(&model);
model.setData(events);

EventModelProxy proxy;
proxy.setSourceModel(&model);

const auto favoritesIndex = model.index(3, 0);
const auto processesIndex = model.index(1, 0);

QCOMPARE(model.rowCount(), 4);
QCOMPARE(proxy.rowCount(), 2);

proxy.setFilterRegularExpression(QStringLiteral("this does not match"));
QCOMPARE(proxy.rowCount(), 0);
proxy.setFilterRegularExpression(QString());
QCOMPARE(proxy.rowCount(), 2);

model.addToFavorites(model.index(0, 0, processesIndex));
QCOMPARE(proxy.rowCount(), 3);

model.removeFromFavorites(model.index(0, 0, favoritesIndex));
QCOMPARE(proxy.rowCount(), 2);
}

void testPrettySymbol_data()
{
QTest::addColumn<QString>("prettySymbol");
Expand Down

0 comments on commit c67c7a3

Please sign in to comment.