OSDN Git Service

Merge branch '2.3'
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmljseditor / quicktoolbar.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 info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "quicktoolbar.h"
34 #include <contextpanewidget.h>
35 #include <quicktoolbarsettingspage.h>
36
37 #include <utils/changeset.h>
38 #include <qmljs/parser/qmljsast_p.h>
39 #include <qmljs/qmljsdocument.h>
40 #include <qmljs/qmljspropertyreader.h>
41 #include <qmljs/qmljsrewriter.h>
42 #include <qmljs/qmljsindenter.h>
43 #include <qmljs/qmljslookupcontext.h>
44 #include <qmljs/qmljscontext.h>
45 #include <qmljs/qmljsbind.h>
46 #include <qmljs/qmljsscopebuilder.h>
47 #include <qmljs/qmljsevaluate.h>
48 #include <texteditor/basetexteditor.h>
49 #include <texteditor/tabsettings.h>
50 #include <coreplugin/icore.h>
51 #include <customcolordialog.h>
52
53 #include <QtCore/QDebug>
54
55 using namespace QmlJS;
56 using namespace AST;
57 using namespace QmlEditorWidgets;
58
59 namespace QmlJSEditor {
60
61 static inline QString textAt(const Document* doc,
62                                       const SourceLocation &from,
63                                       const SourceLocation &to)
64 {
65     return doc->source().mid(from.offset, to.end() - from.begin());
66 }
67
68 static inline const Interpreter::ObjectValue * getPropertyChangesTarget(Node *node, LookupContext::Ptr lookupContext)
69 {
70     UiObjectInitializer *initializer = 0;
71     if (UiObjectDefinition *definition = cast<UiObjectDefinition *>(node))
72         initializer = definition->initializer;
73     if (UiObjectBinding *binding = cast<UiObjectBinding *>(node))
74         initializer = binding->initializer;
75     if (initializer) {
76         for (UiObjectMemberList *members = initializer->members; members; members = members->next) {
77             if (UiScriptBinding *scriptBinding = cast<UiScriptBinding *>(members->member)) {
78                 if (scriptBinding->qualifiedId
79                         && scriptBinding->qualifiedId->name->asString() == QLatin1String("target")
80                         && ! scriptBinding->qualifiedId->next) {
81                     Evaluate evaluator(lookupContext->context());
82                     const Interpreter::Value *targetValue = evaluator(scriptBinding->statement);
83                     if (const Interpreter::ObjectValue *targetObject = Interpreter::value_cast<const Interpreter::ObjectValue *>(targetValue)) {
84                         return targetObject;
85                     } else {
86                         return 0;
87                     }
88                 }
89             }
90         }
91     }
92     return 0;
93 }
94
95 QuickToolBar::QuickToolBar(QObject *parent)
96     : ::QmlJS::IContextPane(parent)
97     , m_editor(0)
98     , m_blockWriting(false)
99 {
100     m_node = 0;
101     contextWidget();
102
103     m_propertyOrder
104                << QLatin1String("id")
105                << QLatin1String("name")
106                << QLatin1String("target")
107                << QLatin1String("property")
108                << QLatin1String("x")
109                << QLatin1String("y")
110                << QLatin1String("width")
111                << QLatin1String("height")
112                << QLatin1String("position")
113                << QLatin1String("color")
114                << QLatin1String("radius")
115                << QLatin1String("text")
116                << QLatin1String("font.family")
117                << QLatin1String("font.bold")
118                << QLatin1String("font.italic")
119                << QLatin1String("font.underline")
120                << QLatin1String("font.strikeout")
121                << QString::null
122                << QLatin1String("states")
123                << QLatin1String("transitions")
124                ;
125 }
126
127 QuickToolBar::~QuickToolBar()
128 {
129     //if the pane was never activated the widget is not in a widget tree
130     if (!m_widget.isNull())
131         delete m_widget.data();
132         m_widget.clear();
133 }
134
135 void QuickToolBar::apply(TextEditor::BaseTextEditor *editor, Document::Ptr document, LookupContext::Ptr lookupContext, AST::Node *node, bool update, bool force)
136 {
137     if (!QuickToolBarSettings::get().enableContextPane && !force && !update) {
138         contextWidget()->hide();
139         return;
140     }
141
142     if (document.isNull())
143         return;
144
145     if (update && editor != m_editor)
146         return; //do not update for different editor
147
148     m_blockWriting = true;
149
150     const Interpreter::ObjectValue *scopeObject = document->bind()->findQmlObject(node);
151
152     bool isPropertyChanges = false;
153
154     if (!lookupContext.isNull() && scopeObject) {
155         m_prototypes.clear();
156         foreach (const Interpreter::ObjectValue *object,
157                  Interpreter::PrototypeIterator(scopeObject, lookupContext->context()).all()) {
158             m_prototypes.append(object->className());
159         }
160
161         if (m_prototypes.contains("PropertyChanges")) {
162             isPropertyChanges = true;
163             const Interpreter::ObjectValue *targetObject = getPropertyChangesTarget(node, lookupContext);
164             m_prototypes.clear();
165             if (targetObject) {
166                 foreach (const Interpreter::ObjectValue *object,
167                          Interpreter::PrototypeIterator(targetObject, lookupContext->context()).all()) {
168                     m_prototypes.append(object->className());
169                 }
170             }
171         }
172     }
173
174     setEnabled(document->isParsedCorrectly());
175     m_editor = editor;
176     contextWidget()->setParent(editor->widget()->parentWidget());
177     contextWidget()->colorDialog()->setParent(editor->widget()->parentWidget());
178
179     if (cast<UiObjectDefinition*>(node) || cast<UiObjectBinding*>(node)) {
180         UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
181         UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);
182
183         QString name;
184         quint32 offset = 0;
185         quint32 end = 0;
186         UiObjectInitializer *initializer = 0;
187         if (objectDefinition) {
188             name = objectDefinition->qualifiedTypeNameId->name->asString();
189             initializer = objectDefinition->initializer;
190             offset = objectDefinition->firstSourceLocation().offset;
191             end = objectDefinition->lastSourceLocation().end();
192         } else if (objectBinding) {
193             name = objectBinding->qualifiedTypeNameId->name->asString();
194             initializer = objectBinding->initializer;
195             offset = objectBinding->firstSourceLocation().offset;
196             end = objectBinding->lastSourceLocation().end();
197         }
198
199         if (lookupContext.isNull()) {
200             if (name != m_oldType)
201                 m_prototypes.clear();
202         }
203
204         m_oldType = name;
205
206         m_prototypes.append(name);
207
208         int line1;
209         int column1;
210         int line2;
211         int column2;
212         m_editor->convertPosition(offset, &line1, &column1); //get line
213         m_editor->convertPosition(end, &line2, &column2); //get line
214
215         QRegion reg;
216         if (line1 > -1 && line2 > -1)
217             reg = m_editor->editorWidget()->translatedLineRegion(line1 - 1, line2);
218
219         QRect rect;
220         rect.setHeight(widget()->height() + 10);
221         rect.setWidth(reg.boundingRect().width() - reg.boundingRect().left());
222         rect.moveTo(reg.boundingRect().topLeft());
223         reg = reg.intersect(rect);
224
225         if (contextWidget()->acceptsType(m_prototypes)) {
226             m_node = 0;
227             PropertyReader propertyReader(document, initializer);
228             QTextCursor tc(editor->editorWidget()->document());
229             tc.setPosition(offset); 
230             QPoint p1 = editor->editorWidget()->mapToParent(editor->editorWidget()->viewport()->mapToParent(editor->editorWidget()->cursorRect(tc).topLeft()) - QPoint(0, contextWidget()->height() + 10));
231             tc.setPosition(end);
232             QPoint p2 = editor->editorWidget()->mapToParent(editor->editorWidget()->viewport()->mapToParent(editor->editorWidget()->cursorRect(tc).bottomLeft()) + QPoint(0, 10));
233             QPoint offset = QPoint(10, 0);
234             if (reg.boundingRect().width() < 400)
235                 offset = QPoint(400 - reg.boundingRect().width() + 10 ,0);
236             QPoint p3 = editor->editorWidget()->mapToParent(editor->editorWidget()->viewport()->mapToParent(reg.boundingRect().topRight()) + offset);
237             p2.setX(p1.x());
238             contextWidget()->setIsPropertyChanges(isPropertyChanges);
239             if (!update)
240                 contextWidget()->setType(m_prototypes);
241             if (!update)
242                 contextWidget()->activate(p3 , p1, p2, QuickToolBarSettings::get().pinContextPane);
243             else
244                 contextWidget()->rePosition(p3 , p1, p2, QuickToolBarSettings::get().pinContextPane);
245             contextWidget()->setOptions(QuickToolBarSettings::get().enableContextPane, QuickToolBarSettings::get().pinContextPane);
246             contextWidget()->setPath(document->path());
247             contextWidget()->setProperties(&propertyReader); 
248             m_doc = document;
249             m_node = node;
250         } else {
251             contextWidget()->setParent(0);
252             contextWidget()->hide();
253             contextWidget()->colorDialog()->hide();
254         }
255     } else {
256         contextWidget()->setParent(0);
257         contextWidget()->hide();
258         contextWidget()->colorDialog()->hide();
259     }
260
261     m_blockWriting = false;
262
263 }
264
265 bool QuickToolBar::isAvailable(TextEditor::BaseTextEditor *, Document::Ptr document, AST::Node *node)
266 {
267     if (document.isNull())
268         return false;
269
270     if (!node)
271         return false;
272
273     QString name;
274
275     UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
276     UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);
277     if (objectDefinition) {
278         name = objectDefinition->qualifiedTypeNameId->name->asString();
279
280     } else if (objectBinding) {
281         name = objectBinding->qualifiedTypeNameId->name->asString();
282     }
283
284     QStringList prototypes;
285     prototypes.append(name);
286
287     if (prototypes.contains("Rectangle") ||
288             prototypes.contains("Image") ||
289             prototypes.contains("BorderImage") ||
290             prototypes.contains("TextEdit") ||
291             prototypes.contains("TextInput") ||
292             prototypes.contains("PropertyAnimation") ||
293             prototypes.contains("NumberAnimation") ||
294             prototypes.contains("Text") ||
295             prototypes.contains("PropertyChanges"))
296         return true;
297
298     return false;
299 }
300
301 void QuickToolBar::setProperty(const QString &propertyName, const QVariant &value)
302 {
303
304     QString stringValue = value.toString();
305     if (value.type() == QVariant::Color)
306         stringValue = QChar('\"') + value.toString() + QChar('\"');
307
308     if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
309         UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
310         UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);
311
312         UiObjectInitializer *initializer = 0;
313         if (objectDefinition)
314             initializer = objectDefinition->initializer;
315         else if (objectBinding)
316             initializer = objectBinding->initializer;
317
318         Utils::ChangeSet changeSet;
319         Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);
320
321         int line = -1;
322         int endLine;
323
324         Rewriter::BindingType bindingType = Rewriter::ScriptBinding;
325
326         if (stringValue.contains("{") && stringValue.contains("}"))
327             bindingType = Rewriter::ObjectBinding;
328
329         PropertyReader propertyReader(m_doc, initializer);
330         if (propertyReader.hasProperty(propertyName)) {
331             rewriter.changeBinding(initializer, propertyName, stringValue, bindingType);
332         } else {
333             rewriter.addBinding(initializer, propertyName, stringValue, bindingType);
334         }
335
336         int column;
337
338         int changeSetPos = changeSet.operationList().last().pos1;
339         int changeSetLength = changeSet.operationList().last().text.length();
340         QTextCursor tc = m_editor->editorWidget()->textCursor();
341         tc.beginEditBlock();
342         changeSet.apply(&tc);
343
344         m_editor->convertPosition(changeSetPos, &line, &column); //get line
345         m_editor->convertPosition(changeSetPos + changeSetLength, &endLine, &column); //get line
346
347         if (line > 0) {
348             TextEditor::TabSettings ts = m_editor->editorWidget()->tabSettings();
349             QmlJSIndenter indenter;
350             indenter.setTabSize(ts.m_tabSize);
351             indenter.setIndentSize(ts.m_indentSize);
352
353             for (int i=line;i<=endLine;i++) {
354                 QTextBlock start = m_editor->editorWidget()->document()->findBlockByNumber(i);
355                 QTextBlock end = m_editor->editorWidget()->document()->findBlockByNumber(i);
356
357                 if (end.isValid()) {
358                     const int indent = indenter.indentForBottomLine(m_editor->editorWidget()->document()->begin(), end.next(), QChar::Null);
359                     ts.indentLine(start, indent);
360                 }
361             }
362         }
363         tc.endEditBlock();
364     }
365 }
366
367 void QuickToolBar::removeProperty(const QString &propertyName)
368 {
369     if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
370         UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
371         UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);
372
373         UiObjectInitializer *initializer = 0;
374         if (objectDefinition)
375             initializer = objectDefinition->initializer;
376         else if (objectBinding)
377             initializer = objectBinding->initializer;
378
379         PropertyReader propertyReader(m_doc, initializer);
380         if (propertyReader.hasProperty(propertyName)) {
381             Utils::ChangeSet changeSet;
382             Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);
383             rewriter.removeBindingByName(initializer, propertyName);
384             QTextCursor tc(m_editor->editorWidget()->document());
385             changeSet.apply(&tc);
386         }
387     }
388 }
389
390 void QuickToolBar::setEnabled(bool b)
391 {
392     if (m_widget)
393         contextWidget()->currentWidget()->setEnabled(b);
394     if (!b)
395         widget()->hide();
396 }
397
398
399 QWidget* QuickToolBar::widget()
400 {
401     return contextWidget();
402 }
403
404
405 void QuickToolBar::onPropertyChanged(const QString &name, const QVariant &value)
406 {
407     if (m_blockWriting)
408         return;
409     if (!m_doc)
410         return;
411
412     setProperty(name, value);
413     m_doc.clear(); //the document is outdated
414 }
415
416 void QuickToolBar::onPropertyRemovedAndChange(const QString &remove, const QString &change, const QVariant &value, bool removeFirst)
417 {
418     if (m_blockWriting)
419         return;
420
421     if (!m_doc)
422         return;
423
424     QTextCursor tc(m_editor->editorWidget()->document());
425     tc.beginEditBlock();
426
427     if (removeFirst) {
428         removeProperty(remove);
429         setProperty(change, value);
430     } else {
431         setProperty(change, value);
432         removeProperty(remove);
433     }
434
435
436     tc.endEditBlock();
437
438     m_doc.clear(); //the document is outdated
439
440 }
441
442 void QuickToolBar::onPinnedChanged(bool b)
443 {
444     QuickToolBarSettings settings = QuickToolBarSettings::get();
445     settings.pinContextPane = b;
446     settings.set();
447 }
448
449 void QuickToolBar::onEnabledChanged(bool b)
450 {
451     QuickToolBarSettings settings = QuickToolBarSettings::get();
452     settings.pinContextPane = b;
453     settings.enableContextPane = b;
454     settings.set();
455 }
456
457 ContextPaneWidget* QuickToolBar::contextWidget()
458 {
459     if (m_widget.isNull()) { //lazily recreate widget
460         m_widget = new ContextPaneWidget;
461         connect(m_widget.data(), SIGNAL(propertyChanged(QString,QVariant)), this, SLOT(onPropertyChanged(QString,QVariant)));
462         connect(m_widget.data(), SIGNAL(removeProperty(QString)), this, SLOT(onPropertyRemoved(QString)));
463         connect(m_widget.data(), SIGNAL(removeAndChangeProperty(QString,QString,QVariant, bool)), this, SLOT(onPropertyRemovedAndChange(QString,QString,QVariant, bool)));
464         connect(m_widget.data(), SIGNAL(enabledChanged(bool)), this, SLOT(onEnabledChanged(bool)));
465         connect(m_widget.data(), SIGNAL(pinnedChanged(bool)), this, SLOT(onPinnedChanged(bool)));
466         connect(m_widget.data(), SIGNAL(closed()), this, SIGNAL(closed()));
467     }
468     return m_widget.data();
469 }
470
471 void QuickToolBar::onPropertyRemoved(const QString &propertyName)
472 {
473     if (m_blockWriting)
474         return;
475
476     if (!m_doc)
477         return;
478
479     removeProperty(propertyName);
480     m_doc.clear(); //the document is outdated
481 }
482
483 } //QmlDesigner