aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/modelinglib/qmt/model_widgets_ui/addrelatedelementsdialog.cpp
blob: 35bfeda6c7db74718cdb7e4955515f8497d7f1a4 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (C) 2018 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "addrelatedelementsdialog.h"
#include "ui_addrelatedelementsdialog.h"

#include <qmt/tasks/diagramscenecontroller.h>
#include <qmt/diagram_controller/dselection.h>
#include <qmt/model/mdiagram.h>
#include <qmt/model/mrelation.h>
#include <qmt/model/mdependency.h>
#include <qmt/model/minheritance.h>
#include <qmt/model/massociation.h>
#include <qmt/model/mconnection.h>
#include <qmt/model_controller/modelcontroller.h>
#include <qmt/model_controller/mvoidvisitor.h>

#include <QStringListModel>


namespace qmt {

namespace {

enum class RelationType {
    Any,
    Dependency,
    Association,
    Inheritance,
    Connection
};

enum class RelationDirection {
    Any,
    Outgoing,
    Incoming,
    Bidirectional
};

enum class ElementType {
    Any,
    Package,
    Component,
    Class,
    Diagram,
    Item,
};

class Filter : public qmt::MVoidConstVisitor {
public:

    void setRelationType(RelationType newRelationType)
    {
        m_relationType = newRelationType;
    }

    void setRelationTypeId(const QString &newRelationTypeId)
    {
        m_relationTypeId = newRelationTypeId;
    }

    void setRelationDirection(RelationDirection newRelationDirection)
    {
        m_relationDirection = newRelationDirection;
    }

    void setRelationStereotypes(const QStringList &newRelationStereotypes)
    {
        m_relationStereotypes = newRelationStereotypes;
    }

    void setElementType(ElementType newElementType)
    {
        m_elementType = newElementType;
    }

    void setElementStereotypes(const QStringList &newElementStereotypes)
    {
        m_elementStereotypes = newElementStereotypes;
    }

    void setObject(const qmt::DObject *dobject, const qmt::MObject *mobject)
    {
        m_dobject = dobject;
        m_mobject = mobject;
    }

    void setRelation(const qmt::MRelation *relation)
    {
        m_relation = relation;
    }

    bool keep() const { return m_keep; }

    void reset()
    {
        m_dobject = nullptr;
        m_mobject = nullptr;
        m_relation = nullptr;
        m_keep = true;
    }

    // MConstVisitor interface
    void visitMObject(const qmt::MObject *object) override
    {
        if (!m_elementStereotypes.isEmpty()) {
            const QStringList stereotypes = object->stereotypes();
            bool containsElementStereotype = std::any_of(
                stereotypes.constBegin(), stereotypes.constEnd(),
                [&](const QString &s) { return m_elementStereotypes.contains(s); });
            if (!containsElementStereotype) {
                m_keep = false;
                return;
            }
        }
    }

    void visitMPackage(const qmt::MPackage *package) override
    {
        if (m_elementType == ElementType::Any || m_elementType == ElementType::Package)
            qmt::MVoidConstVisitor::visitMPackage(package);
        else
            m_keep = false;
    }

    void visitMClass(const qmt::MClass *klass) override
    {
        if (m_elementType == ElementType::Any || m_elementType == ElementType::Class)
            qmt::MVoidConstVisitor::visitMClass(klass);
        else
            m_keep = false;
    }

    void visitMComponent(const qmt::MComponent *component) override
    {
        if (m_elementType == ElementType::Any || m_elementType == ElementType::Component)
            qmt::MVoidConstVisitor::visitMComponent(component);
        else
            m_keep = false;
    }

    void visitMDiagram(const qmt::MDiagram *diagram) override
    {
        if (m_elementType == ElementType::Any || m_elementType == ElementType::Diagram)
            qmt::MVoidConstVisitor::visitMDiagram(diagram);
        else
            m_keep = false;
    }

    void visitMItem(const qmt::MItem *item) override
    {
        if (m_elementType == ElementType::Any || m_elementType == ElementType::Item)
            qmt::MVoidConstVisitor::visitMItem(item);
        else
            m_keep = false;
    }

    void visitMRelation(const qmt::MRelation *relation) override
    {
        if (!m_relationStereotypes.isEmpty()) {
            const QStringList relationStereotypes = relation->stereotypes();
            bool containsRelationStereotype = std::any_of(
                relationStereotypes.constBegin(), relationStereotypes.constEnd(),
                [&](const QString &s) { return m_relationStereotypes.contains(s); });
            if (!containsRelationStereotype) {
                m_keep = false;
                return;
            }
        }
    }

