-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwarningproxymodel.cpp
129 lines (105 loc) · 4.25 KB
/
warningproxymodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This file is part of warning-viewer.
Copyright (C) 2015 Sergio Martins <smartins@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "warningproxymodel.h"
#include "settings.h"
#include <QRegularExpression>
#include <QDebug>
WarningProxyModel::WarningProxyModel(WarningModel *model, Settings *settings, QObject *parent)
: QSortFilterProxyModel(parent)
, m_settings(settings)
, m_availableWarningFilterRegex(settings->warningFilterRegexp())
{
connect(this, &WarningProxyModel::rowsInserted, this, &WarningProxyModel::countChanged);
connect(this, &WarningProxyModel::rowsRemoved, this, &WarningProxyModel::countChanged);
connect(this, &WarningProxyModel::modelReset, this, &WarningProxyModel::countChanged);
connect(this, &WarningProxyModel::layoutChanged, this, &WarningProxyModel::countChanged);
connect(model, &WarningModel::loadFinished, this, &WarningProxyModel::onSourceModelLoaded);
connect(m_settings, &Settings::warningFilterRegexpChanged, this, &WarningProxyModel::setAvailableWarningFilterRegex);
}
bool WarningProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (source_parent.isValid())
return false;
if (m_acceptedWarnings.isEmpty())
return false;
const QModelIndex sourceIndex = sourceModel()->index(source_row, 0);
const Warning warn = sourceIndex.data(WarningModel::WarningRole).value<Warning>();
const QString warningName = warn.m_warningName;
if (!m_acceptedWarnings.isEmpty() && !m_acceptedWarnings.contains(warningName))
return false;
if (!m_text.isEmpty() && !warn.m_completeText.toLower().contains(m_text.toLower()))
return false;
return true;
}
void WarningProxyModel::setAcceptedWarningTypes(const QSet<QString> &warnings)
{
if (warnings != m_acceptedWarnings) {
m_acceptedWarnings = warnings;
invalidateFilter();
}
}
void WarningProxyModel::setText(const QString &filter)
{
if (filter != m_text) {
m_text = filter;
invalidateFilter();
}
}
void WarningProxyModel::onSourceModelLoaded(bool success, const QString &)
{
if (success)
calculateAvailableWarnings();
}
bool WarningProxyModel::isAcceptedWarning(const QString &warning)
{
if (m_availableWarningFilterRegex.isEmpty())
return true;
QRegularExpression re(QString(R"(%1)").arg(m_availableWarningFilterRegex));
QRegularExpressionMatch match = re.match(warning);
return match.hasMatch();
}
void WarningProxyModel::setSourceModel(QAbstractItemModel *model)
{
QSortFilterProxyModel::setSourceModel(model);
calculateAvailableWarnings();
}
void WarningProxyModel::calculateAvailableWarnings()
{
if (!sourceModel())
return;
m_availableWarnings.clear();
const int count = sourceModel()->rowCount();
for (int i = 0; i < count; ++i) {
QString warningName = sourceModel()->index(i, 0).data(WarningModel::WarningNameRole).toString();
if (isAcceptedWarning(warningName))
m_availableWarnings.insert(warningName);
}
emit availableWarningsChanged(m_availableWarnings.size());
}
QSet<QString> WarningProxyModel::availableWarnings() const
{
return m_availableWarnings;
}
void WarningProxyModel::setAvailableWarningFilterRegex(const QString ®ex)
{
if (regex != m_availableWarningFilterRegex) {
m_availableWarningFilterRegex = regex;
calculateAvailableWarnings();
}
}