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