LayoutWorker.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "LayoutWorker.h"
26 
27 #include <cassert> //for testing only
28 #include <map>
29 #include <memory>
30 #include <mutex>
31 #include <string>
32 #include <utility>
33 
34 #include <QApplication>
35 #include <QDesktopWidget>
36 
38 
39 #include "../model/State.h"
40 #include "../model/Transition.h"
41 #include "GraphvizConverter.h"
42 #include "Layout.h"
43 
44 armarx::LayoutWorker::LayoutWorker(size_t workerId, QString name, armarx::GvcPtr gvContext) :
45  QObject(), id(workerId), graphvizContext(gvContext), name(name)
46 {
47  m_graph = std::make_shared<LockableGraph>();
48  std::unique_lock lock(m_graph->mutex);
49  m_graph->graph = agopen(name.toLatin1().data(), Agdirected, NULL);
50  ARMARX_INFO_S << m_graph->graph << " Creating Graph with name " << name.toLatin1().data();
51 
52  int dpiX = qApp->desktop()->logicalDpiX();
53  auto dpi = GraphvizConverter::convertFromFloat(dpiX);
54  //Standardparameter setzen
55  agattr(
56  m_graph->graph,
57  AGRAPH,
58  (char*)"dpi",
59  (char*)"72"); //needs to be 72 because graphviz's bounding box is always calculated iwht 72dpi
60  agattr(m_graph->graph, AGRAPH, (char*)"bb", (char*)"0,0,1210,744");
61  agattr(m_graph->graph, AGRAPH, (char*)"size", (char*)"5,5");
62  agattr(m_graph->graph, AGRAPH, (char*)"overlap", (char*)"prism");
63  agattr(m_graph->graph, AGRAPH, (char*)"splines", (char*)"true");
64  agattr(m_graph->graph, AGRAPH, (char*)"pad", (char*)"0.2");
65  agattr(m_graph->graph, AGRAPH, (char*)"nodesep", (char*)"0.4");
66  agattr(m_graph->graph, AGRAPH, (char*)"sep", (char*)"1");
67  agattr(m_graph->graph, AGRAPH, (char*)"overlap_shrink", (char*)"true");
68  agattr(m_graph->graph, AGRAPH, (char*)"rankdir", (char*)"LR");
69  agattr(m_graph->graph, AGRAPH, (char*)"ratio", (char*)"compress");
70 
71  // agraphattr(m_graph->graph, (char*)"ratio", (char*)"fill");
72  // agraphattr(m_graph->graph, (char*)"orientation", (char*)"landscape");
73  // agraphattr(m_graph->graph, (char*)"notranslate", (char*)"false");
74 
75 
76  //Set default attributes for the future nodes
77 
78  // agattr(m_graph->graph, AGNODE, (char*)"fixedsize", (char*)"true");
79  // agnodeattr(m_graph->graph, (char*)"orientation", (char*)"1,0");
80  // agattr(m_graph->graph, AGNODE, (char*)"shape", (char*)"box");
81  agattr(m_graph->graph, AGNODE, (char*)"margin", (char*)"0,0");
82  agattr(m_graph->graph, AGNODE, (char*)"fontsize", (char*)"7");
83  agattr(m_graph->graph, AGEDGE, (char*)"fontsize", (char*)"14");
84 
85  //Divide the wanted width by the DPI to get the value in points
86  //TODO: wirklich benötigt?
87  double test1 = atof(agget(m_graph->graph, (char*)"dpi"));
88  qreal node_size = 50;
89  QString nodePtsWidth = QString("%1").arg(node_size / test1);
90 
91  //GV uses , instead of . for the separator in floats
92  // agnodeattr(m_graph->graph, (char*)"width", (char*)(nodePtsWidth.replace('.', ",")).toLatin1().data());
93 
94  //Set default attributes for future edges
95  agattr(m_graph->graph, AGEDGE, const_cast<char*>("label"), const_cast<char*>(""));
96  agattr(m_graph->graph, AGEDGE, const_cast<char*>("fontsize"), const_cast<char*>("25"));
97  //TODO: pos als Attribut zu node und edge hinzufügen?
98 }
99 
101 {
102  std::unique_lock lock(m_graph->mutex);
103  agclose(m_graph->graph);
104 }
105 
106 void
108 {
109  emit deleteMe(id);
110 }
111 
112 void
114 {
115  auto start = IceUtil::Time::now();
116 
117  if (true || layoutAll)
118  {
119  // ARMARX_INFO_S << "Layouting with dot";
120  layoutWithAlgo("dot");
121  //broadcast changed substate positions
122  }
123  else
124  {
125  ARMARX_INFO_S << "Layouting with only transitions";
126  layoutWithAlgo("nop"); // neato nop nop2 fdp
127  }
128 
129  ARMARX_INFO_S << name << ": Layouting took "
130  << (IceUtil::Time::now() - start).toMilliSecondsDouble();
131  //broadcast changed transition support points
132  emit layoutingFinished();
133 }
134 
135 void
137 {
138  if (id == workerId)
139  {
140  emit buildGraph(m_graph);
141  }
142 }
143 
144 void
145 armarx::LayoutWorker::layoutWithAlgo(std::string algorithm)
146 {
147  std::unique_lock lock(m_graph->mutex);
148  // ARMARX_INFO_S << "Layouting with graph ptr " << m_graph->graph;
149  // int nodeCard = agnnodes(m_graph->graph);
150  // ARMARX_INFO_S << "State " /*<< m_graph->graph->name*/ << " with " << nodeCard << " substates is to be layouted";
151  // agwrite(m_graph->graph, stdout);
152  gvLayout(graphvizContext, m_graph->graph, algorithm.c_str());
153  // ARMARX_INFO_S /*<< m_graph->graph->name*/ << " succesfully layouted";
154  // gvRenderContext(graphvizContext, m_graph->graph, "dot", NULL);
155  // std::iostream str;
156  attach_attrs(m_graph->graph);
157  // gvRender(graphvizContext, m_graph->graph, "dot", stdout);
158  // gvRenderFilename(graphvizContext, m_graph->graph, "svg", "./graphviz.svg");
159  gvFreeLayout(graphvizContext, m_graph->graph);
160 }
GraphvizConverter.h
armarx::GvcPtr
GVC_t * GvcPtr
Definition: Layout.h:39
armarx::LayoutWorker::isConnected
void isConnected(size_t workerId)
isConnected Notifies that worker with id workerId is now connected to its mediator.
Definition: LayoutWorker.cpp:136
Layout.h
armarx::LayoutWorker::LayoutWorker
LayoutWorker(size_t workerId, QString name, GvcPtr gvContext)
LayoutWorker Sets state and id as specified.
Definition: LayoutWorker.cpp:44
armarx::LayoutWorker::stateDeleted
void stateDeleted()
stateDeleted The corresponding state was deleted.
Definition: LayoutWorker.cpp:107
armarx::GraphvizConverter::convertFromFloat
static std::string convertFromFloat(float number)
Definition: GraphvizConverter.cpp:69
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
Logging.h
armarx::LayoutWorker::~LayoutWorker
~LayoutWorker() override
Definition: LayoutWorker.cpp:100
LayoutWorker.h
armarx::LayoutWorker::layout
void layout(bool layoutAll)
layout Starts layouting.
Definition: LayoutWorker.cpp:113