rewriteactioncompressor.cpp 15 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include <QtCore/QSet>

#include "modelnode.h"
#include "nodelistproperty.h"
#include "nodeproperty.h"
#include "qmltextgenerator.h"
#include "rewriteactioncompressor.h"

using namespace QmlDesigner;
using namespace QmlDesigner::Internal;

static bool nodeOrParentInSet(const ModelNode &node, const QSet<ModelNode> &nodeSet)
{
    ModelNode n = node;
    while (n.isValid()) {
        if (nodeSet.contains(n))
            return true;

        if (!n.hasParentProperty())
            return false;

        n = n.parentProperty().parentModelNode();
    }

    return false;
}

void RewriteActionCompressor::operator()(QList<RewriteAction *> &actions) const
{
Erik Verbruggen's avatar
Erik Verbruggen committed
59
    compressImports(actions);
60
61
    compressRereparentActions(actions);
    compressReparentIntoSameParentActions(actions);
62
63
64
65
66
67
    compressPropertyActions(actions);
    compressAddEditRemoveNodeActions(actions);
    compressAddEditActions(actions);
    compressAddReparentActions(actions);
}

Erik Verbruggen's avatar
Erik Verbruggen committed
68
69
70
71
72
73
74
75
76
77
void RewriteActionCompressor::compressImports(QList<RewriteAction *> &actions) const
{
    QHash<Import, RewriteAction *> addedImports;
    QHash<Import, RewriteAction *> removedImports;

    QMutableListIterator<RewriteAction *> iter(actions);
    iter.toBack();
    while (iter.hasPrevious()) {
        RewriteAction *action = iter.previous();

78
        if (RemoveImportRewriteAction *removeImportAction = action->asRemoveImportRewriteAction()) {
Erik Verbruggen's avatar
Erik Verbruggen committed
79
80
81
82
83
84
85
86
87
88
89
            const Import import = removeImportAction->import();
            if (removedImports.contains(import)) {
                remove(iter);
            } else if (RewriteAction *addImportAction = addedImports.value(import, 0)) {
                actions.removeOne(addImportAction);
                addedImports.remove(import);
                delete addImportAction;
                remove(iter);
            } else {
                removedImports.insert(import, action);
            }
90
        } else if (AddImportRewriteAction *addImportAction = action->asAddImportRewriteAction()) {
Erik Verbruggen's avatar
Erik Verbruggen committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            const Import import = addImportAction->import();
            if (RewriteAction *duplicateAction = addedImports.value(import, 0)) {
                actions.removeOne(duplicateAction);
                addedImports.remove(import);
                delete duplicateAction;
                addedImports.insert(import, action);
            } else if (RewriteAction *removeAction = removedImports.value(import, 0)) {
                actions.removeOne(removeAction);
                removedImports.remove(import);
                delete removeAction;
                remove(iter);
            } else {
                addedImports.insert(import, action);
            }
        }
    }
}

109
void RewriteActionCompressor::compressRereparentActions(QList<RewriteAction *> &actions) const
110
{
111
    QHash<ModelNode, ReparentNodeRewriteAction *> reparentedNodes;
112
113
114
115
116
117

    QMutableListIterator<RewriteAction*> iter(actions);
    iter.toBack();
    while (iter.hasPrevious()) {
        RewriteAction *action = iter.previous();

118
        if (ReparentNodeRewriteAction *reparentAction = action->asReparentNodeRewriteAction()) {
119
120
            const ModelNode reparentedNode = reparentAction->reparentedNode();

121
122
            if (ReparentNodeRewriteAction *otherAction = reparentedNodes.value(reparentedNode, 0)) {
                otherAction->setOldParent(reparentAction->oldParent());
123
124
                remove(iter);
            } else {
125
                reparentedNodes.insert(reparentedNode, reparentAction);
126
127
128
129
130
            }
        }
    }
}

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
void RewriteActionCompressor::compressReparentIntoSameParentActions(QList<RewriteAction *> &actions) const
{
    QMutableListIterator<RewriteAction *> iter(actions);
    iter.toBack();
    while (iter.hasPrevious()) {
        RewriteAction *action = iter.previous();

        if (ReparentNodeRewriteAction *reparentAction = action->asReparentNodeRewriteAction()) {
            const ModelNode targetNode = reparentAction->targetProperty().parentModelNode();
            const ModelNode oldParent = reparentAction->oldParent();
            if (targetNode == oldParent)
                remove(iter);
        }
    }
}

