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 qt-info@nokia.com.
31 **************************************************************************/
35 #include "addarraymembervisitor.h"
36 #include "addobjectvisitor.h"
37 #include "addpropertyvisitor.h"
38 #include "changeimportsvisitor.h"
39 #include "changeobjecttypevisitor.h"
40 #include "changepropertyvisitor.h"
41 #include "moveobjectvisitor.h"
42 #include "moveobjectbeforeobjectvisitor.h"
43 #include "qmlrefactoring.h"
44 #include "removepropertyvisitor.h"
45 #include "removeuiobjectmembervisitor.h"
47 using namespace QmlJS;
48 using namespace QmlDesigner;
49 using namespace QmlDesigner::Internal;
51 QmlRefactoring::QmlRefactoring(const Document::Ptr &doc, TextModifier &modifier, const QStringList &propertyOrder):
53 textModifier(&modifier),
54 m_propertyOrder(propertyOrder)
58 bool QmlRefactoring::reparseDocument()
60 const QString newSource = textModifier->text();
62 // qDebug() << "QmlRefactoring::reparseDocument() new QML source:" << newSource;
64 Document::Ptr tmpDocument(Document::create("<ModelToTextMerger>"));
65 tmpDocument->setSource(newSource);
67 if (tmpDocument->parseQml()) {
68 qmlDocument = tmpDocument;
71 qWarning() << "*** Possible problem: QML file wasn't parsed correctly.";
72 qDebug() << "*** QML text:" << textModifier->text();
77 bool QmlRefactoring::addImport(const Import &import)
79 ChangeImportsVisitor visitor(*textModifier, qmlDocument->source());
80 return visitor.add(qmlDocument->qmlProgram(), import);
83 bool QmlRefactoring::removeImport(const Import &import)
85 ChangeImportsVisitor visitor(*textModifier, qmlDocument->source());
86 return visitor.remove(qmlDocument->qmlProgram(), import);
89 bool QmlRefactoring::addToArrayMemberList(int parentLocation, const QString &propertyName, const QString &content)
91 if (parentLocation < 0)
94 AddArrayMemberVisitor visit(*textModifier, (quint32) parentLocation, propertyName, content);
95 visit.setConvertObjectBindingIntoArrayBinding(true);
96 return visit(qmlDocument->qmlProgram());
99 bool QmlRefactoring::addToObjectMemberList(int parentLocation, const QString &content)
101 if (parentLocation < 0)
104 AddObjectVisitor visit(*textModifier, (quint32) parentLocation, content, m_propertyOrder);
105 return visit(qmlDocument->qmlProgram());
108 bool QmlRefactoring::addProperty(int parentLocation, const QString &name, const QString &value, PropertyType propertyType)
110 if(parentLocation < 0)
113 AddPropertyVisitor visit(*textModifier, (quint32) parentLocation, name, value, propertyType, m_propertyOrder);
114 return visit(qmlDocument->qmlProgram());
117 bool QmlRefactoring::changeProperty(int parentLocation, const QString &name, const QString &value, PropertyType propertyType)
119 if (parentLocation < 0)
122 ChangePropertyVisitor visit(*textModifier, (quint32) parentLocation, name, value, propertyType);
123 return visit(qmlDocument->qmlProgram());
126 bool QmlRefactoring::changeObjectType(int nodeLocation, const QString &newType)
128 if (nodeLocation < 0 || newType.isEmpty())
131 ChangeObjectTypeVisitor visit(*textModifier, (quint32) nodeLocation, newType);
132 return visit(qmlDocument->qmlProgram());
135 bool QmlRefactoring::moveObject(int objectLocation, const QString &targetPropertyName, bool targetIsArrayBinding, int targetParentObjectLocation)
137 if (objectLocation < 0 || targetParentObjectLocation < 0)
140 MoveObjectVisitor visit(*textModifier, (quint32) objectLocation, targetPropertyName, targetIsArrayBinding, (quint32) targetParentObjectLocation, m_propertyOrder);
141 return visit(qmlDocument->qmlProgram());
144 bool QmlRefactoring::moveObjectBeforeObject(int movingObjectLocation, int beforeObjectLocation, bool inDefaultProperty)
146 if (movingObjectLocation < 0 || beforeObjectLocation < -1)
149 if (beforeObjectLocation == -1) {
150 MoveObjectBeforeObjectVisitor visit(*textModifier, movingObjectLocation, inDefaultProperty);
151 return visit(qmlDocument->qmlProgram());
153 MoveObjectBeforeObjectVisitor visit(*textModifier, movingObjectLocation, beforeObjectLocation, inDefaultProperty);
154 return visit(qmlDocument->qmlProgram());
159 bool QmlRefactoring::removeObject(int nodeLocation)
161 if (nodeLocation < 0)
164 RemoveUIObjectMemberVisitor visit(*textModifier, (quint32) nodeLocation);
165 return visit(qmlDocument->qmlProgram());
168 bool QmlRefactoring::removeProperty(int parentLocation, const QString &name)
170 if (parentLocation < 0 || name.isEmpty())
173 RemovePropertyVisitor visit(*textModifier, (quint32) parentLocation, name);
174 return visit(qmlDocument->qmlProgram());