aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/plugin_interface/scxmldocument.h
blob: fe3f9949a8b295e0c0b7f5ea27a88947d0cee769 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <QFileInfo>
#include <QHash>
#include <QMap>
#include <QObject>
#include <QPointF>
#include <QString>
#include <QVector>

QT_FORWARD_DECLARE_CLASS(QUndoStack)
QT_FORWARD_DECLARE_CLASS(QXmlStreamReader)

namespace ScxmlEditor {

namespace PluginInterface {

class ScxmlNamespace;
class ScxmlTag;

/**
 * @brief The ScxmlDocument class represents an SCXML document.
 *
 * This is the root of the document tree, and provides the access to the document's data.
 * The tag-element is ScxmlTag. You can create ScxmlTag-objects outside this class, but you have to call data-manipulation
 * functions from this class to be sure that all views can update their tags. When calling data-manipulation functions, this class
 * will send beginTagChange and endTagChange signals with the correspond values. This class also keep the undo-stack of the changes.
 *
 * When document has changed, the signal documentChanged(bool) will be emitted.
 */
class ScxmlDocument : public QObject
{
    Q_OBJECT

public:
    /**
     * @brief The TagChange enum
     */
    enum TagChange {
        TagAddChild = 0,
        TagAddTags,
        TagRemoveChild,
        TagRemoveTags,
        TagCurrentChanged,
        TagAttributesChanged,
        TagEditorInfoChanged,
        TagChangeParent,
        TagChangeParentRemoveChild,
        TagChangeParentAddChild,
        TagChangeOrder,
        TagContentChanged,
        TagChangeFullNameSpace
    };

    explicit ScxmlDocument(const QString &fileName = QString(), QObject *parent = nullptr);
    explicit ScxmlDocument(const QByteArray &data, QObject *parent = nullptr);
    ~ScxmlDocument() override;

    void addNamespace(ScxmlNamespace *ns);
    ScxmlNamespace *scxmlNamespace(const QString &prefix);

    /**
     * @brief setCurrentTag - inform views that current selected tag has changed
     * @param tag - current tag
     */
    void setCurrentTag(ScxmlTag *tag);
    ScxmlTag *currentTag() const;

    /**
     * @brief setContent - inform views that tag-content has changed
     * @param tag
     * @param content
     */
    void setContent(ScxmlTag *tag, const QString &content);

    /**
     * @brief setValue - inform views that tag-value has changed
     * @param tag
     * @param key
     * @param value
     */
    void setValue(ScxmlTag *tag, const QString &key, const QString &value);

    /**
     * @brief setValue - inform views that tag-value has changed
     * @param tag
     * @param attributeIndex
     * @param value
     */
    void setValue(ScxmlTag *tag, int attributeIndex, const QString &value);

    /**
     * @brief setEditorInfo - inform views (and save undo-command) that editorinfo has changed
     * @param tag
     * @param key
     * @param value
     */
    void setEditorInfo(ScxmlTag *tag, const QString &key, const QString &value);

    /**
     * @brief addTag - inform views that new child will be added to the tree
     * @param parent
     * @param child
     */
    void addTag(ScxmlTag *parent, ScxmlTag *child);
    void addTags(ScxmlTag *parent, const QVector<ScxmlTag*> tags);

    /**
     * @brief removeTag - inform views that tag will be removed
     * @param tag
     */
    void removeTag(ScxmlTag *tag);

    /**
     * @brief changeParent - inform views that tag parent will be changed
     * @param child
     * @param newParent
     * @param tagIndex
     */
    void changeParent(ScxmlTag *child, ScxmlTag *newParent, int tagIndex = -1);

    /**
     * @brief changeOrder - inform vies that tag's child-position will be changed
     * @param child
     * @param newPos
     */
    void changeOrder(ScxmlTag *child, int newPos);

    /**
     * @brief changed - holds the changes-status of the document
     * @return - true if changed, false otherwise
     */
    bool changed() const;

    /**
     * @brief rootTag - return rootTag of the document
     * @return
     */
    ScxmlTag *rootTag() const;
    ScxmlTag *scxmlRootTag() const;
    ScxmlTag *tagForId(const QString &id) const;
    void pushRootTag(ScxmlTag *tag);
    ScxmlTag *popRootTag();

