1 | /* This file is part of the KDE project |
2 | |
3 | Copyright (C) 2011 Dominik Haumann <dhaumann kde org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "variableeditor.h" |
22 | #include "variableitem.h" |
23 | #include "katehelpbutton.h" |
24 | |
25 | #include <QtCore/QVariant> |
26 | #include <QtGui/QCheckBox> |
27 | #include <QtGui/QComboBox> |
28 | #include <QtGui/QGridLayout> |
29 | #include <QtGui/QLabel> |
30 | #include <QtGui/QLineEdit> |
31 | #include <QtGui/QPainter> |
32 | #include <QtGui/QSpinBox> |
33 | |
34 | #include <kfontcombobox.h> |
35 | #include <kiconloader.h> |
36 | #include <klocale.h> |
37 | #include <kcolorcombo.h> |
38 | #include <sonnet/dictionarycombobox.h> |
39 | |
40 | //BEGIN VariableEditor |
41 | VariableEditor::VariableEditor(VariableItem* item, QWidget* parent) |
42 | : QWidget(parent) |
43 | , m_item(item) |
44 | { |
45 | setAttribute(Qt::WA_Hover); |
46 | |
47 | setAutoFillBackground(true); |
48 | QGridLayout* l = new QGridLayout(this); |
49 | l->setMargin(10); |
50 | |
51 | m_checkBox = new QCheckBox(this); |
52 | m_variable = new QLabel(item->variable(), this); |
53 | m_variable->setFocusPolicy(Qt::ClickFocus); |
54 | m_variable->setFocusProxy(m_checkBox); |
55 | m_btnHelp = new KateHelpButton(this); |
56 | m_btnHelp->setIconState(KateHelpButton::IconHidden); |
57 | m_btnHelp->setEnabled(false); |
58 | m_btnHelp->setSection(QLatin1String("variable-" ) + item->variable()); |
59 | |
60 | m_helpText = new QLabel(item->helpText(), this); |
61 | m_helpText->setWordWrap(true); |
62 | |
63 | l->addWidget(m_checkBox, 0, 0, Qt::AlignLeft); |
64 | l->addWidget(m_variable, 0, 1, Qt::AlignLeft); |
65 | l->addWidget(m_btnHelp, 0, 3, Qt::AlignRight); |
66 | l->addWidget(m_helpText, 1, 1, 1, 3); |
67 | |
68 | l->setColumnStretch(0, 0); |
69 | l->setColumnStretch(1, 1); |
70 | l->setColumnStretch(2, 1); |
71 | l->setColumnStretch(3, 0); |
72 | |
73 | setLayout(l); |
74 | |
75 | connect(m_checkBox, SIGNAL(toggled(bool)), this, SLOT(itemEnabled(bool))); |
76 | m_checkBox->setChecked(item->isActive()); |
77 | |
78 | connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(valueChanged())); |
79 | setMouseTracking(true); |
80 | } |
81 | |
82 | VariableEditor::~VariableEditor() |
83 | { |
84 | } |
85 | |
86 | void VariableEditor::enterEvent(QEvent* event) |
87 | { |
88 | QWidget::enterEvent(event); |
89 | m_btnHelp->setIconState(KateHelpButton::IconColored); |
90 | m_btnHelp->setEnabled(true); |
91 | |
92 | update(); |
93 | } |
94 | |
95 | void VariableEditor::leaveEvent(QEvent* event) |
96 | { |
97 | QWidget::leaveEvent(event); |
98 | m_btnHelp->setIconState(KateHelpButton::IconHidden); |
99 | m_btnHelp->setEnabled(false); |
100 | |
101 | update(); |
102 | } |
103 | |
104 | void VariableEditor::paintEvent(QPaintEvent* event) |
105 | { |
106 | QWidget::paintEvent(event); |
107 | |
108 | // draw highlighting rect like in plasma |
109 | if (underMouse()) { |
110 | QPainter painter(this); |
111 | |
112 | painter.setRenderHint(QPainter::Antialiasing); |
113 | |
114 | QColor cornerColor = palette().color(QPalette::Highlight); |
115 | cornerColor.setAlphaF(0.2); |
116 | |
117 | QColor midColor = palette().color(QPalette::Highlight); |
118 | midColor.setAlphaF(0.5); |
119 | |
120 | QRect highlightRect = rect().adjusted(2, 2, -2, -2); |
121 | |
122 | QPen outlinePen; |
123 | outlinePen.setWidth(2); |
124 | |
125 | QLinearGradient gradient(highlightRect.topLeft(), highlightRect.topRight()); |
126 | gradient.setColorAt(0, cornerColor); |
127 | gradient.setColorAt(0.3, midColor); |
128 | gradient.setColorAt(1, cornerColor); |
129 | outlinePen.setBrush(gradient); |
130 | painter.setPen(outlinePen); |
131 | |
132 | const int radius = 5; |
133 | painter.drawRoundedRect(highlightRect, radius, radius); |
134 | } |
135 | } |
136 | |
137 | void VariableEditor::itemEnabled(bool enabled) |
138 | { |
139 | if (enabled) { |
140 | m_variable->setText("<b>" + m_item->variable() + "</b>" ); |
141 | } else { |
142 | m_variable->setText(m_item->variable()); |
143 | } |
144 | m_item->setActive(enabled); |
145 | } |
146 | |
147 | void VariableEditor::activateItem() |
148 | { |
149 | m_checkBox->setChecked(true); |
150 | } |
151 | |
152 | VariableItem* VariableEditor::item() const |
153 | { |
154 | return m_item; |
155 | } |
156 | //END VariableEditor |
157 | |
158 | |
159 | |
160 | |
161 | //BEGIN VariableUintEditor |
162 | VariableIntEditor::VariableIntEditor(VariableIntItem* item, QWidget* parent) |
163 | : VariableEditor(item, parent) |
164 | { |
165 | QGridLayout* l = (QGridLayout *) layout(); |
166 | |
167 | m_spinBox = new QSpinBox(this); |
168 | m_spinBox->setValue(item->value()); |
169 | m_spinBox->setMinimum(item->minValue()); |
170 | m_spinBox->setMaximum(item->maxValue()); |
171 | |
172 | l->addWidget(m_spinBox, 0, 2, Qt::AlignLeft); |
173 | |
174 | connect(m_spinBox, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged())); |
175 | connect(m_spinBox, SIGNAL(valueChanged(int)), this, SLOT(activateItem())); |
176 | connect(m_spinBox, SIGNAL(valueChanged(int)), this, SLOT(setItemValue(int))); |
177 | } |
178 | |
179 | void VariableIntEditor::setItemValue(int newValue) |
180 | { |
181 | static_cast<VariableIntItem*>(item())->setValue(newValue); |
182 | } |
183 | //END VariableUintEditor |
184 | |
185 | |
186 | |
187 | //BEGIN VariableBoolEditor |
188 | VariableBoolEditor::VariableBoolEditor(VariableBoolItem* item, QWidget* parent) |
189 | : VariableEditor(item, parent) |
190 | { |
191 | QGridLayout* l = (QGridLayout *) layout(); |
192 | |
193 | m_comboBox = new QComboBox(this); |
194 | m_comboBox->addItem(i18n("true" )); |
195 | m_comboBox->addItem(i18n("false" )); |
196 | m_comboBox->setCurrentIndex(item->value() ? 0 : 1); |
197 | l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft); |
198 | |
199 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged())); |
200 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(activateItem())); |
201 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setItemValue(int))); |
202 | } |
203 | |
204 | void VariableBoolEditor::setItemValue(int enabled) |
205 | { |
206 | static_cast<VariableBoolItem*>(item())->setValue(enabled == 0); |
207 | } |
208 | //END VariableBoolEditor |
209 | |
210 | |
211 | |
212 | |
213 | //BEGIN VariableStringListEditor |
214 | VariableStringListEditor::VariableStringListEditor(VariableStringListItem* item, QWidget* parent) |
215 | : VariableEditor(item, parent) |
216 | { |
217 | QGridLayout* l = (QGridLayout *) layout(); |
218 | |
219 | m_comboBox = new QComboBox(this); |
220 | m_comboBox->addItems(item->stringList()); |
221 | int index = 0; |
222 | for (int i = 0; i < item->stringList().size(); ++i) { |
223 | if (item->stringList().at(i) == item->value()) { |
224 | index = i; |
225 | break; |
226 | } |
227 | } |
228 | m_comboBox->setCurrentIndex(index); |
229 | l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft); |
230 | |
231 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged())); |
232 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(activateItem())); |
233 | connect(m_comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setItemValue(QString))); |
234 | } |
235 | |
236 | void VariableStringListEditor::setItemValue(const QString& newValue) |
237 | { |
238 | static_cast<VariableStringListItem*>(item())->setValue(newValue); |
239 | } |
240 | //END VariableStringListEditor |
241 | |
242 | |
243 | |
244 | //BEGIN VariableColorEditor |
245 | VariableColorEditor::VariableColorEditor(VariableColorItem* item, QWidget* parent) |
246 | : VariableEditor(item, parent) |
247 | { |
248 | QGridLayout* l = (QGridLayout *) layout(); |
249 | |
250 | m_comboBox = new KColorCombo(this); |
251 | m_comboBox->setColor(item->value()); |
252 | l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft); |
253 | |
254 | connect(m_comboBox, SIGNAL(activated(QColor)), this, SIGNAL(valueChanged())); |
255 | connect(m_comboBox, SIGNAL(activated(QColor)), this, SLOT(activateItem())); |
256 | connect(m_comboBox, SIGNAL(activated(QColor)), this, SLOT(setItemValue(QColor))); |
257 | } |
258 | |
259 | void VariableColorEditor::setItemValue(const QColor& newValue) |
260 | { |
261 | static_cast<VariableColorItem*>(item())->setValue(newValue); |
262 | } |
263 | //END VariableColorEditor |
264 | |
265 | |
266 | |
267 | //BEGIN VariableFontEditor |
268 | VariableFontEditor::VariableFontEditor(VariableFontItem* item, QWidget* parent) |
269 | : VariableEditor(item, parent) |
270 | { |
271 | QGridLayout* l = (QGridLayout *) layout(); |
272 | |
273 | m_comboBox = new KFontComboBox(this); |
274 | m_comboBox->setCurrentFont(item->value()); |
275 | l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft); |
276 | |
277 | connect(m_comboBox, SIGNAL(currentFontChanged(QFont)), this, SIGNAL(valueChanged())); |
278 | connect(m_comboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(activateItem())); |
279 | connect(m_comboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(setItemValue(QFont))); |
280 | } |
281 | |
282 | void VariableFontEditor::setItemValue(const QFont& newValue) |
283 | { |
284 | static_cast<VariableFontItem*>(item())->setValue(newValue); |
285 | } |
286 | //END VariableFontEditor |
287 | |
288 | |
289 | |
290 | //BEGIN VariableStringEditor |
291 | VariableStringEditor::VariableStringEditor(VariableStringItem *item, QWidget *parent) |
292 | :VariableEditor(item, parent) |
293 | { |
294 | QGridLayout *l = (QGridLayout*) layout(); |
295 | |
296 | m_lineEdit = new QLineEdit(this); |
297 | m_lineEdit->setText(item->value()); |
298 | l->addWidget(m_lineEdit, 0, 2, Qt::AlignLeft); |
299 | |
300 | connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(valueChanged())); |
301 | connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(activateItem())); |
302 | connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(setItemValue(QString))); |
303 | } |
304 | |
305 | void VariableStringEditor::setItemValue(const QString &newValue) |
306 | { |
307 | static_cast <VariableStringItem*>(item())->setValue(newValue); |
308 | } |
309 | //END VariableStringEditor |
310 | |
311 | |
312 | |
313 | //BEGIN VariableSpellCheckEditor |
314 | VariableSpellCheckEditor::VariableSpellCheckEditor(VariableSpellCheckItem *item, QWidget *parent) |
315 | : VariableEditor(item, parent) |
316 | { |
317 | QGridLayout *l = (QGridLayout*) layout(); |
318 | |
319 | m_dictionaryCombo = new Sonnet::DictionaryComboBox(this); |
320 | m_dictionaryCombo->setCurrentByDictionary(item->value()); |
321 | l->addWidget(m_dictionaryCombo, 0, 2, Qt::AlignLeft); |
322 | |
323 | connect(m_dictionaryCombo, SIGNAL(dictionaryNameChanged(QString)), this, SIGNAL(valueChanged())); |
324 | connect(m_dictionaryCombo, SIGNAL(dictionaryNameChanged(QString)), this, SLOT(activateItem())); |
325 | connect(m_dictionaryCombo, SIGNAL(dictionaryChanged(QString)), this, SLOT(setItemValue(QString))); |
326 | } |
327 | |
328 | void VariableSpellCheckEditor::setItemValue(const QString &newValue) |
329 | { |
330 | static_cast <VariableSpellCheckItem*>(item())->setValue(newValue); |
331 | } |
332 | |
333 | //END VariableSpellCheckEditor |
334 | |
335 | |
336 | |
337 | //BEGIN VariableRemoveSpacesEditor |
338 | VariableRemoveSpacesEditor::VariableRemoveSpacesEditor(VariableRemoveSpacesItem* item, QWidget* parent) |
339 | : VariableEditor(item, parent) |
340 | { |
341 | QGridLayout* l = (QGridLayout *) layout(); |
342 | |
343 | m_comboBox = new QComboBox(this); |
344 | m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces" , "none" )); |
345 | m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces" , "modified" )); |
346 | m_comboBox->addItem(i18nc("value for variale remove-trailing-spaces" , "all" )); |
347 | m_comboBox->setCurrentIndex(item->value()); |
348 | l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft); |
349 | |
350 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(valueChanged())); |
351 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(activateItem())); |
352 | connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setItemValue(int))); |
353 | } |
354 | |
355 | void VariableRemoveSpacesEditor::setItemValue(int enabled) |
356 | { |
357 | static_cast<VariableRemoveSpacesItem*>(item())->setValue(enabled == 0); |
358 | } |
359 | //END VariableRemoveSpacesEditor |
360 | |
361 | // kate: indent-width 2; replace-tabs on; |
362 | |