aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qmt/model_controller/modelcontroller.h
blob: 8b9f0fdf86a9adfe4e39bf8c8ec4f21e88ed2993 (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
/****************************************************************************
**
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#ifndef QMT_MODELCONTROLLER_H
#define QMT_MODELCONTROLLER_H

#include "qmt/infrastructure/uid.h"

#include <QObject>
#include <QHash>
#include <QMultiHash>

namespace qmt {

class UndoController;
class MElement;
class MObject;
class MPackage;
class MDiagram;
class MRelation;
class MSelection;
class MContainer;
class MReferences;

class QMT_EXPORT ModelController : public QObject
{
    Q_OBJECT

    enum ElementType {
        TypeUnknown,
        TypeObject,
        TypeRelation };

    class Clone;
    class UpdateObjectCommand;
    class UpdateRelationCommand;
    class AddElementsCommand;
    class RemoveElementsCommand;
    class MoveObjectCommand;
    class MoveRelationCommand;

public:
    explicit ModelController(QObject *parent = 0);
    ~ModelController() override;

signals:
    void beginResetModel();
    void endResetModel();
    void beginUpdateObject(int row, const MObject *owner);
    void endUpdateObject(int row, const MObject *owner);
    void beginInsertObject(int row, const MObject *owner);
    void endInsertObject(int row, const MObject *owner);
    void beginRemoveObject(int row, const MObject *owner);
    void endRemoveObject(int row, const MObject *owner);
    void beginMoveObject(int formerRow, const MObject *formerOwner);
    void endMoveObject(int newRow, const MObject *newOwner);
    void beginUpdateRelation(int row, const MObject *owner);
    void endUpdateRelation(int row, const MObject *owner);
    void beginInsertRelation(int row, const MObject *owner);
    void endInsertRelation(int row, const MObject *owner);
    void beginRemoveRelation(int row, const MObject *owner);
    void endRemoveRelation(int row, const MObject *owner);
    void beginMoveRelation(int formerRow, const MObject *formerOwner);
    void endMoveRelation(int newRow, const MObject *newOwner);
    void packageNameChanged(MPackage *package, const QString &oldPackageName);
    void relationEndChanged(MRelation *relation, MObject *endObject);
    void modified();

public:
    MPackage *rootPackage() const { return m_rootPackage; }
    void setRootPackage(MPackage *rootPackage);
    UndoController *undoController() const { return m_undoController; }
    void setUndoController(UndoController *undoController);

    Uid ownerKey(const MElement *element) const;
    MElement *findElement(const Uid &key);
    template<class T>
    T *findElement(const Uid &key) { return dynamic_cast<T *>(findElement(key)); }

    void startResetModel();
    void finishResetModel(bool modified);

    MObject *object(int row, const MObject *owner) const;
    MObject *findObject(const Uid &key) const;
    template<class T>
    T *findObject(const Uid &key) const { return dynamic_cast<T *>(findObject(key)); }
    void addObject(MPackage *parentPackage, MObject *object);
    void removeObject(MObject *object);
    void startUpdateObject(MObject *object);
    void finishUpdateObject(MObject *object, bool cancelled);
    void moveObject(MPackage *newOwner, MObject *object);

    MRelation *findRelation(const Uid &key) const;
    template<class T>
    T *findRelation(const Uid &key) const { return dynamic_cast<T *>(findRelation(key)); }
    void addRelation(MObject *owner, MRelation *relation);
    void removeRelation(MRelation *relation);
    void startUpdateRelation(MRelation *relation);
    void finishUpdateRelation(MRelation *relation, bool cancelled);
    void moveRelation(MObject *newOwner, MRelation *relation);

    QList<MRelation *> findRelationsOfObject(const MObject *object) const;

    MContainer cutElements(const MSelection &modelSelection);
    MContainer copyElements(const MSelection &modelSelection);
    void pasteElements(MObject *owner, const MContainer &modelContainer);
    void deleteElements(const MSelection &modelSelection);

private:
    void deleteElements(const MSelection &modelSelection, const QString &commandLabel);
    void removeRelatedRelations(MObject *object);

public:
    void unloadPackage(MPackage *package);
    void loadPackage(MPackage *package);

private:
    void renewElementKey(MElement *element, QHash<Uid, Uid> *renewedKeys);
    void updateRelationKeys(MElement *element, const QHash<Uid, Uid> &renewedKeys);
    void updateRelationEndKeys(MRelation *relation, const QHash<Uid, Uid> &renewedKeys);
    void mapObject(MObject *object);
    void unmapObject(MObject *object);
    void mapRelation(MRelation *relation);
    void unmapRelation(MRelation *relation);

    MReferences simplify(const MSelection &modelSelection);

    void verifyModelIntegrity() const;
    void verifyModelIntegrity(const MObject *object, QHash<Uid, const MObject *> *objectsMap,
                              QHash<Uid, const MRelation *> *relationsMap,
                              QMultiHash<Uid, MRelation *> *objectRelationsMap) const;

    MPackage *m_rootPackage;
    UndoController *m_undoController;
    QHash<Uid, MObject *> m_objectsMap;
    QHash<Uid, MRelation *> m_relationsMap;
    QMultiHash<Uid, MRelation *> m_objectRelationsMap;
    bool m_isResettingModel;
    QString m_oldPackageName;
};

} // namespace qmt

#endif // QMT_MODELCONTROLLER_H