Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
diagram.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef DIAGRAM_H
24 #define DIAGRAM_H
25 
26 #include <QGraphicsView>
27 #include <QGraphicsProxyWidget>
28 #include <QInputDialog>
29 #include "planargraph.h"
30 #include "latexLink.h"
31 #include "latexcompiler.h"
32 
33 class MainWindow;
34 class QLabel;
35 class DiagramWidget;
36 namespace drawer {
37 class Graph;
38 }
39 
40 class Node;
41 class Edge;
42 
43 enum InteractiveMode {
44 
45  NormalMode,
46  InsertionMode,
47  SelectionMode,
48 };
49 
50 class Proxy;
51 class DiagramRenderer;
52 class Diagram: public QObject
53 {
54 
55  Q_OBJECT
56 
57  friend class MainWindow;
58  friend class MainWidget;
59  friend class DiagramRenderer;
60  friend class DiagramWidget;
61  friend class Edge;
62  friend class Node;
63 
64 public:
65  Diagram(QGraphicsScene *t_scene,
66  qint32 t_X0,
67  qint32 t_Y0,
68  DiagramRenderer *t_renderer);
69 
70 
71  ~Diagram() override;
72 
73  QString getName() const { return name; }
74 
75  void setActiveWidget(DiagramWidget *t_widget);
76 
77  InteractiveMode getInteractiveMode() const;
78  void save(
79  std::string const&fileName
80  ) const;
81  void exportSelfPNG(QString const &fileName);
82  void exportPNG(
83  std::string const&fileName
84  ) const;
85  void exportPDF(
86  std::string const&fileName
87  ) const;
88  std::string getLatexSource() const;
89 
90  void loadGraph(
91  drawer::Graph const&t_graph
92  );
93  void loadLinker(
94  drawer::LatexLinker const& t_link
95  );
96  void itemMoved();
97  void clear();
98  void setOutputPath(
99  std::string const& path
100  );
101  void setFile(
102  std::string const&t_fileName
103  );
104  void setExportFile(
105  std::string const&t_fileName
106  );
107  void forceNodeOnGrid(
108  bool value
109  );
110  void testRoundValues(
111  qreal& x,
112  qreal& y
113  );
114 
115  void insertionMode();
116  void normalMode();
117  void selectionMode();
118 
119  void modificationDone();
120 
121 public slots:
122 
123  void setName(QString const &t_name) { name = t_name; }
124  void setGridSize(qint32 t_gridSize);
125  void showNodes(bool);
126  void reinitGraph();
127  void updateGraph();
128  void nodeMoved();
129  void setFocus(Node const *focused);
130  void resetFocus();
131 
132  void center();
133  void rotateTo(int angle);
134  void moveTo(qint32 X0_new, qint32 Y0_new);
135 
136  size_t getPosNode(Node const*node) const;
137  std::pair<size_t, size_t> getPosEdge(Edge const*edge) const;
138 
139  void addNode(qreal x = 0,
140  qreal y = 0);
141  void addNode(QPointF point);
142  void addEdge(size_t i,
143  size_t j);
144  void addEdge(Node *A,
145  Node *B);
146 
147  Proxy *generateLabel(QString const &text) const;
148  void addLabel(
149  Proxy * label,
150  QPointF const& pos);
151  void addNodeLabel(
152  Node *node,
153  QString const &text
154  );
155  void addEdgeLabel(
156  Edge *edge,
157  drawer::LatexLinker::Edge const &lEdge,
158  QString const &text);
159  void proxyDoubleClicked(Proxy *);
160 
161  void removeEdge(Edge *edge);
162  void removeNode(size_t i);
163  void removeSelected();
164  void removeSelectedNodes();
165  void removeSelectedEdges();
166 
167  void setEdgeName(Edge const *edge,
168  QString const &name);
169  void setNodeName(Node const *node,
170  QString const &name);
171  void setEdgeType(Edge const *edge,
172  drawer::ParticleType type,
173  bool sign = true);
174 
175  void refreshLinker();
176  void refresh();
177 
178  void labelReady(std::string const&);
179 
180 protected:
181  Node* buildNode(qreal x = 0,
182  qreal y = 0);
183  Edge* buildEdge(Node *first,
184  Node *second);
185  void deleteNode(Node *&node);
186  void deleteEdge(Edge *&edge);
187 
188  QPointF getNodeLabelPos(
189  Node const *node,
190  Proxy const *label
191  ) const;
192  QPointF getEdgeLabelPos(
193  Edge const *edge,
194  drawer::LatexLinker::Edge const &lEdge,
195  Proxy const *label
196  ) const;
197 
198  drawer::Graph getScaledGraph() const;
199  void setFocusOn(size_t pos);
200  void move(qreal x, qreal y);
201  void keyPressEvent(QKeyEvent *event) ;
202  void keyReleaseEvent(QKeyEvent *event) ;
203  QPointF getCursorPos() const;
204  void mouseMoveEvent(QMouseEvent *event) ;
205  void mousePressEvent(QMouseEvent *event) ;
206  void mouseReleaseEvent(QMouseEvent *event) ;
207  void mouseDoubleClickEvent(QMouseEvent *event) ;
208  void wheelEvent(QWheelEvent *event) ;
209 
210 public:
211  static constexpr qreal scaleForGraph = 100;
212 private:
213  inline static const std::string defaultFileName = "None";
214 
215  DiagramRenderer *renderer;
216  DiagramWidget *widget;
217  drawer::Graph graph;
218  mutable latexcompiler *latexComp;
219  mutable drawer::LatexLinker link;
220  Node *cursor;
221  Edge *draggedEdge;
222  std::vector<Node*> nodes;
223  std::vector<Edge*> edges;
224  QGraphicsScene *scene;
225  qint32 X0;
226  qint32 Y0;
227  qint32 LX;
228  qint32 LY;
229  qint32 rotation;
230 
231  QString name;
232  std::string outputPath = ".";
233  std::string saveFile = defaultFileName;
234  std::string exportFile = defaultFileName;
235 
236  InteractiveMode mode;
237  int gridSize;
238  bool nodesOnGrid;
239  bool focusNode;
240  bool updatingGraph;
241 };
242 
243 class Proxy: public QGraphicsProxyWidget {
244 
245  Q_OBJECT
246 
247 public:
248 
249  Proxy(DiagramWidget *t_widget)
250  :QGraphicsProxyWidget(),
251  widget(t_widget)
252  {
253 
254  }
255 
256  ~Proxy() override {}
257 
258  void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
259  {
260  QGraphicsItem::mouseMoveEvent(event);
261  }
262 
263  void mousePressEvent(QGraphicsSceneMouseEvent *event) override
264  {
265  QGraphicsItem::mousePressEvent(event);
266  }
267 
268  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
269  {
270  QGraphicsItem::mouseReleaseEvent(event);
271  }
272 
273  void dropEvent(QGraphicsSceneDragDropEvent *event) override
274  {
275  QGraphicsItem::dropEvent(event);
276  }
277 
278  void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
279 
280 signals:
281 
282  void doubleClicked(Proxy *);
283 public:
284 
285  QString data;
286  DiagramWidget *widget;
287 };
288 
289 #endif // DIAGRAM_H
Definition: latexLink.h:52
Definition: node.h:39
Definition: drawer.h:29
Definition: edge.h:40
Definition: planargraph.h:169
Definition: latexcompiler.h:25
Definition: diagramrenderer.h:40
Definition: latexLink.h:116
Definition: diagram.h:52
Definition: diagramwidget.h:43
Definition: diagram.h:243
Definition: mainwindow.h:33
Definition: mainwidget.h:34