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 {
30 IconColored = 0,
31 IconHidden
32 };
33
34public:
35 explicit KateHelpButton(QWidget *parent)
37 {
38 setAutoRaise(true);
39 setIconState(IconColored);
40 setToolTip(i18n("Kate Handbook."));
41
42 connect(this, &KateHelpButton::clicked, this, &KateHelpButton::invokeHelp);
43 }
44
45public:
46 void setSection(const QString &section)
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(m_section, QStringLiteral("kate"));
64 }
65
66private:
67 QString m_section;
68};
69
70// BEGIN VariableEditor
71VariableEditor::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(10, 10, 10, 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, 0, 0, Qt::AlignLeft);
94 l->addWidget(m_variable, 0, 1, Qt::AlignLeft);
95 l->addWidget(m_btnHelp, 0, 3, Qt::AlignRight);
96 l->addWidget(m_helpText, 1, 1, 1, 3);
97
98 l->setColumnStretch(0, 0);
99 l->setColumnStretch(1, 1);
100 l->setColumnStretch(2, 1);
101 l->setColumnStretch(3, 0);
102
103 connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::itemEnabled);
104 m_checkBox->setChecked(item->isActive());
105
106 connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::valueChanged);
107 setMouseTracking(true);
108}
109
110void VariableEditor::enterEvent(QEnterEvent *event)
111{
113 m_btnHelp->setIconState(KateHelpButton::IconColored);
114 m_btnHelp->setEnabled(true);
115
116 update();
117}
118
119void VariableEditor::leaveEvent(QEvent *event)
120{
122 m_btnHelp->setIconState(KateHelpButton::IconHidden);
123 m_btnHelp->setEnabled(false);
124
125 update();
126}
127
128void VariableEditor::paintEvent(QPaintEvent *event)
129{
131
132 // draw highlighting rect like in plasma
133 if (underMouse()) {
134 QPainter painter(this);
135
136 painter.setRenderHint(QPainter::Antialiasing);
137
138 QColor cornerColor = palette().color(QPalette::Highlight);
139 cornerColor.setAlphaF(0.2);
140
141 QColor midColor = palette().color(QPalette::Highlight);
142 midColor.setAlphaF(0.5);
143
144 QRect highlightRect = rect().adjusted(2, 2, -2, -2);
145
146 QPen outlinePen;
147 outlinePen.setWidth(2);
148
149 QLinearGradient gradient(highlightRect.topLeft(), highlightRect.topRight());
150 gradient.setColorAt(0, cornerColor);
151 gradient.setColorAt(0.3, midColor);
152 gradient.setColorAt(1, cornerColor);
153 outlinePen.setBrush(gradient);
154 painter.setPen(outlinePen);
155
156 const int radius = 5;
157 painter.drawRoundedRect(highlightRect, radius, radius);
158 }
159}
160
161void 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
171void VariableEditor::activateItem()
172{
173 m_checkBox->setChecked(true);
174}
175
176VariableItem *VariableEditor::item() const
177{
178 return m_item;
179}
180// END VariableEditor
181
182// BEGIN VariableUintEditor
183VariableIntEditor::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, 0, 2, Qt::AlignLeft);
194
195 connect(m_spinBox, &QSpinBox::valueChanged, this, &VariableIntEditor::valueChanged);
196 connect(m_spinBox, &QSpinBox::valueChanged, this, &VariableIntEditor::activateItem);
197 connect(m_spinBox, &QSpinBox::valueChanged, this, &VariableIntEditor::setItemValue);
198}
199
200void VariableIntEditor::setItemValue(int newValue)
201{
202 static_cast<VariableIntItem *>(item())->setValue(newValue);
203}
204// END VariableUintEditor
205
206// BEGIN VariableBoolEditor
207VariableBoolEditor::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, 0, 2, Qt::AlignLeft);
217
218 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableBoolEditor::valueChanged);
219 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableBoolEditor::activateItem);
220 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableBoolEditor::setItemValue);
221}
222
223void VariableBoolEditor::setItemValue(int enabled)
224{
225 static_cast<VariableBoolItem *>(item())->setValue(enabled == 0);
226}
227// END VariableBoolEditor
228
229// BEGIN VariableStringListEditor
230VariableStringListEditor::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(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, 0, 2, Qt::AlignLeft);
246
247 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableStringListEditor::valueChanged);
248 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableStringListEditor::activateItem);
249 connect(m_comboBox, &QComboBox::currentTextChanged, this, &VariableStringListEditor::setItemValue);
250}
251
252void VariableStringListEditor::setItemValue(const QString &newValue)
253{
254 static_cast<VariableStringListItem *>(item())->setValue(newValue);
255}
256// END VariableStringListEditor
257
258// BEGIN VariableColorEditor
259VariableColorEditor::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, 0, 2, Qt::AlignLeft);
267
268 connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::valueChanged);
269 connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::activateItem);
270 connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::setItemValue);
271}
272
273void VariableColorEditor::setItemValue(const QColor &newValue)
274{
275 static_cast<VariableColorItem *>(item())->setValue(newValue);
276}
277// END VariableColorEditor
278
279// BEGIN VariableFontEditor
280VariableFontEditor::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, 0, 2, Qt::AlignLeft);
288
289 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::valueChanged);
290 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::activateItem);
291 connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::setItemValue);
292}
293
294void VariableFontEditor::setItemValue(const QFont &newValue)
295{
296 static_cast<VariableFontItem *>(item())->setValue(newValue);
297}
298// END VariableFontEditor
299
300// BEGIN VariableStringEditor
301VariableStringEditor::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, 0, 2, Qt::AlignLeft);
309
310 connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::valueChanged);
311 connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::activateItem);
312 connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::setItemValue);
313}
314
315void VariableStringEditor::setItemValue(const QString &newValue)
316{
317 static_cast<VariableStringItem *>(item())->setValue(newValue);
318}
319// END VariableStringEditor
320
321// BEGIN VariableSpellCheckEditor
322VariableSpellCheckEditor::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, 0, 2, Qt::AlignLeft);
330
331 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::valueChanged);
332 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::activateItem);
333 connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryChanged, this, &VariableSpellCheckEditor::setItemValue);
334}
335
336void VariableSpellCheckEditor::setItemValue(const QString &newValue)
337{
338 static_cast<VariableSpellCheckItem *>(item())->setValue(newValue);
339}
340
341// END VariableSpellCheckEditor
342
343// BEGIN VariableRemoveSpacesEditor
344VariableRemoveSpacesEditor::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, 0, 2, Qt::AlignLeft);
355
356 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableRemoveSpacesEditor::valueChanged);
357 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableRemoveSpacesEditor::activateItem);
358 connect(m_comboBox, &QComboBox::currentIndexChanged, this, &VariableRemoveSpacesEditor::setItemValue);
359}
360
361void VariableRemoveSpacesEditor::setItemValue(int enabled)
362{
363 static_cast<VariableRemoveSpacesItem *>(item())->setValue(enabled == 0);
364}
365// END VariableRemoveSpacesEditor
366
367#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 Dec 20 2024 11:56:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.