Skip to content

Commit d5bdab3

Browse files
committed
add QSFP to hide empty rows in eventmodel
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.
1 parent dfbdb76 commit d5bdab3

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

src/models/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(
1010
disassemblymodel.cpp
1111
disassemblyoutput.cpp
1212
eventmodel.cpp
13+
eventmodelproxy.cpp
1314
filterandzoomstack.cpp
1415
formattingutils.cpp
1516
frequencymodel.cpp

src/models/eventmodelproxy.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
SPDX-FileCopyrightText: Lieven Hey <[email protected]>
3+
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
4+
5+
SPDX-License-Identifier: GPL-2.0-or-later
6+
*/
7+
8+
#include "eventmodelproxy.h"
9+
#include "eventmodel.h"
10+
11+
EventModelProxy::EventModelProxy(QObject* parent)
12+
: QSortFilterProxyModel(parent)
13+
{
14+
setDynamicSortFilter(true);
15+
setRecursiveFilteringEnabled(true);
16+
setSortRole(EventModel::SortRole);
17+
setFilterKeyColumn(EventModel::ThreadColumn);
18+
setFilterRole(Qt::DisplayRole);
19+
}
20+
21+
EventModelProxy::~EventModelProxy() = default;
22+
23+
bool EventModelProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
24+
{
25+
// index is invalid -> we are at the root node
26+
// hide categories that have no children (e.g. favorites, tracepoints)
27+
if (!source_parent.isValid()) {
28+
const auto model = sourceModel();
29+
if (!model->hasChildren(model->index(source_row, 0)))
30+
return false;
31+
}
32+
33+
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
34+
}

src/models/eventmodelproxy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
SPDX-FileCopyrightText: Lieven Hey <[email protected]>
3+
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
4+
5+
SPDX-License-Identifier: GPL-2.0-or-later
6+
*/
7+
8+
#pragma once
9+
10+
#include <QSortFilterProxyModel>
11+
12+
class EventModelProxy : public QSortFilterProxyModel
13+
{
14+
Q_OBJECT
15+
public:
16+
explicit EventModelProxy(QObject* parent = nullptr);
17+
~EventModelProxy() override;
18+
19+
protected:
20+
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
21+
};

src/timelinewidget.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "filterandzoomstack.h"
1111
#include "models/eventmodel.h"
12+
#include "models/eventmodelproxy.h"
1213
#include "resultsutil.h"
1314
#include "timelinedelegate.h"
1415

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

6364
auto* eventModel = new EventModel(this);
64-
auto* timeLineProxy = new QSortFilterProxyModel(this);
65-
timeLineProxy->setRecursiveFilteringEnabled(true);
65+
auto* timeLineProxy = new EventModelProxy(this);
6666
timeLineProxy->setSourceModel(eventModel);
67-
timeLineProxy->setSortRole(EventModel::SortRole);
68-
timeLineProxy->setFilterKeyColumn(EventModel::ThreadColumn);
69-
timeLineProxy->setFilterRole(Qt::DisplayRole);
7067
ResultsUtil::connectFilter(ui->timeLineSearch, timeLineProxy);
7168
ui->timeLineView->setModel(timeLineProxy);
7269
ui->timeLineView->setSortingEnabled(true);

tests/modeltests/tst_models.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <models/disassemblymodel.h>
2020
#include <models/eventmodel.h>
21+
#include <models/eventmodelproxy.h>
2122
#include <models/sourcecodemodel.h>
2223

2324
namespace {
@@ -589,6 +590,34 @@ private slots:
589590
QCOMPARE(model.rowCount(favoritesIndex), 0);
590591
}
591592

593+
void testEventModelProxy()
594+
{
595+
const auto events = createEventModelTestData();
596+
EventModel model;
597+
QAbstractItemModelTester tester(&model);
598+
model.setData(events);
599+
600+
EventModelProxy proxy;
601+
proxy.setSourceModel(&model);
602+
603+
const auto favoritesIndex = model.index(3, 0);
604+
const auto processesIndex = model.index(1, 0);
605+
606+
QCOMPARE(model.rowCount(), 4);
607+
QCOMPARE(proxy.rowCount(), 2);
608+
609+
proxy.setFilterRegularExpression(QStringLiteral("this does not match"));
610+
QCOMPARE(proxy.rowCount(), 0);
611+
proxy.setFilterRegularExpression(QString());
612+
QCOMPARE(proxy.rowCount(), 2);
613+
614+
model.addToFavorites(model.index(0, 0, processesIndex));
615+
QCOMPARE(proxy.rowCount(), 3);
616+
617+
model.removeFromFavorites(model.index(0, 0, favoritesIndex));
618+
QCOMPARE(proxy.rowCount(), 2);
619+
}
620+
592621
void testPrettySymbol_data()
593622
{
594623
QTest::addColumn<QString>("prettySymbol");

0 commit comments

Comments
 (0)