]> sigrok.org Git - pulseview.git/blame_incremental - pv/view/viewport.cpp
Viewport: Render all types of RowItem
[pulseview.git] / pv / view / viewport.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#include <cassert>
22#include <cmath>
23#include <algorithm>
24#include <limits>
25
26#include "signal.hpp"
27#include "view.hpp"
28#include "viewitempaintparams.hpp"
29#include "viewport.hpp"
30
31#include <pv/session.hpp>
32
33#include <QMouseEvent>
34
35using std::abs;
36using std::back_inserter;
37using std::copy;
38using std::dynamic_pointer_cast;
39using std::max;
40using std::min;
41using std::none_of;
42using std::numeric_limits;
43using std::shared_ptr;
44using std::stable_sort;
45using std::vector;
46
47namespace pv {
48namespace view {
49
50Viewport::Viewport(View &parent) :
51 ViewWidget(parent),
52 pinch_zoom_active_(false)
53{
54 setAutoFillBackground(true);
55 setBackgroundRole(QPalette::Base);
56}
57
58shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
59{
60 const vector< shared_ptr<ViewItem> > items(this->items());
61 for (auto i = items.rbegin(); i != items.rend(); i++)
62 if ((*i)->enabled() &&
63 (*i)->hit_box_rect(rect()).contains(pt))
64 return *i;
65 return nullptr;
66}
67
68void Viewport::item_hover(const shared_ptr<ViewItem> &item)
69{
70 if (item)
71 setCursor(dynamic_pointer_cast<RowItem>(item) ?
72 Qt::SizeVerCursor : Qt::SizeHorCursor);
73 else
74 unsetCursor();
75}
76
77void Viewport::drag()
78{
79 drag_offset_ = view_.offset();
80}
81
82void Viewport::drag_by(const QPoint &delta)
83{
84 if (drag_offset_ == boost::none)
85 return;
86
87 view_.set_scale_offset(view_.scale(),
88 (*drag_offset_ - delta.x() * view_.scale()));
89}
90
91void Viewport::drag_release()
92{
93 drag_offset_ = boost::none;
94}
95
96vector< shared_ptr<ViewItem> > Viewport::items()
97{
98 vector< shared_ptr<ViewItem> > items;
99 const std::vector< shared_ptr<ViewItem> > view_items(
100 view_.list_by_type<ViewItem>());
101 copy(view_items.begin(), view_items.end(), back_inserter(items));
102 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
103 copy(time_items.begin(), time_items.end(), back_inserter(items));
104 return items;
105}
106
107bool Viewport::touch_event(QTouchEvent *event)
108{
109 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
110
111 if (touchPoints.count() != 2) {
112 pinch_zoom_active_ = false;
113 return false;
114 }
115
116 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
117 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
118
119 if (!pinch_zoom_active_ ||
120 (event->touchPointStates() & Qt::TouchPointPressed)) {
121 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
122 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
123 pinch_zoom_active_ = true;
124 }
125
126 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
127 if (abs(w) >= 1.0) {
128 const double scale =
129 fabs((pinch_offset1_ - pinch_offset0_) / w);
130 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
131 if (scale > 0)
132 view_.set_scale_offset(scale, offset);
133 }
134
135 if (event->touchPointStates() & Qt::TouchPointReleased) {
136 pinch_zoom_active_ = false;
137
138 if (touchPoint0.state() & Qt::TouchPointReleased) {
139 // Primary touch released
140 drag_release();
141 } else {
142 // Update the mouse down fields so that continued
143 // dragging with the primary touch will work correctly
144 mouse_down_point_ = touchPoint0.pos().toPoint();
145 drag();
146 }
147 }
148
149 return true;
150}
151
152void Viewport::paintEvent(QPaintEvent*)
153{
154 vector< shared_ptr<RowItem> > row_items(view_.list_by_type<RowItem>());
155 assert(none_of(row_items.begin(), row_items.end(),
156 [](const shared_ptr<RowItem> &r) { return !r; }));
157
158 stable_sort(row_items.begin(), row_items.end(),
159 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
160 return a->point(QRect()).y() < b->point(QRect()).y(); });
161
162 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
163 assert(none_of(time_items.begin(), time_items.end(),
164 [](const shared_ptr<TimeItem> &t) { return !t; }));
165
166 QPainter p(this);
167 p.setRenderHint(QPainter::Antialiasing);
168
169 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
170
171 for (const shared_ptr<TimeItem> t : time_items)
172 t->paint_back(p, pp);
173 for (const shared_ptr<RowItem> r : row_items)
174 r->paint_back(p, pp);
175
176 for (const shared_ptr<TimeItem> t : time_items)
177 t->paint_mid(p, pp);
178 for (const shared_ptr<RowItem> r : row_items)
179 r->paint_mid(p, pp);
180
181 p.setRenderHint(QPainter::Antialiasing, false);
182
183 for (const shared_ptr<RowItem> r : row_items)
184 r->paint_fore(p, pp);
185 for (const shared_ptr<TimeItem> t : time_items)
186 t->paint_fore(p, pp);
187
188 p.end();
189}
190
191void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
192{
193 assert(event);
194
195 if (event->buttons() & Qt::LeftButton)
196 view_.zoom(2.0, event->x());
197 else if (event->buttons() & Qt::RightButton)
198 view_.zoom(-2.0, event->x());
199}
200
201void Viewport::wheelEvent(QWheelEvent *e)
202{
203 assert(e);
204
205 if (e->orientation() == Qt::Vertical)
206 {
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 }
216 }
217 else if (e->orientation() == Qt::Horizontal)
218 {
219 // Horizontal scrolling is interpreted as moving left/right
220 view_.set_scale_offset(view_.scale(),
221 e->delta() * view_.scale() + view_.offset());
222 }
223}
224
225} // namespace view
226} // namespace pv