From d86834b0c2ecc2df21d70a7db8a2a9d949dccc74 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 13 Jul 2023 17:16:20 +0200 Subject: [PATCH] Support dragging the picture around when we are zoomed. --- video_widget.cpp | 35 +++++++++++++++++++++++++++++++++++ video_widget.h | 8 ++++++++ 2 files changed, 43 insertions(+) diff --git a/video_widget.cpp b/video_widget.cpp index 3cb5fa1..1d899d2 100644 --- a/video_widget.cpp +++ b/video_widget.cpp @@ -32,6 +32,7 @@ extern "C" { #include #include +#include using namespace std; using namespace std::chrono; @@ -493,6 +494,40 @@ void VideoWidget::wheelEvent(QWheelEvent *event) fixup_zoom_matrix(); } +void VideoWidget::mousePressEvent(QMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) { + dragging = true; + last_drag_x = e->position().x(); + last_drag_y = e->position().y(); + } +} + +void VideoWidget::mouseReleaseEvent(QMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) { + dragging = false; + } +} + +void VideoWidget::mouseMoveEvent(QMouseEvent *e) +{ + if (!dragging) { + return; + } + float dx = (e->position().x() - last_drag_x) / width(); + float dy = (e->position().y() - last_drag_y) / height(); + + //zoom_matrix[6] += dx * zoom_matrix[0]; + //zoom_matrix[7] += dy * zoom_matrix[4]; + zoom_matrix[6] += dx; + zoom_matrix[7] -= dy; + fixup_zoom_matrix(); + + last_drag_x = e->position().x(); + last_drag_y = e->position().y(); +} + // Normalize the matrix so that we never get skew or similar, // and also never can zoom or pan too far out. void VideoWidget::fixup_zoom_matrix() diff --git a/video_widget.h b/video_widget.h index 801d5cd..56cf928 100644 --- a/video_widget.h +++ b/video_widget.h @@ -39,6 +39,11 @@ public: void paintGL() override; void wheelEvent(QWheelEvent *event) override; + // For dragging. + void mousePressEvent(QMouseEvent *e); + void mouseReleaseEvent(QMouseEvent *e); + void mouseMoveEvent(QMouseEvent *e); + signals: void position_changed(uint64_t pos); @@ -92,6 +97,9 @@ private: std::string pathname; + bool dragging = false; + float last_drag_x, last_drag_y; + void producer_thread_func(); bool play_video(const std::string &pathname); void internal_rewind(); -- 2.39.2