    /**
     * @brief fileName - return current filename of the document
     * @return
     */
    QString fileName() const;
    void setFileName(const QString &filename);

    /**
     * @brief undoStack - return undo-stack of the document (see UndoStack)
     * @return
     */
    QUndoStack *undoStack() const;

    /**
     * @brief save - save document with given filename
     * @param fileName -
     * @return return true if file has successfully wrote
     */
    bool save(const QString &fileName);
    bool save();
    void load(const QString &fileName);
    bool pasteData(const QByteArray &data, const QPointF &minPos = QPointF(0, 0), const QPointF &pastePos = QPointF(0, 0));
    bool load(QIODevice *io);

    /**
     * @brief nextId - generate and return next id depends on the given key
     * @param key
     * @return return value will be the form key_nextId
     */
    QString nextUniqueId(const QString &key);
    QString getUniqueCopyId(const ScxmlTag *tag);

    /**
     * @brief hasLayouted - tells is there qt-namespace available in the current loaded document
     * @return true or false
     */
    bool hasLayouted() const;

    void setLevelColors(const QVector<QColor> &colors);
    QColor getColor(int depth) const;

    bool hasError() const
    {
        return m_hasError;
    }

    QString lastError() const
    {
        return m_lastError;
    }

    QByteArray content(const QVector<ScxmlTag*> &tags) const;
    QByteArray content(ScxmlTag *tag = nullptr) const;
    void clear(bool createRoot = true);

    // Full NameSpace functions
    QString nameSpaceDelimiter() const;
    bool useFullNameSpace() const;
    void setNameSpaceDelimiter(const QString &delimiter);
    void setUseFullNameSpace(bool use);

    void setUndoRedoRunning(bool para);

    QFileInfo qtBinDir() const;

signals:
    /**
     * @brief documentChanged - this signal will be emitted just after document has changed and is not in clean state
     * @param changed
     */
    void documentChanged(bool changed);

    /**
     * @brief beginTagChange - this signal will be emitted just before some tagchange will be made (see TagChange)
     * @param change
     * @param tag
     * @param value
     */
    void beginTagChange(ScxmlDocument::TagChange change, ScxmlTag *tag, const QVariant &value);

    /**
     * @brief beginTagChange - this signal will be emitted just after some tagchange has made (see TagChange)
     * @param change
     * @param tag
     * @param value
     */
    void endTagChange(ScxmlDocument::TagChange change, ScxmlTag *tag, const QVariant &value);

    /**
     * @brief colorThemeChanged - this signal will be emitted just after color theme has changed
     */
    void colorThemeChanged();

private:
    friend class ScxmlTag;
    friend class ChangeFullNameSpaceCommand;

    void initErrorMessage(const QXmlStreamReader &xml, QIODevice *io);
    bool generateSCXML(QIODevice *io, ScxmlTag *tag = nullptr) const;
    void addChild(ScxmlTag *tag);
    void removeChild(ScxmlTag *tag);
    void printSCXML();
    void deleteRootTags();
    void clearNamespaces();
    void initVariables();
    void addTagRecursive(ScxmlTag *parent, ScxmlTag *tag);
    void removeTagRecursive(ScxmlTag *tag);

    ScxmlTag *createScxmlTag();
    QString m_fileName;
    QUndoStack *m_undoStack;
    QVector<ScxmlTag*> m_tags;
    QHash<QString, int> m_nextIdHash;
    QHash<QString, QString> m_idMap;
    bool m_hasError = false;
    QString m_lastError;
    QVector<ScxmlTag*> m_rootTags;
    QMap<QString, ScxmlNamespace*> m_namespaces;
    QVector<QColor> m_colors;
    bool m_hasLayouted = false;
    QString m_idDelimiter;
    bool m_useFullNameSpace = false;
    ScxmlTag *m_currentTag = nullptr;
    bool m_undoRedoRunning = false;
    QFileInfo m_qtBinDir;
};

} // namespace PluginInterface
} // namespace ScxmlEditor