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 **************************************************************************/
33 #include <QtCore/QDebug>
35 #include "nodeabstractproperty.h"
36 #include "nodelistproperty.h"
37 #include "nodemetainfo.h"
38 #include "rewriteaction.h"
40 using namespace QmlDesigner;
41 using namespace QmlDesigner::Internal;
42 using namespace QmlDesigner;
44 namespace { // anonymous
46 static inline QString toInfo(const Import &import)
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());
59 return QLatin1String("unknown type of import");
62 if (import.hasVersion())
63 txt += QString::fromLatin1("with version \"%1\"").arg(import.version());
65 txt += QLatin1String("without version");
67 if (import.hasAlias())
68 txt += QString::fromLatin1("aliassed as \"%1\"").arg(import.alias());
70 txt += QLatin1String("unaliassed");
75 static inline QString toString(QmlRefactoring::PropertyType 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");
85 } // namespace anonymous
87 bool AddPropertyRewriteAction::execute(QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
89 const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
92 if (m_property.isDefaultProperty()) {
93 result = refactoring.addToObjectMemberList(nodeLocation, m_valueText);
96 qDebug() << "*** AddPropertyRewriteAction::execute failed in addToObjectMemberList("
97 << nodeLocation << ','
98 << m_valueText << ") **"
101 } else if (m_property.isNodeListProperty() && m_property.toNodeListProperty().toModelNodeList().size() > 1) {
102 result = refactoring.addToArrayMemberList(nodeLocation, m_property.name(), m_valueText);
105 qDebug() << "*** AddPropertyRewriteAction::execute failed in addToArrayMemberList("
106 << nodeLocation << ','
107 << m_property.name() << ','
108 << m_valueText << ") **"
112 result = refactoring.addProperty(nodeLocation, m_property.name(), m_valueText, m_propertyType);
115 qDebug() << "*** AddPropertyRewriteAction::execute failed in addProperty("
116 << nodeLocation << ','
117 << m_property.name() << ','
118 << m_valueText << ","
119 << qPrintable(toString(m_propertyType)) << ") **"
127 QString AddPropertyRewriteAction::info() const
129 return QString("AddPropertyRewriteAction for property \"%1\" (type: %2)").arg(m_property.name(), toString(m_propertyType));
132 bool ChangeIdRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
134 const int nodeLocation = positionStore.nodeOffset(m_node);
135 static const QLatin1String idPropertyName("id");
138 if (m_oldId.isEmpty()) {
139 result = refactoring.addProperty(nodeLocation, idPropertyName, m_newId, QmlRefactoring::ScriptBinding);
142 qDebug() << "*** ChangeIdRewriteAction::execute failed in addProperty("
143 << nodeLocation << ','
144 << idPropertyName << ','
145 << m_newId << ", ScriptBinding) **"
148 } else if (m_newId.isEmpty()) {
149 result = refactoring.removeProperty(nodeLocation, idPropertyName);
152 qDebug() << "*** ChangeIdRewriteAction::execute failed in removeProperty("
153 << nodeLocation << ','
154 << idPropertyName << ") **"
158 result = refactoring.changeProperty(nodeLocation, idPropertyName, m_newId, QmlRefactoring::ScriptBinding);
161 qDebug() << "*** ChangeIdRewriteAction::execute failed in changeProperty("
162 << nodeLocation << ','
163 << idPropertyName << ','
164 << m_newId << ", ScriptBinding) **"
172 QString ChangeIdRewriteAction::info() const
174 return QString("ChangeIdRewriteAction from \"%1\" to \"%2\"").arg(m_oldId, m_newId);
177 bool ChangePropertyRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
179 const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
182 if (m_property.isDefaultProperty()) {
183 result = refactoring.addToObjectMemberList(nodeLocation, m_valueText);
186 qDebug() << "*** ChangePropertyRewriteAction::execute failed in addToObjectMemberList("
187 << nodeLocation << ','
188 << m_valueText << ") **"
191 } else if (m_propertyType == QmlRefactoring::ArrayBinding) {
192 result = refactoring.addToArrayMemberList(nodeLocation, m_property.name(), m_valueText);
195 qDebug() << "*** ChangePropertyRewriteAction::execute failed in addToArrayMemberList("
196 << nodeLocation << ','
197 << m_property.name() << ','
198 << m_valueText << ") **"
202 result = refactoring.changeProperty(nodeLocation, m_property.name(), m_valueText, m_propertyType);
205 qDebug() << "*** ChangePropertyRewriteAction::execute failed in changeProperty("
206 << nodeLocation << ','
207 << m_property.name() << ','
208 << m_valueText << ','
209 << qPrintable(toString(m_propertyType)) << ") **"
217 QString ChangePropertyRewriteAction::info() const
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)"));
227 bool ChangeTypeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
229 const int nodeLocation = positionStore.nodeOffset(m_node);
232 QString newNodeType = m_node.type();
233 const int slashIdx = newNodeType.lastIndexOf('.');
235 newNodeType = newNodeType.mid(slashIdx + 1);
237 result = refactoring.changeObjectType(nodeLocation, newNodeType);
239 qDebug() << "*** ChangeTypeRewriteAction::execute failed in changeObjectType("
240 << nodeLocation << ','
241 << newNodeType << ") **"
248 QString ChangeTypeRewriteAction::info() const
250 return QString("ChangeTypeRewriteAction");
253 bool RemoveNodeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
255 const int nodeLocation = positionStore.nodeOffset(m_node);
256 bool result = refactoring.removeObject(nodeLocation);
258 qDebug() << "*** RemoveNodeRewriteAction::execute failed in removeObject("
259 << nodeLocation << ") **"
266 QString RemoveNodeRewriteAction::info() const
268 return QString("RemoveNodeRewriteAction");
271 bool RemovePropertyRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
273 const int nodeLocation = positionStore.nodeOffset(m_property.parentModelNode());
274 bool result = refactoring.removeProperty(nodeLocation, m_property.name());
276 qDebug() << "*** RemovePropertyRewriteAction::execute failed in removeProperty("
277 << nodeLocation << ','
278 << m_property.name() << ") **"
285 QString RemovePropertyRewriteAction::info() const
287 return QString("RemovePropertyRewriteAction for property \"%1\"").arg(m_property.name());
290 bool ReparentNodeRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring, ModelNodePositionStorage &positionStore)
292 const int nodeLocation = positionStore.nodeOffset(m_node);
293 const int targetParentObjectLocation = positionStore.nodeOffset(m_targetProperty.parentModelNode());
294 const bool isArrayBinding = m_targetProperty.isNodeListProperty();
297 QString targetPropertyName;
298 if (!m_targetProperty.isDefaultProperty())
299 targetPropertyName = m_targetProperty.name();
301 result = refactoring.moveObject(nodeLocation, targetPropertyName, isArrayBinding, targetParentObjectLocation);
303 qDebug() << "*** ReparentNodeRewriteAction::execute failed in moveObject("
304 << nodeLocation << ','
305 << targetPropertyName << ','
306 << isArrayBinding << ','
307 << targetParentObjectLocation << ") **"
314 QString ReparentNodeRewriteAction::info() const
316 if (m_node.isValid())
317 return QString("ReparentNodeRewriteAction for node \"%1\" into property \"%2\" of node \"%3\"")
319 m_targetProperty.name(),
320 m_targetProperty.parentModelNode().id());
322 return QString("ReparentNodeRewriteAction for an invalid node");
325 bool MoveNodeRewriteAction::execute(QmlRefactoring &refactoring,
326 ModelNodePositionStorage &positionStore)
328 const int movingNodeLocation = positionStore.nodeOffset(m_movingNode);
329 const int newTrailingNodeLocation = m_newTrailingNode.isValid() ? positionStore.nodeOffset(m_newTrailingNode) : -1;
332 bool inDefaultProperty = (m_movingNode.parentProperty().parentModelNode().metaInfo().defaultPropertyName() == m_movingNode.parentProperty().name());
334 result = refactoring.moveObjectBeforeObject(movingNodeLocation, newTrailingNodeLocation, inDefaultProperty);
336 qDebug() << "*** MoveNodeRewriteAction::execute failed in moveObjectBeforeObject("
337 << movingNodeLocation << ','
338 << newTrailingNodeLocation << ") **"
345 QString MoveNodeRewriteAction::info() const
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());
351 return QString("MoveNodeRewriteAction for node \"%1\" to the end of its containing property").arg(m_movingNode.id());
353 return QString("MoveNodeRewriteAction for an invalid node");
357 bool AddImportRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring,
358 ModelNodePositionStorage &/*positionStore*/)
360 const bool result = refactoring.addImport(m_import);
363 qDebug() << "*** AddImportRewriteAction::execute failed in changeImports ("
364 << m_import.toString()
370 QString AddImportRewriteAction::info() const
372 return toInfo(m_import);
375 bool RemoveImportRewriteAction::execute(QmlDesigner::QmlRefactoring &refactoring,
376 ModelNodePositionStorage &/*positionStore*/)
378 const bool result = refactoring.removeImport(m_import);
381 qDebug() << "*** RemoveImportRewriteAction::execute failed in changeImports ("
382 << m_import.toString()
388 QString RemoveImportRewriteAction::info() const
390 return toInfo(m_import);