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