]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
MinGW: Fix a compile error due to missing std::isnan.
[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>
28290534 24#include <limits>
d2344534 25
2acdb232
JH
26#include "signal.hpp"
27#include "view.hpp"
5b5fa4da 28#include "viewitempaintparams.hpp"
2acdb232 29#include "viewport.hpp"
6fa02541 30
f65cd27b 31#include <pv/session.hpp>
e3f65ace 32
7cd5faf8 33#include <QMouseEvent>
e9c41f8c 34
c8870d9e 35using std::abs;
e9e4e5e7
JH
36using std::back_inserter;
37using std::copy;
e8b969a9 38using std::dynamic_pointer_cast;
8e21a82c 39using std::isnan;
819f4c25
JH
40using std::max;
41using std::min;
beb897c6 42using std::none_of;
28290534 43using std::numeric_limits;
f9abf97e 44using std::shared_ptr;
68b21a71 45using std::stable_sort;
819f4c25 46using std::vector;
e3f65ace 47
cdf7bea7
JH
48namespace pv {
49namespace view {
e9c41f8c 50
cdf7bea7 51Viewport::Viewport(View &parent) :
40aca27e 52 ViewWidget(parent),
28290534 53 drag_offset_(numeric_limits<double>::signaling_NaN()),
8dbbc7f0 54 pinch_zoom_active_(false)
6fa02541 55{
64b60583
JH
56 setAutoFillBackground(true);
57 setBackgroundRole(QPalette::Base);
d7002724
JH
58}
59
e9e4e5e7
JH
60shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
61{
62 const vector< shared_ptr<ViewItem> > items(this->items());
63 for (auto i = items.rbegin(); i != items.rend(); i++)
64 if ((*i)->enabled() &&
65 (*i)->hit_box_rect(rect()).contains(pt))
66 return *i;
67 return nullptr;
68}
69
e8b969a9
JH
70void Viewport::item_hover(const shared_ptr<ViewItem> &item)
71{
72 if (item)
73 setCursor(dynamic_pointer_cast<RowItem>(item) ?
74 Qt::SizeVerCursor : Qt::SizeHorCursor);
75 else
76 unsetCursor();
77}
78
28290534
JH
79void Viewport::drag()
80{
81 drag_offset_ = view_.offset();
82}
83
84void Viewport::drag_by(const QPoint &delta)
85{
86 if (isnan(drag_offset_))
87 return;
88
89 view_.set_scale_offset(view_.scale(), drag_offset_ -
90 delta.x() * view_.scale());
91}
92
93void Viewport::drag_release()
94{
95 drag_offset_ = numeric_limits<double>::signaling_NaN();
96}
97
e9e4e5e7
JH
98vector< shared_ptr<ViewItem> > Viewport::items()
99{
100 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
101 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
102 copy(time_items.begin(), time_items.end(), back_inserter(items));
103 return items;
104}
105
c9743553
JH
106bool Viewport::touch_event(QTouchEvent *event)
107{
108 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
109
110 if (touchPoints.count() != 2) {
111 pinch_zoom_active_ = false;
112 return false;
113 }
114
115 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
116 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
117
118 if (!pinch_zoom_active_ ||
119 (event->touchPointStates() & Qt::TouchPointPressed)) {
120 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
121 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
122 pinch_zoom_active_ = true;
123 }
124
125 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
126 if (abs(w) >= 1.0) {
1c91f1a2
JH
127 const double scale =
128 fabs((pinch_offset1_ - pinch_offset0_) / w);
c9743553
JH
129 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
130 if (scale > 0)
131 view_.set_scale_offset(scale, offset);
132 }
133
134 if (event->touchPointStates() & Qt::TouchPointReleased) {
135 pinch_zoom_active_ = false;
136
137 if (touchPoint0.state() & Qt::TouchPointReleased) {
138 // Primary touch released
28290534 139 drag_release();
c9743553
JH
140 } else {
141 // Update the mouse down fields so that continued
142 // dragging with the primary touch will work correctly
143 mouse_down_point_ = touchPoint0.pos().toPoint();
28290534 144 drag();
c9743553
JH
145 }
146 }
147
148 return true;
149}
150
e314eca4 151void Viewport::paintEvent(QPaintEvent*)
d7002724 152{
8dbbc7f0 153 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
beb897c6
JH
154 assert(none_of(row_items.begin(), row_items.end(),
155 [](const shared_ptr<RowItem> &r) { return !r; }));
156
68b21a71
JH
157 stable_sort(row_items.begin(), row_items.end(),
158 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
be9e7b4b 159 return a->visual_v_offset() < b->visual_v_offset(); });
3e46726a 160
beb897c6
JH
161 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
162 assert(none_of(time_items.begin(), time_items.end(),
163 [](const shared_ptr<TimeItem> &t) { return !t; }));
164
64b60583 165 QPainter p(this);
ce6e73a8 166 p.setRenderHint(QPainter::Antialiasing);
3e46726a 167
36e7001d 168 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
3eb29afd 169
beb897c6
JH
170 for (const shared_ptr<TimeItem> t : time_items)
171 t->paint_back(p, pp);
eae6e30a 172 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 173 r->paint_back(p, pp);
3e46726a 174
beb897c6
JH
175 for (const shared_ptr<TimeItem> t : time_items)
176 t->paint_mid(p, pp);
eae6e30a 177 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 178 r->paint_mid(p, pp);
fe08b6e8 179
eae6e30a 180 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 181 r->paint_fore(p, pp);
beb897c6
JH
182 for (const shared_ptr<TimeItem> t : time_items)
183 t->paint_fore(p, pp);
f76af637 184
64b60583 185 p.end();
6fa02541 186}
009e1503 187
903038a8
JH
188void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
189{
190 assert(event);
191
192 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 193 view_.zoom(2.0, event->x());
903038a8 194 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 195 view_.zoom(-2.0, event->x());
903038a8
JH
196}
197
cdf7bea7 198void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70
JH
199{
200 assert(event);
ffd5cd20
AG
201
202 if (event->orientation() == Qt::Vertical) {
203 // Vertical scrolling is interpreted as zooming in/out
8dbbc7f0 204 view_.zoom(event->delta() / 120, event->x());
23840406
AG
205 } else if (event->orientation() == Qt::Horizontal) {
206 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0
JH
207 view_.set_scale_offset(view_.scale(),
208 event->delta() * view_.scale()
209 + view_.offset());
ffd5cd20 210 }
7cd5faf8
JH
211}
212
cdf7bea7
JH
213} // namespace view
214} // namespace pv