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 "../model/State.h"
26 #include "../model/Transition.h"
27 #include "GraphvizConverter.h"
28 #include "Layout.h"
29 #include "LayoutWorker.h"
30 
32 
33 #include <cassert> //for testing only
34 #include <map>
35 #include <memory>
36 #include <mutex>
37 #include <string>
38 #include <utility>
39 
40 #include <QApplication>
41 #include <QDesktopWidget>
42 
43 armarx::LayoutWorker::LayoutWorker(size_t workerId, QString name, armarx::GvcPtr gvContext)
44  : QObject(),
45  id(workerId),
46  graphvizContext(gvContext),
47  name(name)
48 {
49  m_graph = std::make_shared<LockableGraph>();
50  std::unique_lock lock(m_graph->mutex);
51  m_graph->graph = agopen(name.toLatin1().data(), Agdirected, NULL);
52  ARMARX_INFO_S << m_graph->graph << " Creating Graph with name " << name.toLatin1().data();
53 
54  int dpiX = qApp->desktop()->logicalDpiX();
55  auto dpi = GraphvizConverter::convertFromFloat(dpiX);
56  //Standardparameter setzen
57  agattr(m_graph->graph, AGRAPH, (char*)"dpi", (char*)"72");//needs to be 72 because graphviz's bounding box is always calculated iwht 72dpi
58  agattr(m_graph->graph, AGRAPH, (char*)"bb", (char*)"0,0,1210,744");
59  agattr(m_graph->graph, AGRAPH, (char*)"size", (char*)"5,5");
60  agattr(m_graph->graph, AGRAPH, (char*)"overlap", (char*)"prism");
61  agattr(m_graph->graph, AGRAPH, (char*)"splines", (char*)"true");
62  agattr(m_graph->graph, AGRAPH, (char*)"pad", (char*)"0.2");
63  agattr(m_graph->graph, AGRAPH, (char*)"nodesep", (char*)"0.4");
64  agattr(m_graph->graph, AGRAPH, (char*)"sep", (char*)"1");
65  agattr(m_graph->graph, AGRAPH, (char*)"overlap_shrink", (char*)"true");
66  agattr(m_graph->graph, AGRAPH, (char*)"rankdir", (char*)"LR");
67  agattr(m_graph->graph, AGRAPH, (char*)"ratio", (char*)"compress");
68 
69  // agraphattr(m_graph->graph, (char*)"ratio", (char*)"fill");
70  // agraphattr(m_graph->graph, (char*)"orientation", (char*)"landscape");
71  // agraphattr(m_graph->graph, (char*)"notranslate", (char*)"false");
72 
73 
74  //Set default attributes for the future nodes
75 
76  // agattr(m_graph->graph, AGNODE, (char*)"fixedsize", (char*)"true");
77  // agnodeattr(m_graph->graph, (char*)"orientation", (char*)"1,0");
78  // agattr(m_graph->graph, AGNODE, (char*)"shape", (char*)"box");
79  agattr(m_graph->graph, AGNODE, (char*)"margin", (char*)"0,0");
80  agattr(m_graph->graph, AGNODE, (char*)"fontsize", (char*)"7");
81  agattr(m_graph->graph, AGEDGE, (char*)"fontsize", (char*)"14");
82 
83  //Divide the wanted width by the DPI to get the value in points
84  //TODO: wirklich benötigt?
85  double test1 = atof(agget(m_graph->graph, (char*)"dpi"));
86  qreal node_size = 50;
87  QString nodePtsWidth = QString("%1").arg(node_size / test1);
88 
89  //GV uses , instead of . for the separator in floats
90  // agnodeattr(m_graph->graph, (char*)"width", (char*)(nodePtsWidth.replace('.', ",")).toLatin1().data());
91 
92  //Set default attributes for future edges
93  agattr(m_graph->graph, AGEDGE, const_cast<char*>("label"), const_cast<char*>(""));
94  agattr(m_graph->graph, AGEDGE, const_cast<char*>("fontsize"), const_cast<char*>("25"));
95  //TODO: pos als Attribut zu node und edge hinzufügen?
96 }
97 
99 {
100  std::unique_lock lock(m_graph->mutex);
101  agclose(m_graph->graph);
102 }
103 
105 {
106  emit deleteMe(id);
107 }
108 
109 void armarx::LayoutWorker::layout(bool layoutAll)
110 {
111  auto start = IceUtil::Time::now();
112 
113  if (true || layoutAll)
114  {
115  // ARMARX_INFO_S << "Layouting with dot";
116  layoutWithAlgo("dot");
117  //broadcast changed substate positions
118  }
119  else
120  {
121  ARMARX_INFO_S << "Layouting with only transitions";
122  layoutWithAlgo("nop"); // neato nop nop2 fdp
123  }
124 
125  ARMARX_INFO_S << name << ": Layouting took " << (IceUtil::Time::now() - start).toMilliSecondsDouble();
126  //broadcast changed transition support points
127  emit layoutingFinished();
128 }
129 
131 {
132  if (id == workerId)
133  {
134  emit buildGraph(m_graph);
135  }
136 }
137 
138 void armarx::LayoutWorker::layoutWithAlgo(std::string algorithm)
139 {
140  std::unique_lock lock(m_graph->mutex);
141  // ARMARX_INFO_S << "Layouting with graph ptr " << m_graph->graph;
142  // int nodeCard = agnnodes(m_graph->graph);
143  // ARMARX_INFO_S << "State " /*<< m_graph->graph->name*/ << " with " << nodeCard << " substates is to be layouted";
144  // agwrite(m_graph->graph, stdout);
145  gvLayout(graphvizContext, m_graph->graph, algorithm.c_str());
146  // ARMARX_INFO_S /*<< m_graph->graph->name*/ << " succesfully layouted";
147  // gvRenderContext(graphvizContext, m_graph->graph, "dot", NULL);
148  // std::iostream str;
149  attach_attrs(m_graph->graph);
150  // gvRender(graphvizContext, m_graph->graph, "dot", stdout);
151  // gvRenderFilename(graphvizContext, m_graph->graph, "svg", "./graphviz.svg");
152  gvFreeLayout(graphvizContext, m_graph->graph);
153 
154 }
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:130
Layout.h
armarx::LayoutWorker::LayoutWorker
LayoutWorker(size_t workerId, QString name, GvcPtr gvContext)
LayoutWorker Sets state and id as specified.
Definition: LayoutWorker.cpp:43
armarx::LayoutWorker::stateDeleted
void stateDeleted()
stateDeleted The corresponding state was deleted.
Definition: LayoutWorker.cpp:104
armarx::GraphvizConverter::convertFromFloat
static std::string convertFromFloat(float number)
Definition: GraphvizConverter.cpp:67
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
Logging.h
armarx::LayoutWorker::~LayoutWorker
~LayoutWorker() override
Definition: LayoutWorker.cpp:98
LayoutWorker.h
armarx::LayoutWorker::layout
void layout(bool layoutAll)
layout Starts layouting.
Definition: LayoutWorker.cpp:109