1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "contextpanetextwidget.h"
35 #include "contextpanewidget.h"
36 #include "customcolordialog.h"
37 #include "ui_contextpanetext.h"
38 #include <qmljs/qmljspropertyreader.h>
39 #include <QTimerEvent>
41 namespace QmlEditorWidgets {
43 ContextPaneTextWidget::ContextPaneTextWidget(QWidget *parent) :
45 ui(new Ui::ContextPaneTextWidget),
49 ui->boldButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/bold-h-icon.png")));
50 ui->italicButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/italic-h-icon.png")));
51 ui->underlineButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/underline-h-icon.png")));
52 ui->strikeoutButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/strikeout-h-icon.png")));
54 ui->leftAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentleft-h-icon.png")));
55 ui->centerHAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentcenterh-h-icon.png")));
56 ui->rightAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentright-h-icon.png")));
58 ui->centerVAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentmiddle-h-icon.png")));
60 ui->bottomAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentbottom-h-icon.png")));
61 ui->topAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmenttop-h-icon.png")));
63 ui->colorButton->setShowArrow(false);
64 ui->textColorButton->setShowArrow(false);
66 connect(ui->colorButton, SIGNAL(toggled(bool)), this, SLOT(onColorButtonToggled(bool)));
67 connect(ui->textColorButton, SIGNAL(toggled(bool)), this, SLOT(onTextColorButtonToggled(bool)));
69 ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
70 connect(parentContextWidget->colorDialog(), SIGNAL(accepted(QColor)), this, SLOT(onColorDialogApplied(QColor)));
71 connect(parentContextWidget->colorDialog(), SIGNAL(rejected()), this, SLOT(onColorDialogCancled()));
73 connect(ui->fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(onFontSizeChanged(int)));
74 connect(ui->fontSizeSpinBox, SIGNAL(formatChanged()), this, SLOT(onFontFormatChanged()));
76 connect(ui->boldButton, SIGNAL(toggled(bool)), this, SLOT(onBoldCheckedChanged(bool)));
77 connect(ui->italicButton, SIGNAL(toggled(bool)), this, SLOT(onItalicCheckedChanged(bool)));
78 connect(ui->underlineButton, SIGNAL(toggled(bool)), this, SLOT(onUnderlineCheckedChanged(bool)));
79 connect(ui->strikeoutButton, SIGNAL(toggled(bool)), this, SLOT(onStrikeoutCheckedChanged(bool)));
80 connect(ui->fontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(onCurrentFontChanged(QFont)));
82 connect(ui->centerHAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
83 connect(ui->leftAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
84 connect(ui->rightAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
86 connect(ui->centerVAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
87 connect(ui->topAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
88 connect(ui->bottomAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
90 connect(ui->styleComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onStyleComboBoxChanged(QString)));
93 static inline bool checkIfBoolean(QVariant v)
95 return (v.toString() == "true" || v.toString() == "false");
98 void ContextPaneTextWidget::setProperties(QmlJS::PropertyReader *propertyReader)
100 if (propertyReader->hasProperty(QLatin1String("font.pointSize"))) {
101 QVariant variant = propertyReader->readProperty(QLatin1String("font.pointSize"));
103 ui->fontSizeSpinBox->setValue(variant.toInt(&b));
104 ui->fontSizeSpinBox->setEnabled(b);
105 ui->fontSizeSpinBox->setIsPointSize(true);
106 } else if (!propertyReader->hasProperty(QLatin1String("font.pixelSize"))) {
107 ui->fontSizeSpinBox->setValue(8);
108 ui->fontSizeSpinBox->setIsPointSize(true);
111 if (m_fontSizeTimer > 0) {
112 killTimer(m_fontSizeTimer);
113 m_fontSizeTimer = -1;
116 if (propertyReader->hasProperty(QLatin1String("font.pixelSize"))) {
117 QVariant variant = propertyReader->readProperty(QLatin1String("font.pixelSize"));
119 ui->fontSizeSpinBox->setValue(variant.toInt(&b));
121 ui->fontSizeSpinBox->setEnabled(b);
122 ui->fontSizeSpinBox->setIsPixelSize(true);
125 ui->boldButton->setEnabled(true);
126 if (propertyReader->hasProperty(QLatin1String("font.bold"))) {
127 QVariant v = propertyReader->readProperty(QLatin1String("font.bold"));
128 if (checkIfBoolean(v))
129 ui->boldButton->setChecked(v.toBool());
131 ui->boldButton->setEnabled(false);
133 ui->boldButton->setChecked(false);
136 ui->italicButton->setEnabled(true);
137 if (propertyReader->hasProperty(QLatin1String("font.italic"))) {
138 QVariant v = propertyReader->readProperty(QLatin1String("font.italic"));
139 if (checkIfBoolean(v))
140 ui->italicButton->setChecked(v.toBool());
142 ui->italicButton->setEnabled(false);
144 ui->italicButton->setChecked(false);
147 ui->underlineButton->setEnabled(true);
148 if (propertyReader->hasProperty(QLatin1String("font.underline"))) {
149 QVariant v = propertyReader->readProperty(QLatin1String("font.underline"));
150 if (checkIfBoolean(v))
151 ui->underlineButton->setChecked(v.toBool());
153 ui->underlineButton->setEnabled(false);
155 ui->underlineButton->setChecked(false);
158 ui->strikeoutButton->setEnabled(true);
159 if (propertyReader->hasProperty(QLatin1String("font.strikeout"))) {
160 QVariant v = propertyReader->readProperty(QLatin1String("font.strikeout"));
161 if (checkIfBoolean(v))
162 ui->strikeoutButton->setChecked(v.toBool());
164 ui->strikeoutButton->setEnabled(false);
166 ui->strikeoutButton->setChecked(false);
169 if (propertyReader->hasProperty(QLatin1String("color"))) {
170 ui->colorButton->setColor(propertyReader->readProperty("color").toString());
172 ui->colorButton->setColor(QLatin1String("black"));
175 if (propertyReader->hasProperty(QLatin1String("styleColor"))) {
176 ui->textColorButton->setColor(propertyReader->readProperty("styleColor").toString());
178 ui->textColorButton->setColor(QLatin1String("black"));
181 if (propertyReader->hasProperty(QLatin1String("font.family"))) {
182 QString familyName = propertyReader->readProperty(QLatin1String("font.family")).toString();
184 font.setFamily(familyName);
186 ui->fontComboBox->setCurrentFont(font);
187 if (propertyReader->isBindingOrEnum(QLatin1String("font.family")))
188 ui->fontComboBox->setEnabled(false);
190 ui->fontComboBox->setEnabled(true);
193 if (propertyReader->hasProperty(QLatin1String("horizontalAlignment"))) {
194 QString alignment = propertyReader->readProperty(QLatin1String("horizontalAlignment")).toString();
195 ui->leftAlignmentButton->setChecked(true);
196 ui->leftAlignmentButton->setEnabled(true);
197 if (alignment == QLatin1String("Text.AlignHCenter") || alignment == "AlignHCenter")
198 ui->centerHAlignmentButton->setChecked(true);
199 else if (alignment == QLatin1String("Text.AlignRight") || alignment == QLatin1String("AlignRight"))
200 ui->rightAlignmentButton->setChecked(true);
201 else if (alignment == QLatin1String("Text.AlignLeft") || alignment == QLatin1String("AlignLeft"))
202 ui->leftAlignmentButton->setChecked(true);
204 ui->leftAlignmentButton->setEnabled(false);
206 ui->leftAlignmentButton->setChecked(true);
209 if (propertyReader->hasProperty(QLatin1String("verticalAlignment"))) {
210 QString alignment = propertyReader->readProperty(QLatin1String("verticalAlignment")).toString();
211 ui->topAlignmentButton->setChecked(true);
212 ui->bottomAlignmentButton->setEnabled(true);
213 if (alignment == QLatin1String("Text.AlignVCenter") || alignment == QLatin1String("AlignVCenter"))
214 ui->centerVAlignmentButton->setChecked(true);
215 else if (alignment == QLatin1String("Text.AlignBottom") || alignment == QLatin1String("AlignBottom"))
216 ui->bottomAlignmentButton->setChecked(true);
217 else if (alignment == QLatin1String("Text.Top") || alignment == QLatin1String("AlignTop"))
218 ui->topAlignmentButton->setChecked(true);
220 ui->bottomAlignmentButton->setEnabled(false);
222 ui->topAlignmentButton->setChecked(true);
225 if (propertyReader->hasProperty(QLatin1String("style"))) {
226 QString style = propertyReader->readProperty(QLatin1String("style")).toString();
227 ui->styleComboBox->setCurrentIndex(0);
228 ui->styleComboBox->setEnabled(true);
229 if (style == QLatin1String("Text.Outline") || style == QLatin1String("Outline"))
230 ui->styleComboBox->setCurrentIndex(1);
231 else if (style == QLatin1String("Text.Raised") || style == QLatin1String("Raised"))
232 ui->styleComboBox->setCurrentIndex(2);
233 else if (style == QLatin1String("Text.Sunken") || style == QLatin1String("Sunken"))
234 ui->styleComboBox->setCurrentIndex(3);
235 else if (style == QLatin1String("Text.Normal") || style == QLatin1String("Normal"))
236 ui->styleComboBox->setCurrentIndex(0);
238 ui->styleComboBox->setEnabled(false);
240 ui->styleComboBox->setCurrentIndex(0);
244 void ContextPaneTextWidget::setVerticalAlignmentVisible(bool b)
246 ui->centerVAlignmentButton->setEnabled(b);
247 ui->topAlignmentButton->setEnabled(b);
248 ui->bottomAlignmentButton->setEnabled(b);
251 void ContextPaneTextWidget::setStyleVisible(bool b)
253 ui->styleComboBox->setEnabled(b);
254 ui->styleLabel->setEnabled(b);
255 ui->textColorButton->setEnabled(b);
258 void ContextPaneTextWidget::onTextColorButtonToggled(bool flag)
260 ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
262 ui->colorButton->setChecked(false);
263 QPoint p = mapToGlobal(ui->textColorButton->pos());
264 parentContextWidget->colorDialog()->setupColor(ui->textColorButton->color());
265 p = parentContextWidget->colorDialog()->parentWidget()->mapFromGlobal(p);
266 parentContextWidget->onShowColorDialog(flag, p);
269 void ContextPaneTextWidget::onColorButtonToggled(bool flag)
272 ui->textColorButton->setChecked(false);
273 ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
274 QPoint p = mapToGlobal(ui->colorButton->pos());
275 parentContextWidget->colorDialog()->setupColor(ui->colorButton->color());
276 p = parentContextWidget->colorDialog()->parentWidget()->mapFromGlobal(p);
277 parentContextWidget->onShowColorDialog(flag, p);
280 void ContextPaneTextWidget::onColorDialogApplied(const QColor &)
282 ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
283 parentContextWidget->onShowColorDialog(false, QPoint());
284 if (ui->textColorButton->isChecked())
285 emit propertyChanged(QLatin1String("styleColor"),parentContextWidget->colorDialog()->color());; //write back color
286 if (ui->colorButton->isChecked())
287 emit propertyChanged(QLatin1String("color"),parentContextWidget->colorDialog()->color());; //write back color
288 ui->textColorButton->setChecked(false);
289 ui->colorButton->setChecked(false);
292 void ContextPaneTextWidget::onColorDialogCancled()
294 ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
295 parentContextWidget->onShowColorDialog(false, QPoint());
296 ui->colorButton->setChecked(false);
297 ui->colorButton->setChecked(false);
300 void ContextPaneTextWidget::onFontSizeChanged(int)
302 if (m_fontSizeTimer > 0)
303 killTimer(m_fontSizeTimer);
304 m_fontSizeTimer = startTimer(200);
307 void ContextPaneTextWidget::onFontFormatChanged()
309 int size = ui->fontSizeSpinBox->value();
310 if (ui->fontSizeSpinBox->isPointSize()) {
311 emit removeAndChangeProperty(QLatin1String("font.pixelSize"), QLatin1String("font.pointSize"), size, true);
313 emit removeAndChangeProperty(QLatin1String("font.pointSize"), QLatin1String("font.pixelSize"), size, true);
318 void ContextPaneTextWidget::onBoldCheckedChanged(bool value)
321 emit propertyChanged(QLatin1String("font.bold"), value);
323 emit removeProperty(QLatin1String("font.bold"));
327 void ContextPaneTextWidget::onItalicCheckedChanged(bool value)
330 emit propertyChanged(QLatin1String("font.italic"), value);
332 emit removeProperty(QLatin1String("font.italic"));
335 void ContextPaneTextWidget::onUnderlineCheckedChanged(bool value)
338 emit propertyChanged(QLatin1String("font.underline"), value);
340 emit removeProperty(QLatin1String("font.underline"));
343 void ContextPaneTextWidget::onStrikeoutCheckedChanged(bool value)
346 emit propertyChanged(QLatin1String("font.strikeout"), value);
348 emit removeProperty(QLatin1String("font.strikeout"));
351 void ContextPaneTextWidget::onCurrentFontChanged(const QFont &font)
354 emit propertyChanged(QLatin1String("font.family"), QVariant(QString('\"') + font.family() + QString('\"')));
357 void ContextPaneTextWidget::onHorizontalAlignmentChanged()
360 if (ui->centerHAlignmentButton->isChecked())
361 alignment = QLatin1String("Text.AlignHCenter");
362 else if (ui->leftAlignmentButton->isChecked())
363 alignment = QLatin1String("Text.AlignLeft");
364 else if (ui->rightAlignmentButton->isChecked())
365 alignment = QLatin1String("Text.AlignRight");
366 if (m_horizontalAlignment != alignment) {
367 m_horizontalAlignment = alignment;
368 if (alignment == QLatin1String("Text.AlignLeft"))
369 emit removeProperty(QLatin1String("horizontalAlignment"));
371 emit propertyChanged(QLatin1String("horizontalAlignment"), alignment);
375 void ContextPaneTextWidget::onStyleComboBoxChanged(const QString &style)
377 if (style == QLatin1String("Normal"))
378 emit removeProperty(QLatin1String("style"));
380 emit propertyChanged(QLatin1String("style"), QVariant(QLatin1String("Text.") + style));
383 void ContextPaneTextWidget::onVerticalAlignmentChanged()
386 if (ui->centerVAlignmentButton->isChecked())
387 alignment = QLatin1String("Text.AlignVCenter");
388 else if (ui->topAlignmentButton->isChecked())
389 alignment = QLatin1String("Text.AlignTop");
390 else if (ui->bottomAlignmentButton->isChecked())
391 alignment = QLatin1String("Text.AlignBottom");
392 if (m_verticalAlignment != alignment) {
393 m_verticalAlignment = alignment;
394 if (alignment == QLatin1String("Text.AlignTop"))
395 emit removeProperty(QLatin1String("verticalAlignment"));
397 emit propertyChanged(QLatin1String("verticalAlignment"), alignment);
402 ContextPaneTextWidget::~ContextPaneTextWidget()
407 void ContextPaneTextWidget::changeEvent(QEvent *e)
409 QWidget::changeEvent(e);
411 case QEvent::LanguageChange:
412 ui->retranslateUi(this);
419 void ContextPaneTextWidget::timerEvent(QTimerEvent *event)
421 if (event->timerId() == m_fontSizeTimer) {
422 killTimer(m_fontSizeTimer);
423 m_fontSizeTimer = -1;
424 int value = ui->fontSizeSpinBox->value();
425 if (ui->fontSizeSpinBox->isPointSize())
426 emit propertyChanged(QLatin1String("font.pointSize"), value);
428 emit propertyChanged(QLatin1String("font.pixelSize"), value);