From cdde712a2c90f478884b2b326ad6321b6fa79d46 Mon Sep 17 00:00:00 2001 From: Manuel Dobusch Date: Thu, 26 Jan 2017 16:33:49 +0100 Subject: [PATCH] ui: graph zoom buttons Added simple zoom buttons to graph --- src/lib_gui/qt/graphics/QtGraphicsView.cpp | 50 ++++++++----------- src/lib_gui/qt/graphics/QtGraphicsView.h | 8 ++- src/lib_gui/qt/view/QtGraphView.cpp | 58 ++++++++++++++++++++++ src/lib_gui/qt/view/QtGraphView.h | 15 ++++++ 4 files changed, 101 insertions(+), 30 deletions(-) diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 168d8a75..1fd18431 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -103,6 +103,26 @@ void QtGraphicsView::ensureVisibleAnimated(const QRectF& rect, int xmargin, int move->start(); } +void QtGraphicsView::setMouseWheelCallback(const std::function& callback) +{ + m_mouseWheelCallback = callback; +} + +void QtGraphicsView::updateZoom(float delta) +{ + float factor = 1.0f + 0.001f * delta; + + if (factor <= 0.0f) + { + factor = 0.000001; + } + + double newZoom = m_zoomFactor * factor; + m_zoomFactor = qBound(0.1, newZoom, 100.0); + + updateTransform(); +} + void QtGraphicsView::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && !itemAt(event->pos())) @@ -193,20 +213,7 @@ void QtGraphicsView::keyReleaseEvent(QKeyEvent* event) void QtGraphicsView::wheelEvent(QWheelEvent* event) { - bool zoomDefault = ApplicationSettings::getInstance()->getControlsGraphZoomOnMouseWheel(); - bool shiftPressed = event->modifiers() == Qt::ShiftModifier; - - if (zoomDefault != shiftPressed) - { - if (event->delta() != 0.0f) - { - updateZoom(event->delta()); - } - } - else - { - QGraphicsView::wheelEvent(event); - } + m_mouseWheelCallback(event); } void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event) @@ -381,18 +388,3 @@ void QtGraphicsView::updateTransform() float zoomFactor = m_appZoomFactor * m_zoomFactor; setTransform(QTransform(zoomFactor, 0, 0, zoomFactor, 0, 0)); } - -void QtGraphicsView::updateZoom(float delta) -{ - float factor = 1.0f + 0.001 * delta; - - if (factor <= 0.0f) - { - factor = 0.000001; - } - - double newZoom = m_zoomFactor * factor; - m_zoomFactor = qBound(0.1, newZoom, 100.0); - - updateTransform(); -} diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index 2c845b0b..4d5761a3 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -7,6 +7,8 @@ #include +#include + class QTimer; class QtGraphNode; @@ -25,6 +27,9 @@ public: void ensureVisibleAnimated(const QRectF& rect, int xmargin = 50, int ymargin = 50); + void setMouseWheelCallback(const std::function& callback); + void updateZoom(float delta); + protected: void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); @@ -50,7 +55,6 @@ private: bool moves() const; void updateTransform(); - void updateZoom(float delta); QPoint m_last; @@ -70,6 +74,8 @@ private: QAction* m_exportGraphAction; QAction* m_copyNodeNameAction; + + std::function m_mouseWheelCallback; }; #endif // QT_GRAPHICS_VIEW_H diff --git a/src/lib_gui/qt/view/QtGraphView.cpp b/src/lib_gui/qt/view/QtGraphView.cpp index a1e84b1c..095f17c3 100644 --- a/src/lib_gui/qt/view/QtGraphView.cpp +++ b/src/lib_gui/qt/view/QtGraphView.cpp @@ -40,6 +40,10 @@ QtGraphView::QtGraphView(ViewLayout* viewLayout) , m_refreshFunctor(std::bind(&QtGraphView::doRefreshView, this)) , m_focusInFunctor(std::bind(&QtGraphView::doFocusIn, this, std::placeholders::_1)) , m_focusOutFunctor(std::bind(&QtGraphView::doFocusOut, this, std::placeholders::_1)) + , m_zoomInButton() + , m_zoomOutButton() + , m_zoomInButtonSpeed(20.0f) + , m_zoomOutButtonSpeed(-20.0f) { } @@ -68,9 +72,26 @@ void QtGraphView::initView() view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); view->viewport()->setCursor(Qt::ArrowCursor); + getView()->setMouseWheelCallback(std::bind(&QtGraphView::onMouseWheelEvent, this, std::placeholders::_1)); widget->layout()->addWidget(view); + m_zoomInButton = new QPushButton(widget); + m_zoomInButton->setObjectName("zoom_in_button"); + m_zoomInButton->setText("+"); + m_zoomInButton->setGeometry(QRect(5, 5, 25, 25)); + m_zoomInButton->setAutoRepeat(true); + m_zoomInButton->setToolTip("Zoom in (Shift + Mousewheel forward)"); + connect(m_zoomInButton, SIGNAL(pressed()), this, SLOT(zoomInPressed())); + + m_zoomOutButton = new QPushButton(widget); + m_zoomOutButton->setObjectName("zoom_out_button"); + m_zoomOutButton->setText("-"); + m_zoomOutButton->setGeometry(QRect(5, 34, 25, 25)); + m_zoomOutButton->setAutoRepeat(true); + m_zoomOutButton->setToolTip("Zoom out (Shift + Mousewheel back)"); + connect(m_zoomOutButton, SIGNAL(pressed()), this, SLOT(zoomOutPressed())); + connect(view, SIGNAL(emptySpaceClicked()), this, SLOT(clickedInEmptySpace())); m_scrollSpeedChangeListenerHorizontal.setScrollBar(view->horizontalScrollBar()); @@ -147,6 +168,34 @@ void QtGraphView::clickedInEmptySpace() } } +void QtGraphView::zoomInPressed() +{ + zoom(m_zoomInButtonSpeed); +} + +void QtGraphView::zoomOutPressed() +{ + zoom(m_zoomOutButtonSpeed); +} + +void QtGraphView::onMouseWheelEvent(QWheelEvent* event) +{ + bool zoomDefault = ApplicationSettings::getInstance()->getControlsGraphZoomOnMouseWheel(); + bool shiftPressed = event->modifiers() == Qt::ShiftModifier; + + if (zoomDefault != shiftPressed) + { + if (event->delta() != 0.0f) + { + zoom(event->delta()); + } + } + else + { + // QGraphicsView::wheelEvent(event); + } +} + void QtGraphView::switchToNewGraphData() { m_oldGraph = m_graph; @@ -723,3 +772,12 @@ void QtGraphView::doFocusOut(const std::vector& tokenIds) } } } + +void QtGraphView::zoom(const float delta) +{ + QtGraphicsView* view = getView(); + if (view != NULL) + { + view->updateZoom(delta); + } +} diff --git a/src/lib_gui/qt/view/QtGraphView.h b/src/lib_gui/qt/view/QtGraphView.h index 99fe4362..0eaf1b20 100644 --- a/src/lib_gui/qt/view/QtGraphView.h +++ b/src/lib_gui/qt/view/QtGraphView.h @@ -6,6 +6,8 @@ #include #include +#include + #include "component/view/GraphView.h" #include "qt/utility/QtScrollSpeedChangeListener.h" #include "qt/utility/QtThreadedFunctor.h" @@ -53,6 +55,11 @@ private slots: void finishedTransition(); void clickedInEmptySpace(); + void zoomInPressed(); + void zoomOutPressed(); + + void onMouseWheelEvent(QWheelEvent* event); + private: void switchToNewGraphData(); @@ -70,6 +77,8 @@ private: void doFocusIn(const std::vector& tokenIds); void doFocusOut(const std::vector& tokenIds); + void zoom(const float delta); + std::shared_ptr findNodeRecursive(const std::list>& nodes, Id tokenId); std::shared_ptr createNodeRecursive( @@ -118,6 +127,12 @@ private: QtScrollSpeedChangeListener m_scrollSpeedChangeListenerHorizontal; QtScrollSpeedChangeListener m_scrollSpeedChangeListenerVertical; + + QPushButton* m_zoomInButton; + QPushButton* m_zoomOutButton; + + float m_zoomInButtonSpeed; + float m_zoomOutButtonSpeed; }; #endif // QT_GRAPH_VIEW_H