]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
RowItem: Renamed get_v_offset to v_offset
[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 <cassert>
22 #include <cmath>
23
24 #include "view.h"
25 #include "viewport.h"
26
27 #include "signal.h"
28 #include "../sigsession.h"
29
30 #include <QMouseEvent>
31
32 using std::abs;
33 using std::max;
34 using std::min;
35 using std::shared_ptr;
36 using std::vector;
37
38 namespace pv {
39 namespace view {
40
41 Viewport::Viewport(View &parent) :
42         QWidget(&parent),
43         _view(parent),
44         _mouse_down_valid(false),
45         _pinch_zoom_active(false)
46 {
47         setAttribute(Qt::WA_AcceptTouchEvents, true);
48
49         setMouseTracking(true);
50         setAutoFillBackground(true);
51         setBackgroundRole(QPalette::Base);
52
53         connect(&_view.session(), SIGNAL(signals_changed()),
54                 this, SLOT(on_signals_changed()));
55
56         connect(&_view, SIGNAL(signals_moved()),
57                 this, SLOT(on_signals_moved()));
58
59         // Trigger the initial event manually. The default device has signals
60         // which were created before this object came into being
61         on_signals_changed();
62 }
63
64 int Viewport::get_total_height() const
65 {
66         int h = 0;
67         const vector< shared_ptr<Trace> > traces(_view.get_traces());
68         for (const shared_ptr<Trace> t : traces) {
69                 assert(t);
70                 h = max(t->v_offset() + View::SignalHeight, h);
71         }
72
73         return h;
74 }
75
76 void Viewport::paintEvent(QPaintEvent*)
77 {
78         const vector< shared_ptr<Trace> > traces(_view.get_traces());
79
80         QPainter p(this);
81         p.setRenderHint(QPainter::Antialiasing);
82
83         if (_view.cursors_shown())
84                 _view.cursors().draw_viewport_background(p, rect());
85
86         // Plot the signal
87         for (const shared_ptr<Trace> t : traces)
88         {
89                 assert(t);
90                 t->paint_back(p, 0, width());
91         }
92
93         for (const shared_ptr<Trace> t : traces)
94                 t->paint_mid(p, 0, width());
95
96         for (const shared_ptr<Trace> t : traces)
97                 t->paint_fore(p, 0, width());
98
99         if (_view.cursors_shown())
100                 _view.cursors().draw_viewport_foreground(p, rect());
101
102         p.end();
103 }
104
105 bool Viewport::event(QEvent *event)
106 {
107         switch (event->type()) {
108         case QEvent::TouchBegin:
109         case QEvent::TouchUpdate:
110         case QEvent::TouchEnd:
111                 if (touchEvent(static_cast<QTouchEvent *>(event)))
112                         return true;
113                 break;
114
115         default:
116                 break;
117         }
118
119         return QWidget::event(event);
120 }
121
122 void Viewport::mousePressEvent(QMouseEvent *event)
123 {
124         assert(event);
125
126         if (event->button() == Qt::LeftButton) {
127                 _mouse_down_point = event->pos();
128                 _mouse_down_offset = _view.offset();
129                 _mouse_down_valid = true;
130         }
131 }
132
133 void Viewport::mouseReleaseEvent(QMouseEvent *event)
134 {
135         assert(event);
136
137         if (event->button() == Qt::LeftButton)
138                 _mouse_down_valid = false;
139 }
140
141 void Viewport::mouseMoveEvent(QMouseEvent *event)
142 {
143         assert(event);
144
145         if (event->buttons() & Qt::LeftButton) {
146                 if (!_mouse_down_valid) {
147                         _mouse_down_point = event->pos();
148                         _mouse_down_offset = _view.offset();
149                         _mouse_down_valid = true;
150                 }
151
152                 _view.set_scale_offset(_view.scale(),
153                         _mouse_down_offset +
154                         (_mouse_down_point - event->pos()).x() *
155                         _view.scale());
156         }
157 }
158
159 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
160 {
161         assert(event);
162
163         if (event->buttons() & Qt::LeftButton)
164                 _view.zoom(2.0, event->x());
165         else if (event->buttons() & Qt::RightButton)
166                 _view.zoom(-2.0, event->x());
167 }
168
169 void Viewport::wheelEvent(QWheelEvent *event)
170 {
171         assert(event);
172
173         if (event->orientation() == Qt::Vertical) {
174                 // Vertical scrolling is interpreted as zooming in/out
175                 _view.zoom(event->delta() / 120, event->x());
176         } else if (event->orientation() == Qt::Horizontal) {
177                 // Horizontal scrolling is interpreted as moving left/right
178                 _view.set_scale_offset(_view.scale(),
179                                        event->delta() * _view.scale()
180                                        + _view.offset());
181         }
182 }
183
184 bool Viewport::touchEvent(QTouchEvent *event)
185 {
186         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
187
188         if (touchPoints.count() != 2) {
189                 _pinch_zoom_active = false;
190                 return false;
191         }
192
193         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
194         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
195
196         if (!_pinch_zoom_active ||
197             (event->touchPointStates() & Qt::TouchPointPressed)) {
198                 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
199                 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
200                 _pinch_zoom_active = true;
201         }
202
203         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
204         if (abs(w) >= 1.0) {
205                 double scale = (_pinch_offset1 - _pinch_offset0) / w;
206                 if (scale < 0)
207                         scale = -scale;
208                 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
209                 if (scale > 0)
210                         _view.set_scale_offset(scale, offset);
211         }
212
213         if (event->touchPointStates() & Qt::TouchPointReleased) {
214                 _pinch_zoom_active = false;
215
216                 if (touchPoint0.state() & Qt::TouchPointReleased) {
217                         // Primary touch released
218                         _mouse_down_valid = false;
219                 } else {
220                         // Update the mouse down fields so that continued
221                         // dragging with the primary touch will work correctly
222                         _mouse_down_point = touchPoint0.pos().toPoint();
223                         _mouse_down_offset = _view.offset();
224                         _mouse_down_valid = true;
225                 }
226         }
227
228         return true;
229 }
230
231 void Viewport::on_signals_changed()
232 {
233         const vector< shared_ptr<Trace> > traces(_view.get_traces());
234         for (shared_ptr<Trace> t : traces) {
235                 assert(t);
236                 connect(t.get(), SIGNAL(visibility_changed()),
237                         this, SLOT(update()));
238         }
239 }
240
241 void Viewport::on_signals_moved()
242 {
243         update();
244 }
245
246 } // namespace view
247 } // namespace pv