OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / filemanager / qmlrefactoring.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 <QDebug>
34
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"
46
47 using namespace QmlJS;
48 using namespace QmlDesigner;
49 using namespace QmlDesigner::Internal;
50
51 QmlRefactoring::QmlRefactoring(const Document::Ptr &doc, TextModifier &modifier, const QStringList &propertyOrder):
52         qmlDocument(doc),
53         textModifier(&modifier),
54         m_propertyOrder(propertyOrder)
55 {
56 }
57
58 bool QmlRefactoring::reparseDocument()
59 {
60     const QString newSource = textModifier->text();
61
62 //    qDebug() << "QmlRefactoring::reparseDocument() new QML source:" << newSource;
63
64     Document::Ptr tmpDocument(Document::create("<ModelToTextMerger>"));
65     tmpDocument->setSource(newSource);
66
67     if (tmpDocument->parseQml()) {
68         qmlDocument = tmpDocument;
69         return true;
70     } else {
71         qWarning() << "*** Possible problem: QML file wasn't parsed correctly.";
72         qDebug() << "*** QML text:" << textModifier->text();
73         return false;
74     }
75 }
76
77 bool QmlRefactoring::addImport(const Import &import)
78 {
79     ChangeImportsVisitor visitor(*textModifier, qmlDocument->source());
80     return visitor.add(qmlDocument->qmlProgram(), import);
81 }
82
83 bool QmlRefactoring::removeImport(const Import &import)
84 {
85     ChangeImportsVisitor visitor(*textModifier, qmlDocument->source());
86     return visitor.remove(qmlDocument->qmlProgram(), import);
87 }
88
89 bool QmlRefactoring::addToArrayMemberList(int parentLocation, const QString &propertyName, const QString &content)
90 {
91     if (parentLocation < 0)
92         return false;
93
94     AddArrayMemberVisitor visit(*textModifier, (quint32) parentLocation, propertyName, content);
95     visit.setConvertObjectBindingIntoArrayBinding(true);
96     return visit(qmlDocument->qmlProgram());
97 }
98
99 bool QmlRefactoring::addToObjectMemberList(int parentLocation, const QString &content)
100 {
101     if (parentLocation < 0)
102         return false;
103
104     AddObjectVisitor visit(*textModifier, (quint32) parentLocation, content, m_propertyOrder);
105     return visit(qmlDocument->qmlProgram());
106 }
107
108 bool QmlRefactoring::addProperty(int parentLocation, const QString &name, const QString &value, PropertyType propertyType)
109 {
110     if(parentLocation < 0)
111         return false;
112
113     AddPropertyVisitor visit(*textModifier, (quint32) parentLocation, name, value, propertyType, m_propertyOrder);
114     return visit(qmlDocument->qmlProgram());
115 }
116
117 bool QmlRefactoring::changeProperty(int parentLocation, const QString &name, const QString &value, PropertyType propertyType)
118 {
119     if (parentLocation < 0)
120         return false;
121
122     ChangePropertyVisitor visit(*textModifier, (quint32) parentLocation, name, value, propertyType);
123     return visit(qmlDocument->qmlProgram());
124 }
125
126 bool QmlRefactoring::changeObjectType(int nodeLocation, const QString &newType)
127 {
128     if (nodeLocation < 0 || newType.isEmpty())
129         return false;
130
131     ChangeObjectTypeVisitor visit(*textModifier, (quint32) nodeLocation, newType);
132     return visit(qmlDocument->qmlProgram());
133 }
134
135 bool QmlRefactoring::moveObject(int objectLocation, const QString &targetPropertyName, bool targetIsArrayBinding, int targetParentObjectLocation)
136 {
137     if (objectLocation < 0 || targetParentObjectLocation < 0)
138         return false;
139
140     MoveObjectVisitor visit(*textModifier, (quint32) objectLocation, targetPropertyName, targetIsArrayBinding, (quint32) targetParentObjectLocation, m_propertyOrder);
141     return visit(qmlDocument->qmlProgram());
142 }
143
144 bool QmlRefactoring::moveObjectBeforeObject(int movingObjectLocation, int beforeObjectLocation, bool inDefaultProperty)
145 {
146     if (movingObjectLocation < 0 || beforeObjectLocation < -1)
147         return false;
148
149     if (beforeObjectLocation == -1) {
150         MoveObjectBeforeObjectVisitor visit(*textModifier, movingObjectLocation, inDefaultProperty);
151         return visit(qmlDocument->qmlProgram());
152     } else {
153         MoveObjectBeforeObjectVisitor visit(*textModifier, movingObjectLocation, beforeObjectLocation, inDefaultProperty);
154         return visit(qmlDocument->qmlProgram());
155     }
156     return false;
157 }
158
159 bool QmlRefactoring::removeObject(int nodeLocation)
160 {
161     if (nodeLocation < 0)
162         return false;
163
164     RemoveUIObjectMemberVisitor visit(*textModifier, (quint32) nodeLocation);
165     return visit(qmlDocument->qmlProgram());
166 }
167
168 bool QmlRefactoring::removeProperty(int parentLocation, const QString &name)
169 {
170     if (parentLocation < 0 || name.isEmpty())
171         return false;
172
173     RemovePropertyVisitor visit(*textModifier, (quint32) parentLocation, name);
174     return visit(qmlDocument->qmlProgram());
175 }