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