mclonevisitor.cpp 6.99 KB
Newer Older
1
/****************************************************************************
Jochen Becher's avatar
Jochen Becher committed
2
**
3 4
** Copyright (C) 2016 Jochen Becher
** Contact: https://www.qt.io/licensing/
Jochen Becher's avatar
Jochen Becher committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Jochen Becher's avatar
Jochen Becher committed
15
**
16 17 18 19 20 21 22
** 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.
Jochen Becher's avatar
Jochen Becher committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
**
****************************************************************************/

#include "mclonevisitor.h"

#include "qmt/model/melement.h"
#include "qmt/model/mobject.h"
#include "qmt/model/mclass.h"
#include "qmt/model/mcomponent.h"
#include "qmt/model/mpackage.h"
#include "qmt/model/mdiagram.h"
#include "qmt/model/mcanvasdiagram.h"
#include "qmt/model/mitem.h"
#include "qmt/model/mrelation.h"
#include "qmt/model/massociation.h"
#include "qmt/model/mdependency.h"
#include "qmt/model/minheritance.h"

#include "qmt/diagram/delement.h"
#include "qmt/diagram_controller/dclonevisitor.h"

namespace qmt {

MCloneVisitor::MCloneVisitor()
47
    : m_cloned(0)
Jochen Becher's avatar
Jochen Becher committed
48 49 50 51 52 53
{
}

void MCloneVisitor::visitMElement(const MElement *element)
{
    Q_UNUSED(element);
54
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
55 56 57 58
}

void MCloneVisitor::visitMObject(const MObject *object)
{
59
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
60 61 62 63 64
    visitMElement(object);
}

void MCloneVisitor::visitMPackage(const MPackage *package)
{
65
    if (!m_cloned)
66
        m_cloned = new MPackage(*package);
Jochen Becher's avatar
Jochen Becher committed
67 68 69 70 71
    visitMObject(package);
}

void MCloneVisitor::visitMClass(const MClass *klass)
{
72
    if (!m_cloned)
73
        m_cloned = new MClass(*klass);
Jochen Becher's avatar
Jochen Becher committed
74 75 76 77 78
    visitMObject(klass);
}

void MCloneVisitor::visitMComponent(const MComponent *component)
{
79
    if (!m_cloned)
80
        m_cloned = new MComponent(*component);
Jochen Becher's avatar
Jochen Becher committed
81 82 83 84 85
    visitMObject(component);
}

void MCloneVisitor::visitMDiagram(const MDiagram *diagram)
{
86
    QMT_CHECK(m_cloned);
87
    auto cloned = dynamic_cast<MDiagram *>(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
88
    QMT_CHECK(cloned);
89
    foreach (const DElement *element, diagram->diagramElements()) {
Jochen Becher's avatar
Jochen Becher committed
90 91
        DCloneDeepVisitor visitor;
        element->accept(&visitor);
92
        DElement *clonedElement = visitor.cloned();
93
        cloned->addDiagramElement(clonedElement);
Jochen Becher's avatar
Jochen Becher committed
94 95 96 97 98 99
    }
    visitMObject(diagram);
}

void MCloneVisitor::visitMCanvasDiagram(const MCanvasDiagram *diagram)
{
100
    if (!m_cloned)
101
        m_cloned = new MCanvasDiagram(*diagram);
Jochen Becher's avatar
Jochen Becher committed
102 103 104 105 106
    visitMDiagram(diagram);
}

void MCloneVisitor::visitMItem(const MItem *item)
{
107
    if (!m_cloned)
108
        m_cloned = new MItem(*item);
Jochen Becher's avatar
Jochen Becher committed
109 110 111 112 113
    visitMObject(item);
}

void MCloneVisitor::visitMRelation(const MRelation *relation)
{
114
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
115 116 117 118 119
    visitMElement(relation);
}

void MCloneVisitor::visitMDependency(const MDependency *dependency)
{
120
    if (!m_cloned)
121
        m_cloned = new MDependency(*dependency);
Jochen Becher's avatar
Jochen Becher committed
122 123 124 125 126
    visitMRelation(dependency);
}

void MCloneVisitor::visitMInheritance(const MInheritance *inheritance)
{
127
    if (!m_cloned)
128
        m_cloned = new MInheritance(*inheritance);
Jochen Becher's avatar
Jochen Becher committed
129 130 131 132 133
    visitMRelation(inheritance);
}

void MCloneVisitor::visitMAssociation(const MAssociation *association)
{
134
    if (!m_cloned)
135
        m_cloned = new MAssociation(*association);
Jochen Becher's avatar
Jochen Becher committed
136 137 138 139
    visitMRelation(association);
}

MCloneDeepVisitor::MCloneDeepVisitor()
140
    : m_cloned(0)
Jochen Becher's avatar
Jochen Becher committed
141 142 143 144 145 146
{
}

void MCloneDeepVisitor::visitMElement(const MElement *element)
{
    Q_UNUSED(element);
147
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
148 149 150 151
}

void MCloneDeepVisitor::visitMObject(const MObject *object)
{
152
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
153
    visitMElement(object);
154
    auto cloned = dynamic_cast<MObject *>(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
155
    QMT_CHECK(cloned);
156
    foreach (const Handle<MObject> &handle, object->children()) {
Jochen Becher's avatar
Jochen Becher committed
157 158
        if (handle.hasTarget()) {
            MCloneDeepVisitor visitor;
159
            handle.target()->accept(&visitor);
160
            auto clonedChild = dynamic_cast<MObject *>(visitor.cloned());
161 162
            QMT_CHECK(clonedChild);
            cloned->addChild(clonedChild);
Jochen Becher's avatar
Jochen Becher committed
163
        } else {
164
            cloned->addChild(handle.uid());
Jochen Becher's avatar
Jochen Becher committed
165 166
        }
    }
167
    foreach (const Handle<MRelation> &handle, object->relations()) {
Jochen Becher's avatar
Jochen Becher committed
168 169
        if (handle.hasTarget()) {
            MCloneDeepVisitor visitor;
170
            handle.target()->accept(&visitor);
171
            auto clonedRelation = dynamic_cast<MRelation *>(visitor.cloned());
172 173
            QMT_CHECK(clonedRelation);
            cloned->addRelation(clonedRelation);
Jochen Becher's avatar
Jochen Becher committed
174
        } else {
175
            cloned->addRelation(handle.uid());
Jochen Becher's avatar
Jochen Becher committed
176 177 178 179 180 181
        }
    }
}

void MCloneDeepVisitor::visitMPackage(const MPackage *package)
{
182
    if (!m_cloned)
183
        m_cloned = new MPackage(*package);
Jochen Becher's avatar
Jochen Becher committed
184 185 186 187 188
    visitMObject(package);
}

void MCloneDeepVisitor::visitMClass(const MClass *klass)
{
189
    if (!m_cloned)
190
        m_cloned = new MClass(*klass);
Jochen Becher's avatar
Jochen Becher committed
191 192 193 194 195
    visitMObject(klass);
}

void MCloneDeepVisitor::visitMComponent(const MComponent *component)
{
196
    if (!m_cloned)
197
        m_cloned = new MComponent(*component);
Jochen Becher's avatar
Jochen Becher committed
198 199 200 201 202
    visitMObject(component);
}

void MCloneDeepVisitor::visitMDiagram(const MDiagram *diagram)
{
203
    QMT_CHECK(m_cloned);
204
    auto cloned = dynamic_cast<MDiagram *>(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
205
    QMT_CHECK(cloned);
206
    foreach (const DElement *element, diagram->diagramElements()) {
Jochen Becher's avatar
Jochen Becher committed
207 208
        DCloneDeepVisitor visitor;
        element->accept(&visitor);
209
        DElement *clonedElement = visitor.cloned();
210
        cloned->addDiagramElement(clonedElement);
Jochen Becher's avatar
Jochen Becher committed
211 212 213 214 215 216
    }
    visitMObject(diagram);
}

void MCloneDeepVisitor::visitMCanvasDiagram(const MCanvasDiagram *diagram)
{
217
    if (!m_cloned)
218
        m_cloned = new MCanvasDiagram(*diagram);
Jochen Becher's avatar
Jochen Becher committed
219 220 221 222 223
    visitMDiagram(diagram);
}

void MCloneDeepVisitor::visitMItem(const MItem *item)
{
224
    if (!m_cloned)
225
        m_cloned = new MItem(*item);
Jochen Becher's avatar
Jochen Becher committed
226 227 228 229 230
    visitMObject(item);
}

void MCloneDeepVisitor::visitMRelation(const MRelation *relation)
{
231
    QMT_CHECK(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
232
    visitMElement(relation);
233
    auto cloned = dynamic_cast<MRelation *>(m_cloned);
Jochen Becher's avatar
Jochen Becher committed
234
    QMT_CHECK(cloned);
235 236
    cloned->setEndAUid(relation->endAUid());
    cloned->setEndBUid(relation->endBUid());
Jochen Becher's avatar
Jochen Becher committed
237 238 239 240
}

void MCloneDeepVisitor::visitMDependency(const MDependency *dependency)
{
241
    if (!m_cloned)
242
        m_cloned = new MDependency(*dependency);
Jochen Becher's avatar
Jochen Becher committed
243 244 245 246 247
    visitMRelation(dependency);
}

void MCloneDeepVisitor::visitMInheritance(const MInheritance *inheritance)
{
248
    if (!m_cloned)
249
        m_cloned = new MInheritance(*inheritance);
Jochen Becher's avatar
Jochen Becher committed
250 251 252 253 254
    visitMRelation(inheritance);
}

void MCloneDeepVisitor::visitMAssociation(const MAssociation *association)
{
255
    if (!m_cloned)
256
        m_cloned = new MAssociation(*association);
Jochen Becher's avatar
Jochen Becher committed
257 258 259
    visitMRelation(association);
}

260
} // namespace qmt