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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/***************************************************************************
**
** Copyright (C) 2015 Jochen Becher
** Contact: http://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 http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "componentviewcontroller.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 <cpptools/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 this class is experimental and not finished. Code needs fixes and to be cleaned up!
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);
private:
QString m_elementName;
QStringList m_elementsPath;
int m_maxPathLength = 0;
qmt::MComponent *m_bestComponent = 0;
};
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->getName() == m_elementName) {
QStringList elementPath;
const qmt::MObject *ancestor = component->getOwner();
while (ancestor) {
elementPath.prepend(ancestor->getName());
ancestor = ancestor->getOwner();
}
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 setModelController(qmt::ModelController *modelController);
void visitMComponent(qmt::MComponent *component);
private:
bool haveMatchingStereotypes(const qmt::MObject *object1, const qmt::MObject *object2);
QStringList findFilePathOfComponent(const qmt::MComponent *component);
void collectElementPaths(const ProjectExplorer::FolderNode *folderNode, QMultiHash<QString,
Node> *filePathsMap);
qmt::MComponent *findComponentFromFilePath(const QString &filePath);
bool haveDependency(const qmt::MObject *source, const qmt::MObject *target,
bool inverted = false);
bool haveDependency(const qmt::MObject *source, const QList<qmt::MPackage *> &targets);
private:
qmt::ModelController *m_modelController = 0;
};
void UpdateIncludeDependenciesVisitor::setModelController(qmt::ModelController *modelController)
{
m_modelController = modelController;
}
void UpdateIncludeDependenciesVisitor::visitMComponent(qmt::MComponent *component)
{
CppTools::CppModelManager *cppModelManager = CppTools::CppModelManager::instance();
CPlusPlus::Snapshot snapshot = cppModelManager->snapshot();
QStringList filePaths = findFilePathOfComponent(component);
foreach (const QString &filePath, filePaths) {
CPlusPlus::Document::Ptr document = snapshot.document(filePath);
if (document) {
foreach (const CPlusPlus::Document::Include &include, document->resolvedIncludes()) {
QString includeFilePath = include.resolvedFileName();
qmt::MComponent *includeComponent = findComponentFromFilePath(includeFilePath);
if (includeComponent && includeComponent != component) {
// add dependency between components
if (!haveDependency(component, includeComponent)) {
auto dependency = new qmt::MDependency;
dependency->setFlags(qmt::MElement::REVERSE_ENGINEERED);
dependency->setStereotypes(QStringList() << QStringLiteral("include"));
dependency->setDirection(qmt::MDependency::A_TO_B);
dependency->setSource(component->getUid());
dependency->setTarget(includeComponent->getUid());
m_modelController->addRelation(component, dependency);
}
// search ancestors sharing a common owner
QList<qmt::MPackage *> componentAncestors;
auto ancestor = dynamic_cast<qmt::MPackage *>(component->getOwner());
while (ancestor) {
componentAncestors.append(ancestor);
ancestor = dynamic_cast<qmt::MPackage *>(ancestor->getOwner());
}
QList<qmt::MPackage *> includeComponentAncestors;
ancestor = dynamic_cast<qmt::MPackage *>(includeComponent->getOwner());
while (ancestor) {
includeComponentAncestors.append(ancestor);
ancestor = dynamic_cast<qmt::MPackage *>(ancestor->getOwner());
}
int componentHighestAncestorIndex = componentAncestors.size() - 1;
int includeComponentHighestAncestorIndex = includeComponentAncestors.size() - 1;
QTC_ASSERT(componentAncestors.at(componentHighestAncestorIndex) == includeComponentAncestors.at(includeComponentHighestAncestorIndex), return);
while (componentHighestAncestorIndex > 0 && includeComponentHighestAncestorIndex > 0) {
if (componentAncestors.at(componentHighestAncestorIndex) != includeComponentAncestors.at(includeComponentHighestAncestorIndex))
break;
--componentHighestAncestorIndex;
--includeComponentHighestAncestorIndex;
}
// add dependency between parent packages with same stereotype
int index1 = 0;
int includeComponentLowestIndex = 0;
while (index1 <= componentHighestAncestorIndex
&& includeComponentLowestIndex <= includeComponentHighestAncestorIndex) {
if (!componentAncestors.at(index1)->getStereotypes().isEmpty()) {
int index2 = includeComponentLowestIndex;
while (index2 <= includeComponentHighestAncestorIndex) {
if (haveMatchingStereotypes(componentAncestors.at(index1), includeComponentAncestors.at(index2))) {
if (!haveDependency(componentAncestors.at(index1), includeComponentAncestors.at(index2))) {
auto dependency = new qmt::MDependency;
dependency->setFlags(qmt::MElement::REVERSE_ENGINEERED);
dependency->setStereotypes(QStringList() << QStringLiteral("same stereotype"));
dependency->setDirection(qmt::MDependency::A_TO_B);
dependency->setSource(componentAncestors.at(index1)->getUid());
dependency->setTarget(includeComponentAncestors.at(index2)->getUid());
m_modelController->addRelation(componentAncestors.at(index1), dependency);
}
includeComponentLowestIndex = index2 + 1;
break;
}
++index2;
}
}
++index1;
}
// add dependency between topmost packages with common owner
if (componentAncestors.at(componentHighestAncestorIndex) != includeComponentAncestors.at(includeComponentHighestAncestorIndex)) {
if (!haveDependency(componentAncestors.at(componentHighestAncestorIndex), includeComponentAncestors)) {
auto dependency = new qmt::MDependency;
dependency->setFlags(qmt::MElement::REVERSE_ENGINEERED);
dependency->setStereotypes(QStringList() << QStringLiteral("ancestor"));
dependency->setDirection(qmt::MDependency::A_TO_B);
dependency->setSource(componentAncestors.at(componentHighestAncestorIndex)->getUid());
dependency->setTarget(includeComponentAncestors.at(includeComponentHighestAncestorIndex)->getUid());
m_modelController->addRelation(componentAncestors.at(componentHighestAncestorIndex), dependency);
}
}
// add dependency between parent packages
if (componentHighestAncestorIndex > 0 && includeComponentHighestAncestorIndex > 0) { // check for valid parents
if (componentAncestors.at(0) != includeComponentAncestors.at(0)) {
if (!haveDependency(componentAncestors.at(0), includeComponentAncestors)) {
auto dependency = new qmt::MDependency;
dependency->setFlags(qmt::MElement::REVERSE_ENGINEERED);
dependency->setStereotypes(QStringList() << QStringLiteral("parents"));
dependency->setDirection(qmt::MDependency::A_TO_B);
dependency->setSource(componentAncestors.at(0)->getUid());
dependency->setTarget(includeComponentAncestors.at(0)->getUid());
m_modelController->addRelation(componentAncestors.at(0), dependency);
}
}
}
}
}
}
}
visitMObject(component);
}
bool UpdateIncludeDependenciesVisitor::haveMatchingStereotypes(const qmt::MObject *object1,
const qmt::MObject *object2)
{
return !(object1->getStereotypes().toSet() & object2->getStereotypes().toSet()).isEmpty();
}
QStringList UpdateIncludeDependenciesVisitor::findFilePathOfComponent(const qmt::MComponent *component)
{
QMultiHash<QString, Node> filePaths;
foreach (const ProjectExplorer::Project *project, ProjectExplorer::SessionManager::projects()) {
ProjectExplorer::ProjectNode *projectNode = project->rootProjectNode();
if (projectNode)
collectElementPaths(projectNode, &filePaths);
}
QStringList elementPath;
const qmt::MObject *ancestor = component->getOwner();
while (ancestor) {
elementPath.prepend(ancestor->getName());
ancestor = ancestor->getOwner();
}
QStringList bestFilePaths;
int maxPathLength = 1;
foreach (const Node &node, filePaths.values(component->getName())) {
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)
{
foreach (const ProjectExplorer::FileNode *fileNode, folderNode->fileNodes()) {
QString elementName = qmt::NameController::convertFileNameToElementName(fileNode->filePath().toString());
QFileInfo fileInfo(fileNode->filePath().toString());
QString nodePath = fileInfo.path();
QStringList elementsPath = qmt::NameController::buildElementsPath(nodePath, false);
filePathsMap->insertMulti(elementName, Node(fileNode->filePath().toString(), elementsPath));
}
foreach (const ProjectExplorer::FolderNode *subNode, folderNode->subFolderNodes())
collectElementPaths(subNode, filePathsMap);
}
qmt::MComponent *UpdateIncludeDependenciesVisitor::findComponentFromFilePath(const QString &filePath)
{
FindComponentFromFilePath visitor;
visitor.setFilePath(filePath);
m_modelController->getRootPackage()->accept(&visitor);
return visitor.component();
}
bool UpdateIncludeDependenciesVisitor::haveDependency(const qmt::MObject *source,
const qmt::MObject *target, bool inverted)
{
qmt::MDependency::Direction aToB = qmt::MDependency::A_TO_B;
qmt::MDependency::Direction bToA = qmt::MDependency::B_TO_A;
if (inverted) {
aToB = qmt::MDependency::B_TO_A;
bToA = qmt::MDependency::A_TO_B;
}
foreach (const qmt::Handle<qmt::MRelation> &handle, source->getRelations()) {
if (auto dependency = dynamic_cast<qmt::MDependency *>(handle.getTarget())) {
if (dependency->getSource() == source->getUid()
&& dependency->getTarget() == target->getUid()
&& (dependency->getDirection() == aToB
|| dependency->getDirection() == qmt::MDependency::BIDIRECTIONAL)) {
return true;
}
if (dependency->getSource() == target->getUid()
&& dependency->getTarget() == source->getUid()
&& (dependency->getDirection() == bToA
|| dependency->getDirection() == qmt::MDependency::BIDIRECTIONAL)) {
return true;
}
}
}
if (!inverted)
return haveDependency(target, source, true);
return false;
}
bool UpdateIncludeDependenciesVisitor::haveDependency(const qmt::MObject *source,
const QList<qmt::MPackage *> &targets)
{
foreach (const qmt::MObject *target, targets) {
if (haveDependency(source, target))
return true;
}
return false;
}
class ComponentViewController::ComponentViewControllerPrivate {
public:
PxNodeUtilities *pxnodeUtilities = 0;
qmt::DiagramSceneController *diagramSceneController = 0;
};
ComponentViewController::ComponentViewController(QObject *parent)
: QObject(parent),
d(new ComponentViewControllerPrivate)
{
}
ComponentViewController::~ComponentViewController()
{
delete d;
}
void ComponentViewController::setPxNodeUtilties(PxNodeUtilities *pxnodeUtilities)
{
d->pxnodeUtilities = pxnodeUtilities;
}
void ComponentViewController::setDiagramSceneController(qmt::DiagramSceneController *diagramSceneController)
{
d->diagramSceneController = diagramSceneController;
}
void ComponentViewController::createComponentModel(const ProjectExplorer::FolderNode *folderNode,
qmt::MDiagram *diagram,
const QString anchorFolder)
{
foreach (const ProjectExplorer::FileNode *fileNode, folderNode->fileNodes()) {
QString componentName = qmt::NameController::convertFileNameToElementName(fileNode->filePath().toString());
qmt::MComponent *component = 0;
bool isSource = false;
CppTools::ProjectFile::Kind kind = CppTools::ProjectFile::classify(fileNode->filePath().toString());
switch (kind) {
case CppTools::ProjectFile::CHeader:
case CppTools::ProjectFile::CSource:
case CppTools::ProjectFile::CXXHeader:
case CppTools::ProjectFile::CXXSource:
case CppTools::ProjectFile::ObjCSource:
case CppTools::ProjectFile::ObjCHeader:
case CppTools::ProjectFile::ObjCXXSource:
case CppTools::ProjectFile::ObjCXXHeader:
case CppTools::ProjectFile::CudaSource:
case CppTools::ProjectFile::OpenCLSource:
isSource = true;
break;
case CppTools::ProjectFile::Unclassified:
isSource = false;
break;
}
if (isSource) {
component = new qmt::MComponent;
component->setFlags(qmt::MElement::REVERSE_ENGINEERED);
component->setName(componentName);
}
if (component) {
QStringList relativeElements = qmt::NameController::buildElementsPath(
d->pxnodeUtilities->calcRelativePath(fileNode, anchorFolder), false);
if (d->pxnodeUtilities->findSameObject(relativeElements, component)) {
delete component;
} else {
qmt::MPackage *requestedRootPackage = d->diagramSceneController->findSuitableParentPackage(0, diagram);
qmt::MPackage *bestParentPackage = d->pxnodeUtilities->createBestMatchingPackagePath(requestedRootPackage, relativeElements);
d->diagramSceneController->getModelController()->addObject(bestParentPackage, component);
}
}
}
foreach (const ProjectExplorer::FolderNode *subNode, folderNode->subFolderNodes())
createComponentModel(subNode, diagram, anchorFolder);
}
void ComponentViewController::updateIncludeDependencies(qmt::MPackage *rootPackage)
{
UpdateIncludeDependenciesVisitor visitor;
visitor.setModelController(d->diagramSceneController->getModelController());
rootPackage->accept(&visitor);
}
} // namespace Internal
} // namespace ModelEditor
|