OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmleditorwidgets / contextpanetextwidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
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>
40
41 namespace QmlEditorWidgets {
42
43 ContextPaneTextWidget::ContextPaneTextWidget(QWidget *parent) :
44     QWidget(parent),
45     ui(new Ui::ContextPaneTextWidget),
46     m_fontSizeTimer(-1)
47 {
48     ui->setupUi(this);
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")));
53
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")));
57
58     ui->centerVAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentmiddle-h-icon.png")));
59
60     ui->bottomAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentbottom-h-icon.png")));
61     ui->topAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmenttop-h-icon.png")));
62
63     ui->colorButton->setShowArrow(false);
64     ui->textColorButton->setShowArrow(false);
65
66     connect(ui->colorButton, SIGNAL(toggled(bool)), this, SLOT(onColorButtonToggled(bool)));
67     connect(ui->textColorButton, SIGNAL(toggled(bool)), this, SLOT(onTextColorButtonToggled(bool)));
68
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()));
72
73     connect(ui->fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(onFontSizeChanged(int)));
74     connect(ui->fontSizeSpinBox, SIGNAL(formatChanged()), this, SLOT(onFontFormatChanged()));
75
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)));
81
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()));
85
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()));
89
90     connect(ui->styleComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onStyleComboBoxChanged(QString)));
91 }
92
93 static inline bool checkIfBoolean(QVariant v)
94 {
95     return (v.toString() == "true" || v.toString() == "false");
96 }
97
98 void ContextPaneTextWidget::setProperties(QmlJS::PropertyReader *propertyReader)
99 {
100     if (propertyReader->hasProperty(QLatin1String("font.pointSize"))) {
101         QVariant variant = propertyReader->readProperty(QLatin1String("font.pointSize"));
102         bool b;
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);
109     }
110
111     if (m_fontSizeTimer > 0) {
112         killTimer(m_fontSizeTimer);
113         m_fontSizeTimer = -1;
114     }
115
116     if (propertyReader->hasProperty(QLatin1String("font.pixelSize"))) {
117         QVariant variant = propertyReader->readProperty(QLatin1String("font.pixelSize"));
118         bool b;
119         ui->fontSizeSpinBox->setValue(variant.toInt(&b));
120
121         ui->fontSizeSpinBox->setEnabled(b);
122         ui->fontSizeSpinBox->setIsPixelSize(true);
123     }
124
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());
130         else
131             ui->boldButton->setEnabled(false);
132     } else {
133         ui->boldButton->setChecked(false);
134     }
135
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());
141         else
142             ui->italicButton->setEnabled(false);
143     } else {
144         ui->italicButton->setChecked(false);
145     }
146
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());
152         else
153             ui->underlineButton->setEnabled(false);
154     } else {
155         ui->underlineButton->setChecked(false);
156     }
157
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());
163         else
164             ui->strikeoutButton->setEnabled(false);
165     } else {
166         ui->strikeoutButton->setChecked(false);
167     }
168
169     if (propertyReader->hasProperty(QLatin1String("color"))) {
170         ui->colorButton->setColor(propertyReader->readProperty("color").toString());
171     } else {
172         ui->colorButton->setColor(QLatin1String("black"));
173     }
174
175     if (propertyReader->hasProperty(QLatin1String("styleColor"))) {
176         ui->textColorButton->setColor(propertyReader->readProperty("styleColor").toString());
177     } else {
178         ui->textColorButton->setColor(QLatin1String("black"));
179     }
180
181     if (propertyReader->hasProperty(QLatin1String("font.family"))) {
182         QString familyName = propertyReader->readProperty(QLatin1String("font.family")).toString();
183         QFont font;
184         font.setFamily(familyName);
185
186         ui->fontComboBox->setCurrentFont(font);
187         if (propertyReader->isBindingOrEnum(QLatin1String("font.family")))
188             ui->fontComboBox->setEnabled(false);
189         else
190             ui->fontComboBox->setEnabled(true);
191     }
192
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);
203         else
204             ui->leftAlignmentButton->setEnabled(false);
205     } else {
206         ui->leftAlignmentButton->setChecked(true);
207     }
208
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);
219         else
220             ui->bottomAlignmentButton->setEnabled(false);
221     } else {
222         ui->topAlignmentButton->setChecked(true);
223     }
224
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);
237         else
238             ui->styleComboBox->setEnabled(false);
239     } else {
240         ui->styleComboBox->setCurrentIndex(0);
241     }
242 }
243
244 void ContextPaneTextWidget::setVerticalAlignmentVisible(bool b)
245 {
246     ui->centerVAlignmentButton->setEnabled(b);
247     ui->topAlignmentButton->setEnabled(b);
248     ui->bottomAlignmentButton->setEnabled(b);
249 }
250
251 void ContextPaneTextWidget::setStyleVisible(bool b)
252 {
253     ui->styleComboBox->setEnabled(b);
254     ui->styleLabel->setEnabled(b);
255     ui->textColorButton->setEnabled(b);
256 }
257
258 void ContextPaneTextWidget::onTextColorButtonToggled(bool flag)
259 {
260     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
261     if (flag)
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);
267 }
268
269 void ContextPaneTextWidget::onColorButtonToggled(bool flag)
270 {
271     if (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);
278 }
279
280 void ContextPaneTextWidget::onColorDialogApplied(const QColor &)
281 {
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);
290 }
291
292 void ContextPaneTextWidget::onColorDialogCancled()
293 {
294     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
295     parentContextWidget->onShowColorDialog(false, QPoint());
296     ui->colorButton->setChecked(false);
297     ui->colorButton->setChecked(false);
298 }
299
300 void ContextPaneTextWidget::onFontSizeChanged(int)
301 {  
302     if (m_fontSizeTimer > 0)
303         killTimer(m_fontSizeTimer);
304     m_fontSizeTimer = startTimer(200);
305 }
306
307 void ContextPaneTextWidget::onFontFormatChanged()
308 {
309     int size = ui->fontSizeSpinBox->value();
310     if (ui->fontSizeSpinBox->isPointSize()) {   
311         emit removeAndChangeProperty(QLatin1String("font.pixelSize"), QLatin1String("font.pointSize"), size, true);
312     } else {        
313         emit removeAndChangeProperty(QLatin1String("font.pointSize"), QLatin1String("font.pixelSize"), size, true);
314     }
315
316 }
317
318 void ContextPaneTextWidget::onBoldCheckedChanged(bool value)
319 {
320     if (value)
321         emit propertyChanged(QLatin1String("font.bold"), value);
322     else
323         emit removeProperty(QLatin1String("font.bold"));
324
325 }
326
327 void ContextPaneTextWidget::onItalicCheckedChanged(bool value)
328 {
329     if (value)
330         emit propertyChanged(QLatin1String("font.italic"), value);
331     else
332         emit removeProperty(QLatin1String("font.italic"));
333 }
334
335 void ContextPaneTextWidget::onUnderlineCheckedChanged(bool value)
336 {
337     if (value)
338         emit propertyChanged(QLatin1String("font.underline"), value);
339     else
340         emit removeProperty(QLatin1String("font.underline"));
341 }
342
343 void ContextPaneTextWidget::onStrikeoutCheckedChanged(bool value)
344 {
345     if (value)
346         emit propertyChanged(QLatin1String("font.strikeout"), value);
347     else
348         emit removeProperty(QLatin1String("font.strikeout"));
349 }
350
351 void ContextPaneTextWidget::onCurrentFontChanged(const QFont &font)
352 {
353     font.family();
354     emit propertyChanged(QLatin1String("font.family"), QVariant(QString('\"') + font.family() + QString('\"')));
355 }
356
357 void ContextPaneTextWidget::onHorizontalAlignmentChanged()
358 {
359     QString alignment;
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"));
370         else
371             emit propertyChanged(QLatin1String("horizontalAlignment"), alignment);
372     }
373 }
374
375 void ContextPaneTextWidget::onStyleComboBoxChanged(const QString &style)
376 {
377     if (style == QLatin1String("Normal"))
378         emit removeProperty(QLatin1String("style"));
379     else
380         emit propertyChanged(QLatin1String("style"), QVariant(QLatin1String("Text.") + style));
381 }
382
383 void ContextPaneTextWidget::onVerticalAlignmentChanged()
384 {
385     QString alignment;
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"));
396         else
397             emit propertyChanged(QLatin1String("verticalAlignment"), alignment);
398     }
399 }
400
401
402 ContextPaneTextWidget::~ContextPaneTextWidget()
403 {
404     delete ui;
405 }
406
407 void ContextPaneTextWidget::changeEvent(QEvent *e)
408 {
409     QWidget::changeEvent(e);
410     switch (e->type()) {
411     case QEvent::LanguageChange:
412         ui->retranslateUi(this);
413         break;
414     default:
415         break;
416     }
417 }
418
419 void ContextPaneTextWidget::timerEvent(QTimerEvent *event)
420 {
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);
427         else
428             emit propertyChanged(QLatin1String("font.pixelSize"), value);
429     }
430 }
431
432 } //QmlDesigner