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