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