aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/plugin_interface/undocommands.h
blob: fb3d17c9d7f9402c6ea27d6109ce8d0e30f69678 (plain)
1
2
3
4
5
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "scxmldocument.h"
#include "scxmltag.h"
#include <QUndoCommand>

namespace ScxmlEditor {

namespace PluginInterface {

class BaseUndoCommand : public QUndoCommand
{
public:
    BaseUndoCommand(ScxmlDocument *doc, QUndoCommand *parent = nullptr);

    void undo() override;
    void redo() override;

protected:
    virtual void doUndo() = 0;
    virtual void doRedo() = 0;

private:
    ScxmlDocument *m_doc;
    bool m_firstTime = true;
};

class AddRemoveTagsBeginCommand : public BaseUndoCommand
{
public:
    AddRemoveTagsBeginCommand(ScxmlDocument *doc, ScxmlTag *tag, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

private:
    ScxmlDocument *m_document;
    ScxmlTag *m_tag;
};

class AddRemoveTagsEndCommand : public BaseUndoCommand
{
public:
    AddRemoveTagsEndCommand(ScxmlDocument *doc, ScxmlTag *tag, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

private:
    ScxmlDocument *m_document;
    ScxmlTag *m_tag;
};

/**
 * @brief The AddRemoveTagCommand class provides the undo-command to add/remove tag to/from SCXML-document.
 */
class AddRemoveTagCommand : public BaseUndoCommand
{
public:
    AddRemoveTagCommand(ScxmlDocument *doc, ScxmlTag *parentTag, ScxmlTag *tag, ScxmlDocument::TagChange change, QUndoCommand *parent = nullptr);
    ~AddRemoveTagCommand() override;

    void doUndo() override;
    void doRedo() override;

private:
    void doAction(bool add);
    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QPointer<ScxmlTag> m_parentTag;
    ScxmlDocument::TagChange m_change;
};

/**
 * @brief The SetAttributeCommand class provides the undo-command to set attribute-value for the given ScxmlTag.
 */
class SetAttributeCommand : public BaseUndoCommand
{
public:
    SetAttributeCommand(ScxmlDocument *doc, ScxmlTag *tag, const QString &key, const QString &value, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

    bool mergeWith(const QUndoCommand *other) override;
    int id() const override;

private:
    void doAction(const QString &key, const QString &value);
    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QString m_key;
    QString m_value;
    QString m_oldValue;
};

/**
 * @brief The SetContentCommand class
 */
class SetContentCommand : public BaseUndoCommand
{
public:
    SetContentCommand(ScxmlDocument *doc, ScxmlTag *tag, const QString &content, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

    bool mergeWith(const QUndoCommand *other) override;
    int id() const override;

private:
    void doAction(const QString &content);
    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QString m_content;
    QString m_oldContent;
};

/**
 * @brief The SetEditorInfoCommand class provides the undo-command to set editorinfo-value for the given ScxmlTag.
 */
class SetEditorInfoCommand : public BaseUndoCommand
{
public:
    SetEditorInfoCommand(ScxmlDocument *doc, ScxmlTag *tag, const QString &key, const QString &value, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

    bool mergeWith(const QUndoCommand *other) override;
    int id() const override;

private:
    void doAction(const QString &key, const QString &value);
    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QString m_key;
    QString m_value;
    QString m_oldValue;
};

/**
 * @brief The ChangeParentCommand class provides the undo-commant to change parent of the ScxmlTag.
 */
class ChangeParentCommand : public BaseUndoCommand
{
public:
    ChangeParentCommand(ScxmlDocument *doc, ScxmlTag *tag, ScxmlTag *newParentTag, int tagIndex, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

private:
    void doAction(ScxmlTag *oldParent, ScxmlTag *newParent);

    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QPointer<ScxmlTag> m_newParentTag;
    QPointer<ScxmlTag> m_oldParentTag;
    int m_tagIndex;
};

/**
 * @brief The ChangeOrderCommand class
 */
class ChangeOrderCommand : public BaseUndoCommand
{
public:
    ChangeOrderCommand(ScxmlDocument *doc, ScxmlTag *tag, ScxmlTag *parentTag, int newPos, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

private:
    void doAction(int newPos);

    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_tag;
    QPointer<ScxmlTag> m_parentTag;
    int m_oldPos;
    int m_newPos;
};

/**
 * @brief The ChangeNameSpaceCommand class
 */
class ChangeFullNameSpaceCommand : public BaseUndoCommand
{
public:
    ChangeFullNameSpaceCommand(ScxmlDocument *doc, ScxmlTag *tag, bool state, QUndoCommand *parent = nullptr);

    void doUndo() override;
    void doRedo() override;

private:
    void doAction(bool state);
    void makeIdMap(ScxmlTag *tag, QHash<QString, QString> &map, bool use);
    void updateNameSpace(ScxmlTag *tag, const QHash<QString, QString> &map);

    ScxmlDocument *m_document;
    QPointer<ScxmlTag> m_rootTag;
    bool m_oldState;
    bool m_newState;
};

} // namespace PluginInterface
} // namespace ScxmlEditor