Skip to content
Snippets Groups Projects
Commit 146661b1 authored by Joseph Mirabel's avatar Joseph Mirabel Committed by Joseph Mirabel
Browse files

Add the ability to dynamically move nodes.

parent 328d9337
No related branches found
No related tags found
No related merge requests found
......@@ -103,6 +103,31 @@ void QGVNode::setIcon(const QImage &icon)
_icon = icon;
}
QVariant QGVNode::itemChange (GraphicsItemChange change, const QVariant & value)
{
if (change == ItemPositionChange && _scene) {
// value is the new position.
QString oldStr = getAttribute(QString::fromLocal8Bit("pos"));
qreal gheight = QGVCore::graphHeight(_scene->_graph->graph());
QPointF newPos = value.toPointF();
qreal width = ND_width(_node->node())*DotDefaultDPI;
qreal height = ND_height(_node->node())*DotDefaultDPI;
QString newStr = QGVCore::qtToGvPos (QGVCore::centerToOrigin (newPos, -width, -height), gheight);
if (!newStr.isEmpty()) {
qDebug () << newPos;
if (oldStr != newStr) {
setAttribute ("pos", newStr.toLocal8Bit().data());
}
return newPos;
}
} else if (change == ItemPositionHasChanged && _scene) {
emit _scene->nodeChanged (this);
return value;
}
return QGraphicsItem::itemChange(change, value);
}
void QGVNode::updateLayout()
{
prepareGeometryChange();
......
......@@ -51,6 +51,9 @@ public:
return Type;
}
protected:
QVariant itemChange (GraphicsItemChange change, const QVariant & value);
private:
friend class QGVScene;
friend class QGVSubGraph;
......
......@@ -231,6 +231,17 @@ void QGVScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
void QGVScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
if(item)
{
if(item->type() == QGVNode::Type)
emit nodeMouseRelease (qgraphicsitem_cast<QGVNode*>(item));
}
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
#include <QVarLengthArray>
#include <QPainter>
void QGVScene::drawBackground(QPainter * painter, const QRectF & rect)
......
......@@ -58,6 +58,8 @@ public:
signals:
void nodeContextMenu(QGVNode* node);
void nodeDoubleClick(QGVNode* node);
void nodeChanged (QGVNode* node);
void nodeMouseRelease (QGVNode* node);
void edgeContextMenu(QGVEdge* edge);
void edgeDoubleClick(QGVEdge* edge);
......@@ -72,6 +74,7 @@ public slots:
protected:
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent * contextMenuEvent);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void drawBackground(QPainter * painter, const QRectF & rect);
private:
friend class QGVNode;
......
......@@ -24,6 +24,14 @@ qreal QGVCore::graphHeight(Agraph_t *graph)
return GD_bb(graph).UR.y;
}
QString QGVCore::qtToGvPos (QPointF pos, qreal gheight)
{
float x = pos.x();
float y = pos.y();
//Le repere Y commence du bas dans graphViz et du haut pour Qt !
return QString ("%1,%2").arg (x).arg(gheight - y);
}
QPointF QGVCore::toPoint(pointf p, qreal gheight)
{
//Le repere Y commence du bas dans graphViz et du haut pour Qt !
......
......@@ -38,6 +38,7 @@ class QGVCore
{
public:
static qreal graphHeight(Agraph_t *graph);
static QString qtToGvPos (QPointF pos, qreal gheight);
static QPointF toPoint(pointf p, qreal gheight);
static QPointF toPoint(point p, qreal gheight);
static QPointF centerToOrigin(const QPointF &p, qreal width, qreal height);
......
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