Debian Codegraph @

src/Gui/DAGView/DAGModelGraph.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /***************************************************************************
3 * Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #ifndef DAGMODELGRAPH_H
25 #define DAGMODELGRAPH_H
26
27 #include <bitset>
28 #include <memory>
29
30 #include <boost_graph_adjacency_list.hpp>
31 #include <boost_graph_reverse_graph.hpp>
32 #include <boost/graph/breadth_first_search.hpp>
33 #include <boost/graph/graphviz.hpp>
34 #include <boost/graph/iteration_macros.hpp>
35 #include <boost/graph/topological_sort.hpp>
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/member.hpp>
38 #include <boost/multi_index/ordered_index.hpp>
39 #include <boost/signals2.hpp>
40
41 #include "DAGRectItem.h"
42
43
44 namespace App
45 {
46 class DocumentObject;
47 }
48
49 namespace Gui
50 {
51 class ViewProviderDocumentObject;
52
53 namespace DAG
54 {
55 enum class VisibilityState
56 {
57 None = 0, //<! not determined.
58 On, //<! shown
59 Off //<! hidden
60 };
61
62 enum class FeatureState
63 {
64 None = 0, //<! not determined.
65 Pass, //<! feature updated ok.
66 Fail, //<! feature failed to update.
67 Pending //<! feature is pending an update.
68 };
69
70 // limit of column width? boost::dynamic_bitset?
71 // did a trial run with this set at 4096, not much difference.
72 // going to leave a big number by default and see how it goes.
73 using ColumnMask = std::bitset<1024>;
74
75 /*! @brief Graph vertex information
76 *
77 * My data stored for each vertex;
78 */
79 struct VertexProperty
80 {
81 VertexProperty();
82 std::shared_ptr<RectItem> rectangle; //!< background
83 std::shared_ptr<QGraphicsEllipseItem> point; //!< point
84 std::shared_ptr<QGraphicsPixmapItem> visibleIcon; //!< visible Icon
85 std::shared_ptr<QGraphicsPixmapItem> stateIcon; //!< visible Icon
86 std::shared_ptr<QGraphicsPixmapItem> icon; //!< icon
87 std::shared_ptr<QGraphicsTextItem> text; //!< text
88 boost::signals2::connection connChangeIcon;
89 int row = 0; //!< row for this entry.
90 ColumnMask column = 0; //!< column number containing the point.
91 int topoSortIndex = 0;
92 VisibilityState lastVisibleState = VisibilityState::None; //!< visibility test.
93 FeatureState lastFeatureState = FeatureState::None; //!< feature state test.
94 };
95 /*! @brief boost data for each vertex.
96 *
97 * needed to create an internal index for vertex. needed for listS.
98 * color is needed by some algorithms */
99 using vertex_prop = boost::property<
100 boost::vertex_index_t,
101 std::size_t,
102 boost::property<boost::vertex_color_t, boost::default_color_type, VertexProperty>>;
103
104 /*! @brief Graph edge information
105 *
106 * My data stored for each edge;
107 */
108 struct EdgeProperty
109 {
110 //! Feature relation meta data. Not used right now.
111 enum class BranchTag
112 {
113 None = 0, //!< not defined.
114 Create, //!< create a new branch.
115 Continue, //!< continue a branch.
116 Terminate //!< terminate a branch.
117 };
118 EdgeProperty();
119 BranchTag relation = BranchTag::None;
120 std::shared_ptr<QGraphicsPathItem> connector; //!< line representing link between nodes.
121 };
122 /*! @brief needed to create an internal index for graph edges. needed for setS.*/
123 using edge_prop = boost::property<boost::edge_index_t, std::size_t, EdgeProperty>;
124
125 using Graph
126 = boost::adjacency_list<boost::setS, boost::listS, boost::bidirectionalS, vertex_prop, edge_prop>;
127 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
128 using Edge = boost::graph_traits<Graph>::edge_descriptor;
129 using VertexIterator = boost::graph_traits<Graph>::vertex_iterator;
130 using EdgeIterator = boost::graph_traits<Graph>::edge_iterator;
131 using InEdgeIterator = boost::graph_traits<Graph>::in_edge_iterator;
132 using OutEdgeIterator = boost::graph_traits<Graph>::out_edge_iterator;
133 using VertexAdjacencyIterator = boost::graph_traits<Graph>::adjacency_iterator;
134 using GraphReversed = boost::reverse_graph<Graph, Graph&>;
135 using Path = std::vector<Vertex>; //!< a path or any array of vertices
136
137 template<class GraphEW>
138 class Edge_writer
139 {
140 public:
141 explicit Edge_writer(const GraphEW& graphEWIn)
142 : graphEW(graphEWIn)
143 {}
144 template<class EdgeW>
145 void operator()(std::ostream& out, const EdgeW& /*edgeW*/) const
146 {
147 out << "[label=\"";
148 out << "edge";
149 out << "\"]";
150 }
151
152 private:
153 const GraphEW& graphEW;
154 };
155
156 template<class GraphVW>
157 class Vertex_writer
158 {
159 public:
160 explicit Vertex_writer(const GraphVW& graphVWIn)
161 : graphVW(graphVWIn)
162 {}
163 template<class VertexW>
164 void operator()(std::ostream& out, const VertexW& vertexW) const
165 {
166 out << "[label=\"";
167 out << graphVW[vertexW].text->toPlainText().toLatin1().data();
168 out << "\"]";
169 }
170
171 private:
172 const GraphVW& graphVW;
173 };
174
175 template<class GraphIn>
176 void outputGraphviz(const GraphIn& graphIn, const std::string& filePath)
177 {
178 std::ofstream file(filePath.c_str());
179 boost::write_graphviz(file, graphIn, Vertex_writer<GraphIn>(graphIn), Edge_writer<GraphIn>(graphIn));
180 }
181
182 //! get all the leaves of the templated graph. Not used right now.
183 template<class GraphIn>
184 class RakeLeaves
185 {
186 using GraphInVertex = boost::graph_traits<Graph>::vertex_descriptor;
187 using GraphInVertices = std::vector<GraphInVertex>;
188
189 public:
190 explicit RakeLeaves(const GraphIn& graphIn)
191 : graph(graphIn)
192 {}
193 GraphInVertices operator()() const
194 {
195 GraphInVertices out;
196 BGL_FORALL_VERTICES_T(currentVertex, graph, GraphIn)
197 {
198 if (boost::out_degree(currentVertex, graph) == 0) {
199 out.push_back(currentVertex);
200 }
201 }
202 return out;
203 }
204
205 private:
206 const GraphIn& graph;
207 };
208
209 //! get all the roots of the templated graph. Not used right now.
210 template<class GraphIn>
211 class DigRoots
212 {
213 using GraphInVertex = boost::graph_traits<Graph>::vertex_descriptor;
214 using GraphInVertices = std::vector<GraphInVertex>;
215
216 public:
217 explicit DigRoots(const GraphIn& graphIn)
218 : graph(graphIn)
219 {}
220 GraphInVertices operator()() const
221 {
222 GraphInVertices out;
223 BGL_FORALL_VERTICES_T(currentVertex, graph, GraphIn)
224 {
225 if (boost::in_degree(currentVertex, graph) == 0) {
226 out.push_back(currentVertex);
227 }
228 }
229 return out;
230 }
231
232 private:
233 const GraphIn& graph;
234 };
235
236 /*! @brief Get connected components.
237 */
238 class ConnectionVisitor: public boost::default_bfs_visitor
239 {
240 public:
241 explicit ConnectionVisitor(std::vector<Vertex>& verticesIn)
242 : vertices(verticesIn)
243 {}
244
245 template<typename TVertex, typename TGraph>
246 void discover_vertex(TVertex vertex, TGraph& graph)
247 {
248 Q_UNUSED(graph);
249 vertices.push_back(vertex);
250 }
251
252 private:
253 std::vector<Vertex>& vertices;
254 };
255
256 /*! Multi_index record. */
257 struct GraphLinkRecord
258 {
259 const App::DocumentObject* DObject; //!< document object
260 const Gui::ViewProviderDocumentObject* VPDObject; //!< view provider
261 const RectItem* rectItem; //!< qgraphics item.
262 std::string uniqueName; //!< name for document object.
263 Vertex vertex; //!< vertex in graph.
264
265 //@{
266 //! used as tags.
267 struct ByDObject
268 {
269 };
270 struct ByVPDObject
271 {
272 };
273 struct ByRectItem
274 {
275 };
276 struct ByUniqueName
277 {
278 };
279 struct ByVertex
280 {
281 };
282 //@}
283 };
284
285 namespace BMI = boost::multi_index;
286 using GraphLinkContainer = boost::multi_index_container<
287 GraphLinkRecord,
288 BMI::indexed_by<
289 BMI::ordered_unique<
290 BMI::tag<GraphLinkRecord::ByDObject>,
291 BMI::member<GraphLinkRecord, const App::DocumentObject*, &GraphLinkRecord::DObject>>,
292 BMI::ordered_unique<
293 BMI::tag<GraphLinkRecord::ByVPDObject>,
294 BMI::member<GraphLinkRecord, const Gui::ViewProviderDocumentObject*, &GraphLinkRecord::VPDObject>>,
295 BMI::ordered_unique<
296 BMI::tag<GraphLinkRecord::ByRectItem>,
297 BMI::member<GraphLinkRecord, const RectItem*, &GraphLinkRecord::rectItem>>,
298 BMI::ordered_unique<
299 BMI::tag<GraphLinkRecord::ByUniqueName>,
300 BMI::member<GraphLinkRecord, std::string, &GraphLinkRecord::uniqueName>>,
301 BMI::ordered_unique<
302 BMI::tag<GraphLinkRecord::ByVertex>,
303 BMI::member<GraphLinkRecord, Vertex, &GraphLinkRecord::vertex>>>>;
304
305 bool hasRecord(const App::DocumentObject* dObjectIn, const GraphLinkContainer& containerIn);
306 bool hasRecord(const ViewProviderDocumentObject* VPDObjectIn, const GraphLinkContainer& containerIn);
307 const GraphLinkRecord& findRecord(Vertex vertexIn, const GraphLinkContainer& containerIn);
308 const GraphLinkRecord& findRecord(
309 const App::DocumentObject* dObjectIn,
310 const GraphLinkContainer& containerIn
311 );
312 const GraphLinkRecord& findRecord(
313 const Gui::ViewProviderDocumentObject* VPDObjectIn,
314 const GraphLinkContainer& containerIn
315 );
316 const GraphLinkRecord& findRecord(const RectItem* rectIn, const GraphLinkContainer& containerIn);
317 const GraphLinkRecord& findRecord(const std::string& stringIn, const GraphLinkContainer& containerIn);
318 void eraseRecord(const Gui::ViewProviderDocumentObject* VPDObjectIn, GraphLinkContainer& containerIn);
319 } // namespace DAG
320 } // namespace Gui
321
322 #endif // DAGMODELGRAPH_H