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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "viewitempaintparams.hpp"
29 #include "viewport.hpp"
31 #include <pv/session.hpp>
33 #include <QMouseEvent>
36 using std::back_inserter;
38 using std::dynamic_pointer_cast;
42 using std::numeric_limits;
43 using std::shared_ptr;
44 using std::stable_sort;
50 Viewport::Viewport(View &parent) :
52 drag_offset_(numeric_limits<double>::signaling_NaN()),
53 pinch_zoom_active_(false)
55 setAutoFillBackground(true);
56 setBackgroundRole(QPalette::Base);
59 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
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(rect()).contains(pt))
69 void Viewport::item_hover(const shared_ptr<ViewItem> &item)
72 setCursor(dynamic_pointer_cast<RowItem>(item) ?
73 Qt::SizeVerCursor : Qt::SizeHorCursor);
80 drag_offset_ = view_.offset();
83 void Viewport::drag_by(const QPoint &delta)
85 if (isnan(drag_offset_))
88 view_.set_scale_offset(view_.scale(), drag_offset_ -
89 delta.x() * view_.scale());
92 void Viewport::drag_release()
94 drag_offset_ = numeric_limits<double>::signaling_NaN();
97 vector< shared_ptr<ViewItem> > Viewport::items()
99 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
100 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
101 copy(time_items.begin(), time_items.end(), back_inserter(items));
105 bool Viewport::touch_event(QTouchEvent *event)
107 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
109 if (touchPoints.count() != 2) {
110 pinch_zoom_active_ = false;
114 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
115 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
117 if (!pinch_zoom_active_ ||
118 (event->touchPointStates() & Qt::TouchPointPressed)) {
119 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
120 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
121 pinch_zoom_active_ = true;
124 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
127 fabs((pinch_offset1_ - pinch_offset0_) / w);
128 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
130 view_.set_scale_offset(scale, offset);
133 if (event->touchPointStates() & Qt::TouchPointReleased) {
134 pinch_zoom_active_ = false;
136 if (touchPoint0.state() & Qt::TouchPointReleased) {
137 // Primary touch released
140 // Update the mouse down fields so that continued
141 // dragging with the primary touch will work correctly
142 mouse_down_point_ = touchPoint0.pos().toPoint();
150 void Viewport::paintEvent(QPaintEvent*)
152 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
153 assert(none_of(row_items.begin(), row_items.end(),
154 [](const shared_ptr<RowItem> &r) { return !r; }));
156 stable_sort(row_items.begin(), row_items.end(),
157 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
158 return a->visual_v_offset() < b->visual_v_offset(); });
160 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
161 assert(none_of(time_items.begin(), time_items.end(),
162 [](const shared_ptr<TimeItem> &t) { return !t; }));
165 p.setRenderHint(QPainter::Antialiasing);
167 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
169 for (const shared_ptr<TimeItem> t : time_items)
170 t->paint_back(p, pp);
171 for (const shared_ptr<RowItem> r : row_items)
172 r->paint_back(p, pp);
174 for (const shared_ptr<TimeItem> t : time_items)
176 for (const shared_ptr<RowItem> r : row_items)
179 for (const shared_ptr<RowItem> r : row_items)
180 r->paint_fore(p, pp);
181 for (const shared_ptr<TimeItem> t : time_items)
182 t->paint_fore(p, pp);
187 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
191 if (event->buttons() & Qt::LeftButton)
192 view_.zoom(2.0, event->x());
193 else if (event->buttons() & Qt::RightButton)
194 view_.zoom(-2.0, event->x());
197 void Viewport::wheelEvent(QWheelEvent *event)
201 if (event->orientation() == Qt::Vertical) {
202 // Vertical scrolling is interpreted as zooming in/out
203 view_.zoom(event->delta() / 120, event->x());
204 } else if (event->orientation() == Qt::Horizontal) {
205 // Horizontal scrolling is interpreted as moving left/right
206 view_.set_scale_offset(view_.scale(),
207 event->delta() * view_.scale()