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