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