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 pinch_zoom_active_(false)
54 setAutoFillBackground(true);
55 setBackgroundRole(QPalette::Base);
58 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
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))
68 void Viewport::item_hover(const shared_ptr<ViewItem> &item)
71 setCursor(dynamic_pointer_cast<RowItem>(item) ?
72 Qt::SizeVerCursor : Qt::SizeHorCursor);
79 drag_offset_ = view_.offset();
82 void Viewport::drag_by(const QPoint &delta)
84 if (drag_offset_ == boost::none)
87 view_.set_scale_offset(view_.scale(),
88 (*drag_offset_ - delta.x() * view_.scale()));
91 void Viewport::drag_release()
93 drag_offset_ = boost::none;
96 vector< shared_ptr<ViewItem> > Viewport::items()
98 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
99 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
100 copy(time_items.begin(), time_items.end(), back_inserter(items));
104 bool Viewport::touch_event(QTouchEvent *event)
106 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
108 if (touchPoints.count() != 2) {
109 pinch_zoom_active_ = false;
113 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
114 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
116 if (!pinch_zoom_active_ ||
117 (event->touchPointStates() & Qt::TouchPointPressed)) {
118 pinch_offset0_ = (view_.offset() + view_.scale() * touchPoint0.pos().x()).convert_to<double>();
119 pinch_offset1_ = (view_.offset() + view_.scale() * touchPoint1.pos().x()).convert_to<double>();
120 pinch_zoom_active_ = true;
123 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
126 fabs((pinch_offset1_ - pinch_offset0_) / w);
127 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
129 view_.set_scale_offset(scale, offset);
132 if (event->touchPointStates() & Qt::TouchPointReleased) {
133 pinch_zoom_active_ = false;
135 if (touchPoint0.state() & Qt::TouchPointReleased) {
136 // Primary touch released
139 // Update the mouse down fields so that continued
140 // dragging with the primary touch will work correctly
141 mouse_down_point_ = touchPoint0.pos().toPoint();
149 void Viewport::paintEvent(QPaintEvent*)
151 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
152 assert(none_of(row_items.begin(), row_items.end(),
153 [](const shared_ptr<RowItem> &r) { return !r; }));
155 stable_sort(row_items.begin(), row_items.end(),
156 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
157 return a->visual_v_offset() < b->visual_v_offset(); });
159 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
160 assert(none_of(time_items.begin(), time_items.end(),
161 [](const shared_ptr<TimeItem> &t) { return !t; }));
164 p.setRenderHint(QPainter::Antialiasing);
166 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
168 for (const shared_ptr<TimeItem> t : time_items)
169 t->paint_back(p, pp);
170 for (const shared_ptr<RowItem> r : row_items)
171 r->paint_back(p, pp);
173 for (const shared_ptr<TimeItem> t : time_items)
175 for (const shared_ptr<RowItem> r : row_items)
178 for (const shared_ptr<RowItem> r : row_items)
179 r->paint_fore(p, pp);
180 for (const shared_ptr<TimeItem> t : time_items)
181 t->paint_fore(p, pp);
186 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
190 if (event->buttons() & Qt::LeftButton)
191 view_.zoom(2.0, event->x());
192 else if (event->buttons() & Qt::RightButton)
193 view_.zoom(-2.0, event->x());
196 void Viewport::wheelEvent(QWheelEvent *e)
200 if (e->orientation() == Qt::Vertical)
202 if (e->modifiers() & Qt::ControlModifier) {
203 // Vertical scrolling with the control key pressed
204 // is intrepretted as vertical scrolling
205 view_.set_v_offset(-view_.owner_visual_v_offset() -
206 (e->delta() * height()) / (8 * 120));
208 // Vertical scrolling is interpreted as zooming in/out
209 view_.zoom(e->delta() / 120, e->x());
212 else if (e->orientation() == Qt::Horizontal)
214 // Horizontal scrolling is interpreted as moving left/right
215 view_.set_scale_offset(view_.scale(),
216 e->delta() * view_.scale() + view_.offset());