1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "customparserssettingspage.h"
27 
28 #include "customparser.h"
29 #include "customparserconfigdialog.h"
30 #include "projectexplorer.h"
31 #include "projectexplorerconstants.h"
32 
33 #include <utils/algorithm.h>
34 #include <utils/qtcassert.h>
35 
36 #include <QHBoxLayout>
37 #include <QLabel>
38 #include <QList>
39 #include <QListWidget>
40 #include <QPushButton>
41 #include <QUuid>
42 #include <QVBoxLayout>
43 
44 namespace ProjectExplorer {
45 namespace Internal {
46 
47 class CustomParsersSettingsWidget final : public Core::IOptionsPageWidget
48 {
49     Q_DECLARE_TR_FUNCTIONS(ProjectExplorer::Internal::CustomParsersSettingsPage)
50 
51 public:
CustomParsersSettingsWidget()52     CustomParsersSettingsWidget()
53     {
54         m_customParsers = ProjectExplorerPlugin::customParsers();
55         resetListView();
56 
57         const auto mainLayout = new QVBoxLayout(this);
58         const auto widgetLayout = new QHBoxLayout;
59         mainLayout->addLayout(widgetLayout);
60         const auto hintLabel = new QLabel(tr(
61             "Custom output parsers defined here can be enabled individually "
62             "in the project's build or run settings."));
63         mainLayout->addWidget(hintLabel);
64         widgetLayout->addWidget(&m_parserListView);
65         const auto buttonLayout = new QVBoxLayout;
66         widgetLayout->addLayout(buttonLayout);
67         const auto addButton = new QPushButton(tr("Add..."));
68         const auto removeButton = new QPushButton(tr("Remove"));
69         const auto editButton = new QPushButton("Edit...");
70         buttonLayout->addWidget(addButton);
71         buttonLayout->addWidget(removeButton);
72         buttonLayout->addWidget(editButton);
73         buttonLayout->addStretch(1);
74 
75         connect(addButton, &QPushButton::clicked, [this] {
76             CustomParserConfigDialog dlg(this);
77             dlg.setSettings(CustomParserSettings());
78             if (dlg.exec() != QDialog::Accepted)
79                 return;
80             CustomParserSettings newParser = dlg.settings();
81             newParser.id = Utils::Id::fromString(QUuid::createUuid().toString());
82             newParser.displayName = tr("New Parser");
83             m_customParsers << newParser;
84             resetListView();
85         });
86         connect(removeButton, &QPushButton::clicked, [this] {
87             const QList<QListWidgetItem *> sel = m_parserListView.selectedItems();
88             QTC_ASSERT(sel.size() == 1, return);
89             m_customParsers.removeAt(m_parserListView.row(sel.first()));
90             delete sel.first();
91         });
92         connect(editButton, &QPushButton::clicked, [this] {
93             const QList<QListWidgetItem *> sel = m_parserListView.selectedItems();
94             QTC_ASSERT(sel.size() == 1, return);
95             CustomParserSettings &s = m_customParsers[m_parserListView.row(sel.first())];
96             CustomParserConfigDialog dlg(this);
97             dlg.setSettings(s);
98             if (dlg.exec() != QDialog::Accepted)
99                 return;
100             s.error = dlg.settings().error;
101             s.warning = dlg.settings().warning;
102         });
103 
104         connect(&m_parserListView, &QListWidget::itemChanged, [this](QListWidgetItem *item) {
105             m_customParsers[m_parserListView.row(item)].displayName = item->text();
106             resetListView();
107         });
108 
109         const auto updateButtons = [this, removeButton, editButton] {
110             const bool enable = !m_parserListView.selectedItems().isEmpty();
111             removeButton->setEnabled(enable);
112             editButton->setEnabled(enable);
113         };
114         updateButtons();
115         connect(m_parserListView.selectionModel(), &QItemSelectionModel::selectionChanged,
116                 updateButtons);
117     }
118 
119 private:
apply()120     void apply() override { ProjectExplorerPlugin::setCustomParsers(m_customParsers); }
121 
resetListView()122     void resetListView()
123     {
124         m_parserListView.clear();
125         Utils::sort(m_customParsers,
126                     [](const CustomParserSettings &s1, const CustomParserSettings &s2) {
127             return s1.displayName < s2.displayName;
128         });
129         for (const CustomParserSettings &s : qAsConst(m_customParsers)) {
130             const auto item = new QListWidgetItem(s.displayName);
131             item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
132             m_parserListView.addItem(item);
133         }
134     }
135 
136     QListWidget m_parserListView;
137     QList<CustomParserSettings> m_customParsers;
138 };
139 
CustomParsersSettingsPage()140 CustomParsersSettingsPage::CustomParsersSettingsPage()
141 {
142     setId(Constants::CUSTOM_PARSERS_SETTINGS_PAGE_ID);
143     setDisplayName(CustomParsersSettingsWidget::tr("Custom Output Parsers"));
144     setCategory(Constants::BUILD_AND_RUN_SETTINGS_CATEGORY);
145     setWidgetCreator([] { return new CustomParsersSettingsWidget; });
146 }
147 
148 } // namespace Internal
149 } // namespace ProjectExplorer
150