2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "viewitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
39 using std::back_inserter;
41 using std::dynamic_pointer_cast;
42 using std::none_of; // NOLINT. Used in assert()s.
43 using std::shared_ptr;
44 using std::stable_sort;
51 Viewport::Viewport(View &parent) :
53 pinch_zoom_active_(false)
55 setAutoFillBackground(true);
56 setBackgroundRole(QPalette::Base);
58 // Set up settings and event handlers
59 GlobalSettings settings;
60 allow_vertical_dragging_ = settings.value(GlobalSettings::Key_View_AllowVerticalDragging).toBool();
62 GlobalSettings::add_change_handler(this);
67 GlobalSettings::remove_change_handler(this);
70 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
72 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
73 const vector< shared_ptr<ViewItem> > items(this->items());
74 for (auto i = items.rbegin(); i != items.rend(); i++)
75 if ((*i)->enabled() && (*i)->hit_box_rect(pp).contains(pt))
80 void Viewport::item_hover(const shared_ptr<ViewItem> &item, QPoint pos)
82 if (item && item->is_draggable(pos))
83 setCursor(dynamic_pointer_cast<ViewItem>(item) ?
84 Qt::SizeHorCursor : Qt::SizeVerCursor);
91 drag_offset_ = view_.offset();
93 if (allow_vertical_dragging_)
94 drag_v_offset_ = view_.owner_visual_v_offset();
97 void Viewport::drag_by(const QPoint &delta)
99 if (drag_offset_ == boost::none)
102 view_.set_scale_offset(view_.scale(),
103 (*drag_offset_ - delta.x() * view_.scale()));
105 if (allow_vertical_dragging_)
106 view_.set_v_offset(-drag_v_offset_ - delta.y());
109 void Viewport::drag_release()
111 drag_offset_ = boost::none;
114 vector< shared_ptr<ViewItem> > Viewport::items()
116 vector< shared_ptr<ViewItem> > items;
117 const vector< shared_ptr<ViewItem> > view_items(
118 view_.list_by_type<ViewItem>());
119 copy(view_items.begin(), view_items.end(), back_inserter(items));
120 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
121 copy(time_items.begin(), time_items.end(), back_inserter(items));
125 bool Viewport::touch_event(QTouchEvent *event)
127 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
128 QList<QEventPoint> touchPoints = event->points();
130 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
133 if (touchPoints.count() != 2) {
134 pinch_zoom_active_ = false;
137 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
138 if (event->device()->type() == QInputDevice::DeviceType::TouchPad) {
142 const QEventPoint &touchPoint0 = touchPoints.first();
143 const QEventPoint &touchPoint1 = touchPoints.last();
145 if (!pinch_zoom_active_ ||
146 (event->touchPointStates() & QEventPoint::Pressed)) {
147 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.position().x()).convert_to<double>();
148 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.position().x()).convert_to<double>();
149 pinch_zoom_active_ = true;
152 double w = touchPoint1.position().x() - touchPoint0.position().x();
155 fabs((pinch_offset1_ - pinch_offset0_) / w);
156 double offset = pinch_offset0_ - touchPoint0.position().x() * scale;
158 view_.set_scale_offset(scale, offset);
161 if (event->device()->type() == QTouchDevice::TouchPad) {
165 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
166 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
168 if (!pinch_zoom_active_ ||
169 (event->touchPointStates() & Qt::TouchPointPressed)) {
170 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
171 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
172 pinch_zoom_active_ = true;
175 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
178 fabs((pinch_offset1_ - pinch_offset0_) / w);
179 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
181 view_.set_scale_offset(scale, offset);
185 if (event->touchPointStates() & Qt::TouchPointReleased) {
186 pinch_zoom_active_ = false;
188 if (touchPoint0.state() & Qt::TouchPointReleased) {
189 // Primary touch released
192 // Update the mouse down fields so that continued
193 // dragging with the primary touch will work correctly
194 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
195 mouse_down_point_ = touchPoint0.position().toPoint();
197 mouse_down_point_ = touchPoint0.pos().toPoint();
206 void Viewport::paintEvent(QPaintEvent*)
208 typedef void (ViewItem::*LayerPaintFunc)(
209 QPainter &p, ViewItemPaintParams &pp);
210 LayerPaintFunc layer_paint_funcs[] = {
211 &ViewItem::paint_back, &ViewItem::paint_mid,
212 &ViewItem::paint_fore, nullptr};
214 vector< shared_ptr<ViewItem> > row_items(view_.list_by_type<ViewItem>());
215 assert(none_of(row_items.begin(), row_items.end(),
216 [](const shared_ptr<ViewItem> &r) { return !r; }));
218 stable_sort(row_items.begin(), row_items.end(),
219 [](const shared_ptr<ViewItem> &a, const shared_ptr<ViewItem> &b) {
220 return a->drag_point(QRect()).y() < b->drag_point(QRect()).y(); });
222 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
223 assert(none_of(time_items.begin(), time_items.end(),
224 [](const shared_ptr<TimeItem> &t) { return !t; }));
228 // Disable antialiasing for high-DPI displays
229 bool use_antialiasing =
230 window()->windowHandle()->screen()->devicePixelRatio() < 2.0;
231 p.setRenderHint(QPainter::Antialiasing, use_antialiasing);
233 for (LayerPaintFunc *paint_func = layer_paint_funcs;
234 *paint_func; paint_func++) {
235 ViewItemPaintParams time_pp(rect(), view_.scale(), view_.offset());
236 for (const shared_ptr<TimeItem>& t : time_items)
237 (t.get()->*(*paint_func))(p, time_pp);
239 ViewItemPaintParams row_pp(rect(), view_.scale(), view_.offset());
240 for (const shared_ptr<ViewItem>& r : row_items)
241 (r.get()->*(*paint_func))(p, row_pp);
247 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
251 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
252 if (event->buttons() & Qt::LeftButton)
253 view_.zoom(2.0, event->position().x());
254 else if (event->buttons() & Qt::RightButton)
255 view_.zoom(-2.0, event->position().x());
257 if (event->buttons() & Qt::LeftButton)
258 view_.zoom(2.0, event->x());
259 else if (event->buttons() & Qt::RightButton)
260 view_.zoom(-2.0, event->x());
264 void Viewport::wheelEvent(QWheelEvent *event)
268 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
269 int delta = (event->angleDelta().x() != 0) ? event->angleDelta().x() : event->angleDelta().y();
271 int delta = event->delta();
274 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
275 if (event->angleDelta().y() != 0) {
277 if (event->orientation() == Qt::Vertical) {
279 if (event->modifiers() & Qt::ControlModifier) {
280 // Vertical scrolling with the control key pressed
281 // is intrepretted as vertical scrolling
282 view_.set_v_offset(-view_.owner_visual_v_offset() -
283 (delta * height()) / (8 * 120));
284 } else if (event->modifiers() & Qt::ShiftModifier) {
285 // Vertical scrolling with the shift key pressed
286 // acts as horizontal scrolling like in Gimp
288 view_.set_scale_offset(view_.scale(),
289 - delta * view_.scale() + view_.offset());
291 // Vertical scrolling is interpreted as zooming in/out
292 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
293 view_.zoom(delta / 120.0, event->position().x());
295 view_.zoom(delta / 120.0, event->x());
298 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
299 } else if (event->angleDelta().x() != 0) {
301 } else if (event->orientation() == Qt::Horizontal) {
303 // Horizontal scrolling is interpreted as moving left/right
304 view_.set_scale_offset(view_.scale(),
305 delta * view_.scale() + view_.offset());
309 void Viewport::on_setting_changed(const QString &key, const QVariant &value)
311 if (key == GlobalSettings::Key_View_AllowVerticalDragging)
312 allow_vertical_dragging_ = value.toBool();