From ce5c3e8626bf7c66d1135c9a186a3e759fc65aa6 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Tue, 19 Apr 2016 23:13:41 +0200 Subject: [PATCH] ui: Added timer stopper to QtGraphicsView to stop movement if keyup event not registered --- src/lib_gui/qt/graphics/QtGraphicsView.cpp | 15 ++++++++++++++- src/lib_gui/qt/graphics/QtGraphicsView.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 232b8594..ced83cf1 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -18,6 +18,10 @@ QtGraphicsView::QtGraphicsView(QWidget* parent) m_timer = std::make_shared(this); connect(m_timer.get(), SIGNAL(timeout()), this, SLOT(update())); + + m_timerStopper = std::make_shared(this); + m_timerStopper->setSingleShot(true); + connect(m_timerStopper.get(), SIGNAL(timeout()), this, SLOT(stopTimer())); } float QtGraphicsView::getZoomFactor() const @@ -54,6 +58,8 @@ void QtGraphicsView::mouseReleaseEvent(QMouseEvent *event) void QtGraphicsView::keyPressEvent(QKeyEvent* event) { + bool moved = moves(); + switch (event->key()) { case Qt::Key_W: @@ -80,10 +86,12 @@ void QtGraphicsView::keyPressEvent(QKeyEvent* event) return; } - if (moves()) + if (!moved && moves()) { m_timer->start(20); } + + m_timerStopper->start(1000); } void QtGraphicsView::keyReleaseEvent(QKeyEvent* event) @@ -183,6 +191,11 @@ void QtGraphicsView::update() } } +void QtGraphicsView::stopTimer() +{ + m_timer->stop(); +} + bool QtGraphicsView::moves() const { return m_up || m_down || m_left || m_right; diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index 1fc6482b..01b74e1a 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -34,6 +34,7 @@ signals: private slots: void update(); + void stopTimer(); private: bool moves() const; @@ -53,6 +54,7 @@ private: bool m_shift; std::shared_ptr m_timer; + std::shared_ptr m_timerStopper; }; #endif // QT_GRAPHICS_VIEW_H