    void visitMDependency(const qmt::MDependency *dependency) override
    {
        if (m_relationDirection != RelationDirection::Any) {
            bool keep = false;
            if (m_relationDirection == RelationDirection::Outgoing) {
                if (dependency->direction() == qmt::MDependency::AToB && dependency->endAUid() == m_mobject->uid())
                    keep = true;
                else if (dependency->direction() == qmt::MDependency::BToA && dependency->endBUid() == m_mobject->uid())
                    keep = true;
            } else if (m_relationDirection == RelationDirection::Incoming) {
                if (dependency->direction() == qmt::MDependency::AToB && dependency->endBUid() == m_mobject->uid())
                    keep = true;
                else if (dependency->direction() == qmt::MDependency::BToA && dependency->endAUid() == m_mobject->uid())
                    keep = true;
            } else if (m_relationDirection == RelationDirection::Bidirectional) {
                if (dependency->direction() == qmt::MDependency::Bidirectional)
                    keep = true;
            }
            m_keep = keep;
            if (!keep)
                return;
        }
        if (m_relationType == RelationType::Any || m_relationType == RelationType::Dependency)
            qmt::MVoidConstVisitor::visitMDependency(dependency);
        else
            m_keep = false;
    }

    bool testDirection(const qmt::MRelation *relation)
    {
        if (m_relationDirection != RelationDirection::Any) {
            bool keep = false;
            if (m_relationDirection == RelationDirection::Outgoing) {
                if (relation->endAUid() == m_mobject->uid())
                    keep = true;
            } else if (m_relationDirection == RelationDirection::Incoming) {
                if (relation->endBUid() == m_mobject->uid())
                    keep = true;
            }
            m_keep = keep;
            if (!keep)
                return false;
        }
        return true;
    }

    void visitMInheritance(const qmt::MInheritance *inheritance) override
    {
        if (!testDirection(inheritance))
            return;
        if (m_relationType == RelationType::Any || m_relationType == RelationType::Inheritance)
            qmt::MVoidConstVisitor::visitMInheritance(inheritance);
        else
            m_keep = false;
    }

    void visitMAssociation(const qmt::MAssociation *association) override
    {
        if (!testDirection(association))
            return;
        if (m_relationType == RelationType::Any || m_relationType == RelationType::Association)
            qmt::MVoidConstVisitor::visitMAssociation(association);
        else
            m_keep = false;
    }

    void visitMConnection(const qmt::MConnection *connection) override
    {
        if (!testDirection(connection))
            return;
        if (m_relationType == RelationType::Any || m_relationType == RelationType::Connection)
            qmt::MVoidConstVisitor::visitMConnection(connection);
        else
            m_keep = false;
    }

private:
    RelationType m_relationType = RelationType::Any;
    QString m_relationTypeId;
    RelationDirection m_relationDirection = RelationDirection::Any;
    QStringList m_relationStereotypes;
    ElementType m_elementType = ElementType::Any;
    QStringList m_elementStereotypes;
    const qmt::DObject *m_dobject = nullptr;
    const qmt::MObject *m_mobject = nullptr;
    const qmt::MRelation *m_relation = nullptr;
    bool m_keep = true;
};
} // namespace

class AddRelatedElementsDialog::Private {
public:
    qmt::DiagramSceneController *m_diagramSceneController = nullptr;
    qmt::DSelection m_selection;
    qmt::Uid m_diagramUid;
    QStringListModel m_relationTypeModel;
    QStringListModel m_relationDirectionModel;
    QStringListModel m_relationStereotypesModel;
    QStringListModel m_elementTypeModel;
    QStringListModel m_elementStereotypesModel;
    Filter m_filter;
};

AddRelatedElementsDialog::AddRelatedElementsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddRelatedElementsDialog),
    d(new Private)
{
    ui->setupUi(this);
    connect(ui->RelationTypeCombobox, &QComboBox::currentIndexChanged, this, &AddRelatedElementsDialog::updateNumberOfElements);
    connect(ui->DirectionCombobox, &QComboBox::currentIndexChanged, this, &AddRelatedElementsDialog::updateNumberOfElements);
    connect(ui->StereotypesCombobox, &QComboBox::currentTextChanged, this, &AddRelatedElementsDialog::updateNumberOfElements);
    connect(ui->ElementTypeComboBox, &QComboBox::currentIndexChanged, this, &AddRelatedElementsDialog::updateNumberOfElements);
    connect(ui->ElementStereotypesCombobox, &QComboBox::currentTextChanged, this, &AddRelatedElementsDialog::updateNumberOfElements);
    connect(this, &QDialog::accepted, this, &AddRelatedElementsDialog::onAccepted);
}

