From 77902419262d570ddc02d55cb4010f19d307527b Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Mon, 23 Mar 2015 16:16:26 +0100 Subject: [PATCH] ui: panning GraphView and resizing after node was moved --- src/app/qt/element/QtMainWindow.cpp | 3 +++ src/app/qt/view/QtGraphView.cpp | 24 ++++++++++++++----- src/app/qt/view/QtGraphView.h | 4 ++++ src/app/qt/view/graphElements/QtGraphNode.cpp | 9 ++++--- src/app/qt/view/graphElements/QtGraphNode.h | 2 +- .../QtGraphNodeComponentMoveable.cpp | 5 ++-- .../component/controller/GraphController.cpp | 1 + src/lib/component/view/GraphView.h | 2 ++ .../component/view/graphElements/GraphNode.h | 1 - 9 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/app/qt/element/QtMainWindow.cpp b/src/app/qt/element/QtMainWindow.cpp index e0953014..9c2e43c5 100644 --- a/src/app/qt/element/QtMainWindow.cpp +++ b/src/app/qt/element/QtMainWindow.cpp @@ -26,6 +26,9 @@ QtMainWindow::QtMainWindow() setCentralWidget(nullptr); setDockNestingEnabled(true); setWindowIcon(QIcon("./data/gui/icon/logo_1024_1024.png")); + + QApplication::setOverrideCursor(Qt::ArrowCursor); + setupProjectMenu(); setupEditMenu(); setupViewMenu(); diff --git a/src/app/qt/view/QtGraphView.cpp b/src/app/qt/view/QtGraphView.cpp index 0c08520e..dc21f1fe 100644 --- a/src/app/qt/view/QtGraphView.cpp +++ b/src/app/qt/view/QtGraphView.cpp @@ -12,17 +12,18 @@ #include "qt/utility/utilityQt.h" #include "qt/view/QtViewWidgetWrapper.h" +#include "qt/view/graphElements/nodeComponents/QtGraphNodeComponentClickable.h" +#include "qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.h" #include "qt/view/graphElements/QtGraphEdge.h" #include "qt/view/graphElements/QtGraphNode.h" #include "qt/view/graphElements/QtGraphNodeAccess.h" -#include "qt/view/graphElements/nodeComponents/QtGraphNodeComponentClickable.h" -#include "qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.h" QtGraphView::QtGraphView(ViewLayout* viewLayout) : GraphView(viewLayout) , m_rebuildGraphFunctor( std::bind(&QtGraphView::doRebuildGraph, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)) , m_clearFunctor(std::bind(&QtGraphView::doClear, this)) + , m_resizeFunctor(std::bind(&QtGraphView::doResize, this)) { } @@ -48,6 +49,7 @@ void QtGraphView::initView() QGraphicsScene* scene = new QGraphicsScene(widget); QGraphicsView* view = new QGraphicsView(widget); view->setScene(scene); + view->setDragMode(QGraphicsView::ScrollHandDrag); widget->layout()->addWidget(view); } @@ -69,6 +71,11 @@ void QtGraphView::clear() m_clearFunctor(); } +void QtGraphView::resizeView() +{ + m_resizeFunctor(); +} + Vec2i QtGraphView::getViewSize() const { QGraphicsView* view = getView(); @@ -98,12 +105,10 @@ void QtGraphView::switchToNewGraphData() m_nodes.clear(); m_edges.clear(); - QGraphicsView* view = getView(); - - int margin = 25; - view->setSceneRect(itemsBoundingRect(m_oldNodes).adjusted(-margin, -margin, margin, margin).translated(m_sceneRectOffset)); + doResize(); // Manually hover the item below the mouse cursor. + QGraphicsView* view = getView(); QPointF point = view->mapToScene(view->mapFromGlobal(QCursor::pos())); QGraphicsItem* item = view->scene()->itemAt(point, QTransform()); if (item) @@ -189,6 +194,13 @@ void QtGraphView::doClear() m_oldGraph.reset(); } +void QtGraphView::doResize() +{ + int margin = 25; + QGraphicsView* view = getView(); + view->setSceneRect(itemsBoundingRect(m_oldNodes).adjusted(-margin, -margin, margin, margin).translated(m_sceneRectOffset)); +} + std::shared_ptr QtGraphView::findNodeRecursive(const std::list>& nodes, Id tokenId) { for (const std::shared_ptr& node : nodes) diff --git a/src/app/qt/view/QtGraphView.h b/src/app/qt/view/QtGraphView.h index f884119a..4c012e6b 100644 --- a/src/app/qt/view/QtGraphView.h +++ b/src/app/qt/view/QtGraphView.h @@ -35,6 +35,8 @@ public: virtual void rebuildGraph(std::shared_ptr graph, const std::vector& nodes, const std::vector& edges); virtual void clear(); + virtual void resizeView(); + virtual Vec2i getViewSize() const; private slots: @@ -47,6 +49,7 @@ private: void doRebuildGraph(std::shared_ptr graph, const std::vector& nodes, const std::vector& edges); void doClear(); + void doResize(); std::shared_ptr findNodeRecursive(const std::list>& nodes, Id tokenId); @@ -68,6 +71,7 @@ private: QtThreadedFunctor, const std::vector&, const std::vector&> m_rebuildGraphFunctor; QtThreadedFunctor m_clearFunctor; + QtThreadedFunctor m_resizeFunctor; std::shared_ptr m_graph; std::shared_ptr m_oldGraph; diff --git a/src/app/qt/view/graphElements/QtGraphNode.cpp b/src/app/qt/view/graphElements/QtGraphNode.cpp index ec996886..f9d20de1 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.cpp +++ b/src/app/qt/view/graphElements/QtGraphNode.cpp @@ -9,6 +9,7 @@ #include "qt/graphics/QtRoundedRectItem.h" #include "qt/utility/QtDeviceScaledPixmap.h" +#include "qt/utility/QtGraphPostprocessor.h" #include "qt/view/graphElements/nodeComponents/QtGraphNodeComponent.h" #include "qt/view/graphElements/QtGraphEdge.h" @@ -106,11 +107,13 @@ bool QtGraphNode::setPosition(const Vec2i& position) return false; } -void QtGraphNode::moveTo(const Vec2i& position) +void QtGraphNode::moved() { - if (setPosition(position) && m_data) + QtGraphPostprocessor::alignNodeOnRaster(this); + + if (m_data) { - MessageGraphNodeMove(m_data->getId(), position).dispatch(); + MessageGraphNodeMove(m_data->getId(), getPosition()).dispatch(); } } diff --git a/src/app/qt/view/graphElements/QtGraphNode.h b/src/app/qt/view/graphElements/QtGraphNode.h index 00bee876..c16b02b9 100644 --- a/src/app/qt/view/graphElements/QtGraphNode.h +++ b/src/app/qt/view/graphElements/QtGraphNode.h @@ -42,7 +42,7 @@ public: virtual Vec2i getPosition() const; virtual bool setPosition(const Vec2i& position); - virtual void moveTo(const Vec2i& position); + virtual void moved(); virtual Vec2i getSize() const; virtual void setSize(const Vec2i& size); diff --git a/src/app/qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.cpp b/src/app/qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.cpp index 08a4f6b6..4c21f9c0 100644 --- a/src/app/qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.cpp +++ b/src/app/qt/view/graphElements/nodeComponents/QtGraphNodeComponentMoveable.cpp @@ -2,7 +2,6 @@ #include -#include "qt/utility/QtGraphPostprocessor.h" #include "qt/view/graphElements/QtGraphNode.h" QtGraphNodeComponentMoveable::QtGraphNodeComponentMoveable(const std::weak_ptr& graphNode) @@ -32,7 +31,7 @@ void QtGraphNodeComponentMoveable::nodeMouseMoveEvent(QGraphicsSceneMouseEvent* std::shared_ptr node = m_graphNode.lock(); if (node != NULL) { - node->moveTo(Vec2i(event->scenePos().x() - m_mouseOffset.x, event->scenePos().y() - m_mouseOffset.y)); + node->setPosition(Vec2i(event->scenePos().x() - m_mouseOffset.x, event->scenePos().y() - m_mouseOffset.y)); event->accept(); } } @@ -47,6 +46,6 @@ void QtGraphNodeComponentMoveable::nodeMouseReleaseEvent(QGraphicsSceneMouseEven std::shared_ptr node = m_graphNode.lock(); if (node != NULL) { - QtGraphPostprocessor::alignNodeOnRaster(node.get()); + node->moved(); } } diff --git a/src/lib/component/controller/GraphController.cpp b/src/lib/component/controller/GraphController.cpp index bbf9fe50..c5bb698d 100644 --- a/src/lib/component/controller/GraphController.cpp +++ b/src/lib/component/controller/GraphController.cpp @@ -78,6 +78,7 @@ void GraphController::handleMessage(MessageGraphNodeMove* message) if (node) { node->position = message->position; + getView()->resizeView(); } } diff --git a/src/lib/component/view/GraphView.h b/src/lib/component/view/GraphView.h index b507d337..b39c6e09 100644 --- a/src/lib/component/view/GraphView.h +++ b/src/lib/component/view/GraphView.h @@ -25,6 +25,8 @@ public: std::shared_ptr graph, const std::vector& nodes, const std::vector& edges) = 0; virtual void clear() = 0; + virtual void resizeView() = 0; + virtual Vec2i getViewSize() const = 0; }; diff --git a/src/lib/component/view/graphElements/GraphNode.h b/src/lib/component/view/graphElements/GraphNode.h index bb62aa07..25374707 100644 --- a/src/lib/component/view/graphElements/GraphNode.h +++ b/src/lib/component/view/graphElements/GraphNode.h @@ -29,7 +29,6 @@ public: virtual Vec2i getPosition() const = 0; virtual bool setPosition(const Vec2i& position) = 0; - virtual void moveTo(const Vec2i& position) = 0; virtual Vec2i getSize() const = 0; virtual void setSize(const Vec2i& size) = 0;