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