OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / model / rewriteaction.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 <QtCore/QDebug>
34
35 #include "nodeabstractproperty.h"
36 #include "nodelistproperty.h"
37 #include "nodemetainfo.h"
38 #include "rewriteaction.h"
39
40 using namespace QmlDesigner;
41 using namespace QmlDesigner::Internal;
42 using namespace QmlDesigner;
43
44 namespace { // anonymous
45
46 static inline QString toInfo(const Import &import)
47 {
48     QString txt;
49
50     if (import.isEmpty()) {
51         return QLatin1String("empty import");
52     } else if (import.isFileImport()) {
53         txt = QLatin1String("import file \"%1\"");
54         txt = txt.arg(import.url());
55     } else if (import.isLibraryImport()) {
56         txt = QLatin1String("import library \"%1\"");
57         txt = txt.arg(import.file());
58     } else {
59         return QLatin1String("unknown type of import");
60     }
61
62     if (import.hasVersion())
63         txt += QString::fromLatin1("with version \"%1\"").arg(import.version());
64     else
65         txt += QLatin1String("without version");
66
67     if (import.hasAlias())
68         txt += QString::fromLatin1("aliassed as \"%1\"").arg(import.alias());
69     else
70         txt += QLatin1String("unaliassed");
71
72     return txt;
73 }
74
75 static inline QString toString(QmlRefactoring::PropertyType type)
76 {
77     switch (type) {
78         case QmlRefactoring::ArrayBinding:  return QLatin1String("array binding");
79         case QmlRefactoring::ObjectBinding: return QLatin1String("object binding");
80         case QmlRefactoring::ScriptBinding: return QLatin1String("script binding");
81         default:                            return QLatin1String("UNKNOWN");
82     }
83 }
84
85 } // namespace anonymous
86
87 bool AddPropertyRewriteAction::execute(QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
88 {
89     const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
90     bool result = false;
91
92     if (m_property.isDefaultProperty()) {
93         result = refactoring.addToObjectMemberList(nodeLocation, m_valueText);
94
95         if (!result) {
96             qDebug() << "*** AddPropertyRewriteAction::execute failed in addToObjectMemberList("
97                     << nodeLocation << ','
98                     << m_valueText << ") **"
99                     << info();
100         }
101     } else if (m_property.isNodeListProperty() && m_property.toNodeListProperty().toModelNodeList().size() > 1) {
102         result = refactoring.addToArrayMemberList(nodeLocation, m_property.name(), m_valueText);
103
104         if (!result) {
105             qDebug() << "*** AddPropertyRewriteAction::execute failed in addToArrayMemberList("
106                     << nodeLocation << ','
107                     << m_property.name() << ','
108                     << m_valueText << ") **"
109                     << info();
110         }
111     } else {
112         result = refactoring.addProperty(nodeLocation, m_property.name(), m_valueText, m_propertyType);
113
114         if (!result) {
115             qDebug() << "*** AddPropertyRewriteAction::execute failed in addProperty("
116                     << nodeLocation << ','
117                     << m_property.name() << ','
118                     << m_valueText << ","
119                     << qPrintable(toString(m_propertyType)) << ") **"
120                     << info();
121         }
122     }
123
124     return result;
125 }
126
127 QString AddPropertyRewriteAction::info() const
128 {
129     return QString("AddPropertyRewriteAction for property \"%1\" (type: %2)").arg(m_property.name(), toString(m_propertyType));
130 }
131
132 bool ChangeIdRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
133 {
134     const int nodeLocation = positionStore.nodeOffset(m_node);
135     static const QLatin1String idPropertyName("id");
136     bool result = false;
137
138     if (m_oldId.isEmpty()) {
139         result = refactoring.addProperty(nodeLocation, idPropertyName, m_newId, QmlRefactoring::ScriptBinding);
140
141         if (!result) {
142             qDebug() << "*** ChangeIdRewriteAction::execute failed in addProperty("
143                     << nodeLocation << ','
144                     << idPropertyName << ','
145                     << m_newId << ", ScriptBinding) **"
146                     << info();
147         }
148     } else if (m_newId.isEmpty()) {
149         result = refactoring.removeProperty(nodeLocation, idPropertyName);
150
151         if (!result) {
152             qDebug() << "*** ChangeIdRewriteAction::execute failed in removeProperty("
153                     << nodeLocation << ','
154                     << idPropertyName << ") **"
155                     << info();
156         }
157     } else {
158         result = refactoring.changeProperty(nodeLocation, idPropertyName, m_newId, QmlRefactoring::ScriptBinding);
159
160         if (!result) {
161             qDebug() << "*** ChangeIdRewriteAction::execute failed in changeProperty("
162                     << nodeLocation << ','
163                     << idPropertyName << ','
164                     << m_newId << ", ScriptBinding) **"
165                     << info();
166         }
167     }
168
169     return result;
170 }
171
172 QString ChangeIdRewriteAction::info() const
173 {
174     return QString("ChangeIdRewriteAction from \"%1\" to \"%2\"").arg(m_oldId, m_newId);
175 }
176
177 bool ChangePropertyRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
178 {
179     const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
180     bool result = false;
181
182     if (m_property.isDefaultProperty()) {
183         result = refactoring.addToObjectMemberList(nodeLocation, m_valueText);
184
185         if (!result) {
186             qDebug() << "*** ChangePropertyRewriteAction::execute failed in addToObjectMemberList("
187                     << nodeLocation << ','
188                     << m_valueText << ") **"
189                     << info();
190         }
191     } else if (m_propertyType == QmlRefactoring::ArrayBinding) {
192         result = refactoring.addToArrayMemberList(nodeLocation, m_property.name(), m_valueText);
193
194         if (!result) {
195             qDebug() << "*** ChangePropertyRewriteAction::execute failed in addToArrayMemberList("
196                     << nodeLocation << ','
197                     << m_property.name() << ','
198                     << m_valueText << ") **"
199                     << info();
200         }
201     } else {
202         result = refactoring.changeProperty(nodeLocation, m_property.name(), m_valueText, m_propertyType);
203
204         if (!result) {
205             qDebug() << "*** ChangePropertyRewriteAction::execute failed in changeProperty("
206                     << nodeLocation << ','
207                     << m_property.name() << ','
208                     << m_valueText << ','
209                     << qPrintable(toString(m_propertyType)) << ") **"
210                     << info();
211         }
212     }
213
214     return result;
215 }
216
217 QString ChangePropertyRewriteAction::info() const
218 {
219     return QString("ChangePropertyRewriteAction for property \"%1\" (type: %2) of node \"%3\" with value >>%4<< and contained object \"%5\"")
220              .arg(m_property.name(),
221                   toString(m_propertyType),
222                   (m_property.parentModelNode().isValid() ? m_property.parentModelNode().id() : "(invalid)"),
223                   QString(m_valueText).replace('\n', "\\n"),
224                   (m_containedModelNode.isValid() ? m_containedModelNode.id() : "(none)"));
225 }
226
227 bool ChangeTypeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
228 {
229     const int nodeLocation = positionStore.nodeOffset(m_node);
230     bool result = false;
231
232     QString newNodeType = m_node.type();
233     const int slashIdx = newNodeType.lastIndexOf('.');
234     if (slashIdx != -1)
235         newNodeType = newNodeType.mid(slashIdx + 1);
236
237     result = refactoring.changeObjectType(nodeLocation, newNodeType);
238     if (!result) {
239         qDebug() << "*** ChangeTypeRewriteAction::execute failed in changeObjectType("
240                 << nodeLocation << ','
241                 << newNodeType << ") **"
242                 << info();
243     }
244
245     return result;
246 }
247
248 QString ChangeTypeRewriteAction::info() const
249 {
250     return QString("ChangeTypeRewriteAction");
251 }
252
253 bool RemoveNodeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
254 {
255     const int nodeLocation = positionStore.nodeOffset(m_node);
256     bool result = refactoring.removeObject(nodeLocation);
257     if (!result) {
258         qDebug() << "*** RemoveNodeRewriteAction::execute failed in removeObject("
259                 << nodeLocation << ") **"
260                 << info();
261     }
262
263     return result;
264 }
265
266 QString RemoveNodeRewriteAction::info() const
267 {
268     return QString("RemoveNodeRewriteAction");
269 }
270
271 bool RemovePropertyRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
272 {
273     const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
274     bool result = refactoring.removeProperty(nodeLocation, m_property.name());
275     if (!result) {
276         qDebug() << "*** RemovePropertyRewriteAction::execute failed in removeProperty("
277                 << nodeLocation << ','
278                 << m_property.name() << ") **"
279                 << info();
280     }
281
282     return result;
283 }
284
285 QString RemovePropertyRewriteAction::info() const
286 {
287     return QString("RemovePropertyRewriteAction for property \"%1\"").arg(m_property.name());
288 }
289
290 bool ReparentNodeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
291 {
292     const int nodeLocation = positionStore.nodeOffset(m_node);
293     const int targetParentObjectLocation = positionStore.nodeOffset(m_targetProperty.parentModelNode());
294     const bool isArrayBinding = m_targetProperty.isNodeListProperty();
295     bool result = false;
296
297     QString targetPropertyName;
298     if (!m_targetProperty.isDefaultProperty())
299         targetPropertyName = m_targetProperty.name();
300
301     result = refactoring.moveObject(nodeLocation, targetPropertyName, isArrayBinding, targetParentObjectLocation);
302     if (!result) {
303         qDebug() << "*** ReparentNodeRewriteAction::execute failed in moveObject("
304                 << nodeLocation << ','
305                 << targetPropertyName << ','
306                 << isArrayBinding << ','
307                 << targetParentObjectLocation << ") **"
308                 << info();
309     }
310
311     return result;
312 }
313
314 QString ReparentNodeRewriteAction::info() const
315 {
316     if (m_node.isValid())
317         return QString("ReparentNodeRewriteAction for node \"%1\" into property \"%2\" of node \"%3\"")
318                 .arg(m_node.id(),
319                      m_targetProperty.name(),
320                      m_targetProperty.parentModelNode().id());
321     else
322         return QString("ReparentNodeRewriteAction for an invalid node");
323 }
324
325 bool MoveNodeRewriteAction::execute(QmlRefactoring &refactoring,
326                                     ModelNodePositionStorage &positionStore)
327 {
328     const int movingNodeLocation = positionStore.nodeOffset(m_movingNode);
329     const int newTrailingNodeLocation = m_newTrailingNode.isValid() ? positionStore.nodeOffset(m_newTrailingNode) : -1;
330     bool result = false;
331
332     bool inDefaultProperty = (m_movingNode.parentProperty().parentModelNode().metaInfo().defaultPropertyName() == m_movingNode.parentProperty().name());
333
334     result = refactoring.moveObjectBeforeObject(movingNodeLocation, newTrailingNodeLocation, inDefaultProperty);
335     if (!result) {
336         qDebug() << "*** MoveNodeRewriteAction::execute failed in moveObjectBeforeObject("
337                 << movingNodeLocation << ','
338                 << newTrailingNodeLocation << ") **"
339                 << info();
340     }
341
342     return result;
343 }
344
345 QString MoveNodeRewriteAction::info() const
346 {
347     if (m_movingNode.isValid()) {
348         if (m_newTrailingNode.isValid())
349             return QString("MoveNodeRewriteAction for node \"%1\" before node \"%2\"").arg(m_movingNode.id(), m_newTrailingNode.id());
350         else
351             return QString("MoveNodeRewriteAction for node \"%1\" to the end of its containing property").arg(m_movingNode.id());
352     } else {
353         return QString("MoveNodeRewriteAction for an invalid node");
354     }
355 }
356
357 bool AddImportRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring,
358                                      ModelNodePositionStorage &/*positionStore*/)
359 {
360     const bool result = refactoring.addImport(m_import);
361
362     if (!result)
363         qDebug() << "*** AddImportRewriteAction::execute failed in changeImports ("
364                  << m_import.toString()
365                  << ") **"
366                  << info();
367     return result;
368 }
369
370 QString AddImportRewriteAction::info() const
371 {
372     return toInfo(m_import);
373 }
374
375 bool RemoveImportRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring,
376                                         ModelNodePositionStorage &/*positionStore*/)
377 {
378     const bool result = refactoring.removeImport(m_import);
379
380     if (!result)
381         qDebug() << "*** RemoveImportRewriteAction::execute failed in changeImports ("
382                  << m_import.toString()
383                  << ") **"
384                  << info();
385     return result;
386 }
387
388 QString RemoveImportRewriteAction::info() const
389 {
390     return toInfo(m_import);
391 }