147
148
149
150
151
152
153
154
155
156
void RewriteActionCompressor::compressAddEditRemoveNodeActions(QList<RewriteAction *> &actions) const
{
    QHash<ModelNode, RewriteAction *> removedNodes;
    QSet<RewriteAction *> removeActionsToRemove;

    QMutableListIterator<RewriteAction*> iter(actions);
    iter.toBack();
    while (iter.hasPrevious()) {
        RewriteAction *action = iter.previous();

157
        if (RemoveNodeRewriteAction *removeNodeAction = action->asRemoveNodeRewriteAction()) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
            const ModelNode modelNode = removeNodeAction->node();

            if (removedNodes.contains(modelNode))
                remove(iter);
            else
                removedNodes.insert(modelNode, action);
        } else if (action->asAddPropertyRewriteAction() || action->asChangePropertyRewriteAction()) {
            AbstractProperty property;
            ModelNode containedModelNode;
            if (action->asAddPropertyRewriteAction()) {
                property = action->asAddPropertyRewriteAction()->property();
                containedModelNode = action->asAddPropertyRewriteAction()->containedModelNode();
            } else {
                property = action->asChangePropertyRewriteAction()->property();
                containedModelNode = action->asChangePropertyRewriteAction()->containedModelNode();
            }

            if (removedNodes.contains(property.parentModelNode()))
                remove(iter);
            else if (removedNodes.contains(containedModelNode)) {
                remove(iter);
                removeActionsToRemove.insert(removedNodes[containedModelNode]);
            }
181
        } else if (RemovePropertyRewriteAction *removePropertyAction = action->asRemovePropertyRewriteAction()) {
182
183
184
185
            const AbstractProperty property = removePropertyAction->property();

            if (removedNodes.contains(property.parentModelNode()))
                remove(iter);
186
        } else if (ChangeIdRewriteAction *changeIdAction = action->asChangeIdRewriteAction()) {
187
188
            if (removedNodes.contains(changeIdAction->node()))
                remove(iter);
189
        } else if (ChangeTypeRewriteAction *changeTypeAction = action->asChangeTypeRewriteAction()) {
190
191
            if (removedNodes.contains(changeTypeAction->node()))
                remove(iter);
192
        } else if (ReparentNodeRewriteAction *reparentAction = action->asReparentNodeRewriteAction()) {
193
194
195
196
197
198
199
200
201
202
203
204
205
206
            if (removedNodes.contains(reparentAction->reparentedNode()))
                remove(iter);
        }
    }

    foreach (RewriteAction *action, removeActionsToRemove) {
        actions.removeOne(action);
        delete action;
    }
}

void RewriteActionCompressor::compressPropertyActions(QList<RewriteAction *> &actions) const
{
    QHash<AbstractProperty, RewriteAction *> removedProperties;
207
    QHash<AbstractProperty, ChangePropertyRewriteAction *> changedProperties;
208
209
210
211
212
213
214
    QSet<AbstractProperty> addedProperties;

    QMutableListIterator<RewriteAction*> iter(actions);
    iter.toBack();
    while (iter.hasPrevious()) {
        RewriteAction *action = iter.previous();

215
        if (RemovePropertyRewriteAction *removeAction = action->asRemovePropertyRewriteAction()) {
216
            removedProperties.insert(removeAction->property(), action);
217
        } else if (ChangePropertyRewriteAction *changeAction = action->asChangePropertyRewriteAction()) {
218
219
220
221
222
223
224
225
226
227
            const AbstractProperty property = changeAction->property();

            if (removedProperties.contains(property)) {
                remove(iter);
            } else if (changedProperties.contains(property)) {
                if (!property.isValid() || !property.isDefaultProperty())
                    remove(iter);
            } else {
                changedProperties.insert(property, changeAction);
            }
228
        } else if (AddPropertyRewriteAction *addAction = action->asAddPropertyRewriteAction()) {
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
            const AbstractProperty property = addAction->property();

            if (RewriteAction *removeAction = removedProperties.value(property, 0)) {
                actions.removeOne(removeAction);
                removedProperties.remove(property);
                delete removeAction;
                remove(iter);
            } else {
                if (changedProperties.contains(property))
                    changedProperties.remove(property);

                addedProperties.insert(property);
            }
        }
    }
}

