1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 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 #pragma once
27 
28 #include "jsonfieldpage.h"
29 
30 #include <utils/pathchooser.h>
31 
32 #include <QWidget>
33 #include <QString>
34 #include <QVariant>
35 #include <QDir>
36 
37 #include <memory>
38 #include <vector>
39 
40 QT_BEGIN_NAMESPACE
41 class QStandardItem;
42 class QStandardItemModel;
43 class QItemSelectionModel;
44 QT_END_NAMESPACE
45 
46 namespace ProjectExplorer {
47 
48 // --------------------------------------------------------------------
49 // JsonFieldPage::Field::FieldPrivate:
50 // --------------------------------------------------------------------
51 
52 class JsonFieldPage::Field::FieldPrivate
53 {
54 public:
55     QString m_name;
56     QString m_displayName;
57     QString m_toolTip;
58     bool m_isMandatory = false;
59     bool m_hasSpan = false;
60     bool m_hasUserChanges = false;
61 
62     QVariant m_visibleExpression;
63     QVariant m_enabledExpression;
64     QVariant m_isCompleteExpando;
65     QString m_isCompleteExpandoMessage;
66     QString m_persistenceKey;
67 
68     QLabel *m_label = nullptr;
69     QWidget *m_widget = nullptr;
70 
71     QString m_type;
72 };
73 
74 // --------------------------------------------------------------------
75 // Field Implementations:
76 // --------------------------------------------------------------------
77 
78 class LabelField : public JsonFieldPage::Field
79 {
80 private:
81     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
82     bool parseData(const QVariant &data, QString *errorMessage) override;
83 
84     bool m_wordWrap = false;
85     QString m_text;
86 };
87 
88 class SpacerField : public JsonFieldPage::Field
89 {
90 public:
suppressName()91     bool suppressName() const override { return true; }
92 
93 private:
94     bool parseData(const QVariant &data, QString *errorMessage) override;
95     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
96 
97     int m_factor = 1;
98 };
99 
100 class LineEditField : public JsonFieldPage::Field
101 {
102 private:
103     bool parseData(const QVariant &data, QString *errorMessage) override;
104     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
105 
106     void setup(JsonFieldPage *page, const QString &name) override;
107 
108     bool validate(Utils::MacroExpander *expander, QString *message) override;
109     void initializeData(Utils::MacroExpander *expander) override;
110 
111     void fromSettings(const QVariant &value) override;
112     QVariant toSettings() const override;
113 
114     void setupCompletion(Utils::FancyLineEdit *lineEdit);
115 
116     bool m_isModified = false;
117     bool m_isValidating = false;
118     bool m_restoreLastHistoryItem = false;
119     bool m_isPassword = false;
120     QString m_placeholderText;
121     QString m_defaultText;
122     QString m_disabledText;
123     QString m_historyId;
124     QRegularExpression m_validatorRegExp;
125     QString m_fixupExpando;
126     mutable QString m_currentText;
127 
128     enum class Completion { Classes, Namespaces, None };
129     Completion m_completion = Completion::None;
130 };
131 
132 class TextEditField : public JsonFieldPage::Field
133 {
134 private:
135     bool parseData(const QVariant &data, QString *errorMessage) override;
136     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
137 
138     void setup(JsonFieldPage *page, const QString &name) override;
139 
140     bool validate(Utils::MacroExpander *expander, QString *message) override;
141     void initializeData(Utils::MacroExpander *expander) override;
142 
143     void fromSettings(const QVariant &value) override;
144     QVariant toSettings() const override;
145 
146     QString m_defaultText;
147     bool m_acceptRichText = false;
148     QString m_disabledText;
149 
150     mutable QString m_currentText;
151 };
152 
153 class PathChooserField : public JsonFieldPage::Field
154 {
155 private:
156     bool parseData(const QVariant &data, QString *errorMessage) override;
157 
158     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
159     void setEnabled(bool e) override;
160 
161     void setup(JsonFieldPage *page, const QString &name) override;
162 
163     bool validate(Utils::MacroExpander *expander, QString *message) override;
164     void initializeData(Utils::MacroExpander *expander) override;
165 
166     void fromSettings(const QVariant &value) override;
167     QVariant toSettings() const override;
168 
169     QString m_path;
170     QString m_basePath;
171     QString m_historyId;
172     Utils::PathChooser::Kind m_kind = Utils::PathChooser::ExistingDirectory;
173 
174     QString m_currentPath;
175 };
176 
177 class CheckBoxField : public JsonFieldPage::Field
178 {
179 public:
suppressName()180     bool suppressName() const override { return true; }
181 
182 private:
183     bool parseData(const QVariant &data, QString *errorMessage) override;
184 
185     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
186 
187     void setup(JsonFieldPage *page, const QString &name) override;
188 
189     bool validate(Utils::MacroExpander *expander, QString *message) override;
190     void initializeData(Utils::MacroExpander *expander) override;
191     void fromSettings(const QVariant &value) override;
192     QVariant toSettings() const override;
193 
194     QString m_checkedValue;
195     QString m_uncheckedValue;
196     QVariant m_checkedExpression;
197 
198     bool m_isModified = false;
199 };
200 
201 class ListField : public JsonFieldPage::Field
202 {
203 public:
204     enum SpecialRoles {
205         ValueRole = Qt::UserRole,
206         ConditionRole = Qt::UserRole + 1,
207         IconStringRole = Qt::UserRole + 2
208     };
209     ListField();
210     ~ListField() override;
211 
212     protected:
213     bool parseData(const QVariant &data, QString *errorMessage) override;
214 
215     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override = 0;
216     void setup(JsonFieldPage *page, const QString &name) override = 0;
217 
218     bool validate(Utils::MacroExpander *expander, QString *message) override;
219     void initializeData(Utils::MacroExpander *expander) override;
220     QStandardItemModel *itemModel();
221     QItemSelectionModel *selectionModel() const;
222     void setSelectionModel(QItemSelectionModel *selectionModel);
223     QSize maxIconSize();
224 
225 private:
226     void addPossibleIconSize(const QIcon &icon);
227     void updateIndex();
228 
229     void fromSettings(const QVariant &value) override;
230     QVariant toSettings() const override;
231 
232     std::vector<std::unique_ptr<QStandardItem>> m_itemList;
233     QStandardItemModel *m_itemModel = nullptr;
234     QItemSelectionModel *m_selectionModel = nullptr;
235     int m_index = -1;
236     int m_disabledIndex = -1;
237     QSize m_maxIconSize;
238 
239     mutable int m_savedIndex = -1;
240 };
241 
242 class ComboBoxField : public ListField
243 {
244 private:
245     void setup(JsonFieldPage *page, const QString &name) override;
246     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
247     void initializeData(Utils::MacroExpander *expander) override;
248     QVariant toSettings() const override;
249 };
250 
251 class IconListField : public ListField
252 {
253 public:
254     void setup(JsonFieldPage *page, const QString &name) override;
255     QWidget *createWidget(const QString &displayName, JsonFieldPage *page) override;
256     void initializeData(Utils::MacroExpander *expander) override;
257 };
258 
259 } // namespace ProjectExplorer
260