]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
0e0ae8ae4ad0807f5b99abe1f31ca458f437af94
[pulseview.git] / pv / view / viewport.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "view.h"
22 #include "viewport.h"
23
24 #include "signal.h"
25 #include "../sigsession.h"
26
27 #include <QMouseEvent>
28
29 #include <boost/foreach.hpp>
30
31 using namespace boost;
32 using namespace std;
33
34 namespace pv {
35 namespace view {
36
37 Viewport::Viewport(View &parent) :
38         QWidget(&parent),
39         _view(parent)
40 {
41         setMouseTracking(true);
42         setAutoFillBackground(true);
43         setBackgroundRole(QPalette::Base);
44
45         connect(&_view, SIGNAL(signals_moved()),
46                 this, SLOT(on_signals_moved()));
47 }
48
49 int Viewport::get_total_height() const
50 {
51         int h = 0;
52         const vector< shared_ptr<Trace> > traces(_view.get_traces());
53         BOOST_FOREACH(const shared_ptr<Trace> t, traces) {
54                 assert(t);
55                 h = max(t->get_v_offset() + View::SignalHeight, h);
56         }
57
58         return h;
59 }
60
61 void Viewport::paintEvent(QPaintEvent*)
62 {
63         const vector< shared_ptr<Trace> > traces(_view.get_traces());
64
65         QPainter p(this);
66         p.setRenderHint(QPainter::Antialiasing);
67
68         if (_view.cursors_shown())
69                 _view.cursors().draw_viewport_background(p, rect());
70
71         // Plot the signal
72         const int v_offset = _view.v_offset();
73         BOOST_FOREACH(const shared_ptr<Trace> t, traces)
74         {
75                 assert(t);
76                 t->paint(p, t->get_v_offset() - v_offset, 0, width(),
77                         _view.scale(), _view.offset());
78         }
79
80         if (_view.cursors_shown())
81                 _view.cursors().draw_viewport_foreground(p, rect());
82
83         p.end();
84 }
85
86 void Viewport::mousePressEvent(QMouseEvent *event)
87 {
88         assert(event);
89
90         _mouse_down_point = event->pos();
91         _mouse_down_offset = _view.offset();
92 }
93
94 void Viewport::mouseMoveEvent(QMouseEvent *event)
95 {
96         assert(event);
97
98         if (event->buttons() & Qt::LeftButton)
99         {
100                 _view.set_scale_offset(_view.scale(),
101                         _mouse_down_offset +
102                         (_mouse_down_point - event->pos()).x() *
103                         _view.scale());
104         }
105 }
106
107 void Viewport::wheelEvent(QWheelEvent *event)
108 {
109         assert(event);
110
111         if (event->orientation() == Qt::Vertical) {
112                 // Vertical scrolling is interpreted as zooming in/out
113                 _view.zoom(event->delta() / 120, event->x());
114         } else if (event->orientation() == Qt::Horizontal) {
115                 // Horizontal scrolling is interpreted as moving left/right
116                 _view.set_scale_offset(_view.scale(),
117                                        event->delta() * _view.scale()
118                                        + _view.offset());
119         }
120 }
121
122 void Viewport::on_signals_moved()
123 {
124         update();
125 }
126
127 } // namespace view
128 } // namespace pv