From: Joel Holdsworth Date: Sun, 28 Oct 2012 18:00:50 +0000 (+0000) Subject: Added cursors X-Git-Tag: pulseview-0.1.0~231 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f76af6375b8aea6b7edb2d6ee838e1589c3490f3;p=pulseview.git Added cursors --- diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index c5f194f2..83863e61 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -31,6 +31,8 @@ #include #include +using namespace std; + namespace pv { namespace view { @@ -123,12 +125,26 @@ void Ruler::paintEvent(QPaintEvent *event) division++; } + // Draw the cursors + draw_cursors(p); + // Draw the hover mark draw_hover_mark(p); p.end(); } +void Ruler::draw_cursors(QPainter &p) +{ + if(!_view.cursors_shown()) + return; + + const QRect r = rect(); + pair &cursors = _view.cursors(); + cursors.first.paint_label(p, r); + cursors.second.paint_label(p, r); +} + void Ruler::draw_hover_mark(QPainter &p) { const int x = _view.hover_point().x(); diff --git a/pv/view/ruler.h b/pv/view/ruler.h index 1bda0642..1381502d 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -48,6 +48,8 @@ private: void paintEvent(QPaintEvent *event); private: + void draw_cursors(QPainter &p); + /** * Draw a hover arrow under the cursor position. */ diff --git a/pv/view/view.cpp b/pv/view/view.cpp index 54871b5e..7d3dc7f6 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -53,6 +53,8 @@ const int View::MaxScrollValue = INT_MAX / 2; const int View::SignalHeight = 50; +const QColor View::CursorAreaColour(220, 231, 243); + View::View(SigSession &session, QWidget *parent) : QAbstractScrollArea(parent), _session(session), @@ -63,6 +65,9 @@ View::View(SigSession &session, QWidget *parent) : _scale(1e-6), _offset(0), _v_offset(0), + _show_cursors(false), + _cursors(pair(Cursor(*this, 0.0), + Cursor(*this, 1.0))), _hover_point(-1, -1) { connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), @@ -128,6 +133,23 @@ void View::set_scale_offset(double scale, double offset) _viewport->update(); } +bool View::cursors_shown() const +{ + return _show_cursors; +} + +void View::show_cursors(bool show) +{ + _show_cursors = show; + _ruler->update(); + _viewport->update(); +} + +std::pair& View::cursors() +{ + return _cursors; +} + const QPoint& View::hover_point() const { return _hover_point; diff --git a/pv/view/view.h b/pv/view/view.h index ac2835a4..13dd766c 100644 --- a/pv/view/view.h +++ b/pv/view/view.h @@ -23,8 +23,12 @@ #include +#include + #include +#include "cursor.h" + namespace pv { class SigSession; @@ -50,6 +54,8 @@ private: public: static const int SignalHeight; + static const QColor CursorAreaColour; + public: explicit View(SigSession &session, QWidget *parent = 0); @@ -77,6 +83,21 @@ public: */ void set_scale_offset(double scale, double offset); + /** + * Returns true if cursors are displayed. false otherwise. + */ + bool cursors_shown() const; + + /** + * Shows or hides the cursors. + */ + void show_cursors(bool show = true); + + /** + * Returns a reference to the pair of cursors. + */ + std::pair& cursors(); + const QPoint& hover_point() const; signals: @@ -118,6 +139,9 @@ private: int _v_offset; + bool _show_cursors; + std::pair _cursors; + QPoint _hover_point; }; diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index 99e08b27..2dcf9dc7 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -63,6 +63,8 @@ void Viewport::paintEvent(QPaintEvent *event) QPainter p(this); p.setRenderHint(QPainter::Antialiasing); + draw_cursors_background(p); + // Plot the signal int offset = -_view.v_offset(); BOOST_FOREACH(const shared_ptr s, sigs) @@ -77,6 +79,8 @@ void Viewport::paintEvent(QPaintEvent *event) offset += View::SignalHeight; } + draw_cursors_foreground(p); + p.end(); } @@ -112,5 +116,33 @@ void Viewport::wheelEvent(QWheelEvent *event) _view.zoom(event->delta() / 120, event->x()); } +void Viewport::draw_cursors_background(QPainter &p) +{ + if(!_view.cursors_shown()) + return; + + p.setPen(Qt::NoPen); + p.setBrush(QBrush(View::CursorAreaColour)); + + const pair &c = _view.cursors(); + const float x1 = (c.first.time() - _view.offset()) / _view.scale(); + const float x2 = (c.second.time() - _view.offset()) / _view.scale(); + const int l = (int)max(min(x1, x2), 0.0f); + const int r = (int)min(max(x1, x2), (float)width()); + + p.drawRect(l, 0, r - l, height()); +} + +void Viewport::draw_cursors_foreground(QPainter &p) +{ + if(!_view.cursors_shown()) + return; + + const QRect r = rect(); + pair &cursors = _view.cursors(); + cursors.first.paint(p, r); + cursors.second.paint(p, r); +} + } // namespace view } // namespace pv diff --git a/pv/view/viewport.h b/pv/view/viewport.h index a33d4045..49b97e3b 100644 --- a/pv/view/viewport.h +++ b/pv/view/viewport.h @@ -51,6 +51,11 @@ private: void mouseReleaseEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); +private: + void draw_cursors_background(QPainter &p); + + void draw_cursors_foreground(QPainter &p); + private: View &_view;