OSDN Git Service

Update license.
[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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "contextpanetextwidget.h"
34 #include "contextpanewidget.h"
35 #include "customcolordialog.h"
36 #include "ui_contextpanetext.h"
37 #include <qmljs/qmljspropertyreader.h>
38 #include <QtCore/QTimerEvent>
39 #include <QtCore/QVariant>
40 namespace QmlEditorWidgets {
41
42 ContextPaneTextWidget::ContextPaneTextWidget(QWidget *parent) :
43     QWidget(parent),
44     ui(new Ui::ContextPaneTextWidget),
45     m_fontSizeTimer(-1)
46 {
47     ui->setupUi(this);
48     ui->boldButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/bold-h-icon.png")));
49     ui->italicButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/italic-h-icon.png")));
50     ui->underlineButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/underline-h-icon.png")));
51     ui->strikeoutButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/strikeout-h-icon.png")));
52
53     ui->leftAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentleft-h-icon.png")));
54     ui->centerHAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentcenterh-h-icon.png")));
55     ui->rightAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentright-h-icon.png")));
56
57     ui->centerVAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentmiddle-h-icon.png")));
58
59     ui->bottomAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmentbottom-h-icon.png")));
60     ui->topAlignmentButton->setIcon(QIcon(QLatin1String(":/qmldesigner/images/alignmenttop-h-icon.png")));
61
62     ui->colorButton->setShowArrow(false);
63     ui->textColorButton->setShowArrow(false);
64
65     connect(ui->colorButton, SIGNAL(toggled(bool)), this, SLOT(onColorButtonToggled(bool)));
66     connect(ui->textColorButton, SIGNAL(toggled(bool)), this, SLOT(onTextColorButtonToggled(bool)));
67
68     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
69     connect(parentContextWidget->colorDialog(), SIGNAL(accepted(QColor)), this, SLOT(onColorDialogApplied(QColor)));
70     connect(parentContextWidget->colorDialog(), SIGNAL(rejected()), this, SLOT(onColorDialogCancled()));
71
72     connect(ui->fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(onFontSizeChanged(int)));
73     connect(ui->fontSizeSpinBox, SIGNAL(formatChanged()), this, SLOT(onFontFormatChanged()));
74
75     connect(ui->boldButton, SIGNAL(toggled(bool)), this, SLOT(onBoldCheckedChanged(bool)));
76     connect(ui->italicButton, SIGNAL(toggled(bool)), this, SLOT(onItalicCheckedChanged(bool)));
77     connect(ui->underlineButton, SIGNAL(toggled(bool)), this, SLOT(onUnderlineCheckedChanged(bool)));
78     connect(ui->strikeoutButton, SIGNAL(toggled(bool)), this, SLOT(onStrikeoutCheckedChanged(bool)));
79     connect(ui->fontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(onCurrentFontChanged(QFont)));
80
81     connect(ui->centerHAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
82     connect(ui->leftAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
83     connect(ui->rightAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onHorizontalAlignmentChanged()));
84
85     connect(ui->centerVAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
86     connect(ui->topAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
87     connect(ui->bottomAlignmentButton, SIGNAL(toggled(bool)), this, SLOT(onVerticalAlignmentChanged()));
88
89     connect(ui->styleComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onStyleComboBoxChanged(QString)));
90 }
91
92 static inline bool checkIfBoolean(QVariant v)
93 {
94     return (v.toString() == "true" || v.toString() == "false");
95 }
96
97 void ContextPaneTextWidget::setProperties(QmlJS::PropertyReader *propertyReader)
98 {
99     if (propertyReader->hasProperty(QLatin1String("font.pointSize"))) {
100         QVariant variant = propertyReader->readProperty(QLatin1String("font.pointSize"));
101         bool b;
102         ui->fontSizeSpinBox->setValue(variant.toInt(&b));        
103         ui->fontSizeSpinBox->setEnabled(b);
104         ui->fontSizeSpinBox->setIsPointSize(true);
105     } else if (!propertyReader->hasProperty(QLatin1String("font.pixelSize"))) {
106         ui->fontSizeSpinBox->setValue(8);
107         ui->fontSizeSpinBox->setIsPointSize(true);
108     }
109
110     if (m_fontSizeTimer > 0) {
111         killTimer(m_fontSizeTimer);
112         m_fontSizeTimer = -1;
113     }
114
115     if (propertyReader->hasProperty(QLatin1String("font.pixelSize"))) {
116         QVariant variant = propertyReader->readProperty(QLatin1String("font.pixelSize"));
117         bool b;
118         ui->fontSizeSpinBox->setValue(variant.toInt(&b));
119
120         ui->fontSizeSpinBox->setEnabled(b);
121         ui->fontSizeSpinBox->setIsPixelSize(true);
122     }
123
124     ui->boldButton->setEnabled(true);
125     if (propertyReader->hasProperty(QLatin1String("font.bold"))) {
126         QVariant v = propertyReader->readProperty(QLatin1String("font.bold"));
127         if (checkIfBoolean(v))
128             ui->boldButton->setChecked(v.toBool());
129         else
130             ui->boldButton->setEnabled(false);
131     } else {
132         ui->boldButton->setChecked(false);
133     }
134
135     ui->italicButton->setEnabled(true);
136     if (propertyReader->hasProperty(QLatin1String("font.italic"))) {
137         QVariant v = propertyReader->readProperty(QLatin1String("font.italic"));
138         if (checkIfBoolean(v))
139             ui->italicButton->setChecked(v.toBool());
140         else
141             ui->italicButton->setEnabled(false);
142     } else {
143         ui->italicButton->setChecked(false);
144     }
145
146     ui->underlineButton->setEnabled(true);
147     if (propertyReader->hasProperty(QLatin1String("font.underline"))) {
148         QVariant v = propertyReader->readProperty(QLatin1String("font.underline"));
149         if (checkIfBoolean(v))
150             ui->underlineButton->setChecked(v.toBool());
151         else
152             ui->underlineButton->setEnabled(false);
153     } else {
154         ui->underlineButton->setChecked(false);
155     }
156
157     ui->strikeoutButton->setEnabled(true);
158     if (propertyReader->hasProperty(QLatin1String("font.strikeout"))) {
159         QVariant v = propertyReader->readProperty(QLatin1String("font.strikeout"));
160         if (checkIfBoolean(v))
161             ui->strikeoutButton->setChecked(v.toBool());
162         else
163             ui->strikeoutButton->setEnabled(false);
164     } else {
165         ui->strikeoutButton->setChecked(false);
166     }
167
168     if (propertyReader->hasProperty(QLatin1String("color"))) {
169         ui->colorButton->setColor(propertyReader->readProperty("color").toString());
170     } else {
171         ui->colorButton->setColor(QLatin1String("black"));
172     }
173
174     if (propertyReader->hasProperty(QLatin1String("styleColor"))) {
175         ui->textColorButton->setColor(propertyReader->readProperty("styleColor").toString());
176     } else {
177         ui->textColorButton->setColor(QLatin1String("black"));
178     }
179
180     if (propertyReader->hasProperty(QLatin1String("font.family"))) {
181         QString familyName = propertyReader->readProperty(QLatin1String("font.family")).toString();
182         QFont font;
183         font.setFamily(familyName);
184
185         ui->fontComboBox->setCurrentFont(font);
186         if (propertyReader->isBindingOrEnum(QLatin1String("font.family")))
187             ui->fontComboBox->setEnabled(false);
188         else
189             ui->fontComboBox->setEnabled(true);
190     }
191
192     if (propertyReader->hasProperty(QLatin1String("horizontalAlignment"))) {
193         QString alignment = propertyReader->readProperty(QLatin1String("horizontalAlignment")).toString();
194         ui->leftAlignmentButton->setChecked(true);
195         ui->leftAlignmentButton->setEnabled(true);
196         if (alignment == QLatin1String("Text.AlignHCenter") || alignment == "AlignHCenter")
197             ui->centerHAlignmentButton->setChecked(true);
198         else if (alignment == QLatin1String("Text.AlignRight") || alignment == QLatin1String("AlignRight"))
199             ui->rightAlignmentButton->setChecked(true);
200         else if (alignment == QLatin1String("Text.AlignLeft") || alignment == QLatin1String("AlignLeft"))
201             ui->leftAlignmentButton->setChecked(true);
202         else
203             ui->leftAlignmentButton->setEnabled(false);
204     } else {
205         ui->leftAlignmentButton->setChecked(true);
206     }
207
208     if (propertyReader->hasProperty(QLatin1String("verticalAlignment"))) {
209         QString alignment = propertyReader->readProperty(QLatin1String("verticalAlignment")).toString();
210         ui->topAlignmentButton->setChecked(true);
211         ui->bottomAlignmentButton->setEnabled(true);
212         if (alignment == QLatin1String("Text.AlignVCenter") || alignment == QLatin1String("AlignVCenter"))
213             ui->centerVAlignmentButton->setChecked(true);
214         else if (alignment == QLatin1String("Text.AlignBottom") || alignment == QLatin1String("AlignBottom"))
215             ui->bottomAlignmentButton->setChecked(true);
216         else if (alignment == QLatin1String("Text.Top") || alignment == QLatin1String("AlignTop"))
217             ui->topAlignmentButton->setChecked(true);
218         else
219             ui->bottomAlignmentButton->setEnabled(false);
220     } else {
221         ui->topAlignmentButton->setChecked(true);
222     }
223
224     if (propertyReader->hasProperty(QLatin1String("style"))) {
225         QString style = propertyReader->readProperty(QLatin1String("style")).toString();
226         ui->styleComboBox->setCurrentIndex(0);
227         ui->styleComboBox->setEnabled(true);
228         if (style == QLatin1String("Text.Outline") || style == QLatin1String("Outline"))
229             ui->styleComboBox->setCurrentIndex(1);
230         else if (style == QLatin1String("Text.Raised") || style == QLatin1String("Raised"))
231             ui->styleComboBox->setCurrentIndex(2);
232         else if (style == QLatin1String("Text.Sunken") || style == QLatin1String("Sunken"))
233             ui->styleComboBox->setCurrentIndex(3);
234         else if (style == QLatin1String("Text.Normal") || style == QLatin1String("Normal"))
235             ui->styleComboBox->setCurrentIndex(0);
236         else
237             ui->styleComboBox->setEnabled(false);
238     } else {
239         ui->styleComboBox->setCurrentIndex(0);
240     }
241 }
242
243 void ContextPaneTextWidget::setVerticalAlignmentVisible(bool b)
244 {
245     ui->centerVAlignmentButton->setEnabled(b);
246     ui->topAlignmentButton->setEnabled(b);
247     ui->bottomAlignmentButton->setEnabled(b);
248 }
249
250 void ContextPaneTextWidget::setStyleVisible(bool b)
251 {
252     ui->styleComboBox->setEnabled(b);
253     ui->styleLabel->setEnabled(b);
254     ui->textColorButton->setEnabled(b);
255 }
256
257 void ContextPaneTextWidget::onTextColorButtonToggled(bool flag)
258 {
259     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
260     if (flag)
261         ui->colorButton->setChecked(false);
262     QPoint p = mapToGlobal(ui->textColorButton->pos());
263     parentContextWidget->colorDialog()->setupColor(ui->textColorButton->color());
264     p = parentContextWidget->colorDialog()->parentWidget()->mapFromGlobal(p);
265     parentContextWidget->onShowColorDialog(flag, p);
266 }
267
268 void ContextPaneTextWidget::onColorButtonToggled(bool flag)
269 {
270     if (flag)
271         ui->textColorButton->setChecked(false);
272     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
273     QPoint p = mapToGlobal(ui->colorButton->pos());
274     parentContextWidget->colorDialog()->setupColor(ui->colorButton->color());
275     p = parentContextWidget->colorDialog()->parentWidget()->mapFromGlobal(p);    
276     parentContextWidget->onShowColorDialog(flag, p);
277 }
278
279 void ContextPaneTextWidget::onColorDialogApplied(const QColor &)
280 {
281     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
282     parentContextWidget->onShowColorDialog(false, QPoint());
283     if (ui->textColorButton->isChecked())
284         emit  propertyChanged(QLatin1String("styleColor"),parentContextWidget->colorDialog()->color());; //write back color
285     if (ui->colorButton->isChecked())
286         emit  propertyChanged(QLatin1String("color"),parentContextWidget->colorDialog()->color());; //write back color
287     ui->textColorButton->setChecked(false);
288     ui->colorButton->setChecked(false);
289 }
290
291 void ContextPaneTextWidget::onColorDialogCancled()
292 {
293     ContextPaneWidget *parentContextWidget = qobject_cast<ContextPaneWidget*>(parentWidget());
294     parentContextWidget->onShowColorDialog(false, QPoint());
295     ui->colorButton->setChecked(false);
296     ui->colorButton->setChecked(false);
297 }
298
299 void ContextPaneTextWidget::onFontSizeChanged(int)
300 {  
301     if (m_fontSizeTimer > 0)
302         killTimer(m_fontSizeTimer);
303     m_fontSizeTimer = startTimer(200);
304 }
305
306 void ContextPaneTextWidget::onFontFormatChanged()
307 {
308     int size = ui->fontSizeSpinBox->value();
309     if (ui->fontSizeSpinBox->isPointSize()) {   
310         emit removeAndChangeProperty(QLatin1String("font.pixelSize"), QLatin1String("font.pointSize"), size, true);
311     } else {        
312         emit removeAndChangeProperty(QLatin1String("font.pointSize"), QLatin1String("font.pixelSize"), size, true);
313     }
314
315 }
316
317 void ContextPaneTextWidget::onBoldCheckedChanged(bool value)
318 {
319     if (value)
320         emit propertyChanged(QLatin1String("font.bold"), value);
321     else
322         emit removeProperty(QLatin1String("font.bold"));
323
324 }
325
326 void ContextPaneTextWidget::onItalicCheckedChanged(bool value)
327 {
328     if (value)
329         emit propertyChanged(QLatin1String("font.italic"), value);
330     else
331         emit removeProperty(QLatin1String("font.italic"));
332 }
333
334 void ContextPaneTextWidget::onUnderlineCheckedChanged(bool value)
335 {
336     if (value)
337         emit propertyChanged(QLatin1String("font.underline"), value);
338     else
339         emit removeProperty(QLatin1String("font.underline"));
340 }
341
342 void ContextPaneTextWidget::onStrikeoutCheckedChanged(bool value)
343 {
344     if (value)
345         emit propertyChanged(QLatin1String("font.strikeout"), value);
346     else
347         emit removeProperty(QLatin1String("font.strikeout"));
348 }
349
350 void ContextPaneTextWidget::onCurrentFontChanged(const QFont &font)
351 {
352     font.family();
353     emit propertyChanged(QLatin1String("font.family"), QVariant(QString('\"') + font.family() + QString('\"')));
354 }
355
356 void ContextPaneTextWidget::onHorizontalAlignmentChanged()
357 {
358     QString alignment;
359     if (ui->centerHAlignmentButton->isChecked())
360         alignment = QLatin1String("Text.AlignHCenter");
361     else if (ui->leftAlignmentButton->isChecked())
362         alignment = QLatin1String("Text.AlignLeft");
363     else if (ui->rightAlignmentButton->isChecked())
364         alignment = QLatin1String("Text.AlignRight");
365     if (m_horizontalAlignment != alignment) {
366         m_horizontalAlignment = alignment;
367         if (alignment == QLatin1String("Text.AlignLeft"))
368             emit removeProperty(QLatin1String("horizontalAlignment"));
369         else
370             emit propertyChanged(QLatin1String("horizontalAlignment"), alignment);
371     }
372 }
373
374 void ContextPaneTextWidget::onStyleComboBoxChanged(const QString &style)
375 {
376     if (style == QLatin1String("Normal"))
377         emit removeProperty(QLatin1String("style"));
378     else
379         emit propertyChanged(QLatin1String("style"), QVariant(QLatin1String("Text.") + style));
380 }
381
382 void ContextPaneTextWidget::onVerticalAlignmentChanged()
383 {
384     QString alignment;
385     if (ui->centerVAlignmentButton->isChecked())
386         alignment = QLatin1String("Text.AlignVCenter");
387     else if (ui->topAlignmentButton->isChecked())
388         alignment = QLatin1String("Text.AlignTop");
389     else if (ui->bottomAlignmentButton->isChecked())
390         alignment = QLatin1String("Text.AlignBottom");
391     if (m_verticalAlignment != alignment) {
392         m_verticalAlignment = alignment;
393         if (alignment == QLatin1String("Text.AlignTop"))
394             emit removeProperty(QLatin1String("verticalAlignment"));
395         else
396             emit propertyChanged(QLatin1String("verticalAlignment"), alignment);
397     }
398 }
399
400
401 ContextPaneTextWidget::~ContextPaneTextWidget()
402 {
403     delete ui;
404 }
405
406 void ContextPaneTextWidget::changeEvent(QEvent *e)
407 {
408     QWidget::changeEvent(e);
409     switch (e->type()) {
410     case QEvent::LanguageChange:
411         ui->retranslateUi(this);
412         break;
413     default:
414         break;
415     }
416 }
417
418 void ContextPaneTextWidget::timerEvent(QTimerEvent *event)
419 {
420     if (event->timerId() == m_fontSizeTimer) {
421         killTimer(m_fontSizeTimer);
422         m_fontSizeTimer = -1;
423         int value = ui->fontSizeSpinBox->value();
424         if (ui->fontSizeSpinBox->isPointSize())
425             emit propertyChanged(QLatin1String("font.pointSize"), value);
426         else
427             emit propertyChanged(QLatin1String("font.pixelSize"), value);
428     }
429 }
430
431 } //QmlDesigner