]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
Signal: Save and load signal names as UTF-8 strings.
[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"
27#include "viewport.hpp"
6fa02541 28
f65cd27b 29#include <pv/session.hpp>
e3f65ace 30
7cd5faf8 31#include <QMouseEvent>
e9c41f8c 32
c8870d9e 33using std::abs;
819f4c25
JH
34using std::max;
35using std::min;
f9abf97e 36using std::shared_ptr;
68b21a71 37using std::stable_sort;
819f4c25 38using std::vector;
e3f65ace 39
cdf7bea7
JH
40namespace pv {
41namespace view {
e9c41f8c 42
cdf7bea7 43Viewport::Viewport(View &parent) :
64b60583 44 QWidget(&parent),
8dbbc7f0
JH
45 view_(parent),
46 mouse_down_valid_(false),
47 pinch_zoom_active_(false)
6fa02541 48{
4b4f1c0d
MC
49 setAttribute(Qt::WA_AcceptTouchEvents, true);
50
d7002724 51 setMouseTracking(true);
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());
68b21a71
JH
62 stable_sort(row_items.begin(), row_items.end(),
63 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 64 return a->visual_v_offset() < b->visual_v_offset(); });
3e46726a 65
64b60583 66 QPainter p(this);
ce6e73a8 67 p.setRenderHint(QPainter::Antialiasing);
3e46726a 68
8dbbc7f0
JH
69 if (view_.cursors_shown())
70 view_.cursors().draw_viewport_background(p, rect());
f76af637 71
3e46726a 72 // Plot the signal
eae6e30a 73 for (const shared_ptr<RowItem> r : row_items)
3e46726a 74 {
eae6e30a
JH
75 assert(r);
76 r->paint_back(p, 0, width());
3e46726a
JH
77 }
78
eae6e30a
JH
79 for (const shared_ptr<RowItem> r : row_items)
80 r->paint_mid(p, 0, width());
fe08b6e8 81
eae6e30a
JH
82 for (const shared_ptr<RowItem> r : row_items)
83 r->paint_fore(p, 0, width());
fe08b6e8 84
8dbbc7f0
JH
85 if (view_.cursors_shown())
86 view_.cursors().draw_viewport_foreground(p, rect());
f76af637 87
64b60583 88 p.end();
6fa02541 89}
009e1503 90
4b4f1c0d
MC
91bool Viewport::event(QEvent *event)
92{
300fc11e 93 switch (event->type()) {
4b4f1c0d
MC
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
cdf7bea7 108void Viewport::mousePressEvent(QMouseEvent *event)
7cd5faf8
JH
109{
110 assert(event);
1dd95c70 111
300fc11e 112 if (event->button() == Qt::LeftButton) {
8dbbc7f0
JH
113 mouse_down_point_ = event->pos();
114 mouse_down_offset_ = view_.offset();
115 mouse_down_valid_ = true;
4b4f1c0d
MC
116 }
117}
118
119void Viewport::mouseReleaseEvent(QMouseEvent *event)
120{
121 assert(event);
122
123 if (event->button() == Qt::LeftButton)
8dbbc7f0 124 mouse_down_valid_ = false;
7cd5faf8
JH
125}
126
cdf7bea7 127void Viewport::mouseMoveEvent(QMouseEvent *event)
7cd5faf8
JH
128{
129 assert(event);
1dd95c70 130
300fc11e 131 if (event->buttons() & Qt::LeftButton) {
8dbbc7f0
JH
132 if (!mouse_down_valid_) {
133 mouse_down_point_ = event->pos();
134 mouse_down_offset_ = view_.offset();
135 mouse_down_valid_ = true;
4b4f1c0d
MC
136 }
137
8dbbc7f0
JH
138 view_.set_scale_offset(view_.scale(),
139 mouse_down_offset_ +
140 (mouse_down_point_ - event->pos()).x() *
141 view_.scale());
1dd95c70 142 }
7cd5faf8
JH
143}
144
903038a8
JH
145void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
146{
147 assert(event);
148
149 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 150 view_.zoom(2.0, event->x());
903038a8 151 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 152 view_.zoom(-2.0, event->x());
903038a8
JH
153}
154
cdf7bea7 155void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
156{
157 assert(event);
ffd5cd20
AG
158
159 if (event->orientation() == Qt::Vertical) {
160 // Vertical scrolling is interpreted as zooming in/out
8dbbc7f0 161 view_.zoom(event->delta() / 120, event->x());
23840406
AG
162 } else if (event->orientation() == Qt::Horizontal) {
163 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0
JH
164 view_.set_scale_offset(view_.scale(),
165 event->delta() * view_.scale()
166 + view_.offset());
ffd5cd20 167 }
7cd5faf8
JH
168}
169
4b4f1c0d
MC
170bool Viewport::touchEvent(QTouchEvent *event)
171{
172 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
173
174 if (touchPoints.count() != 2) {
8dbbc7f0 175 pinch_zoom_active_ = false;
4b4f1c0d
MC
176 return false;
177 }
178
179 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
180 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
181
8dbbc7f0 182 if (!pinch_zoom_active_ ||
4b4f1c0d 183 (event->touchPointStates() & Qt::TouchPointPressed)) {
8dbbc7f0
JH
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;
4b4f1c0d
MC
187 }
188
189 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
190 if (abs(w) >= 1.0) {
8dbbc7f0 191 double scale = (pinch_offset1_ - pinch_offset0_) / w;
4b4f1c0d
MC
192 if (scale < 0)
193 scale = -scale;
8dbbc7f0 194 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
4b4f1c0d 195 if (scale > 0)
8dbbc7f0 196 view_.set_scale_offset(scale, offset);
4b4f1c0d
MC
197 }
198
199 if (event->touchPointStates() & Qt::TouchPointReleased) {
8dbbc7f0 200 pinch_zoom_active_ = false;
4b4f1c0d
MC
201
202 if (touchPoint0.state() & Qt::TouchPointReleased) {
203 // Primary touch released
8dbbc7f0 204 mouse_down_valid_ = false;
4b4f1c0d
MC
205 } else {
206 // Update the mouse down fields so that continued
207 // dragging with the primary touch will work correctly
8dbbc7f0
JH
208 mouse_down_point_ = touchPoint0.pos().toPoint();
209 mouse_down_offset_ = view_.offset();
210 mouse_down_valid_ = true;
4b4f1c0d
MC
211 }
212 }
213
214 return true;
215}
216
07204819
JH
217void Viewport::on_signals_moved()
218{
219 update();
220}
221
cdf7bea7
JH
222} // namespace view
223} // namespace pv