]> sigrok.org Git - pulseview.git/blame - pv/view/viewport.cpp
Prefer std::make_shared().
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6fa02541
JH
18 */
19
d2344534 20#include <cassert>
61d64fe9 21#include <cmath>
68b21a71 22#include <algorithm>
28290534 23#include <limits>
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
f4e57597
SA
34#include <QDebug>
35
c8870d9e 36using std::abs;
e9e4e5e7
JH
37using std::back_inserter;
38using std::copy;
e8b969a9 39using std::dynamic_pointer_cast;
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 48namespace pv {
f4e57597
SA
49namespace views {
50namespace TraceView {
e9c41f8c 51
cdf7bea7 52Viewport::Viewport(View &parent) :
40aca27e 53 ViewWidget(parent),
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{
cbf0f87e 62 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
e9e4e5e7
JH
63 const vector< shared_ptr<ViewItem> > items(this->items());
64 for (auto i = items.rbegin(); i != items.rend(); i++)
65 if ((*i)->enabled() &&
cbf0f87e 66 (*i)->hit_box_rect(pp).contains(pt))
e9e4e5e7
JH
67 return *i;
68 return nullptr;
69}
70
e8b969a9
JH
71void Viewport::item_hover(const shared_ptr<ViewItem> &item)
72{
d75c5d12 73 if (item && item->is_draggable())
e8b969a9
JH
74 setCursor(dynamic_pointer_cast<RowItem>(item) ?
75 Qt::SizeVerCursor : Qt::SizeHorCursor);
76 else
77 unsetCursor();
78}
79
28290534
JH
80void Viewport::drag()
81{
82 drag_offset_ = view_.offset();
838d0522 83 drag_v_offset_ = view_.owner_visual_v_offset();
28290534
JH
84}
85
86void Viewport::drag_by(const QPoint &delta)
87{
60d9b99a 88 if (drag_offset_ == boost::none)
28290534
JH
89 return;
90
60d9b99a
JS
91 view_.set_scale_offset(view_.scale(),
92 (*drag_offset_ - delta.x() * view_.scale()));
838d0522
SA
93
94 view_.set_v_offset(-drag_v_offset_ - delta.y());
28290534
JH
95}
96
97void Viewport::drag_release()
98{
60d9b99a 99 drag_offset_ = boost::none;
28290534
JH
100}
101
e9e4e5e7
JH
102vector< shared_ptr<ViewItem> > Viewport::items()
103{
cc88566c 104 vector< shared_ptr<ViewItem> > items;
e94f43c6
JH
105 const std::vector< shared_ptr<ViewItem> > view_items(
106 view_.list_by_type<ViewItem>());
107 copy(view_items.begin(), view_items.end(), back_inserter(items));
e9e4e5e7
JH
108 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
109 copy(time_items.begin(), time_items.end(), back_inserter(items));
110 return items;
111}
112
c9743553
JH
113bool Viewport::touch_event(QTouchEvent *event)
114{
115 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
116
117 if (touchPoints.count() != 2) {
118 pinch_zoom_active_ = false;
119 return false;
120 }
121
122 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
123 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
124
125 if (!pinch_zoom_active_ ||
126 (event->touchPointStates() & Qt::TouchPointPressed)) {
60d9b99a
JS
127 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
128 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
c9743553
JH
129 pinch_zoom_active_ = true;
130 }
131
132 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
133 if (abs(w) >= 1.0) {
1c91f1a2
JH
134 const double scale =
135 fabs((pinch_offset1_ - pinch_offset0_) / w);
c9743553
JH
136 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
137 if (scale > 0)
138 view_.set_scale_offset(scale, offset);
139 }
140
141 if (event->touchPointStates() & Qt::TouchPointReleased) {
142 pinch_zoom_active_ = false;
143
144 if (touchPoint0.state() & Qt::TouchPointReleased) {
145 // Primary touch released
28290534 146 drag_release();
c9743553
JH
147 } else {
148 // Update the mouse down fields so that continued
149 // dragging with the primary touch will work correctly
150 mouse_down_point_ = touchPoint0.pos().toPoint();
28290534 151 drag();
c9743553
JH
152 }
153 }
154
155 return true;
156}
157
e314eca4 158void Viewport::paintEvent(QPaintEvent*)
d7002724 159{
e94f43c6
JH
160 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
161 assert(none_of(row_items.begin(), row_items.end(),
162 [](const shared_ptr<RowItem> &r) { return !r; }));
beb897c6 163
e94f43c6
JH
164 stable_sort(row_items.begin(), row_items.end(),
165 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
166 return a->point(QRect()).y() < b->point(QRect()).y(); });
3e46726a 167
beb897c6
JH
168 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
169 assert(none_of(time_items.begin(), time_items.end(),
170 [](const shared_ptr<TimeItem> &t) { return !t; }));
171
64b60583 172 QPainter p(this);
ce6e73a8 173 p.setRenderHint(QPainter::Antialiasing);
3e46726a 174
36e7001d 175 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
3eb29afd 176
beb897c6
JH
177 for (const shared_ptr<TimeItem> t : time_items)
178 t->paint_back(p, pp);
e94f43c6 179 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 180 r->paint_back(p, pp);
3e46726a 181
beb897c6
JH
182 for (const shared_ptr<TimeItem> t : time_items)
183 t->paint_mid(p, pp);
e94f43c6 184 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 185 r->paint_mid(p, pp);
fe08b6e8 186
e94f43c6 187 for (const shared_ptr<RowItem> r : row_items)
3eb29afd 188 r->paint_fore(p, pp);
34ec0968
JH
189
190 p.setRenderHint(QPainter::Antialiasing, false);
beb897c6
JH
191 for (const shared_ptr<TimeItem> t : time_items)
192 t->paint_fore(p, pp);
f76af637 193
64b60583 194 p.end();
6fa02541 195}
009e1503 196
903038a8
JH
197void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
198{
199 assert(event);
200
201 if (event->buttons() & Qt::LeftButton)
8dbbc7f0 202 view_.zoom(2.0, event->x());
903038a8 203 else if (event->buttons() & Qt::RightButton)
8dbbc7f0 204 view_.zoom(-2.0, event->x());
903038a8
JH
205}
206
d9ea9628 207void Viewport::wheelEvent(QWheelEvent *event)
1dd95c70 208{
d9ea9628 209 assert(event);
208c6fc3 210
d9ea9628
UH
211 if (event->orientation() == Qt::Vertical) {
212 if (event->modifiers() & Qt::ControlModifier) {
208c6fc3
JH
213 // Vertical scrolling with the control key pressed
214 // is intrepretted as vertical scrolling
215 view_.set_v_offset(-view_.owner_visual_v_offset() -
d9ea9628 216 (event->delta() * height()) / (8 * 120));
208c6fc3
JH
217 } else {
218 // Vertical scrolling is interpreted as zooming in/out
d9ea9628 219 view_.zoom(event->delta() / 120, event->x());
208c6fc3 220 }
d9ea9628 221 } else if (event->orientation() == Qt::Horizontal) {
23840406 222 // Horizontal scrolling is interpreted as moving left/right
8dbbc7f0 223 view_.set_scale_offset(view_.scale(),
d9ea9628 224 event->delta() * view_.scale() + view_.offset());
ffd5cd20 225 }
7cd5faf8
JH
226}
227
f4e57597
SA
228} // namespace TraceView
229} // namespace views
cdf7bea7 230} // namespace pv