]>
Commit | Line | Data |
---|---|---|
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 | ||
35 | using std::abs; | |
36 | using std::back_inserter; | |
37 | using std::copy; | |
38 | using std::dynamic_pointer_cast; | |
39 | using std::max; | |
40 | using std::min; | |
41 | using std::none_of; | |
42 | using std::numeric_limits; | |
43 | using std::shared_ptr; | |
44 | using std::stable_sort; | |
45 | using std::vector; | |
46 | ||
47 | namespace pv { | |
48 | namespace view { | |
49 | ||
50 | Viewport::Viewport(View &parent) : | |
51 | ViewWidget(parent), | |
52 | pinch_zoom_active_(false) | |
53 | { | |
54 | setAutoFillBackground(true); | |
55 | setBackgroundRole(QPalette::Base); | |
56 | } | |
57 | ||
58 | shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt) | |
59 | { | |
60 | const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset()); | |
61 | const vector< shared_ptr<ViewItem> > items(this->items()); | |
62 | for (auto i = items.rbegin(); i != items.rend(); i++) | |
63 | if ((*i)->enabled() && | |
64 | (*i)->hit_box_rect(pp).contains(pt)) | |
65 | return *i; | |
66 | return nullptr; | |
67 | } | |
68 | ||
69 | void Viewport::item_hover(const shared_ptr<ViewItem> &item) | |
70 | { | |
71 | if (item && item->is_draggable()) | |
72 | setCursor(dynamic_pointer_cast<RowItem>(item) ? | |
73 | Qt::SizeVerCursor : Qt::SizeHorCursor); | |
74 | else | |
75 | unsetCursor(); | |
76 | } | |
77 | ||
78 | void Viewport::drag() | |
79 | { | |
80 | drag_offset_ = view_.offset(); | |
81 | } | |
82 | ||
83 | void Viewport::drag_by(const QPoint &delta) | |
84 | { | |
85 | if (drag_offset_ == boost::none) | |
86 | return; | |
87 | ||
88 | view_.set_scale_offset(view_.scale(), | |
89 | (*drag_offset_ - delta.x() * view_.scale())); | |
90 | } | |
91 | ||
92 | void Viewport::drag_release() | |
93 | { | |
94 | drag_offset_ = boost::none; | |
95 | } | |
96 | ||
97 | vector< shared_ptr<ViewItem> > Viewport::items() | |
98 | { | |
99 | vector< shared_ptr<ViewItem> > items; | |
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)); | |
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 | ||
108 | bool 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)) { | |
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>(); | |
124 | pinch_zoom_active_ = true; | |
125 | } | |
126 | ||
127 | double w = touchPoint1.pos().x() - touchPoint0.pos().x(); | |
128 | if (abs(w) >= 1.0) { | |
129 | const double scale = | |
130 | fabs((pinch_offset1_ - pinch_offset0_) / w); | |
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 | |
141 | drag_release(); | |
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(); | |
146 | drag(); | |
147 | } | |
148 | } | |
149 | ||
150 | return true; | |
151 | } | |
152 | ||
153 | void Viewport::paintEvent(QPaintEvent*) | |
154 | { | |
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; })); | |
158 | ||
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(); }); | |
162 | ||
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 | ||
167 | QPainter p(this); | |
168 | p.setRenderHint(QPainter::Antialiasing); | |
169 | ||
170 | const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset()); | |
171 | ||
172 | for (const shared_ptr<TimeItem> t : time_items) | |
173 | t->paint_back(p, pp); | |
174 | for (const shared_ptr<RowItem> r : row_items) | |
175 | r->paint_back(p, pp); | |
176 | ||
177 | for (const shared_ptr<TimeItem> t : time_items) | |
178 | t->paint_mid(p, pp); | |
179 | for (const shared_ptr<RowItem> r : row_items) | |
180 | r->paint_mid(p, pp); | |
181 | ||
182 | for (const shared_ptr<RowItem> r : row_items) | |
183 | r->paint_fore(p, pp); | |
184 | ||
185 | p.setRenderHint(QPainter::Antialiasing, false); | |
186 | for (const shared_ptr<TimeItem> t : time_items) | |
187 | t->paint_fore(p, pp); | |
188 | ||
189 | p.end(); | |
190 | } | |
191 | ||
192 | void Viewport::mouseDoubleClickEvent(QMouseEvent *event) | |
193 | { | |
194 | assert(event); | |
195 | ||
196 | if (event->buttons() & Qt::LeftButton) | |
197 | view_.zoom(2.0, event->x()); | |
198 | else if (event->buttons() & Qt::RightButton) | |
199 | view_.zoom(-2.0, event->x()); | |
200 | } | |
201 | ||
202 | void Viewport::wheelEvent(QWheelEvent *e) | |
203 | { | |
204 | assert(e); | |
205 | ||
206 | if (e->orientation() == Qt::Vertical) | |
207 | { | |
208 | if (e->modifiers() & Qt::ControlModifier) { | |
209 | // Vertical scrolling with the control key pressed | |
210 | // is intrepretted as vertical scrolling | |
211 | view_.set_v_offset(-view_.owner_visual_v_offset() - | |
212 | (e->delta() * height()) / (8 * 120)); | |
213 | } else { | |
214 | // Vertical scrolling is interpreted as zooming in/out | |
215 | view_.zoom(e->delta() / 120, e->x()); | |
216 | } | |
217 | } | |
218 | else if (e->orientation() == Qt::Horizontal) | |
219 | { | |
220 | // Horizontal scrolling is interpreted as moving left/right | |
221 | view_.set_scale_offset(view_.scale(), | |
222 | e->delta() * view_.scale() + view_.offset()); | |
223 | } | |
224 | } | |
225 | ||
226 | } // namespace view | |
227 | } // namespace pv |