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

source code of ktexteditor/src/variableeditor/variableeditor.cpp