]> sigrok.org Git - pulseview.git/blobdiff - pv/view/viewport.cpp
Viewport: Allow horizontal scrolling via mouse wheel tilting
[pulseview.git] / pv / view / viewport.cpp
index 2dcf9dc78f83ac740b3d3051d639ee8caa88c288..90f2b4b2a5a922d63a7931e47d9482c621f4d088 100644 (file)
@@ -21,8 +21,8 @@
 #include "view.h"
 #include "viewport.h"
 
+#include "signal.h"
 #include "../sigsession.h"
-#include "../signal.h"
 
 #include <QMouseEvent>
 
@@ -41,24 +41,28 @@ Viewport::Viewport(View &parent) :
        setMouseTracking(true);
        setAutoFillBackground(true);
        setBackgroundRole(QPalette::Base);
+
+       connect(&_view, SIGNAL(signals_moved()),
+               this, SLOT(on_signals_moved()));
 }
 
 int Viewport::get_total_height() const
 {
-       int height = 0;
-       BOOST_FOREACH(const shared_ptr<Signal> s,
-               _view.session().get_signals()) {
+       int h = 0;
+       const vector< shared_ptr<Signal> > sigs(
+               _view.session().get_signals());
+       BOOST_FOREACH(const shared_ptr<Signal> s, sigs) {
                assert(s);
-               height += View::SignalHeight;
+               h = max(s->get_v_offset() + View::SignalHeight, h);
        }
 
-       return height;
+       return h;
 }
 
 void Viewport::paintEvent(QPaintEvent *event)
 {
-       const vector< shared_ptr<Signal> > &sigs =
-               _view.session().get_signals();
+       const vector< shared_ptr<Signal> > sigs(
+               _view.session().get_signals());
 
        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);
@@ -66,17 +70,15 @@ void Viewport::paintEvent(QPaintEvent *event)
        draw_cursors_background(p);
 
        // Plot the signal
-       int offset = -_view.v_offset();
+       const int v_offset = _view.v_offset();
        BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
        {
                assert(s);
 
-               const QRect signal_rect(0, offset,
+               const QRect signal_rect(0, s->get_v_offset() - v_offset,
                        width(), View::SignalHeight);
 
                s->paint(p, signal_rect, _view.scale(), _view.offset());
-
-               offset += View::SignalHeight;
        }
 
        draw_cursors_foreground(p);
@@ -96,7 +98,7 @@ void Viewport::mouseMoveEvent(QMouseEvent *event)
 {
        assert(event);
 
-       if(event->buttons() & Qt::LeftButton)
+       if (event->buttons() & Qt::LeftButton)
        {
                _view.set_scale_offset(_view.scale(),
                        _mouse_down_offset +
@@ -113,12 +115,21 @@ void Viewport::mouseReleaseEvent(QMouseEvent *event)
 void Viewport::wheelEvent(QWheelEvent *event)
 {
        assert(event);
-       _view.zoom(event->delta() / 120, event->x());
+
+       if (event->orientation() == Qt::Vertical) {
+               // Vertical scrolling is interpreted as zooming in/out
+               _view.zoom(event->delta() / 120, event->x());
+       } else if (event->orientation() == Qt::Horizontal) {
+               // Horizontal scrolling is interpreted as moving left/right
+               _view.set_scale_offset(_view.scale(),
+                                      event->delta() * _view.scale()
+                                      + _view.offset());
+       }
 }
 
 void Viewport::draw_cursors_background(QPainter &p)
 {
-       if(!_view.cursors_shown())
+       if (!_view.cursors_shown())
                return;
 
        p.setPen(Qt::NoPen);
@@ -135,7 +146,7 @@ void Viewport::draw_cursors_background(QPainter &p)
 
 void Viewport::draw_cursors_foreground(QPainter &p)
 {
-       if(!_view.cursors_shown())
+       if (!_view.cursors_shown())
                return;
 
        const QRect r = rect();
@@ -144,5 +155,10 @@ void Viewport::draw_cursors_foreground(QPainter &p)
        cursors.second.paint(p, r);
 }
 
+void Viewport::on_signals_moved()
+{
+       update();
+}
+
 } // namespace view
 } // namespace pv