Changeset View
Changeset View
Standalone View
Standalone View
plugins/meson/settings/mesonoptionbaseview.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "mesonoptionbaseview.h" | ||||
21 | #include "mesonlisteditor.h" | ||||
22 | #include "ui_mesonoptionbaseview.h" | ||||
23 | #include <QCheckBox> | ||||
24 | #include <QComboBox> | ||||
25 | #include <QLineEdit> | ||||
26 | #include <QPalette> | ||||
27 | #include <QPushButton> | ||||
28 | #include <QSpinBox> | ||||
29 | #include <QtGlobal> | ||||
30 | #include <debug.h> | ||||
31 | #include <kcolorscheme.h> | ||||
32 | | ||||
33 | using namespace std; | ||||
34 | | ||||
35 | // Helper class for RAII signal blocking | ||||
36 | class SignalBlocker | ||||
37 | { | ||||
38 | public: | ||||
39 | SignalBlocker(QWidget* widget) | ||||
40 | : m_widget(widget) | ||||
41 | { | ||||
42 | if (m_widget) { | ||||
43 | m_widget->blockSignals(true); | ||||
44 | } | ||||
45 | } | ||||
46 | | ||||
47 | ~SignalBlocker() | ||||
48 | { | ||||
49 | if (m_widget) { | ||||
50 | m_widget->blockSignals(false); | ||||
51 | } | ||||
52 | } | ||||
53 | | ||||
54 | private: | ||||
55 | QWidget* m_widget = nullptr; | ||||
56 | }; | ||||
57 | | ||||
58 | MesonOptionBaseView::MesonOptionBaseView(OPT_PTR option, QWidget* parent) | ||||
59 | : QWidget(parent) | ||||
60 | { | ||||
61 | Q_ASSERT(option); | ||||
62 | | ||||
63 | m_ui = new Ui::MesonOptionBaseView; | ||||
64 | m_ui->setupUi(this); | ||||
65 | | ||||
66 | m_ui->l_name->setText(option->name() + QStringLiteral(":")); | ||||
67 | m_ui->l_name->setToolTip(option->description()); | ||||
68 | setToolTip(option->description()); | ||||
69 | } | ||||
70 | | ||||
71 | MesonOptionBaseView::~MesonOptionBaseView() | ||||
72 | { | ||||
73 | if (m_ui) { | ||||
74 | delete m_ui; | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | // Base class functions | ||||
79 | | ||||
80 | int MesonOptionBaseView::nameWidth() | ||||
81 | { | ||||
82 | return m_ui->l_name->fontMetrics().boundingRect(m_ui->l_name->text()).width() + 25; | ||||
83 | } | ||||
84 | | ||||
85 | void MesonOptionBaseView::setMinNameWidth(int width) | ||||
86 | { | ||||
87 | m_ui->l_name->setMinimumWidth(width); | ||||
88 | } | ||||
89 | | ||||
90 | void MesonOptionBaseView::setInputWidget(QWidget* input) | ||||
91 | { | ||||
92 | QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); | ||||
93 | sizePolicy.setHorizontalStretch(0); | ||||
94 | sizePolicy.setVerticalStretch(0); | ||||
95 | sizePolicy.setHeightForWidth(input->sizePolicy().hasHeightForWidth()); | ||||
96 | input->setSizePolicy(sizePolicy); | ||||
97 | input->setToolTip(option()->description()); | ||||
98 | m_ui->layout->insertWidget(1, input); | ||||
99 | updateInput(); | ||||
100 | setChanged(false); | ||||
101 | } | ||||
102 | | ||||
103 | void MesonOptionBaseView::reset() | ||||
104 | { | ||||
105 | option()->reset(); | ||||
106 | updateInput(); | ||||
107 | setChanged(false); | ||||
108 | } | ||||
109 | | ||||
110 | void MesonOptionBaseView::setChanged(bool changed) | ||||
111 | { | ||||
112 | KColorScheme scheme(QPalette::Normal); | ||||
113 | KColorScheme::ForegroundRole role; | ||||
114 | | ||||
115 | if (changed) { | ||||
116 | m_ui->l_name->setStyleSheet(QStringLiteral("font-weight: bold")); | ||||
117 | m_ui->b_reset->setDisabled(false); | ||||
118 | role = KColorScheme::NeutralText; | ||||
119 | } else { | ||||
120 | m_ui->l_name->setStyleSheet(QStringLiteral("")); | ||||
121 | m_ui->b_reset->setDisabled(true); | ||||
122 | role = KColorScheme::NormalText; | ||||
123 | } | ||||
124 | | ||||
125 | QPalette pal = m_ui->l_name->palette(); | ||||
126 | pal.setColor(QPalette::Foreground, scheme.foreground(role).color()); | ||||
127 | m_ui->l_name->setPalette(pal); | ||||
128 | emit configChanged(); | ||||
129 | } | ||||
130 | | ||||
131 | // Derived class constructors | ||||
132 | | ||||
133 | MesonOptionArrayView::MesonOptionArrayView(OPT_PTR option, QWidget* parent) | ||||
134 | : MesonOptionBaseView(option, parent) | ||||
135 | , m_option(dynamic_pointer_cast<MesonOptionArray>(option)) | ||||
136 | { | ||||
137 | Q_ASSERT(m_option); | ||||
138 | | ||||
139 | m_input = new QPushButton(this); | ||||
140 | connect(m_input, &QPushButton::clicked, this, [this]() { | ||||
141 | MesonListEditor editor(m_option->rawValue(), this); | ||||
142 | if (editor.exec() == QDialog::Accepted) { | ||||
143 | m_option->setValue(editor.content()); | ||||
144 | m_input->setText(m_option->value()); | ||||
145 | setChanged(m_option->isUpdated()); | ||||
146 | } | ||||
147 | }); | ||||
148 | setInputWidget(m_input); | ||||
149 | } | ||||
150 | | ||||
151 | MesonOptionBoolView::MesonOptionBoolView(OPT_PTR option, QWidget* parent) | ||||
152 | : MesonOptionBaseView(option, parent) | ||||
153 | , m_option(dynamic_pointer_cast<MesonOptionBool>(option)) | ||||
154 | { | ||||
155 | Q_ASSERT(m_option); | ||||
156 | | ||||
157 | m_input = new QCheckBox(this); | ||||
158 | connect(m_input, &QCheckBox::stateChanged, this, &MesonOptionBoolView::updated); | ||||
159 | setInputWidget(m_input); | ||||
160 | } | ||||
161 | | ||||
162 | MesonOptionComboView::MesonOptionComboView(OPT_PTR option, QWidget* parent) | ||||
163 | : MesonOptionBaseView(option, parent) | ||||
164 | , m_option(dynamic_pointer_cast<MesonOptionCombo>(option)) | ||||
165 | { | ||||
166 | Q_ASSERT(m_option); | ||||
167 | | ||||
168 | m_input = new QComboBox(this); | ||||
169 | m_input->clear(); | ||||
170 | m_input->addItems(m_option->choices()); | ||||
171 | m_input->setEditable(false); | ||||
172 | connect(m_input, qOverload<int>(&QComboBox::currentIndexChanged), this, &MesonOptionComboView::updated); | ||||
173 | setInputWidget(m_input); | ||||
174 | } | ||||
175 | | ||||
176 | MesonOptionIntegerView::MesonOptionIntegerView(OPT_PTR option, QWidget* parent) | ||||
177 | : MesonOptionBaseView(option, parent) | ||||
178 | , m_option(dynamic_pointer_cast<MesonOptionInteger>(option)) | ||||
179 | { | ||||
180 | Q_ASSERT(m_option); | ||||
181 | | ||||
182 | m_input = new QSpinBox(this); | ||||
183 | m_input->setMinimum(INT32_MIN); | ||||
184 | m_input->setMaximum(INT32_MAX); | ||||
185 | connect(m_input, qOverload<int>(&QSpinBox::valueChanged), this, &MesonOptionIntegerView::updated); | ||||
186 | setInputWidget(m_input); | ||||
187 | } | ||||
188 | | ||||
189 | MesonOptionStringView::MesonOptionStringView(OPT_PTR option, QWidget* parent) | ||||
190 | : MesonOptionBaseView(option, parent) | ||||
191 | , m_option(dynamic_pointer_cast<MesonOptionString>(option)) | ||||
192 | { | ||||
193 | Q_ASSERT(m_option); | ||||
194 | | ||||
195 | m_input = new QLineEdit(this); | ||||
196 | connect(m_input, &QLineEdit::textChanged, this, &MesonOptionStringView::updated); | ||||
197 | setInputWidget(m_input); | ||||
198 | } | ||||
199 | | ||||
200 | // Option getters | ||||
201 | | ||||
202 | MesonOptionBase* MesonOptionArrayView::option() | ||||
203 | { | ||||
204 | return m_option.get(); | ||||
205 | } | ||||
206 | | ||||
207 | MesonOptionBase* MesonOptionBoolView::option() | ||||
208 | { | ||||
209 | return m_option.get(); | ||||
210 | } | ||||
211 | | ||||
212 | MesonOptionBase* MesonOptionComboView::option() | ||||
213 | { | ||||
214 | return m_option.get(); | ||||
215 | } | ||||
216 | | ||||
217 | MesonOptionBase* MesonOptionIntegerView::option() | ||||
218 | { | ||||
219 | return m_option.get(); | ||||
220 | } | ||||
221 | | ||||
222 | MesonOptionBase* MesonOptionStringView::option() | ||||
223 | { | ||||
224 | return m_option.get(); | ||||
225 | } | ||||
226 | | ||||
227 | // Updaters for the input widget | ||||
228 | | ||||
229 | void MesonOptionArrayView::updateInput() | ||||
230 | { | ||||
231 | SignalBlocker block(m_input); | ||||
232 | m_input->setText(m_option->value()); | ||||
233 | } | ||||
234 | | ||||
235 | void MesonOptionBoolView::updateInput() | ||||
236 | { | ||||
237 | SignalBlocker block(m_input); | ||||
238 | m_input->setCheckState(m_option->rawValue() ? Qt::Checked : Qt::Unchecked); | ||||
239 | } | ||||
240 | | ||||
241 | void MesonOptionComboView::updateInput() | ||||
242 | { | ||||
243 | SignalBlocker block(m_input); | ||||
244 | m_input->setCurrentText(m_option->rawValue()); | ||||
245 | } | ||||
246 | | ||||
247 | void MesonOptionIntegerView::updateInput() | ||||
248 | { | ||||
249 | SignalBlocker block(m_input); | ||||
250 | m_input->setValue(m_option->rawValue()); | ||||
251 | } | ||||
252 | | ||||
253 | void MesonOptionStringView::updateInput() | ||||
254 | { | ||||
255 | SignalBlocker block(m_input); | ||||
256 | m_input->setText(m_option->rawValue()); | ||||
257 | } | ||||
258 | | ||||
259 | // Slots to update the option value | ||||
260 | | ||||
261 | void MesonOptionBoolView::updated() | ||||
262 | { | ||||
263 | m_option->setValue(m_input->isChecked()); | ||||
264 | setChanged(m_option->isUpdated()); | ||||
265 | } | ||||
266 | | ||||
267 | void MesonOptionComboView::updated() | ||||
268 | { | ||||
269 | m_option->setValue(m_input->currentText()); | ||||
270 | setChanged(m_option->isUpdated()); | ||||
271 | } | ||||
272 | | ||||
273 | void MesonOptionIntegerView::updated() | ||||
274 | { | ||||
275 | m_option->setValue(m_input->value()); | ||||
276 | setChanged(m_option->isUpdated()); | ||||
277 | } | ||||
278 | | ||||
279 | void MesonOptionStringView::updated() | ||||
280 | { | ||||
281 | m_option->setValue(m_input->text()); | ||||
282 | setChanged(m_option->isUpdated()); | ||||
283 | } |