AddRelatedElementsDialog::~AddRelatedElementsDialog()
{
    delete d;
    delete ui;
}

void AddRelatedElementsDialog::setDiagramSceneController(qmt::DiagramSceneController *diagramSceneController)
{
    d->m_diagramSceneController = diagramSceneController;
}

void AddRelatedElementsDialog::setElements(const qmt::DSelection &selection, qmt::MDiagram *diagram)
{
    d->m_selection = selection;
    d->m_diagramUid = diagram->uid();
    QStringList relationTypes = {"Any", "Dependency", "Association", "Inheritance"};
    d->m_relationTypeModel.setStringList(relationTypes);
    ui->RelationTypeCombobox->setModel(&d->m_relationTypeModel);
    QStringList relationDirections = {"Any", "Outgoing (->)", "Incoming (<-)", "Bidirectional (<->)"};
    d->m_relationDirectionModel.setStringList(relationDirections);
    ui->DirectionCombobox->setModel(&d->m_relationDirectionModel);
    QStringList relationStereotypes = { };
    d->m_relationStereotypesModel.setStringList(relationStereotypes);
    ui->StereotypesCombobox->setModel(&d->m_relationStereotypesModel);
    QStringList elementTypes = {"Any", "Package", "Component", "Class", "Diagram", "Item"};
    d->m_elementTypeModel.setStringList(elementTypes);
    ui->ElementTypeComboBox->setModel(&d->m_elementTypeModel);
    QStringList elementStereotypes = { };
    d->m_elementStereotypesModel.setStringList(elementStereotypes);
    ui->ElementStereotypesCombobox->setModel(&d->m_elementStereotypesModel);
    updateNumberOfElements();
}

void AddRelatedElementsDialog::onAccepted()
{
    qmt::MDiagram *diagram = d->m_diagramSceneController->modelController()->findElement<qmt::MDiagram>(d->m_diagramUid);
    if (diagram) {
        updateFilter();
        d->m_diagramSceneController->addRelatedElements(
            d->m_selection, diagram,
            [this](qmt::DObject *dobject, qmt::MObject *mobject, qmt::MRelation *relation) -> bool
            {
                return this->filter(dobject, mobject, relation);
            });
    }
}

void AddRelatedElementsDialog::updateFilter()
{
    d->m_filter.setRelationType((RelationType) ui->RelationTypeCombobox->currentIndex());
    d->m_filter.setRelationDirection((RelationDirection) ui->DirectionCombobox->currentIndex());
    d->m_filter.setRelationStereotypes(ui->StereotypesCombobox->currentText().split(',', Qt::SkipEmptyParts));
    d->m_filter.setElementType((ElementType) ui->ElementTypeComboBox->currentIndex());
    d->m_filter.setElementStereotypes(ui->ElementStereotypesCombobox->currentText().split(',', Qt::SkipEmptyParts));
}

bool AddRelatedElementsDialog::filter(qmt::DObject *dobject, qmt::MObject *mobject, qmt::MRelation *relation)
{
    d->m_filter.reset();
    d->m_filter.setObject(dobject, mobject);
    d->m_filter.setRelation(relation);
    relation->accept(&d->m_filter);
    if (!d->m_filter.keep())
        return false;
    qmt::MObject *targetObject = nullptr;
    if (relation->endAUid() != mobject->uid())
        targetObject = d->m_diagramSceneController->modelController()->findObject(relation->endAUid());
    else if (relation->endBUid() != mobject->uid())
        targetObject = d->m_diagramSceneController->modelController()->findObject(relation->endBUid());
    if (!targetObject)
        return false;
    targetObject->accept(&d->m_filter);
    return d->m_filter.keep();
}

void AddRelatedElementsDialog::updateNumberOfElements()
{
    qmt::MDiagram *diagram = d->m_diagramSceneController->modelController()->findElement<qmt::MDiagram>(d->m_diagramUid);
    if (diagram) {
        updateFilter();
        ui->NumberOfMatchingElementsValue->setText(QString::number(d->m_diagramSceneController->countRelatedElements(
            d->m_selection, diagram,
            [this](qmt::DObject *dobject, qmt::MObject *mobject, qmt::MRelation *relation) -> bool
            {
                return this->filter(dobject, mobject, relation);
            })));
    }
}

} // namespace qmt