diff --git a/lib/qtgraph/graphwidget.cpp b/lib/qtgraph/graphwidget.cpp index d69f458..6eb4a7b 100644 --- a/lib/qtgraph/graphwidget.cpp +++ b/lib/qtgraph/graphwidget.cpp @@ -16,7 +16,8 @@ namespace std { template <> struct hash { - std::size_t operator()(const float2& f2) const { + std::size_t operator()(const float2& f2) const + { std::size_t h1 = std::hash()(f2.x); std::size_t h2 = std::hash()(f2.y); return h1 ^ (h2 << 1); @@ -25,10 +26,16 @@ namespace std { } // for the map -bool operator< (const float2& v1, const float2& v2) { +bool operator< (const float2& v1, const float2& v2) +{ return length(v1) < length(v2); } +float2 inline float2FromQPointF(const QPointF& p) +{ + return float2(p.x(), p.y()); +} + GraphWidget::GraphWidget(Graph* graph, QWidget *p) : QGraphicsView(p) @@ -50,8 +57,8 @@ GraphWidget::GraphWidget(Graph* graph, QWidget *p) void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos) { - float2 old_v = float2(oldPos.x(), oldPos.y()); - float2 new_v = float2(newPos.x(), newPos.y()); + float2 old_v = float2FromQPointF(oldPos); + float2 new_v = float2FromQPointF(newPos); m_graph->modifyVertex(old_v, new_v); } @@ -84,6 +91,27 @@ void GraphWidget::keyPressEvent(QKeyEvent *e) case Qt::Key_Minus: zoomOut(); break; + case Qt::Key_Insert: { + QList selectedItems = scene()->selectedItems(); + if (selectedItems.isEmpty()) + break; + + QGraphicsItem* selectedItem = selectedItems.first(); + Node* selectedNode = dynamic_cast(selectedItem); + + const QPoint global_p = QCursor::pos(); + const QPoint widget_p = mapFromGlobal(global_p); + const QPointF scene_p = mapToScene(widget_p); + + Node *node = new Node(this); + scene()->addItem(node); + node->setPos(scene_p.x(), scene_p.y()); + scene()->addItem(new Edge(selectedNode, node)); + + const float2 source_pos = float2FromQPointF(selectedItem->pos()); + const float2 destination_pos = float2FromQPointF(scene_p); + m_graph->addEdge(source_pos, destination_pos); + } default: QGraphicsView::keyPressEvent(e); } diff --git a/lib/qtgraph/graphwidget.hpp b/lib/qtgraph/graphwidget.hpp index 24e2bc1..94148ef 100644 --- a/lib/qtgraph/graphwidget.hpp +++ b/lib/qtgraph/graphwidget.hpp @@ -15,6 +15,7 @@ public: GraphWidget(Graph* graph, QWidget *parent = 0); void itemMoved(const QPointF oldPos, const QPointF newPos); + void updateFromGraph(); public slots: diff --git a/lib/qtgraph/node.cpp b/lib/qtgraph/node.cpp index bc3db07..ad67c0d 100644 --- a/lib/qtgraph/node.cpp +++ b/lib/qtgraph/node.cpp @@ -9,10 +9,11 @@ Node::Node(GraphWidget *graphWidget) - : graph(graphWidget) + : m_graphWidget(graphWidget) { setFlag(ItemIsMovable); setFlag(ItemSendsGeometryChanges); + setFlag(ItemIsSelectable); setCacheMode(DeviceCoordinateCache); setZValue(-1); } @@ -42,18 +43,18 @@ QPainterPath Node::shape() const return path; } -void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) +void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget *) { painter->setPen(Qt::NoPen); painter->setBrush(Qt::darkGray); painter->drawEllipse(-7, -7, 20, 20); QRadialGradient gradient(-3, -3, 10); - if (option->state & QStyle::State_Sunken) { + if (isSelected()) { gradient.setCenter(3, 3); gradient.setFocalPoint(3, 3); - gradient.setColorAt(1, QColor(Qt::yellow).light(120)); - gradient.setColorAt(0, QColor(Qt::darkYellow).light(120)); + gradient.setColorAt(0, Qt::red); + gradient.setColorAt(1, Qt::darkRed); } else { gradient.setColorAt(0, Qt::yellow); gradient.setColorAt(1, Qt::darkYellow); @@ -73,7 +74,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) const QPointF old_pos = pos(); const QPointF new_pos = value.toPointF(); - graph->itemMoved(old_pos, new_pos); + m_graphWidget->itemMoved(old_pos, new_pos); break; } default: diff --git a/lib/qtgraph/node.hpp b/lib/qtgraph/node.hpp index 93d8f76..2bd0285 100644 --- a/lib/qtgraph/node.hpp +++ b/lib/qtgraph/node.hpp @@ -32,7 +32,7 @@ protected: private: QList edgeList; QPointF newPos; - GraphWidget *graph; + GraphWidget *m_graphWidget; }; #endif