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