KTextEditor

variableeditor.cpp
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)
34 {
35 setAutoRaise(true);
36 setIconState(IconColored);
37 setToolTip(i18n("Kate Handbook."));
38
39 connect(this, &KateHelpButton::clicked, this, &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(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(10, 10, 10, 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, 0, 0, Qt::AlignLeft);
91 l->addWidget(m_variable, 0, 1, Qt::AlignLeft);
92 l->addWidget(m_btnHelp, 0, 3, Qt::AlignRight);
93 l->addWidget(m_helpText, 1, 1, 1, 3);
94
95 l->setColumnStretch(0, 0);
96 l->setColumnStretch(1, 1);
97 l->setColumnStretch(2, 1);
98 l->setColumnStretch(3, 0);
99
100 connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::itemEnabled);
101 m_checkBox->setChecked(item->isActive());
102
103 connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::valueChanged);
104 setMouseTracking(true);
105}
106
107void VariableEditor::enterEvent(QEnterEvent *event)
108{
110 m_btnHelp->setIconState(KateHelpButton::IconColored);
111 m_btnHelp->setEnabled(true);
112
113 update();
114}
115
116void VariableEditor::leaveEvent(QEvent *event)
117{
119 m_btnHelp->setIconState(KateHelpButton::IconHidden);
120 m_btnHelp->setEnabled(false);
121
122 update();
123}
124
125void VariableEditor::paintEvent(QPaintEvent *event)
126{
128
129 // draw highlighting rect like in plasma
130 if (underMouse()) {
131 QPainter painter(this);
132
133 painter.setRenderHint(QPainter::Antialiasing);
134
135 QColor cornerColor = palette().color(QPalette::Highlight);
136 cornerColor.setAlphaF(0.2);
137
138 QColor midColor = palette().color(QPalette::Highlight);
139 midColor.setAlphaF(0.5);
140
141 QRect highlightRect = rect().adjusted(2, 2, -2, -2);
142
143 QPen outlinePen;
144 outlinePen.setWidth(2);
145
146 QLinearGradient gradient(highlightRect.topLeft(), highlightRect.topRight());
147 gradient.setColorAt(0, cornerColor);
148 gradient.setColorAt(0.3, midColor);
149 gradient.setColorAt(1, cornerColor);
150 outlinePen.setBrush(gradient);
151 painter.setPen(outlinePen);
152
153 const int radius = 5;
154 painter.drawRoundedRect(highlightRect, radius, 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, 0, 2, Qt::AlignLeft);
191
192 connect(m_spinBox, &QSpinBox::valueChanged, this, &VariableIntEditor::valueChanged);
193 connect(m_spinBox, &QSpinBox::valueChanged, this, &VariableIntEditor::activateItem);
194 connect(m_spinBox, &QSpinBox::valueChanged, this, &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, 0, 2, Qt::AlignLeft);
214
215 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableBoolEditor::valueChanged);
216 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableBoolEditor::activateItem);
217 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &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(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, 0, 2, Qt::AlignLeft);
243
244 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableStringListEditor::valueChanged);
245 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableStringListEditor::activateItem);
246 connect(m_comboBox, &QComboBox::currentTextChanged, this, &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, 0, 2, Qt::AlignLeft);
264
265 connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::valueChanged);
266 connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::activateItem);
267 connect(m_comboBox, &KColorCombo::activated, this, &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, 0, 2, Qt::AlignLeft);
285
286 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::valueChanged);
287 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::activateItem);
288 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &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, 0, 2, Qt::AlignLeft);
306
307 connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::valueChanged);
308 connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::activateItem);
309 connect(m_lineEdit, &QLineEdit::textChanged, this, &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, 0, 2, Qt::AlignLeft);
327
328 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::valueChanged);
329 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::activateItem);
330 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryChanged, this, &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, 0, 2, Qt::AlignLeft);
352
353 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableRemoveSpacesEditor::valueChanged);
354 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableRemoveSpacesEditor::activateItem);
355 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &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"
void activated(const QColor &col)
void dictionaryChanged(const QString &dictionary)
void dictionaryNameChanged(const QString &dictionaryName)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void invokeHelp(const QString &anchor=QString(), const QString &appname=QString())
void setChecked(bool)
void clicked(bool checked)
void setIcon(const QIcon &icon)
void toggled(bool checked)
void setAlphaF(float alpha)
void currentIndexChanged(int index)
void currentTextChanged(const QString &text)
void currentFontChanged(const QFont &font)
void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment)
void setColumnStretch(int column, int stretch)
QIcon fromTheme(const QString &name)
void setText(const QString &)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
const_reference at(qsizetype i) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void setBrush(const QBrush &brush)
void setWidth(int width)
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
QPoint topLeft() const const
QPoint topRight() const const
void valueChanged(int i)
AlignLeft
ClickFocus
WA_Hover
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setAutoRaise(bool enable)
void setEnabled(bool)
virtual void enterEvent(QEnterEvent *event)
virtual bool event(QEvent *event) override
virtual void leaveEvent(QEvent *event)
virtual void paintEvent(QPaintEvent *event)
void setToolTip(const QString &)
bool underMouse() const const
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:02:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.