void RewriteActionCompressor::compressAddEditActions(QList<RewriteAction *> &actions) const
{
    QSet<ModelNode> addedNodes;
    QSet<RewriteAction *> dirtyActions;

    QMutableListIterator<RewriteAction*> iter(actions);
    while (iter.hasNext()) {
        RewriteAction *action = iter.next();

        if (action->asAddPropertyRewriteAction() || action->asChangePropertyRewriteAction()) {
            AbstractProperty property;
            ModelNode containedNode;

259
            if (AddPropertyRewriteAction *addAction = action->asAddPropertyRewriteAction()) {
260
261
                property = addAction->property();
                containedNode = addAction->containedModelNode();
262
            } else if (ChangePropertyRewriteAction *changeAction = action->asChangePropertyRewriteAction()) {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
                property = changeAction->property();
                containedNode = changeAction->containedModelNode();
            }

            if (property.isValid() && addedNodes.contains(property.parentModelNode())) {
                remove(iter);
                continue;
            }

            if (!containedNode.isValid())
                continue;

            if (nodeOrParentInSet(containedNode, addedNodes)) {
                remove(iter);
            } else {
                addedNodes.insert(containedNode);
                dirtyActions.insert(action);
            }
281
        } else if (ChangeIdRewriteAction *changeIdAction = action->asChangeIdRewriteAction()) {
282
283
284
            if (nodeOrParentInSet(changeIdAction->node(), addedNodes)) {
                remove(iter);
            }
285
        } else if (ChangeTypeRewriteAction *changeTypeAction = action->asChangeTypeRewriteAction()) {
286
287
288
289
290
291
292
293
294
            if (nodeOrParentInSet(changeTypeAction->node(), addedNodes)) {
                remove(iter);
            }
        }
    }

    QmlTextGenerator gen(m_propertyOrder);
    foreach (RewriteAction *action, dirtyActions) {
        RewriteAction *newAction = 0;
295
        if (AddPropertyRewriteAction *addAction = action->asAddPropertyRewriteAction()) {
296
297
298
299
            newAction = new AddPropertyRewriteAction(addAction->property(),
                                                     gen(addAction->containedModelNode()),
                                                     addAction->propertyType(),
                                                     addAction->containedModelNode());
300
        } else if (ChangePropertyRewriteAction *changeAction = action->asChangePropertyRewriteAction()) {
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
            newAction = new ChangePropertyRewriteAction(changeAction->property(),
                                                        gen(changeAction->containedModelNode()),
                                                        changeAction->propertyType(),
                                                        changeAction->containedModelNode());
        }

        const int idx = actions.indexOf(action);
        if (newAction && idx >= 0) {
            actions[idx] = newAction;
        }
    }
}

void RewriteActionCompressor::compressAddReparentActions(QList<RewriteAction *> &actions) const
{
    QMap<ModelNode, RewriteAction*> addedNodes;

    QMutableListIterator<RewriteAction*> iter(actions);
    while (iter.hasNext()) {
        RewriteAction *action = iter.next();

        if (action->asAddPropertyRewriteAction() || action->asChangePropertyRewriteAction()) {
            ModelNode containedNode;

325
            if (AddPropertyRewriteAction *addAction = action->asAddPropertyRewriteAction()) {
326
                containedNode = addAction->containedModelNode();
327
            } else if (ChangePropertyRewriteAction *changeAction = action->asChangePropertyRewriteAction()) {
328
329
330
331
332
333
334
                containedNode = changeAction->containedModelNode();
            }

            if (!containedNode.isValid())
                continue;

            addedNodes.insert(containedNode, action);
335
        } else if (ReparentNodeRewriteAction *reparentAction = action->asReparentNodeRewriteAction()) {
336
337
338
339
340
            if (addedNodes.contains(reparentAction->reparentedNode())) {
                RewriteAction *previousAction = addedNodes[reparentAction->reparentedNode()];
                actions.removeOne(previousAction);

                RewriteAction *replacementAction = 0;
341
                if (AddPropertyRewriteAction *addAction = previousAction->asAddPropertyRewriteAction()) {
342
343
344
345
                    replacementAction = new AddPropertyRewriteAction(reparentAction->targetProperty(),
                                                                     addAction->valueText(),
                                                                     reparentAction->propertyType(),
                                                                     addAction->containedModelNode());
346
                } else if (ChangePropertyRewriteAction *changeAction = previousAction->asChangePropertyRewriteAction()) {
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
                    replacementAction = new AddPropertyRewriteAction(reparentAction->targetProperty(),
                                                                     changeAction->valueText(),
                                                                     reparentAction->propertyType(),
                                                                     changeAction->containedModelNode());
                }

                iter.setValue(replacementAction);
                delete previousAction;
                delete action;
            }
        }
    }
}

void RewriteActionCompressor::remove(QMutableListIterator<RewriteAction*> &iter) const
{
    delete iter.value();
    iter.remove();
}