From ca4ec3eab886e62c0f9d0cc91636383a74ad5d7b Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Thu, 1 Nov 2012 22:00:06 +0000 Subject: [PATCH] Added cursor dragging --- pv/view/cursor.cpp | 9 +++++++-- pv/view/cursor.h | 7 +++++++ pv/view/ruler.cpp | 39 +++++++++++++++++++++++++++++++++++++-- pv/view/ruler.h | 7 +++++++ pv/view/timemarker.cpp | 1 + pv/view/timemarker.h | 11 +++++++++++ pv/view/view.cpp | 11 +++++++++++ pv/view/view.h | 2 ++ 8 files changed, 83 insertions(+), 4 deletions(-) diff --git a/pv/view/cursor.cpp b/pv/view/cursor.cpp index 3ae5f58a..b854f2c4 100644 --- a/pv/view/cursor.cpp +++ b/pv/view/cursor.cpp @@ -43,11 +43,16 @@ Cursor::Cursor(const View &view, double time) : { } -void Cursor::paint_label(QPainter &p, const QRect &rect) +QRectF Cursor::get_label_rect(const QRect &rect) const { const float x = (_time - _view.offset()) / _view.scale(); - const QRectF r(x - Size/2, rect.height() - Size - Offset, + return QRectF(x - Size/2, rect.height() - Size - Offset, Size, Size); +} + +void Cursor::paint_label(QPainter &p, const QRect &rect) +{ + const QRectF r(get_label_rect(rect)); p.setPen(LineColour); p.setBrush(QBrush(FillColour)); diff --git a/pv/view/cursor.h b/pv/view/cursor.h index ec51b81a..3ee02005 100644 --- a/pv/view/cursor.h +++ b/pv/view/cursor.h @@ -47,6 +47,13 @@ public: Cursor(const View &view, double time); public: + /** + * Gets the marker label rectangle. + * @param rect The rectangle of the ruler client area. + * @return Returns the label rectangle. + */ + QRectF get_label_rect(const QRect &rect) const; + /** * Paints the cursor's label to the ruler. * @param p The painter to draw with. diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index 83863e61..bbafdb0f 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -19,6 +19,8 @@ */ #include "ruler.h" + +#include "cursor.h" #include "view.h" #include "viewport.h" @@ -47,7 +49,8 @@ const int Ruler::HoverArrowSize = 5; Ruler::Ruler(View &parent) : QWidget(&parent), - _view(parent) + _view(parent), + _grabbed_marker(NULL) { setMouseTracking(true); @@ -134,6 +137,38 @@ void Ruler::paintEvent(QPaintEvent *event) p.end(); } +void Ruler::mouseMoveEvent(QMouseEvent *e) +{ + if(!_grabbed_marker) + return; + + _grabbed_marker->set_time(_view.offset() + + ((double)e->x() + 0.5) * _view.scale()); +} + +void Ruler::mousePressEvent(QMouseEvent *e) +{ + if(e->buttons() & Qt::LeftButton) { + _grabbed_marker = NULL; + + if(_view.cursors_shown()) { + std::pair &cursors = + _view.cursors(); + if(cursors.first.get_label_rect( + rect()).contains(e->pos())) + _grabbed_marker = &cursors.first; + else if(cursors.second.get_label_rect( + rect()).contains(e->pos())) + _grabbed_marker = &cursors.second; + } + } +} + +void Ruler::mouseReleaseEvent(QMouseEvent *) +{ + _grabbed_marker = NULL; +} + void Ruler::draw_cursors(QPainter &p) { if(!_view.cursors_shown()) @@ -148,7 +183,7 @@ void Ruler::draw_cursors(QPainter &p) void Ruler::draw_hover_mark(QPainter &p) { const int x = _view.hover_point().x(); - if(x == -1) + if(x == -1 || _grabbed_marker) return; p.setPen(QPen(Qt::NoPen)); diff --git a/pv/view/ruler.h b/pv/view/ruler.h index 1381502d..129b70cb 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -26,6 +26,7 @@ namespace pv { namespace view { +class TimeMarker; class View; class Ruler : public QWidget @@ -47,6 +48,10 @@ public: private: void paintEvent(QPaintEvent *event); + void mouseMoveEvent(QMouseEvent *e); + void mousePressEvent(QMouseEvent *e); + void mouseReleaseEvent(QMouseEvent *); + private: void draw_cursors(QPainter &p); @@ -60,6 +65,8 @@ private slots: private: View &_view; + + TimeMarker *_grabbed_marker; }; } // namespace view diff --git a/pv/view/timemarker.cpp b/pv/view/timemarker.cpp index 49d90e39..ec0ab9a4 100644 --- a/pv/view/timemarker.cpp +++ b/pv/view/timemarker.cpp @@ -50,6 +50,7 @@ double TimeMarker::time() const void TimeMarker::set_time(double time) { _time = time; + time_changed(); } void TimeMarker::paint(QPainter &p, const QRect &rect) diff --git a/pv/view/timemarker.h b/pv/view/timemarker.h index c02dab15..08ed9389 100644 --- a/pv/view/timemarker.h +++ b/pv/view/timemarker.h @@ -23,6 +23,7 @@ #include #include +#include class QPainter; class QRect; @@ -68,6 +69,13 @@ public: */ virtual void paint(QPainter &p, const QRect &rect); + /** + * Gets the marker label rectangle. + * @param rect The rectangle of the ruler client area. + * @return Returns the label rectangle. + */ + virtual QRectF get_label_rect(const QRect &rect) const = 0; + /** * Paints the marker's label to the ruler. * @param p The painter to draw with. @@ -75,6 +83,9 @@ public: */ virtual void paint_label(QPainter &p, const QRect &rect) = 0; +signals: + void time_changed(); + protected: const View &_view; const QColor &_colour; diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 7d3dc7f6..d2f708e5 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -77,6 +77,11 @@ View::View(SigSession &session, QWidget *parent) : connect(&_session, SIGNAL(data_updated()), this, SLOT(data_updated())); + connect(&_cursors.first, SIGNAL(time_changed()), + this, SLOT(marker_time_changed())); + connect(&_cursors.second, SIGNAL(time_changed()), + this, SLOT(marker_time_changed())); + setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); setViewport(_viewport); @@ -286,5 +291,11 @@ void View::data_updated() _viewport->update(); } +void View::marker_time_changed() +{ + _ruler->update(); + _viewport->update(); +} + } // namespace view } // namespace pv diff --git a/pv/view/view.h b/pv/view/view.h index 13dd766c..f196cfe0 100644 --- a/pv/view/view.h +++ b/pv/view/view.h @@ -122,6 +122,8 @@ private slots: void data_updated(); + void marker_time_changed(); + private: SigSession &_session; -- 2.30.2