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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "customparserssettingspage.h"
#include "customparser.h"
#include "customparserconfigdialog.h"
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "projectexplorertr.h"
#include <utils/algorithm.h>
#include <utils/itemviews.h>
#include <utils/qtcassert.h>
#include <QAbstractTableModel>
#include <QHeaderView>
#include <QHBoxLayout>
#include <QLabel>
#include <QList>
#include <QPushButton>
#include <QVBoxLayout>
namespace ProjectExplorer {
namespace Internal {
class CustomParsersModel : public QAbstractTableModel
{
public:
explicit CustomParsersModel(QObject *parent = nullptr);
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
bool add(const ProjectExplorer::CustomParserSettings& s);
bool remove(const QModelIndex &index);
void apply();
protected:
QList<ProjectExplorer::CustomParserSettings> m_customParsers;
};
CustomParsersModel::CustomParsersModel(QObject *parent)
: QAbstractTableModel(parent)
, m_customParsers(ProjectExplorerPlugin::customParsers())
{
connect(
ProjectExplorerPlugin::instance(),
&ProjectExplorerPlugin::customParsersChanged,
this,
[this] {
beginResetModel();
m_customParsers = ProjectExplorerPlugin::customParsers();
endResetModel();
});
}
QVariant CustomParsersModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant result;
if (orientation == Qt::Vertical)
return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (section) {
case 0:
result = Tr::tr("Name");
break;
case 1:
result = Tr::tr("Build default");
break;
case 2:
result = Tr::tr("Run default");
break;
}
break;
case Qt::ToolTipRole:
switch (section) {
case 0:
result = Tr::tr("The name of the custom parser.");
break;
case 1:
result = Tr::tr("This custom parser is used by default for all build configurations of "
"the project.");
break;
case 2:
result = Tr::tr(
"This custom parser is used by default for all run configurations of the project.");
break;
}
}
return result;
}
int CustomParsersModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 3;
}
int CustomParsersModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_customParsers.size();
}
QVariant CustomParsersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
const CustomParserSettings &s = m_customParsers[index.row()];
switch (role) {
case Qt::CheckStateRole:
switch (index.column()) {
case 1:
return s.buildDefault ? Qt::Checked : Qt::Unchecked;
case 2:
return s.runDefault ? Qt::Checked : Qt::Unchecked;
}
break;
case Qt::DisplayRole:
switch (index.column()) {
case 0:
return s.displayName;
}
break;
case Qt::EditRole:
switch (index.column()) {
case 1:
return s.buildDefault;
case 2:
return s.runDefault;
}
break;
case Qt::TextAlignmentRole:
switch (index.column()) {
case 1:
case 2:
return Qt::AlignCenter;
}
break;
case Qt::UserRole:
return QVariant::fromValue<CustomParserSettings>(s);
}
return QVariant();
}
bool CustomParsersModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if (index.row() >= m_customParsers.size())
return false;
CustomParserSettings &s = m_customParsers[index.row()];
bool result = true;
switch (role) {
case Qt::EditRole:
switch (index.column()) {
case 0:
s.displayName = value.toString();
emit dataChanged(index, index);
break;
}
break;
case Qt::CheckStateRole:
switch (index.column()) {
case 1:
s.buildDefault = value == Qt::Checked;
emit dataChanged(index, index);
break;
case 2:
s.runDefault = value == Qt::Checked;
emit dataChanged(index, index);
break;
}
break;
case Qt::UserRole:
if (value.canConvert<CustomParserSettings>()) {
s = value.value<CustomParserSettings>();
emit dataChanged(index, index);
} else
result = false;
}
return result;
}
Qt::ItemFlags CustomParsersModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.column() > 0)
flags |= Qt::ItemIsUserCheckable;
else
flags |= Qt::ItemIsEditable;
return flags;
}
bool CustomParsersModel::add(const CustomParserSettings &s)
{
beginInsertRows(index(-1, -1), m_customParsers.size(), m_customParsers.size());
m_customParsers.append(s);
endInsertRows();
return true;
}
bool CustomParsersModel::remove(const QModelIndex &index)
{
beginRemoveRows(index.parent(), index.row(), index.row());
m_customParsers.removeAt(index.row());
endRemoveRows();
return true;
}
void CustomParsersModel::apply()
{
ProjectExplorerPlugin::setCustomParsers(m_customParsers);
}
class CustomParsersSettingsWidget final : public Core::IOptionsPageWidget
{
public:
CustomParsersSettingsWidget()
{
Utils::TreeView *parserView = new Utils::TreeView(this);
parserView->setModel(&m_model);
parserView->setSelectionMode(QAbstractItemView::SingleSelection);
parserView->setSelectionBehavior(QAbstractItemView::SelectRows);
const auto mainLayout = new QVBoxLayout(this);
const auto widgetLayout = new QHBoxLayout;
mainLayout->addLayout(widgetLayout);
const auto hintLabel = new QLabel(Tr::tr(
"Custom output parsers defined here can be enabled individually "
"in the project's build or run settings."));
mainLayout->addWidget(hintLabel);
widgetLayout->addWidget(parserView);
const auto buttonLayout = new QVBoxLayout;
widgetLayout->addLayout(buttonLayout);
const auto addButton = new QPushButton(Tr::tr("Add..."));
const auto removeButton = new QPushButton(Tr::tr("Remove"));
const auto editButton = new QPushButton("Edit...");
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(removeButton);
buttonLayout->addWidget(editButton);
buttonLayout->addStretch(1);
connect(addButton, &QPushButton::clicked, this, [this] {
CustomParserConfigDialog dlg(this);
dlg.setSettings(CustomParserSettings());
if (dlg.exec() != QDialog::Accepted)
return;
CustomParserSettings newParser = dlg.settings();
newParser.id = Utils::Id::generate();
newParser.displayName = Tr::tr("New Parser");
m_model.add(newParser);
});
connect(removeButton, &QPushButton::clicked, this, [this, parserView] {
m_model.remove(parserView->currentIndex());
});
connect(editButton, &QPushButton::clicked, this, [this, parserView] {
CustomParserSettings s = m_model.data(parserView->currentIndex(), Qt::UserRole).value<CustomParserSettings>();
CustomParserConfigDialog dlg(this);
dlg.setSettings(s);
if (dlg.exec() != QDialog::Accepted)
return;
s.error = dlg.settings().error;
s.warning = dlg.settings().warning;
m_model.setData(parserView->currentIndex(), QVariant::fromValue<CustomParserSettings>(s), Qt::UserRole);
});
const auto updateButtons = [editButton, parserView, removeButton] {
const bool enable = parserView->currentIndex().isValid();
removeButton->setEnabled(enable);
editButton->setEnabled(enable);
};
updateButtons();
connect(parserView->selectionModel(), &QItemSelectionModel::selectionChanged,
updateButtons);
}
private:
void apply() override { m_model.apply(); }
CustomParsersModel m_model;
};
CustomParsersSettingsPage::CustomParsersSettingsPage()
{
setId(Constants::CUSTOM_PARSERS_SETTINGS_PAGE_ID);
setDisplayName(Tr::tr("Custom Output Parsers"));
setCategory(Constants::BUILD_AND_RUN_SETTINGS_CATEGORY);
setWidgetCreator([] { return new CustomParsersSettingsWidget; });
}
} // namespace Internal
} // namespace ProjectExplorer
|