aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/common/structuremodel.cpp
blob: 5097bf0e4d4711492c70e1733bbf958a4c04af6c (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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

#include "structuremodel.h"
#include "scxmldocument.h"
#include "scxmltag.h"

#include <QMimeData>
#include <QUndoStack>

using namespace ScxmlEditor::PluginInterface;
using namespace ScxmlEditor::Common;

StructureModel::StructureModel(QObject *parent)
    : QAbstractItemModel(parent)
{
    m_icons.addIcon(State, QIcon(":/scxmleditor/images/state.png"));
    m_icons.addIcon(Parallel, QIcon(":/scxmleditor/images/parallel.png"));
    m_icons.addIcon(Initial, QIcon(":/scxmleditor/images/initial.png"));
    m_icons.addIcon(Final, QIcon(":/scxmleditor/images/final.png"));
}

void StructureModel::setDocument(ScxmlDocument *document)
{
    beginResetModel();
    if (m_document)
        disconnect(m_document, nullptr, this, nullptr);

    m_document = document;
    if (m_document) {
        connect(m_document.data(), &ScxmlDocument::beginTagChange, this, &StructureModel::beginTagChange);
        connect(m_document.data(), &ScxmlDocument::endTagChange, this, &StructureModel::endTagChange);
    }
    endResetModel();
}

int StructureModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 1;
}

bool StructureModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != Qt::EditRole || value.toString().isEmpty())
        return false;

    ScxmlTag *tag = getItem(index);
    if (!tag || tag->tagType() > MetadataItem)
        return false;

    tag->setTagName(value.toString());
    emit dataChanged(index, index);
    m_document->setCurrentTag(tag);

    return true;
}

QVariant StructureModel::data(const QModelIndex &index, int role) const
{
    ScxmlTag *tag = getItem(index);
    if (!tag)
        return QVariant();

    switch (role) {
    case TagTypeRole:
        return tag->tagType();
    case Qt::DecorationRole:
        return m_icons.icon(tag->tagType());
    case Qt::DisplayRole: {
        switch (tag->tagType()) {
        case State:
        case Parallel:
        case Initial:
        case Data:
        case Final:
            if (tag->hasAttribute("id"))
                return tag->attribute("id");
            break;
        case Transition:
            if (tag->hasAttribute("event"))
                return tag->attribute("event");
            break;
        default:
            break;
        }

        return tag->tagName();
    }
    case Qt::EditRole:
        return tag->tagName(false);

    default:
        break;
    }

    return QVariant();
}

ScxmlTag *StructureModel::getItem(const QModelIndex &parent, int source_row) const
{
    const ScxmlTag *tag = getItem(parent);
    if (tag)
        return tag->child(source_row);

    return nullptr;
}

ScxmlTag *StructureModel::getItem(const QModelIndex &index) const
{
    if (index.isValid()) {
        auto tag = static_cast<ScxmlTag*>(index.internalPointer());
        if (tag)
            return tag;
    }
    return m_document ? m_document->rootTag() : nullptr;
}

QModelIndex StructureModel::index(int row, int column, const QModelIndex &index) const
{
    if (!index.isValid() && m_document)
        return createIndex(0, 0, m_document->rootTag());

    const ScxmlTag *tag = getItem(index);
    if (tag) {
        ScxmlTag *childTag = tag->child(row);
        if (childTag)
            return createIndex(row, column, childTag);
    }

    return QModelIndex();
}

QModelIndex StructureModel::parent(const QModelIndex &index) const
{
    if (!m_document)
        return QModelIndex();

    if (!index.isValid())
        return QModelIndex();

    const ScxmlTag *child = getItem(index);
    if (child && child != m_document->rootTag()) {
        ScxmlTag *parentTag = child->parentTag();
        if (parentTag)
            return createIndex(parentTag->index(), 0, parentTag);
    }

    return QModelIndex();
}

int StructureModel::rowCount(const QModelIndex &index) const
{
    if (!index.isValid())
        return m_document ? 1 : 0;

    const ScxmlTag *tag = getItem(index);

    if (tag)
        return tag->childCount();

    return 0;
}

