]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
Added ViewWidget as a common ancestor of all view widgets
[pulseview.git] / pv / view / viewport.cpp
CommitLineData
6fa02541 1/*
b3f22de0 2 * This file is part of the PulseView project.
6fa02541
JH
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
d2344534 21#include <cassert>
61d64fe9 22#include <cmath>
68b21a71 23#include <algorithm>
d2344534 24
2acdb232
JH
25#include "signal.hpp"
26#include "view.hpp"
5b5fa4da 27#include "viewitempaintparams.hpp"
2acdb232 28#include "viewport.hpp"
6fa02541 29
f65cd27b 30#include <pv/session.hpp>
e3f65ace 31
7cd5faf8 32#include <QMouseEvent>
e9c41f8c 33
c8870d9e 34using std::abs;
819f4c25
JH
35using std::max;
36using std::min;
beb897c6 37using std::none_of;
f9abf97e 38using std::shared_ptr;
68b21a71 39using std::stable_sort;
819f4c25 40using std::vector;
e3f65ace 41
cdf7bea7
JH
42namespace pv {
43namespace view {
e9c41f8c 44
cdf7bea7 45Viewport::Viewport(View &parent) :
40aca27e 46 ViewWidget(parent),
8dbbc7f0
JH
47 mouse_down_valid_(false),
48 pinch_zoom_active_(false)
6fa02541 49{
4b4f1c0d
MC
50 setAttribute(Qt::WA_AcceptTouchEvents, true);
51
64b60583
JH
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
07204819 54
8dbbc7f0 55 connect(&view_, SIGNAL(signals_moved()),
07204819 56 this, SLOT(on_signals_moved()));
d7002724
JH
57}
58
e314eca4 59void Viewport::paintEvent(QPaintEvent*)
d7002724 60{
8dbbc7f0 61 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
beb897c6
JH
62 assert(none_of(row_items.begin(), row_items.end(),
63 [](const shared_ptr<RowItem> &r) { return !r; }));
64
68b21a71
JH
65 stable_sort(row_items.begin(), row_items.end(),
66 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 67 return a->visual_v_offset() < b->visual_v_offset(); });
3e46726a 68
beb897c6
JH
69 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
70 assert(none_of(time_items.begin(), time_items.end(),
71 [](const shared_ptr<TimeItem> &t) { return !t; }));
72
64b60583 73 QPainter p(this);
ce6e73a8 74 p.setRenderHint(QPainter::Antialiasing);
3e46726a 75
36e7001d 76 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
3eb29afd 77
beb897c6
JH
78 for (const shared_ptr<TimeItem> t : time_items)
79 t->paint_back(p, pp);
eae6e30a 80 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 81 r->paint_back(p, pp);
3e46726a 82
beb897c6
JH
83 for (const shared_ptr<TimeItem> t : time_items)
84 t->paint_mid(p, pp);
eae6e30a 85 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 86 r->paint_mid(p, pp);
fe08b6e8 87
eae6e30a 88 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 89 r->paint_fore(p, pp);
beb897c6
JH
90 for (const shared_ptr<TimeItem> t : time_items)
91 t->paint_fore(p, pp);
f76af637 92
64b60583 93 p.end();
6fa02541 94}
009e1503 95
4b4f1c0d
MC
96bool Viewport::event(QEvent *event)
97{
300fc11e 98 switch (event->type()) {
4b4f1c0d
MC
99 case QEvent::TouchBegin:
100 case QEvent::TouchUpdate:
101 case QEvent::TouchEnd:
102 if (touchEvent(static_cast<QTouchEvent *>(event)))
103 return true;
104 break;
105
106 default:
107 break;
108 }
109
110 return QWidget::event(event);
111}
112
cdf7bea7 113void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
114{
115 assert(event);
1dd95c70 116
300fc11e 117 if (event->button() == Qt::LeftButton) {
8dbbc7f0
JH
118 mouse_down_point_ = event->pos();
119 mouse_down_offset_ = view_.offset();
120 mouse_down_valid_ = true;
4b4f1c0d
MC
121 }
122}
123
124void Viewport::mouseReleaseEvent(QMouseEvent *event)
125{
126 assert(event);
127
128 if (event->button() == Qt::LeftButton)
8dbbc7f0 129 mouse_down_valid_ = false;
7cd5faf8
JH
130}
131
cdf7bea7 132void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
133{
134 assert(event);
1dd95c70 135
300fc11e 136 if (event->buttons() & Qt::LeftButton) {
8dbbc7f0
JH
137 if (!mouse_down_valid_) {
138 mouse_down_point_ = event->pos();
139 mouse_down_offset_ = view_.offset();
140 mouse_down_valid_ = true;
4b4f1c0d
MC
141 }
142
8dbbc7f0
JH
143 view_.set_scale_offset(view_.scale(),
144 mouse_down_offset_ +
145 (mouse_down_point_ - event->pos()).x() *
146 view_.scale());
1dd95c70 147 }
7cd5faf8
JH
148}
149
903038a8
JH
150void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
151{
152 assert(event);
153
154 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 155 view_.zoom(2.0, event->x());
903038a8 156 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 157 view_.zoom(-2.0, event->x());
903038a8
JH
158}
159
cdf7bea7 160void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
161{
162 assert(event);
ffd5cd20
AG
163
164 if (event->orientation() == Qt::Vertical) {
165 // Vertical scrolling is interpreted as zooming in/out
8dbbc7f0 166 view_.zoom(event->delta() / 120, event->x());
23840406
AG
167 } else if (event->orientation() == Qt::Horizontal) {
168 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0
JH
169 view_.set_scale_offset(view_.scale(),
170 event->delta() * view_.scale()
171 + view_.offset());
ffd5cd20 172 }
7cd5faf8
JH
173}
174
4b4f1c0d
MC
175bool Viewport::touchEvent(QTouchEvent *event)
176{
177 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
178
179 if (touchPoints.count() != 2) {
8dbbc7f0 180 pinch_zoom_active_ = false;
4b4f1c0d
MC
181 return false;
182 }
183
184 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
185 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
186
8dbbc7f0 187 if (!pinch_zoom_active_ ||
4b4f1c0d 188 (event->touchPointStates() & Qt::TouchPointPressed)) {
8dbbc7f0
JH
189 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
190 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
191 pinch_zoom_active_ = true;
4b4f1c0d
MC
192 }
193
194 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
195 if (abs(w) >= 1.0) {
8dbbc7f0 196 double scale = (pinch_offset1_ - pinch_offset0_) / w;
4b4f1c0d
MC
197 if (scale < 0)
198 scale = -scale;
8dbbc7f0 199 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
4b4f1c0d 200 if (scale > 0)
8dbbc7f0 201 view_.set_scale_offset(scale, offset);
4b4f1c0d
MC
202 }
203
204 if (event->touchPointStates() & Qt::TouchPointReleased) {
8dbbc7f0 205 pinch_zoom_active_ = false;
4b4f1c0d
MC
206
207 if (touchPoint0.state() & Qt::TouchPointReleased) {
208 // Primary touch released
8dbbc7f0 209 mouse_down_valid_ = false;
4b4f1c0d
MC
210 } else {
211 // Update the mouse down fields so that continued
212 // dragging with the primary touch will work correctly
8dbbc7f0
JH
213 mouse_down_point_ = touchPoint0.pos().toPoint();
214 mouse_down_offset_ = view_.offset();
215 mouse_down_valid_ = true;
4b4f1c0d
MC
216 }
217 }
218
219 return true;
220}
221
07204819
JH
222void Viewport::on_signals_moved()
223{
224 update();
225}
226
cdf7bea7
JH
227} // namespace view
228} // namespace pv