1
/***************************************************************************
2
* This program is free software; you can redistribute it and/or modify *
3
* it under the terms of the GNU General Public License as published by *
4
* the Free Software Foundation; either version 2 of the License, or *
5
* (at your option) any later version. *
7
* copyright (C) 2012-2014 *
8
* Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
9
***************************************************************************/
11
#include "layoutgenerator.h"
13
#include "associationline.h"
14
#include "associationwidget.h"
15
#include "debug_utils.h"
16
#include "floatingtextwidget.h"
17
#include "umlwidget.h"
20
#include <KConfigGroup>
21
#include <KDesktopFile>
22
#include <KStandardDirs>
31
#include <QTemporaryFile>
32
//#include <QTextStream>
34
#define LAYOUTGENERATOR_DEBUG
35
//#define LAYOUTGENERATOR_DATA_DEBUG
37
#ifdef LAYOUTGENERATOR_DEBUG
39
static QString pngViewer()
52
static QString textViewer()
69
LayoutGenerator::LayoutGenerator()
71
setUseFullNodeLabels(false);
75
* Return state if layout generator is enabled.
76
* It is enabled when the dot application has been found.
78
* @return true if enabled
80
bool LayoutGenerator::isEnabled()
82
Settings::OptionState& optionState = Settings::optionState();
83
if (optionState.autoLayoutState.autoDotPath) {
84
m_dotPath = currentDotPath();
86
else if (!optionState.autoLayoutState.dotPath.isEmpty()) {
87
m_dotPath = optionState.autoLayoutState.dotPath;
89
return !m_dotPath.isEmpty();
93
* Return the path where dot is installed.
95
* @return string with dot path
97
QString LayoutGenerator::currentDotPath()
99
QString executable = KStandardDirs::findExe("dot");
100
if (!executable.isEmpty()) {
101
QFileInfo fi(executable);
102
return fi.absolutePath();
105
// search for dot installation
106
QString appDir(qgetenv("ProgramFiles"));
108
dir.setFilter(QDir::Dirs);
109
dir.setNameFilters(QStringList() << "Graphviz*");
110
dir.setSorting(QDir::Reversed);
111
QFileInfoList list = dir.entryInfoList();
112
if (list.size() > 0) {
113
QString dotPath = list.at(0).absoluteFilePath();
114
QString exePath = QFile::exists(dotPath + "\\bin") ? dotPath + "\\bin" : dotPath;
115
return QFile::exists(exePath + "\\dot.exe") ? exePath : "";
122
* generate layout and apply it to the given diagram.
124
* @return true if generating succeeded
126
bool LayoutGenerator::generate(UMLScene *scene, const QString &variant)
130
QTemporaryFile xdotOut;
132
uWarning() << "Could not apply autolayout because graphviz installation has not been found.";
136
#ifdef LAYOUTGENERATOR_DEBUG
137
in.setAutoRemove(false);
138
out.setAutoRemove(false);
139
xdotOut.setAutoRemove(false);
142
// generate filenames
150
#ifdef LAYOUTGENERATOR_DEBUG
151
qDebug() << textViewer() << in.fileName();
152
qDebug() << textViewer() << out.fileName();
153
qDebug() << textViewer() << xdotOut.fileName();
156
if (!createDotFile(scene, in.fileName(), variant))
159
QString executable = m_dotPath + "/" + m_generator;
163
args << "-o" << out.fileName() << "-Tplain-ext" << in.fileName();
164
p.start(executable, args);
168
args << "-o" << xdotOut.fileName() << "-Txdot" << in.fileName();
169
p.start(executable, args);
172
#ifdef LAYOUTGENERATOR_DEBUG
173
QTemporaryFile pngFile;
174
pngFile.setAutoRemove(false);
175
pngFile.setFileTemplate(QDir::tempPath() + "/umbrello-layoutgenerator-XXXXXX.png");
178
qDebug() << pngViewer() << pngFile.fileName();
180
args << "-o" << pngFile.fileName() << "-Tpng" << in.fileName();
181
p.start(executable, args);
185
if (!readGeneratedDotFile(out.fileName()))
187
if (!readGeneratedDotFile(xdotOut.fileName()))
195
* apply auto layout to the given scene
197
* @return true if autolayout has been applied
199
bool LayoutGenerator::apply(UMLScene *scene)
201
foreach(AssociationWidget *assoc, scene->associationList()) {
202
AssociationLine *path = assoc->associationLine();
203
QString type = Uml::AssociationType::toString(assoc->associationType()).toLower();
204
QString key = "type::" + type;
207
if (m_edgeParameters.contains("id::" + key) && m_edgeParameters["id::" + key] == "swap")
208
id = fixID(Uml::ID::toString(assoc->widgetLocalIDForRole(Uml::RoleType::A)) + Uml::ID::toString(assoc->widgetLocalIDForRole(Uml::RoleType::B)));
210
id = fixID(Uml::ID::toString(assoc->widgetLocalIDForRole(Uml::RoleType::B)) + Uml::ID::toString(assoc->widgetLocalIDForRole(Uml::RoleType::A)));
212
// adjust associations not used in the dot file
213
if (!m_edges.contains(id)) {
215
AssociationLine *path = assoc->associationLine();
216
if (path->count() > 2 && assoc->widgetLocalIDForRole(Uml::RoleType::A) != assoc->widgetLocalIDForRole(Uml::RoleType::B)) {
217
while(path->count() > 2)
218
path->removePoint(1);
223
EdgePoints &p = m_edges[id];
226
while(path->count() > 1) {
227
path->removePoint(0);
229
path->setEndPoints(mapToScene(p[0]), mapToScene(p[len-1]));
231
// set label position
232
QPointF &l = m_edgeLabelPosition[id];
233
FloatingTextWidget *tw = assoc->nameWidget();
235
tw->setPos(mapToScene(l));
237
// FIXME: set remaining association line points
239
for(int i = 1; i < len-1; i++) {
240
path->insertPoint(i, mapToScene((p[i]));
244
* here stuff could be added to add more points from information returned by dot.
248
foreach(UMLWidget *widget, scene->widgetList()) {
249
QString id = Uml::ID::toString(widget->localID());
250
if (!m_nodes.contains(id))
252
QPoint p = origin(id);
254
widget->setY(p.y()-widget->height());
255
widget->adjustAssocs(widget->x(), widget->y()); // adjust assoc lines
258
foreach(AssociationWidget *assoc, scene->associationList()) {
259
assoc->calculateEndingPoints();
260
if (assoc->associationLine())
261
assoc->associationLine()->update();
262
assoc->resetTextPositions();
268
* Return a list of available templates for a given scene type
270
* @param scene The diagram
271
* @param configFiles will contain the collected list of config files
272
* @return true if collecting succeeds
274
bool LayoutGenerator::availableConfigFiles(UMLScene *scene, QHash<QString,QString> &configFiles)
276
QString diagramType = Uml::DiagramType::toString(scene->type()).toLower();
279
QStringList fileNames = dirs.findAllResources("data", QString("umbrello/layouts/%1*.desktop").arg(diagramType));
280
foreach(const QString &fileName, fileNames) {
281
QFileInfo fi(fileName);
283
if (fi.baseName().contains("-"))
284
baseName = fi.baseName().remove(diagramType + "-");
285
else if (fi.baseName() == diagramType)
286
baseName = fi.baseName();
288
baseName = "default";
289
KDesktopFile desktopFile(fileName);
290
configFiles[baseName] = desktopFile.readName();
296
* Return the origin of node based on the bottom/left corner
298
* @param id The widget id to fetch the origin from
299
* @return QPoint instance with the coordinates
301
QPoint LayoutGenerator::origin(const QString &id)
303
QString key = fixID(id);
304
if (!m_nodes.contains(key)) {
305
#ifdef LAYOUTGENERATOR_DATA_DEBUG
310
QRectF &r = m_nodes[key];
311
QPoint p(m_origin.x() + r.x() - r.width()/2, m_boundingRect.height() - r.y() + r.height()/2 + m_origin.y());
312
#ifdef LAYOUTGENERATOR_DATA_DEBUG
319
* Read generated dot file and extract positions
320
* of the contained widgets.
322
* @return true if extracting succeeded
324
bool LayoutGenerator::readGeneratedDotFile(const QString &fileName)
326
QFile file(fileName);
327
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
330
QTextStream in(&file);
331
while (!in.atEnd()) {
332
QString line = in.readLine();
333
while(line.endsWith(','))
334
line += in.readLine();
342
* Parse line from dot generated plain-ext output format
344
* The format is documented at http://graphviz.org/content/output-formats#dplain-ext and looks like:
346
* graph 1 28.083 10.222
347
* node ITfDmJvJE00m 8.0833 8.7361 0.86111 0.45833 QObject solid box black lightgrey
348
* edge sL4cKPpHnJkU sL4cKPpHnJkU 7 8.1253 7.2568 8.2695 7.2687 8.375 7.3127 8.375 7.3889 8.375 7.4377 8.3317 7.4733 8.2627 7.4957 Aggregation 8.8472 7.3889 solid black
350
* @param line line in dot plain-ext output format
351
* @return true if line could be parsed successfully
353
bool LayoutGenerator::parseLine(const QString &line)
355
QStringList a = line.split(' ');
356
if (a[0] == "graph") {
357
m_boundingRect = QRectF(0, 0, a[2].toDouble()*m_scale, a[3].toDouble()*m_scale);
359
} else if (a[0] == "node") {
360
QString key = fixID(a[1]);
361
m_nodes[key] = QRectF(a[2].toDouble()*m_scale, a[3].toDouble()*m_scale, a[4].toDouble()*m_scale, a[5].toDouble()*m_scale);
363
} else if (a[0] == "edge") {
364
QString key = fixID(a[1]+a[2]);
366
int len = a[3].toInt();
367
for(int i = 0; i < len; i++)
368
p.append(QPointF(a[i*2+4].toDouble()*m_scale, a[i*2+5].toDouble()*m_scale));
373
double x = a[b+1].toDouble(&ok);
376
double y = a[b+2].toDouble(&ok);
379
m_edgeLabelPosition[key] = QPointF(x*m_scale, y*m_scale);
382
} else if (a[0] == "stop") {
389
typedef QMap<QString,QStringList> ParameterList;
391
bool LayoutGenerator::splitParameters(QMap<QString,QStringList> &map, const QString &s)
393
// FIXME: add shape=box without '"'
394
static QRegExp rx("([a-zA-Z_]+)=\"([a-zA-Z0-9.- #]+)\"");
395
static QRegExp rx2("([a-zA-Z_]+)=([a-zA-Z0-9.- #]+)");
399
* while ((pos = rx2.indexIn(s, pos)) != -1) {
400
* QString key = rx2.cap(1);
401
* QString value = rx2.cap(2);
403
* pos += rx2.matchedLength();
404
* //qDebug() << key << value;
405
* if (map.contains(key))
408
* map[key] = QStringList() << value;
412
while ((pos = rx.indexIn(s, pos)) != -1) {
413
QString key = rx.cap(1);
414
QString value = rx.cap(2);
416
pos += rx.matchedLength();
417
//qDebug() << key << value;
422
data = value.split(' ');
423
} else if (key.startsWith('_')) {
424
data = value.split(' ');
426
else if (key == "label")
427
data = QStringList() << value;
429
data = value.split(',');
431
if (map.contains(key))
442
graph [splines=polyline, rankdir=BT, outputorder=nodesfirst, ranksep="0.5", nodesep="0.5"];
444
graph [bb="0,0,2893,638",
445
_draw_="c 9 -#ffffffff C 9 -#ffffffff P 4 0 -1 0 638 2894 638 2894 -1 ",
447
XC0weWhArzOJ [label=note, shape=box, width="2.5833", height="0.86111", pos="93,31", _draw_="c 9 -#000000ff p 4 186 62 0 62 0 0 186 0 ", _ldraw_="F 14.000000 11 -Times-Roman c 9 -#000000ff T 93 27 0 24 4 -note "];
448
sL4cKPpHnJkU -> ITfDmJvJE00m [arrowhead=normal, weight="1.0", label=" ", pos="e,2326.3,600.47 2299.7,543.57 2306.1,557.22 2314.9,575.99 2322.1,591.39", lp="2319,572", _draw_="c 9 -#000000ff B 4 2300 544 2306 557 2315 576 2322 591 ", _hdraw_="S 5 -solid c 9 -#000000ff C 9 -#000000ff P 3 2319 593 2326 600 2325 590 ", _ldraw_="F 14.000000 11 -Times-Roman c 9 -#000000ff T 2319 568 0 4 1 - "];
449
sL4cKPpHnJkU -> sL4cKPpHnJkU [label=" ", arrowtail=odiamond, dir=back, constraint=false, pos="s,2339.3,516.43 2351.5,516.59 2365.1,517.35 2375,520.16 2375,525 2375,531.2 2358.7,534.06 2339.3,533.57", lp="2377,525", _draw_="c 9 -#000000ff B 7 2351 517 2365 517 2375 520 2375 525 2375 531 2359 534 2339 534 ", _tdraw_="S 5 -solid c 9 -#000000ff p 4 2351 517 2345 521 2339 516 2345 513 ", _ldraw_="F 14.000000 11 -Times-Roman c 9 -#000000ff T 2377 521 0 4 1 - "];
452
bool LayoutGenerator::parseLine(const QString &line)
454
static QRegExp m_cols("^[\t ]*(.*)[\t ]*\\[(.*)\\]");
455
static int m_level = -1;
457
if (line.contains('{')) {
461
else if (line.contains('}')) {
466
if (m_cols.indexIn(line, pos) == -1)
469
QString keyword = m_cols.cap(1).trimmed();
470
QString attributeString = m_cols.cap(2);
471
uDebug() << keyword << attributeString;
472
ParameterList attributes;
473
splitParameters(attributes, attributeString);
474
uDebug() << attributes;
476
if (keyword == "graph") {
477
if (attributes.contains("bb")) {
478
QStringList &a = attributes["bb"];
479
m_boundingRect.setLeft(a[0].toDouble());
480
m_boundingRect.setTop(a[1].toDouble());
481
m_boundingRect.setRight(a[2].toDouble());
482
m_boundingRect.setBottom(a[3].toDouble());
484
} else if (keyword == "node") {
486
} else if (keyword == "edge") {
489
} else if (line.contains("->")) {
490
QStringList k = keyword.split(" ");
493
QString key = fixID(k[0]+k[2]);
495
if (attributes.contains("pos")) {
496
QStringList &a = attributes["pos"];
499
for(int i = 1; i < a.size(); i++) {
500
QStringList b = a[i].split(',');
501
QPointF p(b[0].toDouble(), b[1].toDouble());
504
QStringList b = a[0].split(',');
505
QPointF p(b[0].toDouble(), b[1].toDouble());
508
m_edges[key] = points;
510
if (0 && attributes.contains("_draw_")) {
511
QStringList &a = attributes["_draw_"];
512
if (a.size() < 5 || (a[3] != "L" && a[3] != "p"))
514
int size = a[4].toInt();
517
for(int i = 0; i < size; i++) {
518
QPointF p(a[i*2+5].toDouble(), a[i*2+6].toDouble());
521
m_edges[key] = points;
527
QRectF f(0, 0, 0, 0);
528
QString id = fixID(keyword);
529
if (attributes.contains("pos")) {
530
QStringList &a = attributes["pos"];
531
QStringList b = a[0].split(",");
532
f.setLeft(b[0].toDouble());
533
f.setTop(b[1].toDouble());
535
if (attributes.contains("height")) {
536
QStringList &a = attributes["height"];
537
f.setHeight(a[0].toDouble()*scale);
540
if (attributes.contains("width")) {
541
QStringList &a = attributes["width"];
542
f.setWidth(a[0].toDouble()*scale);
544
uDebug() << "adding" << id << f;
552
* map dot coordinate to scene coordinate
553
* @param p dot point to map
554
* @return uml scene coordinate
556
QPointF LayoutGenerator::mapToScene(const QPointF &p)
558
return QPointF(p.x()+ m_origin.x(), m_boundingRect.height() - p.y() + m_origin.y());
562
static QDebug operator<<(QDebug out, LayoutGenerator &c)
564
out << "LayoutGenerator:"
565
<< "m_boundingRect:" << c.m_boundingRect
566
<< "m_nodes:" << c.m_nodes
567
<< "m_edges:" << c.m_edges
568
<< "m_scale:" << c.m_scale
569
<< "m_executable:" << c.m_executable;