1 | /* |
2 | SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #include "variableeditor.h" |
8 | #include "variableitem.h" |
9 | |
10 | #include <QCheckBox> |
11 | #include <QComboBox> |
12 | #include <QFontComboBox> |
13 | #include <QGridLayout> |
14 | #include <QLabel> |
15 | #include <QLineEdit> |
16 | #include <QPainter> |
17 | #include <QSpinBox> |
18 | #include <QToolButton> |
19 | #include <QVariant> |
20 | |
21 | #include <KColorCombo> |
22 | #include <KHelpClient> |
23 | #include <KLocalizedString> |
24 | #include <sonnet/dictionarycombobox.h> |
25 | |
26 | class KateHelpButton : public QToolButton |
27 | { |
28 | public: |
29 | enum IconState { |
30 | IconColored = 0, |
31 | IconHidden |
32 | }; |
33 | |
34 | public: |
35 | explicit KateHelpButton(QWidget *parent) |
36 | : QToolButton(parent) |
37 | { |
38 | setAutoRaise(true); |
39 | setIconState(IconColored); |
40 | setToolTip(i18n("Kate Handbook." )); |
41 | |
42 | connect(sender: this, signal: &KateHelpButton::clicked, context: this, slot: &KateHelpButton::invokeHelp); |
43 | } |
44 | |
45 | public: |
46 | void setSection(const QString §ion) |
47 | { |
48 | m_section = section; |
49 | } |
50 | |
51 | void setIconState(IconState state) |
52 | { |
53 | if (state == IconHidden) { |
54 | setIcon(QIcon()); |
55 | } else { |
56 | setIcon(QIcon::fromTheme(QStringLiteral("help-contents" ))); |
57 | } |
58 | |
59 | update(); |
60 | } |
61 | void invokeHelp() |
62 | { |
63 | KHelpClient::invokeHelp(anchor: m_section, QStringLiteral("kate" )); |
64 | } |
65 | |
66 | private: |
67 | QString m_section; |
68 | }; |
69 | |
70 | // BEGIN VariableEditor |
71 | VariableEditor::VariableEditor(VariableItem *item, QWidget *parent) |
72 | : QWidget(parent) |
73 | , m_item(item) |
74 | { |
75 | setAttribute(Qt::WA_Hover); |
76 | |
77 | setAutoFillBackground(true); |
78 | QGridLayout *l = new QGridLayout(this); |
79 | l->setContentsMargins(left: 10, top: 10, right: 10, bottom: 10); |
80 | |
81 | m_checkBox = new QCheckBox(this); |
82 | m_variable = new QLabel(item->variable(), this); |
83 | m_variable->setFocusPolicy(Qt::ClickFocus); |
84 | m_variable->setFocusProxy(m_checkBox); |
85 | m_btnHelp = new KateHelpButton(this); |
86 | m_btnHelp->setIconState(KateHelpButton::IconHidden); |
87 | m_btnHelp->setEnabled(false); |
88 | m_btnHelp->setSection(QLatin1String("variable-" ) + item->variable()); |
89 | |
90 | m_helpText = new QLabel(item->helpText(), this); |
91 | m_helpText->setWordWrap(true); |
92 | |
93 | l->addWidget(m_checkBox, row: 0, column: 0, Qt::AlignLeft); |
94 | l->addWidget(m_variable, row: 0, column: 1, Qt::AlignLeft); |
95 | l->addWidget(m_btnHelp, row: 0, column: 3, Qt::AlignRight); |
96 | l->addWidget(m_helpText, row: 1, column: 1, rowSpan: 1, columnSpan: 3); |
97 | |
98 | l->setColumnStretch(column: 0, stretch: 0); |
99 | l->setColumnStretch(column: 1, stretch: 1); |
100 | l->setColumnStretch(column: 2, stretch: 1); |
101 | l->setColumnStretch(column: 3, stretch: 0); |
102 | |
103 | connect(sender: m_checkBox, signal: &QCheckBox::toggled, context: this, slot: &VariableEditor::itemEnabled); |
104 | m_checkBox->setChecked(item->isActive()); |
105 | |
106 | connect(sender: m_checkBox, signal: &QCheckBox::toggled, context: this, slot: &VariableEditor::valueChanged); |
107 | setMouseTracking(true); |
108 | } |
109 | |
110 | void VariableEditor::enterEvent(QEnterEvent *event) |
111 | { |
112 | QWidget::enterEvent(event); |
113 | m_btnHelp->setIconState(KateHelpButton::IconColored); |
114 | m_btnHelp->setEnabled(true); |
115 | |
116 | update(); |
117 | } |
118 | |
119 | void VariableEditor::leaveEvent(QEvent *event) |
120 | { |
121 | QWidget::leaveEvent(event); |
122 | m_btnHelp->setIconState(KateHelpButton::IconHidden); |
123 | m_btnHelp->setEnabled(false); |
124 | |
125 | update(); |
126 | } |
127 | |
128 | void VariableEditor::paintEvent(QPaintEvent *event) |
129 | { |
130 | QWidget::paintEvent(event); |
131 | |
132 | // draw highlighting rect like in plasma |
133 | if (underMouse()) { |
134 | QPainter painter(this); |
135 | |
136 | painter.setRenderHint(hint: QPainter::Antialiasing); |
137 | |
138 | QColor cornerColor = palette().color(cr: QPalette::Highlight); |
139 | cornerColor.setAlphaF(0.2); |
140 | |
141 | QColor midColor = palette().color(cr: QPalette::Highlight); |
142 | midColor.setAlphaF(0.5); |
143 | |
144 | QRect highlightRect = rect().adjusted(xp1: 2, yp1: 2, xp2: -2, yp2: -2); |
145 | |
146 | QPen outlinePen; |
147 | outlinePen.setWidth(2); |
148 | |
149 | QLinearGradient gradient(highlightRect.topLeft(), highlightRect.topRight()); |
150 | gradient.setColorAt(pos: 0, color: cornerColor); |
151 | gradient.setColorAt(pos: 0.3, color: midColor); |
152 | gradient.setColorAt(pos: 1, color: cornerColor); |
153 | outlinePen.setBrush(gradient); |
154 | painter.setPen(outlinePen); |
155 | |
156 | const int radius = 5; |
157 | painter.drawRoundedRect(rect: highlightRect, xRadius: radius, yRadius: radius); |
158 | } |
159 | } |
160 | |
161 | void VariableEditor::itemEnabled(bool enabled) |
162 | { |
163 | if (enabled) { |
164 | m_variable->setText(QLatin1String("<b>" ) + m_item->variable() + QLatin1String("</b>" )); |
165 | } else { |
166 | m_variable->setText(m_item->variable()); |
167 | } |
168 | m_item->setActive(enabled); |
169 | } |
170 | |
171 | void VariableEditor::activateItem() |
172 | { |
173 | m_checkBox->setChecked(true); |
174 | } |
175 | |
176 | VariableItem *VariableEditor::item() const |
177 | { |
178 | return m_item; |
179 | } |
180 | // END VariableEditor |
181 | |
182 | // BEGIN VariableUintEditor |
183 | VariableIntEditor::VariableIntEditor(VariableIntItem *item, QWidget *parent) |
184 | : VariableEditor(item, parent) |
185 | { |
186 | QGridLayout *l = (QGridLayout *)layout(); |
187 | |
188 | m_spinBox = new QSpinBox(this); |
189 | m_spinBox->setValue(item->value()); |
190 | m_spinBox->setMinimum(item->minValue()); |
191 | m_spinBox->setMaximum(item->maxValue()); |
192 | |
193 | l->addWidget(m_spinBox, row: 0, column: 2, Qt::AlignLeft); |
194 | |
195 | connect(sender: m_spinBox, signal: &QSpinBox::valueChanged, context: this, slot: &VariableIntEditor::valueChanged); |
196 | connect(sender: m_spinBox, signal: &QSpinBox::valueChanged, context: this, slot: &VariableIntEditor::activateItem); |
197 | connect(sender: m_spinBox, signal: &QSpinBox::valueChanged, context: this, slot: &VariableIntEditor::setItemValue); |
198 | } |
199 | |
200 | void VariableIntEditor::setItemValue(int newValue) |
201 | { |
202 | static_cast<VariableIntItem *>(item())->setValue(newValue); |
203 | } |
204 | // END VariableUintEditor |
205 | |
206 | // BEGIN VariableBoolEditor |
207 | VariableBoolEditor::VariableBoolEditor(VariableBoolItem *item, QWidget *parent) |
208 | : VariableEditor(item, parent) |
209 | { |
210 | QGridLayout *l = (QGridLayout *)layout(); |
211 | |
212 | m_comboBox = new QComboBox(this); |
213 | m_comboBox->addItem(i18n("true" )); |
214 | m_comboBox->addItem(i18n("false" )); |
215 | m_comboBox->setCurrentIndex(item->value() ? 0 : 1); |
216 | l->addWidget(m_comboBox, row: 0, column: 2, Qt::AlignLeft); |
217 | |
218 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableBoolEditor::valueChanged); |
219 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableBoolEditor::activateItem); |
220 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableBoolEditor::setItemValue); |
221 | } |
222 | |
223 | void VariableBoolEditor::setItemValue(int enabled) |
224 | { |
225 | static_cast<VariableBoolItem *>(item())->setValue(enabled == 0); |
226 | } |
227 | // END VariableBoolEditor |
228 | |
229 | // BEGIN VariableStringListEditor |
230 | VariableStringListEditor::VariableStringListEditor(VariableStringListItem *item, QWidget *parent) |
231 | : VariableEditor(item, parent) |
232 | { |
233 | QGridLayout *l = (QGridLayout *)layout(); |
234 | |
235 | m_comboBox = new QComboBox(this); |
236 | m_comboBox->addItems(texts: item->stringList()); |
237 | int index = 0; |
238 | for (int i = 0; i < item->stringList().size(); ++i) { |
239 | if (item->stringList().at(i) == item->value()) { |
240 | index = i; |
241 | break; |
242 | } |
243 | } |
244 | m_comboBox->setCurrentIndex(index); |
245 | l->addWidget(m_comboBox, row: 0, column: 2, Qt::AlignLeft); |
246 | |
247 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableStringListEditor::valueChanged); |
248 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableStringListEditor::activateItem); |
249 | connect(sender: m_comboBox, signal: &QComboBox::currentTextChanged, context: this, slot: &VariableStringListEditor::setItemValue); |
250 | } |
251 | |
252 | void VariableStringListEditor::setItemValue(const QString &newValue) |
253 | { |
254 | static_cast<VariableStringListItem *>(item())->setValue(newValue); |
255 | } |
256 | // END VariableStringListEditor |
257 | |
258 | // BEGIN VariableColorEditor |
259 | VariableColorEditor::VariableColorEditor(VariableColorItem *item, QWidget *parent) |
260 | : VariableEditor(item, parent) |
261 | { |
262 | QGridLayout *l = (QGridLayout *)layout(); |
263 | |
264 | m_comboBox = new KColorCombo(this); |
265 | m_comboBox->setColor(item->value()); |
266 | l->addWidget(m_comboBox, row: 0, column: 2, Qt::AlignLeft); |
267 | |
268 | connect(sender: m_comboBox, signal: &KColorCombo::activated, context: this, slot: &VariableColorEditor::valueChanged); |
269 | connect(sender: m_comboBox, signal: &KColorCombo::activated, context: this, slot: &VariableColorEditor::activateItem); |
270 | connect(sender: m_comboBox, signal: &KColorCombo::activated, context: this, slot: &VariableColorEditor::setItemValue); |
271 | } |
272 | |
273 | void VariableColorEditor::setItemValue(const QColor &newValue) |
274 | { |
275 | static_cast<VariableColorItem *>(item())->setValue(newValue); |
276 | } |
277 | // END VariableColorEditor |
278 | |
279 | // BEGIN VariableFontEditor |
280 | VariableFontEditor::VariableFontEditor(VariableFontItem *item, QWidget *parent) |
281 | : VariableEditor(item, parent) |
282 | { |
283 | QGridLayout *l = (QGridLayout *)layout(); |
284 | |
285 | m_comboBox = new QFontComboBox(this); |
286 | m_comboBox->setCurrentFont(item->value()); |
287 | l->addWidget(m_comboBox, row: 0, column: 2, Qt::AlignLeft); |
288 | |
289 | connect(sender: m_comboBox, signal: &QFontComboBox::currentFontChanged, context: this, slot: &VariableFontEditor::valueChanged); |
290 | connect(sender: m_comboBox, signal: &QFontComboBox::currentFontChanged, context: this, slot: &VariableFontEditor::activateItem); |
291 | connect(sender: m_comboBox, signal: &QFontComboBox::currentFontChanged, context: this, slot: &VariableFontEditor::setItemValue); |
292 | } |
293 | |
294 | void VariableFontEditor::setItemValue(const QFont &newValue) |
295 | { |
296 | static_cast<VariableFontItem *>(item())->setValue(newValue); |
297 | } |
298 | // END VariableFontEditor |
299 | |
300 | // BEGIN VariableStringEditor |
301 | VariableStringEditor::VariableStringEditor(VariableStringItem *item, QWidget *parent) |
302 | : VariableEditor(item, parent) |
303 | { |
304 | QGridLayout *l = (QGridLayout *)layout(); |
305 | |
306 | m_lineEdit = new QLineEdit(this); |
307 | m_lineEdit->setText(item->value()); |
308 | l->addWidget(m_lineEdit, row: 0, column: 2, Qt::AlignLeft); |
309 | |
310 | connect(sender: m_lineEdit, signal: &QLineEdit::textChanged, context: this, slot: &VariableStringEditor::valueChanged); |
311 | connect(sender: m_lineEdit, signal: &QLineEdit::textChanged, context: this, slot: &VariableStringEditor::activateItem); |
312 | connect(sender: m_lineEdit, signal: &QLineEdit::textChanged, context: this, slot: &VariableStringEditor::setItemValue); |
313 | } |
314 | |
315 | void VariableStringEditor::setItemValue(const QString &newValue) |
316 | { |
317 | static_cast<VariableStringItem *>(item())->setValue(newValue); |
318 | } |
319 | // END VariableStringEditor |
320 | |
321 | // BEGIN VariableSpellCheckEditor |
322 | VariableSpellCheckEditor::VariableSpellCheckEditor(VariableSpellCheckItem *item, QWidget *parent) |
323 | : VariableEditor(item, parent) |
324 | { |
325 | QGridLayout *l = (QGridLayout *)layout(); |
326 | |
327 | m_dictionaryCombo = new Sonnet::DictionaryComboBox(this); |
328 | m_dictionaryCombo->setCurrentByDictionary(item->value()); |
329 | l->addWidget(m_dictionaryCombo, row: 0, column: 2, Qt::AlignLeft); |
330 | |
331 | connect(sender: m_dictionaryCombo, signal: &Sonnet::DictionaryComboBox::dictionaryNameChanged, context: this, slot: &VariableSpellCheckEditor::valueChanged); |
332 | connect(sender: m_dictionaryCombo, signal: &Sonnet::DictionaryComboBox::dictionaryNameChanged, context: this, slot: &VariableSpellCheckEditor::activateItem); |
333 | connect(sender: m_dictionaryCombo, signal: &Sonnet::DictionaryComboBox::dictionaryChanged, context: this, slot: &VariableSpellCheckEditor::setItemValue); |
334 | } |
335 | |
336 | void VariableSpellCheckEditor::setItemValue(const QString &newValue) |
337 | { |
338 | static_cast<VariableSpellCheckItem *>(item())->setValue(newValue); |
339 | } |
340 | |
341 | // END VariableSpellCheckEditor |
342 | |
343 | // BEGIN VariableRemoveSpacesEditor |
344 | VariableRemoveSpacesEditor::VariableRemoveSpacesEditor(VariableRemoveSpacesItem *item, QWidget *parent) |
345 | : VariableEditor(item, parent) |
346 | { |
347 | QGridLayout *l = (QGridLayout *)layout(); |
348 | |
349 | m_comboBox = new QComboBox(this); |
350 | m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces" , "none" )); |
351 | m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces" , "modified" )); |
352 | m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces" , "all" )); |
353 | m_comboBox->setCurrentIndex(item->value()); |
354 | l->addWidget(m_comboBox, row: 0, column: 2, Qt::AlignLeft); |
355 | |
356 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableRemoveSpacesEditor::valueChanged); |
357 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableRemoveSpacesEditor::activateItem); |
358 | connect(sender: m_comboBox, signal: &QComboBox::currentIndexChanged, context: this, slot: &VariableRemoveSpacesEditor::setItemValue); |
359 | } |
360 | |
361 | void VariableRemoveSpacesEditor::setItemValue(int enabled) |
362 | { |
363 | static_cast<VariableRemoveSpacesItem *>(item())->setValue(enabled == 0); |
364 | } |
365 | // END VariableRemoveSpacesEditor |
366 | |
367 | #include "moc_variableeditor.cpp" |
368 | |