libyui-qt-graph  2.44.6
QY2Graph.h
1 /*
2  * Copyright (C) 2009-2012 Novell, Inc
3  * This library is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version 2.1 of the
6  * License, or (at your option) version 3.0 of the License. This library
7  * is distributed in the hope that it will be useful, but WITHOUT ANY
8  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  * License for more details. You should have received a copy of the GNU
11  * Lesser General Public License along with this library; if not, write
12  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  * Floor, Boston, MA 02110-1301 USA
14  */
15 
16 /*
17  * File: QY2Graph.h
18  * Author: Arvin Schnell <aschnell@suse.de>
19  */
20 
21 
22 #ifndef QY2Graph_h
23 #define QY2Graph_h
24 
25 #include <graphviz/gvc.h>
26 #include <string>
27 
28 #include <QGraphicsView>
29 #include <QGraphicsScene>
30 #include <QGraphicsPathItem>
31 #include <QPicture>
32 #include <QContextMenuEvent>
33 #include <QMouseEvent>
34 
35 
36 /**
37  * The QY2Graph widget shows a graph layouted by graphviz in a
38  * QGraphicsView/QGraphicsScene.
39  */
40 class QY2Graph : public QGraphicsView
41 {
42  Q_OBJECT
43 
44 public:
45 
46  QY2Graph(const std::string& filename, const std::string& layoutAlgorithm, QWidget* parent = 0);
47 
48  QY2Graph(graph_t* graph, QWidget* parent = 0);
49 
50  virtual ~QY2Graph();
51 
52  virtual void renderGraph(const std::string& filename, const std::string& layoutAlgorithm);
53  virtual void renderGraph(graph_t* graph);
54 
55  void clearGraph();
56 
57 signals:
58 
59  void backgroundContextMenuEvent(QContextMenuEvent* event);
60  void nodeContextMenuEvent(QContextMenuEvent* event, const QString& name);
61  void nodeDoubleClickEvent(QMouseEvent* event, const QString& name);
62 
63 protected:
64 
65  void keyPressEvent(QKeyEvent* event);
66  void wheelEvent(QWheelEvent* event);
67  void contextMenuEvent(QContextMenuEvent* event);
68  void mouseDoubleClickEvent(QMouseEvent* event);
69 
70 private:
71 
72  void init();
73 
74  void scaleView(qreal scaleFactor);
75 
76  QGraphicsScene* scene;
77 
78  QRectF graphRect;
79 
80  QPointF gToQ(const pointf& p, bool upside_down = true) const;
81 
82  QString aggetToQString(void* obj, const char* name, const QString& fallback) const;
83  QColor aggetToQColor(void* obj, const char* name, const QColor& fallback) const;
84  Qt::PenStyle aggetToQPenStyle(void* obj, const char* name, const Qt::PenStyle fallback) const;
85 
86  QPainterPath makeShape(node_t* node) const;
87  QPolygonF makeShapeHelper(node_t* node) const;
88 
89  QPainterPath makeBezier(const bezier& bezier) const;
90 
91  void drawLabel(const textlabel_t* textlabel, QPainter* painter) const;
92 
93  void drawArrow(const QLineF& line, const QColor& color, QPainter* painter) const;
94 
95 };
96 
97 
98 class QY2Node : public QObject, public QGraphicsPathItem
99 {
100  Q_OBJECT
101 
102 public:
103 
104  QY2Node(const QPainterPath& path, const QPicture& picture, const QString& name);
105 
106  void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
107 
108 private:
109 
110  QPicture picture;
111 
112 public:
113 
114  QString name;
115 
116 };
117 
118 
119 class QY2Edge : public QGraphicsPathItem
120 {
121 
122 public:
123 
124  QY2Edge(const QPainterPath& path, const QPicture& picture);
125 
126  QRectF boundingRect() const;
127 
128  void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
129 
130 private:
131 
132  QPicture picture;
133 
134 };
135 
136 
137 #endif // QY2Graph_h
The QY2Graph widget shows a graph layouted by graphviz in a QGraphicsView/QGraphicsScene.
Definition: QY2Graph.h:40