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