Skip to content
Snippets Groups Projects
Commit 30c617ec authored by Nicolas's avatar Nicolas
Browse files

add file

parents
No related branches found
No related tags found
No related merge requests found
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "QGVScene.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_scene = new QGVScene("TEST", this);
//_scene->loadLayout("digraph simple{1 -> 2;2 -> 1;}");
_scene->setGraphAttribute("splines", "ortho");
_scene->setGraphAttribute("rankdir", "LR");
//_scene->setGraphAttribute("concentrate", "true");
_scene->setGraphAttribute("nodesep", "0.4");
_scene->setNodeAttribute("shape", "box");
_scene->setNodeAttribute("style", "filled");
_scene->setNodeAttribute("fillcolor", "white");
_scene->setNodeAttribute("height", "1");
_scene->setEdgeAttribute("minlen", "3");
QGVNode *node1 = _scene->addNode("DSA");
QGVNode *node2 = _scene->addNode("COT");
QGVNode *node3 = _scene->addNode("MIT");
QGVNode *node4 = _scene->addNode("ISS");
_scene->addEdge(node1, node2, "TTL");
_scene->addEdge(node1, node2, "SER");
_scene->addEdge(node1, node3, "RAZ");
_scene->addEdge(node2, node3, "SECU");
_scene->addEdge(node2, node4, "STATUS");
_scene->addEdge(node4, node3, "ACK");
_scene->addEdge(node4, node2, "ERROR");
_scene->addEdge(node4, node2, "ETH");
_scene->addEdge(node4, node2, "RS232");
_scene->applyLayout();
connect(_scene, SIGNAL(nodeContextMenu(QGVNode*)), SLOT(nodeContextMenu(QGVNode*)));
connect(_scene, SIGNAL(nodeDoubleClick(QGVNode*)), SLOT(nodeDoubleClick(QGVNode*)));
ui->graphicsView->setScene(_scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::nodeContextMenu(QGVNode *node)
{
QMenu menu(node->name());
menu.addSeparator();
menu.addAction("Edit");
menu.addAction("Options");
QAction *action = menu.exec(QCursor::pos());
if(action == 0)
return;
}
#include <QMessageBox>
void MainWindow::nodeDoubleClick(QGVNode *node)
{
QMessageBox::information(this, tr("Node double clicked"), tr("Node %1").arg(node->name()));
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGVScene.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void nodeContextMenu(QGVNode* node);
void nodeDoubleClick(QGVNode* node);
private:
Ui::MainWindow *ui;
QGVScene *_scene;
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>817</width>
<height>567</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGraphicsViewEc" name="graphicsView">
<property name="renderHints">
<set>QPainter::Antialiasing|QPainter::TextAntialiasing</set>
</property>
<property name="dragMode">
<enum>QGraphicsView::ScrollHandDrag</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>817</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QGraphicsViewEc</class>
<extends>QGraphicsView</extends>
<header>QGraphicsViewEc.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
#include <QGVEdge.h>
#include <QGVScene.h>
#include <QDebug>
#include <QPainter>
QGVEdge::QGVEdge(QGVNode *source, QGVNode *target, const QString &label, QGVScene *scene) : _scene(scene), _source(source), _target(target)
{
//_edge = agidedge(_scene->_graph, _source->_node, _target->_node, _scene->_edges.count()+1, TRUE);
_edge = agedge(_scene->_graph, _source->_node, _target->_node, NULL, TRUE);
Q_ASSERT(_edge);
if(_edge == NULL)
{
qWarning()<<"Invalid egde :"<<label;
//_edge = agedge(_scene->_graph, _source->_node, _target->_node, NULL, FALSE);
}
//_source->_edges.append(this);
//_target->_edges.append(this);
_scene->_edges.append(this);
_scene->addItem(this);
setLabel(label);
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
QGVEdge::QGVEdge(Agedge_t *edge, QGVScene *scene) : _edge(edge), _scene(scene)
{
updateLayout();
}
QGVEdge::~QGVEdge()
{
_source->_edges.removeOne(this);
_target->_edges.removeOne(this);
_scene->removeItem(this);
_scene->_edges.removeOne(this);
//agdeledget(_scene->_graph, _edge);
}
QString QGVEdge::label() const
{
return QString::fromLocal8Bit(ED_label(_edge)->text);
}
QRectF QGVEdge::boundingRect() const
{
return _path.boundingRect();
}
QPainterPath QGVEdge::shape() const
{
QPainterPathStroker ps;
ps.setCapStyle(_pen.capStyle());
ps.setWidth(_pen.widthF() + 10);
ps.setJoinStyle(_pen.joinStyle());
ps.setMiterLimit(_pen.miterLimit());
return ps.createStroke(_path);
}
void QGVEdge::setLabel(const QString &label)
{
setAttribute("xlabel", label);
}
void QGVEdge::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
if(isSelected())
painter->setPen(QPen(Qt::SolidPattern, 1, Qt::DashLine));
else
painter->setPen(_pen);
painter->drawPath(_path);
textlabel_t *xlabel = ED_xlabel(_edge);
if(xlabel)
painter->drawText(xlabel->pos.x - xlabel->dimen.x/2, GD_bb(_scene->_graph).UR.y - xlabel->pos.y + 4, xlabel->text);
painter->setBrush(Qt::SolidPattern);
painter->drawPolygon(_arrow);
painter->restore();
}
void QGVEdge::setAttribute(const QString &name, const QString &value)
{
agsafeset(_edge, name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
}
void QGVEdge::updateLayout()
{
prepareGeometryChange();
_path = QPainterPath();
const splines* spl = ED_spl(_edge);
if((spl->list != 0) && (spl->list->size%3 == 1))
{
qreal ymax = GD_bb(_scene->_graph).UR.y;
//If there is a starting point, draw a line from it to the first curve point
if(spl->list->sflag)
{
_path.moveTo(spl->list->sp.x, (ymax - spl->list->sp.y));
_path.lineTo(spl->list->list[0].x, (ymax - spl->list->list[0].y));
}
else
_path.moveTo(spl->list->list[0].x, (ymax - spl->list->list[0].y));
//Loop over the curve points
for(int i=1; i<spl->list->size; i+=3)
_path.cubicTo(spl->list->list[i].x,(ymax - spl->list->list[i].y), spl->list->list[i+1].x,
(ymax - spl->list->list[i+1].y),
spl->list->list[i+2].x,
(ymax - spl->list->list[i+2].y));
//If there is an ending point, draw a line to it
if(spl->list->eflag)
_path.lineTo(spl->list->ep.x, (ymax - spl->list->ep.y));
//Calcul de la fleche
QLineF line(spl->list->list[spl->list->size-1].x, (ymax - spl->list->list[spl->list->size-1].y), spl->list->ep.x, (ymax - spl->list->ep.y));
QLineF n(line.normalVector());
QPointF o(n.dx() / 3.0, n.dy() / 3.0);
QPolygonF polygon;
polygon.append(line.p1() + o);
polygon.append(line.p2());
polygon.append(line.p1() - o);
_arrow = polygon;
}
//ED_edge_type(_edge)
_pen.setWidth(1);
//qDebug()<<_name<<_path;
}
#ifndef QGVEDGE_H
#define QGVEDGE_H
#include <QGraphicsItem>
#include <QPen>
#include <gvc.h>
#include <cgraph.h>
class QGVNode;
class QGVScene;
class QGVEdge : public QGraphicsItem
{
public:
QGVEdge(QGVNode *source, QGVNode *target, const QString &label, QGVScene *scene);
QGVEdge(Agedge_t *edge, QGVScene *scene);
~QGVEdge();
QString label() const;
QRectF boundingRect() const;
QPainterPath shape() const;
void setLabel(const QString &label);
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
void setAttribute(const QString &name, const QString &value);
void updateLayout();
enum { Type = UserType + 3 };
int type() const
{
return Type;
}
private:
friend class QGVSubGraph;
QGVScene *_scene;
Agedge_t* _edge;
QGVNode *_source;
QGVNode *_target;
QPainterPath _path;
QPen _pen;
QPolygonF _arrow;
};
#endif // QGVEDGE_H
#include <QGVNode.h>
#include <QGVScene.h>
#include <QDebug>
#include <QPainter>
QGVNode::QGVNode(const QString &label, QGVScene *scene): _scene(scene)
{
_node = agnode(_scene->_graph, NULL, TRUE);
Q_ASSERT(_node);
if(_node == NULL)
qWarning()<<"Invalid node :"<<label;
_scene->_nodes.append(this);
_scene->addItem(this);
setAttribute("label", label);
setFlag(QGraphicsItem::ItemIsSelectable, true);
//setFlag(QGraphicsItem::ItemIsMovable, true);
//setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
}
QGVNode::QGVNode(Agnode_t* node, QGVScene *scene): _node(node), _scene(scene)
{
updateLayout();
}
QGVNode::~QGVNode()
{
_scene->removeItem(this);
_scene->_nodes.removeOne(this);
//agdelnode(_scene->_graph, _node);
}
QString QGVNode::name() const
{
return QString::fromLocal8Bit(ND_label(_node)->text);
}
QRectF QGVNode::boundingRect() const
{
return QRectF(0,0, _width, _height);
}
void QGVNode::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
painter->setPen(_pen);
if(isSelected())
{
QBrush tbrush(_brush);
tbrush.setColor(tbrush.color().darker(120));
painter->setBrush(tbrush);
}
else
painter->setBrush(_brush);
painter->drawPath(_path);
const QRectF rect = boundingRect();
//painter->drawRect(rect);
painter->drawText(rect.adjusted(2,2,-2,-2), Qt::AlignCenter , QGVNode::name());
painter->restore();
}
void QGVNode::setAttribute(const QString &name, const QString &value)
{
agsafeset(_node, name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
}
void QGVNode::updateLayout()
{
prepareGeometryChange();
_width = ND_width(_node)*DotDefaultDPI;
_height = ND_height(_node)*DotDefaultDPI;
//Node Position (center)
boxf bb = GD_bb(_scene->_graph);
qreal x = ND_coord(_node).x - _width/2;
qreal y = bb.UR.y - ND_coord(_node).y - _height/2;
setPos(x, y);
//Node on top
setZValue(1);
//Node path
_path = makeShape(_node);
_pen.setWidth(1);
const char* style = agget(_node, "style");
if(style && strcmp(style, "filled") == 0)
{
_brush.setStyle(Qt::SolidPattern);
_brush.setColor(QString(agget(_node, "fillcolor")));
}
setToolTip(agget(_node, "tooltip"));
//qDebug()<<QGVNode::name()<<_width<<_height<<pos();
}
QPainterPath QGVNode::makeShape(Agnode_t* node) const
{
QPainterPath path;
const char* name = ND_shape(node)->name;
if ((strcmp(name, "rectangle") == 0) ||
(strcmp(name, "box") == 0) ||
(strcmp(name, "hexagon") == 0) ||
(strcmp(name, "polygon") == 0) ||
(strcmp(name, "diamond") == 0))
{
QPolygonF polygon = makeShapeHelper(node);
polygon.append(polygon[0]);
path.addPolygon(polygon);
}
else if ((strcmp(name, "ellipse") == 0) ||
(strcmp(name, "circle") == 0))
{
QPolygonF polygon = makeShapeHelper(node);
path.addEllipse(QRectF(polygon[0], polygon[1]));
}
else
{
qWarning("unsupported shape %s", name);
}
return path;
}
QPolygonF QGVNode::makeShapeHelper(Agnode_t* node) const
{
const polygon_t* poly = (polygon_t*) ND_shape_info(node);
if (poly->peripheries != 1)
qWarning("unsupported number of peripheries %d", poly->peripheries);
const int sides = poly->sides;
const pointf* vertices = poly->vertices;
QPolygonF polygon;
for (int side = 0; side < sides; side++)
polygon.append(QPointF(vertices[side].x + _width/2, vertices[side].y + _height/2));
return polygon;
}
#ifndef QGVNODE_H
#define QGVNODE_H
#include <QGraphicsItem>
#include <QPen>
#include <gvc.h>
#include <cgraph.h>
class QGVEdge;
class QGVScene;
class QGVNode : public QGraphicsItem
{
public:
QGVNode(const QString &label, QGVScene *scene);
QGVNode(Agnode_t* node, QGVScene *scene);
~QGVNode();
QString name() const;
QRectF boundingRect() const;
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
void setAttribute(const QString &name, const QString &value);
void updateLayout();
enum { Type = UserType + 2 };
int type() const
{
return Type;
}
private:
QPainterPath makeShape(Agnode_t* node) const;
QPolygonF makeShapeHelper(Agnode_t* node) const;
friend class QGVEdge;
friend class QGVSubGraph;
double _height, _width;
QPainterPath _path;
QPen _pen;
QBrush _brush;
QGVScene *_scene;
Agnode_t* _node;
QList<QGVEdge*> _edges;
};
#endif // QGVNODE_H
#include "QGVScene.h"
#include <QDebug>
QGVScene::QGVScene(const QString &name, QObject *parent) : QGraphicsScene(parent)
{
_context = gvContext();
_graph = agopen(name.toLocal8Bit().data(), Agdirected, NULL);
//setGraphAttribute("fontname", QFont().family());
}
QGVScene::~QGVScene()
{
gvFreeLayout(_context, _graph);
agclose(_graph);
gvFreeContext(_context);
}
void QGVScene::setGraphAttribute(const QString &name, const QString &value)
{
agattr(_graph, AGRAPH, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
void QGVScene::setNodeAttribute(const QString &name, const QString &value)
{
agattr(_graph, AGNODE, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
void QGVScene::setEdgeAttribute(const QString &name, const QString &value)
{
agattr(_graph, AGEDGE, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
QGVNode *QGVScene::addNode(const QString &label)
{
return new QGVNode(label, this);
}
QGVEdge *QGVScene::addEdge(QGVNode *source, QGVNode *target, const QString &label)
{
return new QGVEdge(source, target, label, this);
}
QGVSubGraph *QGVScene::addSubGraph(const QString &name)
{
return new QGVSubGraph(name, this);
}
void QGVScene::setRootNode(QGVNode *node)
{
Q_ASSERT(_nodes.contains(node));
agset(_graph, "root", node->name().toLocal8Bit().data());
}
void QGVScene::loadLayout(const QString &text)
{
/*
agsetfile("simple.gv");
FILE* fp = fopen("simple.gv", "r");
if(fp)
_graph = agread(fp, NULL);
qDebug("ok");
*/
//_graph = agmemread(text.toLocal8Bit().data());
if(gvLayout(_context, _graph, "dot") != 0)
{
qCritical()<<"Layout render error"<<agerrors()<<QString::fromLocal8Bit(aglasterr());
return;
}
//Read nodes and edges
for (Agnode_t* node = agfstnode(_graph); node != NULL; node = agnxtnode(_graph, node))
{
_nodes.append(new QGVNode(node, this));
for (Agedge_t* edge = agfstout(_graph, node); edge != NULL; edge = agnxtout(_graph, edge))
_edges.append(new QGVEdge(edge, this));
}
}
void QGVScene::applyLayout()
{
//gvFreeLayout(_context, _graph);
if(gvLayout(_context, _graph, "dot") != 0)
{
qCritical()<<"Layout render error"<<agerrors()<<QString::fromLocal8Bit(aglasterr());
return;
}
//Debug output
gvRenderFilename(_context, _graph, "canon", "debug.dot");
gvRenderFilename(_context, _graph, "png", "debug.png");
foreach(QGVNode* node, _nodes)
node->updateLayout();
foreach(QGVEdge* edge, _edges)
edge->updateLayout();
foreach(QGVSubGraph* sgraph, _subGraphs)
sgraph->updateLayout();
update();
}
#include <QGraphicsSceneContextMenuEvent>
void QGVScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
{
QGraphicsItem *item = itemAt(contextMenuEvent->scenePos());
if(item)
{
item->setSelected(true);
QGVNode *node = qgraphicsitem_cast<QGVNode*>(item);
if(node)
emit nodeContextMenu(node);
QGVEdge *edge = qgraphicsitem_cast<QGVEdge*>(item);
if(edge)
emit edgeContextMenu(edge);
}
QGraphicsScene::contextMenuEvent(contextMenuEvent);
}
void QGVScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsItem *item = itemAt(mouseEvent->scenePos());
if(item)
{
QGVNode *node = qgraphicsitem_cast<QGVNode*>(item);
if(node)
emit nodeDoubleClick(node);
QGVEdge *edge = qgraphicsitem_cast<QGVEdge*>(item);
if(edge)
emit edgeDoubleClick(edge);
}
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
#include <QVarLengthArray>
#include <QPainter>
void QGVScene::drawBackground(QPainter * painter, const QRectF & rect)
{
const int gridSize = 25;
const qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
const qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
QVarLengthArray<QLineF, 100> lines;
for (qreal x = left; x < rect.right(); x += gridSize)
lines.append(QLineF(x, rect.top(), x, rect.bottom()));
for (qreal y = top; y < rect.bottom(); y += gridSize)
lines.append(QLineF(rect.left(), y, rect.right(), y));
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setPen(QColor(Qt::lightGray).lighter(110));
painter->drawLines(lines.data(), lines.size());
painter->setPen(Qt::black);
//painter->drawRect(sceneRect());
}
#ifndef QGVSCENE_H
#define QGVSCENE_H
#include <QGraphicsScene>
#include <QGVNode.h>
#include <QGVEdge.h>
#include <QGVSubGraph.h>
#include <gvc.h>
#include <cgraph.h>
const qreal DotDefaultDPI = 72.0;
class QGVScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit QGVScene(const QString &name, QObject *parent = 0);
~QGVScene();
void setGraphAttribute(const QString &name, const QString &value);
void setNodeAttribute(const QString &name, const QString &value);
void setEdgeAttribute(const QString &name, const QString &value);
QGVNode* addNode(const QString& label);
QGVEdge* addEdge(QGVNode* source, QGVNode* target, const QString& label=QString());
QGVSubGraph* addSubGraph(const QString& name);
/*
//TODO
void removeNode(QGVNode* node);
QGVNode* findNode(const QString& name);
void clearNodes();
void removeEdge(QGVNode* source, QGVNode* target);
QGVEdge* findEdge(const QString& name);
void setFont(QFont font);
*/
void setRootNode(QGVNode *node);
void loadLayout(const QString &text);
void applyLayout();
signals:
void nodeContextMenu(QGVNode* node);
void nodeDoubleClick(QGVNode* node);
void edgeContextMenu(QGVEdge* edge);
void edgeDoubleClick(QGVEdge* edge);
public slots:
protected:
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent * contextMenuEvent);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void drawBackground(QPainter * painter, const QRectF & rect);
private:
friend class QGVNode;
friend class QGVEdge;
friend class QGVSubGraph;
GVC_t *_context;
Agraph_t *_graph;
//QFont _font;
QList<QGVNode*> _nodes;
QList<QGVEdge*> _edges;
QList<QGVSubGraph*> _subGraphs;
};
#endif // QGVSCENE_H
#include "QGVSubGraph.h"
#include <QGVScene.h>
#include <QDebug>
#include <QPainter>
QGVSubGraph::QGVSubGraph(const QString &name, QGVScene *scene): _scene(scene)
{
_sgraph = agsubg(_scene->_graph, name.toLocal8Bit().data(), TRUE);
Q_ASSERT(_sgraph);
_scene->_subGraphs.append(this);
_scene->addItem(this);
setAttribute("label", name);
setAttribute("color", "blue");
}
QGVSubGraph::QGVSubGraph(Agraph_t* subGraph, QGVScene *scene): _sgraph(subGraph), _scene(scene)
{
updateLayout();
}
QGVSubGraph::~QGVSubGraph()
{
_scene->removeItem(this);
_scene->_subGraphs.removeOne(this);
//agdelnode(_scene->_graph, _sgraph);
}
QString QGVSubGraph::name() const
{
return QString::fromLocal8Bit(GD_label(_sgraph)->text);
}
QGVNode *QGVSubGraph::addNode(const QString &name)
{
QGVNode *node = new QGVNode(name, _scene);
agsubnode(_sgraph, node->_node, TRUE);
return node;
}
QGVEdge *QGVSubGraph::addEdge(QGVNode *source, QGVNode *target, const QString &label)
{
QGVEdge *edge = new QGVEdge(source, target, label, _scene);
agsubedge(_sgraph, edge->_edge, TRUE);
return edge;
}
QRectF QGVSubGraph::boundingRect() const
{
return QRectF(0,0, _width, _height);
}
void QGVSubGraph::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
painter->save();
painter->drawRect(boundingRect());
painter->restore();
}
void QGVSubGraph::setAttribute(const QString &name, const QString &value)
{
agsafeset(_sgraph, name.toLocal8Bit().data(), value.toLocal8Bit().data(), "");
//agattr(_sgraph, AGRAPH, name.toLocal8Bit().data(), value.toLocal8Bit().data());
}
void QGVSubGraph::updateLayout()
{
prepareGeometryChange();
_height = 50;
_width = 50;
pointf size = GD_bb(_sgraph).UR;
_height = size.x*DotDefaultDPI;
_width =size.y*DotDefaultDPI;
setPos(size.x, size.y);
}
#ifndef QGVSUBGRAPH_H
#define QGVSUBGRAPH_H
#include <QGraphicsItem>
#include <gvc.h>
#include <cgraph.h>
class QGVNode;
class QGVEdge;
class QGVScene;
class QGVSubGraph : public QGraphicsItem
{
public:
explicit QGVSubGraph(const QString &name, QGVScene *scene);
QGVSubGraph(Agraph_t* subGraph, QGVScene *scene);
~QGVSubGraph();
QString name() const;
QGVNode* addNode(const QString& name);
QGVEdge* addEdge(QGVNode* source, QGVNode* target, const QString& label=QString());
QRectF boundingRect() const;
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
void setAttribute(const QString &name, const QString &value);
void updateLayout();
enum { Type = UserType + 3 };
int type() const
{
return Type;
}
private:
double _height, _width;
QGVScene *_scene;
Agraph_t *_sgraph;
};
#endif // QGVSUBGRAPH_H
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-17T09:06:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QGraphViz
TEMPLATE = app
#Configure GraphViz
GRAPHVIZ_DIR = $$PWD/GraphViz
DEFINES += WIN32_DLL
DEFINES += GVDLL
DEFINES += WITH_CGRAPH
INCLUDEPATH += $$GRAPHVIZ_DIR/include
LIBS += -L$$GRAPHVIZ_DIR/lib/release/lib -lgvc -lcgraph -lgraph -lcdt
SOURCES += main.cpp\
MainWindow.cpp \
QGVScene.cpp \
QGraphicsViewEc.cpp \
QGVNode.cpp \
QGVEdge.cpp \
QGVSubGraph.cpp
HEADERS += MainWindow.h \
QGVScene.h \
QGraphicsViewEc.h \
QGVNode.h \
QGVEdge.h \
QGVSubGraph.h
FORMS += MainWindow.ui
#include "QGraphicsViewEc.h"
#include <QWheelEvent>
#include <qmath.h>
QGraphicsViewEc::QGraphicsViewEc(QWidget* parent) : QGraphicsView(parent)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
void QGraphicsViewEc::wheelEvent(QWheelEvent* event)
{
qreal scaleFactor = qPow(2.0, event->delta() / 240.0); //How fast we zoom
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if(0.05 < factor && factor < 10) //Zoom factor limitation
scale(scaleFactor, scaleFactor);
}
#ifndef QGRAPHICSVIEWEC_H
#define QGRAPHICSVIEWEC_H
#include <QGraphicsView>
class QGraphicsViewEc : public QGraphicsView
{
Q_OBJECT
public:
QGraphicsViewEc(QWidget *parent = 0);
protected:
virtual void wheelEvent(QWheelEvent* event);
};
#endif // QGRAPHICSVIEWEC_H
main.cpp 0 → 100644
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment