1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
31 **************************************************************************/
33 #include "quicktoolbar.h"
34 #include <contextpanewidget.h>
35 #include <quicktoolbarsettingspage.h>
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>
53 #include <QtCore/QDebug>
55 using namespace QmlJS;
57 using namespace QmlEditorWidgets;
59 namespace QmlJSEditor {
61 static inline QString textAt(const Document* doc,
62 const SourceLocation &from,
63 const SourceLocation &to)
65 return doc->source().mid(from.offset, to.end() - from.begin());
68 static inline const Interpreter::ObjectValue * getPropertyChangesTarget(Node *node, LookupContext::Ptr lookupContext)
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;
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)) {
95 QuickToolBar::QuickToolBar(QObject *parent)
96 : ::QmlJS::IContextPane(parent)
98 , m_blockWriting(false)
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")
122 << QLatin1String("states")
123 << QLatin1String("transitions")
127 QuickToolBar::~QuickToolBar()
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();
135 void QuickToolBar::apply(TextEditor::BaseTextEditor *editor, Document::Ptr document, LookupContext::Ptr lookupContext, AST::Node *node, bool update, bool force)
137 if (!QuickToolBarSettings::get().enableContextPane && !force && !update) {
138 contextWidget()->hide();
142 if (document.isNull())
145 if (update && editor != m_editor)
146 return; //do not update for different editor
148 m_blockWriting = true;
150 const Interpreter::ObjectValue *scopeObject = document->bind()->findQmlObject(node);
152 bool isPropertyChanges = false;
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());
161 if (m_prototypes.contains("PropertyChanges")) {
162 isPropertyChanges = true;
163 const Interpreter::ObjectValue *targetObject = getPropertyChangesTarget(node, lookupContext);
164 m_prototypes.clear();
166 foreach (const Interpreter::ObjectValue *object,
167 Interpreter::PrototypeIterator(targetObject, lookupContext->context()).all()) {
168 m_prototypes.append(object->className());
174 setEnabled(document->isParsedCorrectly());
176 contextWidget()->setParent(editor->widget()->parentWidget());
177 contextWidget()->colorDialog()->setParent(editor->widget()->parentWidget());
179 if (cast<UiObjectDefinition*>(node) || cast<UiObjectBinding*>(node)) {
180 UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
181 UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);
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();
199 if (lookupContext.isNull()) {
200 if (name != m_oldType)
201 m_prototypes.clear();
206 m_prototypes.append(name);
212 m_editor->convertPosition(offset, &line1, &column1); //get line
213 m_editor->convertPosition(end, &line2, &column2); //get line
216 if (line1 > -1 && line2 > -1)
217 reg = m_editor->editorWidget()->translatedLineRegion(line1 - 1, line2);
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);
225 if (contextWidget()->acceptsType(m_prototypes)) {
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));
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);
238 contextWidget()->setIsPropertyChanges(isPropertyChanges);
240 contextWidget()->setType(m_prototypes);
242 contextWidget()->activate(p3 , p1, p2, QuickToolBarSettings::get().pinContextPane);
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);
251 contextWidget()->setParent(0);
252 contextWidget()->hide();
253 contextWidget()->colorDialog()->hide();
256 contextWidget()->setParent(0);
257 contextWidget()->hide();
258 contextWidget()->colorDialog()->hide();
261 m_blockWriting = false;
265 bool QuickToolBar::isAvailable(TextEditor::BaseTextEditor *, Document::Ptr document, AST::Node *node)
267 if (document.isNull())
275 UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(node);
276 UiObjectBinding *objectBinding = cast<UiObjectBinding*>(node);
277 if (objectDefinition) {
278 name = objectDefinition->qualifiedTypeNameId->name->asString();
280 } else if (objectBinding) {
281 name = objectBinding->qualifiedTypeNameId->name->asString();
284 QStringList prototypes;
285 prototypes.append(name);
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"))
301 void QuickToolBar::setProperty(const QString &propertyName, const QVariant &value)
304 QString stringValue = value.toString();
305 if (value.type() == QVariant::Color)
306 stringValue = QChar('\"') + value.toString() + QChar('\"');
308 if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
309 UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
310 UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);
312 UiObjectInitializer *initializer = 0;
313 if (objectDefinition)
314 initializer = objectDefinition->initializer;
315 else if (objectBinding)
316 initializer = objectBinding->initializer;
318 Utils::ChangeSet changeSet;
319 Rewriter rewriter(m_doc->source(), &changeSet, m_propertyOrder);
324 Rewriter::BindingType bindingType = Rewriter::ScriptBinding;
326 if (stringValue.contains("{") && stringValue.contains("}"))
327 bindingType = Rewriter::ObjectBinding;
329 PropertyReader propertyReader(m_doc, initializer);
330 if (propertyReader.hasProperty(propertyName)) {
331 rewriter.changeBinding(initializer, propertyName, stringValue, bindingType);
333 rewriter.addBinding(initializer, propertyName, stringValue, bindingType);
338 int changeSetPos = changeSet.operationList().last().pos1;
339 int changeSetLength = changeSet.operationList().last().text.length();
340 QTextCursor tc = m_editor->editorWidget()->textCursor();
342 changeSet.apply(&tc);
344 m_editor->convertPosition(changeSetPos, &line, &column); //get line
345 m_editor->convertPosition(changeSetPos + changeSetLength, &endLine, &column); //get line
348 TextEditor::TabSettings ts = m_editor->editorWidget()->tabSettings();
349 QmlJSIndenter indenter;
350 indenter.setTabSize(ts.m_tabSize);
351 indenter.setIndentSize(ts.m_indentSize);
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);
358 const int indent = indenter.indentForBottomLine(m_editor->editorWidget()->document()->begin(), end.next(), QChar::Null);
359 ts.indentLine(start, indent);
367 void QuickToolBar::removeProperty(const QString &propertyName)
369 if (cast<UiObjectDefinition*>(m_node) || cast<UiObjectBinding*>(m_node)) {
370 UiObjectDefinition *objectDefinition = cast<UiObjectDefinition*>(m_node);
371 UiObjectBinding *objectBinding = cast<UiObjectBinding*>(m_node);
373 UiObjectInitializer *initializer = 0;
374 if (objectDefinition)
375 initializer = objectDefinition->initializer;
376 else if (objectBinding)
377 initializer = objectBinding->initializer;
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);
390 void QuickToolBar::setEnabled(bool b)
393 contextWidget()->currentWidget()->setEnabled(b);
399 QWidget* QuickToolBar::widget()
401 return contextWidget();
405 void QuickToolBar::onPropertyChanged(const QString &name, const QVariant &value)
412 setProperty(name, value);
413 m_doc.clear(); //the document is outdated
416 void QuickToolBar::onPropertyRemovedAndChange(const QString &remove, const QString &change, const QVariant &value, bool removeFirst)
424 QTextCursor tc(m_editor->editorWidget()->document());
428 removeProperty(remove);
429 setProperty(change, value);
431 setProperty(change, value);
432 removeProperty(remove);
438 m_doc.clear(); //the document is outdated
442 void QuickToolBar::onPinnedChanged(bool b)
444 QuickToolBarSettings settings = QuickToolBarSettings::get();
445 settings.pinContextPane = b;
449 void QuickToolBar::onEnabledChanged(bool b)
451 QuickToolBarSettings settings = QuickToolBarSettings::get();
452 settings.pinContextPane = b;
453 settings.enableContextPane = b;
457 ContextPaneWidget* QuickToolBar::contextWidget()
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()));
468 return m_widget.data();
471 void QuickToolBar::onPropertyRemoved(const QString &propertyName)
479 removeProperty(propertyName);
480 m_doc.clear(); //the document is outdated