Qt::DropActions StructureModel::supportedDropActions() const
{
    return Qt::MoveAction;
}

QStringList StructureModel::mimeTypes() const
{
    return QAbstractItemModel::mimeTypes();
}

QMimeData *StructureModel::mimeData(const QModelIndexList &indexes) const
{
    if (indexes.count() == 1)
        m_dragTag = getItem(indexes.first());

    return QAbstractItemModel::mimeData(indexes);
}

bool StructureModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &p) const
{
    Q_UNUSED(data)
    Q_UNUSED(action)
    Q_UNUSED(row)
    Q_UNUSED(column)

    const ScxmlTag *tag = getItem(p);
    return tag && m_dragTag && (tag->tagType() == State || tag->tagType() == Parallel || tag->tagType() == Scxml);
}

bool StructureModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &p)
{
    Q_UNUSED(data)
    Q_UNUSED(action)
    Q_UNUSED(row)
    Q_UNUSED(column)

    ScxmlTag *tag = getItem(p);
    if (tag && m_dragTag && tag != m_dragTag && (tag->tagType() == State || tag->tagType() == Parallel || tag->tagType() == Scxml)) {
        m_document->undoStack()->beginMacro(tr("Change parent"));
        m_document->changeParent(m_dragTag, tag);
        m_document->undoStack()->endMacro();
        m_dragTag = nullptr;
        return true;
    }

    m_dragTag = nullptr;
    return false;
}

Qt::ItemFlags StructureModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);

    const ScxmlTag *tag = getItem(index);
    if (index.isValid() && tag) {
        switch (tag->tagType()) {
        case State:
        case Parallel:
        case Initial:
        case Final:
        case History:
            defaultFlags |= Qt::ItemIsDragEnabled;
            Q_FALLTHROUGH();
        case Scxml:
            defaultFlags |= Qt::ItemIsDropEnabled;
            break;
        default:
            break;
        }
    }

    if (tag) {
        if (tag->tagType() == UnknownTag || tag->tagType() == MetadataItem)
            defaultFlags |= Qt::ItemIsEditable;
    }

    return defaultFlags;
}

void StructureModel::updateData()
{
    emit dataChanged(QModelIndex(), QModelIndex());
}

void StructureModel::beginTagChange(ScxmlDocument::TagChange change, ScxmlTag *tag, const QVariant &value)
{
    if (!tag)
        return;

    switch (change) {
    case ScxmlDocument::TagAddChild:
    case ScxmlDocument::TagChangeParentAddChild:
        beginInsertRows(createIndex(tag->index(), 0, tag), value.toInt(), value.toInt());
        break;
    case ScxmlDocument::TagRemoveChild:
    case ScxmlDocument::TagChangeParentRemoveChild:
        beginRemoveRows(createIndex(tag->index(), 0, tag), value.toInt(), value.toInt());
        break;
    case ScxmlDocument::TagChangeOrder: {
        int r1 = tag->index();
        int r2 = value.toInt();
        if (r2 > r1)
            r2++;

        QModelIndex ind = createIndex(r1, 0, tag);
        beginMoveRows(ind, r1, r1, ind, r2);
        break;
    }
    default:
        break;
    }
}

void StructureModel::endTagChange(ScxmlDocument::TagChange change, ScxmlTag *tag, const QVariant &value)
{
    if (!tag)
        return;

    switch (change) {
    case ScxmlDocument::TagAttributesChanged: {
        emit dataChanged(QModelIndex(), QModelIndex());
        break;
    }
    case ScxmlDocument::TagAddChild:
    case ScxmlDocument::TagChangeParentAddChild: {
        endInsertRows();
        emit childAdded(createIndex(0, 0, tag->child(value.toInt())));
        break;
    }
    case ScxmlDocument::TagChangeParentRemoveChild: {
        endRemoveRows();
        break;
    }
    case ScxmlDocument::TagRemoveChild: {
        endRemoveRows();
        break;
    }
    case ScxmlDocument::TagChangeOrder: {
        endMoveRows();
        break;
    }
    case ScxmlDocument::TagCurrentChanged: {
        emit selectIndex(createIndex(tag->index(), 0, tag));
        break;
    }
    default:
        break;
    }
}