aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/modeleditor/componentviewcontroller.cpp
blob: ef8e9a477d0629345385581c8eeb811509bf93f4 (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
// Copyright (C) 2016 Jochen Becher
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "componentviewcontroller.h"

#include "modelutilities.h"
#include "packageviewcontroller.h"
#include "pxnodeutilities.h"

#include "qmt/controller/namecontroller.h"
#include "qmt/model_controller/mchildrenvisitor.h"
#include "qmt/model_controller/modelcontroller.h"
#include "qmt/model/mcomponent.h"
#include "qmt/model/mdependency.h"
#include "qmt/model/mpackage.h"
#include "qmt/tasks/diagramscenecontroller.h"

#include <cppeditor/cppmodelmanager.h>
#include <cplusplus/CppDocument.h>

#include <projectexplorer/session.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/project.h>

#include <utils/qtcassert.h>

#include <QFileInfo>

// TODO implement removing include dependencies that are not longer used
// TODO refactor add/remove relations between ancestor packages into extra controller class

using namespace Utils;

namespace ModelEditor {
namespace Internal {

class FindComponentFromFilePath :
        public qmt::MChildrenVisitor
{
public:
    void setFilePath(const QString &filePath);
    qmt::MComponent *component() const { return m_bestComponent; }
    void visitMComponent(qmt::MComponent *component) final;

private:
    QString m_elementName;
    QStringList m_elementsPath;
    int m_maxPathLength = 0;
    qmt::MComponent *m_bestComponent = nullptr;
};

void FindComponentFromFilePath::setFilePath(const QString &filePath)
{
    m_elementName = qmt::NameController::convertFileNameToElementName(filePath);
    QFileInfo fileInfo(filePath);
    m_elementsPath = qmt::NameController::buildElementsPath(fileInfo.path(), false);
}

void FindComponentFromFilePath::visitMComponent(qmt::MComponent *component)
{
    if (component->name() == m_elementName) {
        QStringList elementPath;
        const qmt::MObject *ancestor = component->owner();
        while (ancestor) {
            elementPath.prepend(ancestor->name());
            ancestor = ancestor->owner();
        }
        int i = elementPath.size() - 1;
        int j = m_elementsPath.size() - 1;
        while (i >= 0 && j >= 0 && elementPath.at(i) == m_elementsPath.at(j)) {
            --i;
            --j;
        }
        int pathLength = elementPath.size() - 1 - i;
        if (pathLength > m_maxPathLength) {
            m_maxPathLength = pathLength;
            m_bestComponent = component;
        }
    }
    visitMObject(component);
}

class UpdateIncludeDependenciesVisitor :
        public qmt::MChildrenVisitor
{
    class Node
    {
    public:
        Node() = default;
        Node(const QString &filePath, const QStringList &elementPath)
            : m_filePath(filePath),
              m_elementPath(elementPath)
        {
        }

        QString m_filePath;
        QStringList m_elementPath;
    };

public:
    void setPackageViewController(PackageViewController *packageViewController);
    void setModelController(qmt::ModelController *modelController);
    void setModelUtilities(ModelUtilities *modelUtilities);
    void updateFilePaths();
    void visitMComponent(qmt::MComponent *component) final;

private:
    QStringList findFilePathOfComponent(const qmt::MComponent *component);
    void collectElementPaths(const ProjectExplorer::FolderNode *folderNode, QMultiHash<QString,
                             Node> *filePathsMap);
    qmt::MComponent *findComponentFromFilePath(const QString &filePath);

private:
    PackageViewController *m_packageViewController = nullptr;
    qmt::ModelController *m_modelController = nullptr;
    ModelUtilities *m_modelUtilities = nullptr;
    QMultiHash<QString, Node> m_filePaths;
    QHash<QString, qmt::MComponent *> m_filePathComponentsMap;
};

void UpdateIncludeDependenciesVisitor::setPackageViewController(PackageViewController *packageViewController)
{
    m_packageViewController = packageViewController;
}

void UpdateIncludeDependenciesVisitor::setModelController(qmt::ModelController *modelController)
{
    m_modelController = modelController;
}

void UpdateIncludeDependenciesVisitor::setModelUtilities(ModelUtilities *modelUtilities)
{
    m_modelUtilities = modelUtilities;
}

void UpdateIncludeDependenciesVisitor::updateFilePaths()
{
    m_filePaths.clear();
    for (const ProjectExplorer::Project *project : ProjectExplorer::SessionManager::projects()) {
        ProjectExplorer::ProjectNode *projectNode = project->rootProjectNode();
        if (projectNode)
            collectElementPaths(projectNode, &m_filePaths);
    }
}

void UpdateIncludeDependenciesVisitor::visitMComponent(qmt::MComponent *component)
{
    CppEditor::CppModelManager *cppModelManager = CppEditor::CppModelManager::instance();
    CPlusPlus::Snapshot snapshot = cppModelManager->snapshot();

    const QStringList filePaths = findFilePathOfComponent(component);
    for (const QString &filePath : filePaths) {
        CPlusPlus::Document::Ptr document = snapshot.document(FilePath::fromString(filePath));
        if (document) {
            const QList<CPlusPlus::Document::Include> includes = document->resolvedIncludes();
            for (const CPlusPlus::Document::Include &include : includes) {
                Utils::FilePath includeFilePath = include.resolvedFileName();
                // replace proxy header with real one
                CPlusPlus::Document::Ptr includeDocument = snapshot.document(includeFilePath);
                if (includeDocument) {
                    QList<CPlusPlus::Document::Include> includes = includeDocument->resolvedIncludes();
                    if (includes.count() == 1 &&
                            includes.at(0).resolvedFileName().fileName() == includeFilePath.fileName())
                    {
                        includeFilePath = includes.at(0).resolvedFileName();
                    }
                }
                qmt::MComponent *includeComponent = findComponentFromFilePath(includeFilePath.toString());
                if (includeComponent && includeComponent != component) {
                    // add dependency between components
                    if (!m_modelUtilities->haveDependency(component, includeComponent)) {
                        auto dependency = new qmt::MDependency;
                        dependency->setFlags(qmt::MElement::ReverseEngineered);
                        dependency->setStereotypes({"include"});
                        dependency->setDirection(qmt::MDependency::AToB);
                        dependency->setSource(component->uid());
                        dependency->setTarget(includeComponent->uid());
                        m_modelController->addRelation(component, dependency);
                    }
                    m_packageViewController->createAncestorDependencies(component, includeComponent);
                }
            }
        }
    }
    visitMObject(component);
}

QStringList UpdateIncludeDependenciesVisitor::findFilePathOfComponent(const qmt::MComponent *component)
{
    QStringList elementPath;
    const qmt::MObject *ancestor = component->owner();
    while (ancestor) {
        elementPath.prepend(ancestor->name());
        ancestor = ancestor->owner();
    }
    QStringList bestFilePaths;
    int maxPathLength = 1;
    const QList<Node> nodes = m_filePaths.values(component->name());
    for (const Node &node : nodes) {
        int i = elementPath.size() - 1;
        int j = node.m_elementPath.size() - 1;
        while (i >= 0 && j >= 0 && elementPath.at(i) == node.m_elementPath.at(j)) {
            --i;
            --j;
        }
        int pathLength = elementPath.size() - 1 - i;
        if (pathLength > maxPathLength)
            bestFilePaths.clear();
        if (pathLength >= maxPathLength) {
            maxPathLength = pathLength;
            bestFilePaths.append(node.m_filePath);
        }
    }
    return bestFilePaths;
}

void UpdateIncludeDependenciesVisitor::collectElementPaths(const ProjectExplorer::FolderNode *folderNode,
                                                           QMultiHash<QString, Node> *filePathsMap)
{
    const QList<ProjectExplorer::FileNode *> fileNodes = folderNode->fileNodes();
    for (const ProjectExplorer::FileNode *fileNode : fileNodes) {
        QString elementName = qmt::NameController::convertFileNameToElementName(fileNode->filePath().toString());
        QFileInfo fileInfo = fileNode->filePath().toFileInfo();
        QString nodePath = fileInfo.path();
        QStringList elementsPath = qmt::NameController::buildElementsPath(nodePath, false);
        filePathsMap->insert(elementName, Node(fileNode->filePath().toString(), elementsPath));
    }
    const QList<ProjectExplorer::FolderNode *> subNodes = folderNode->folderNodes();
    for (const ProjectExplorer::FolderNode *subNode : subNodes)
        collectElementPaths(subNode, filePathsMap);
}

qmt::MComponent *UpdateIncludeDependenciesVisitor::findComponentFromFilePath(const QString &filePath)
{
    const auto it = m_filePathComponentsMap.constFind(filePath);
    if (it != m_filePathComponentsMap.cend())
        return it.value();

    FindComponentFromFilePath visitor;
    visitor.setFilePath(filePath);
    m_modelController->rootPackage()->accept(&visitor);
    qmt::MComponent *component = visitor.component();
    m_filePathComponentsMap.insert(filePath, component);
    return component;
}

class ComponentViewController::ComponentViewControllerPrivate {
public:
    ModelUtilities *modelUtilities = nullptr;
    PackageViewController *packageViewController = nullptr;
    PxNodeUtilities *pxnodeUtilities = nullptr;
    qmt::DiagramSceneController *diagramSceneController = nullptr;
};

ComponentViewController::ComponentViewController(QObject *parent)
    : QObject(parent),
      d(new ComponentViewControllerPrivate)
{
}

ComponentViewController::~ComponentViewController()
{
    delete d;
}

void ComponentViewController::setModelUtilities(ModelUtilities *modelUtilities)
{
    d->modelUtilities = modelUtilities;
}

void ComponentViewController::setPackageViewController(PackageViewController *packageViewController)
{
    d->packageViewController = packageViewController;
}

void ComponentViewController::setPxNodeUtilties(PxNodeUtilities *pxnodeUtilities)
{
    d->pxnodeUtilities = pxnodeUtilities;
}

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

void ComponentViewController::createComponentModel(const QString &filePath,
                                                   qmt::MDiagram *diagram,
                                                   const QString &anchorFolder)
{
    d->diagramSceneController->modelController()->startResetModel();
    doCreateComponentModel(filePath, diagram, anchorFolder, false);
    doCreateComponentModel(filePath, diagram, anchorFolder, true);
    d->diagramSceneController->modelController()->finishResetModel(true);
}

void ComponentViewController::updateIncludeDependencies(qmt::MPackage *rootPackage)
{
    d->diagramSceneController->modelController()->startResetModel();
    UpdateIncludeDependenciesVisitor visitor;
    visitor.setPackageViewController(d->packageViewController);
    visitor.setModelController(d->diagramSceneController->modelController());
    visitor.setModelUtilities(d->modelUtilities);
    visitor.updateFilePaths();
    rootPackage->accept(&visitor);
    d->diagramSceneController->modelController()->finishResetModel(true);
}

void ComponentViewController::doCreateComponentModel(const QString &filePath, qmt::MDiagram *diagram,
                                                     const QString &anchorFolder, bool scanHeaders)
{
    for (const QString &fileName : QDir(filePath).entryList(QDir::Files)) {
        QString file = filePath + "/" + fileName;
        QString componentName = qmt::NameController::convertFileNameToElementName(file);
        qmt::MComponent *component = nullptr;
        bool isSource = false;
        CppEditor::ProjectFile::Kind kind = CppEditor::ProjectFile::classify(file);
        switch (kind) {
        case CppEditor::ProjectFile::CSource:
        case CppEditor::ProjectFile::CXXSource:
        case CppEditor::ProjectFile::ObjCSource:
        case CppEditor::ProjectFile::ObjCXXSource:
        case CppEditor::ProjectFile::CudaSource:
        case CppEditor::ProjectFile::OpenCLSource:
            isSource = !scanHeaders;
            break;
        case CppEditor::ProjectFile::AmbiguousHeader:
        case CppEditor::ProjectFile::CHeader:
        case CppEditor::ProjectFile::CXXHeader:
        case CppEditor::ProjectFile::ObjCHeader:
        case CppEditor::ProjectFile::ObjCXXHeader:
            isSource = scanHeaders && !d->pxnodeUtilities->isProxyHeader(file);
            break;
        case CppEditor::ProjectFile::Unclassified:
        case CppEditor::ProjectFile::Unsupported:
            isSource = false;
            break;
        }
        if (isSource) {
            component = new qmt::MComponent;
            component->setFlags(qmt::MElement::ReverseEngineered);
            component->setName(componentName);
        }
        if (component) {
            QStringList relativeElements = qmt::NameController::buildElementsPath(
                        d->pxnodeUtilities->calcRelativePath(file, anchorFolder), false);
            if (d->pxnodeUtilities->findSameObject(relativeElements, component)) {
                delete component;
            } else {
                qmt::MPackage *requestedRootPackage = d->diagramSceneController->findSuitableParentPackage(nullptr, diagram);
                qmt::MPackage *bestParentPackage = d->pxnodeUtilities->createBestMatchingPackagePath(requestedRootPackage, relativeElements);
                d->diagramSceneController->modelController()->addObject(bestParentPackage, component);
            }
        }
    }
    for (const QString &fileName : QDir(filePath).entryList(QDir::Dirs|QDir::NoDotAndDotDot)) {
        QString file = filePath + "/" + fileName;
        doCreateComponentModel(file, diagram, anchorFolder, scanHeaders);
    }
}

} // namespace Internal
} // namespace ModelEditor