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
27 #include "viewitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
35 using std::back_inserter;
40 using std::shared_ptr;
41 using std::stable_sort;
47 Viewport::Viewport(View &parent) :
49 mouse_down_valid_(false),
50 pinch_zoom_active_(false)
52 setAttribute(Qt::WA_AcceptTouchEvents, true);
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 vector< shared_ptr<ViewItem> > Viewport::items()
70 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
71 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
72 copy(time_items.begin(), time_items.end(), back_inserter(items));
76 void Viewport::paintEvent(QPaintEvent*)
78 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
79 assert(none_of(row_items.begin(), row_items.end(),
80 [](const shared_ptr<RowItem> &r) { return !r; }));
82 stable_sort(row_items.begin(), row_items.end(),
83 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
84 return a->visual_v_offset() < b->visual_v_offset(); });
86 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
87 assert(none_of(time_items.begin(), time_items.end(),
88 [](const shared_ptr<TimeItem> &t) { return !t; }));
91 p.setRenderHint(QPainter::Antialiasing);
93 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
95 for (const shared_ptr<TimeItem> t : time_items)
97 for (const shared_ptr<RowItem> r : row_items)
100 for (const shared_ptr<TimeItem> t : time_items)
102 for (const shared_ptr<RowItem> r : row_items)
105 for (const shared_ptr<RowItem> r : row_items)
106 r->paint_fore(p, pp);
107 for (const shared_ptr<TimeItem> t : time_items)
108 t->paint_fore(p, pp);
113 bool Viewport::event(QEvent *event)
115 switch (event->type()) {
116 case QEvent::TouchBegin:
117 case QEvent::TouchUpdate:
118 case QEvent::TouchEnd:
119 if (touchEvent(static_cast<QTouchEvent *>(event)))
127 return QWidget::event(event);
130 void Viewport::mousePressEvent(QMouseEvent *event)
134 if (event->button() == Qt::LeftButton) {
135 mouse_down_point_ = event->pos();
136 mouse_down_offset_ = view_.offset();
137 mouse_down_valid_ = true;
141 void Viewport::mouseReleaseEvent(QMouseEvent *event)
145 if (event->button() == Qt::LeftButton)
146 mouse_down_valid_ = false;
149 void Viewport::mouseMoveEvent(QMouseEvent *event)
153 if (event->buttons() & Qt::LeftButton) {
154 if (!mouse_down_valid_) {
155 mouse_down_point_ = event->pos();
156 mouse_down_offset_ = view_.offset();
157 mouse_down_valid_ = true;
160 view_.set_scale_offset(view_.scale(),
162 (mouse_down_point_ - event->pos()).x() *
167 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
171 if (event->buttons() & Qt::LeftButton)
172 view_.zoom(2.0, event->x());
173 else if (event->buttons() & Qt::RightButton)
174 view_.zoom(-2.0, event->x());
177 void Viewport::wheelEvent(QWheelEvent *event)
181 if (event->orientation() == Qt::Vertical) {
182 // Vertical scrolling is interpreted as zooming in/out
183 view_.zoom(event->delta() / 120, event->x());
184 } else if (event->orientation() == Qt::Horizontal) {
185 // Horizontal scrolling is interpreted as moving left/right
186 view_.set_scale_offset(view_.scale(),
187 event->delta() * view_.scale()
192 bool Viewport::touchEvent(QTouchEvent *event)
194 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
196 if (touchPoints.count() != 2) {
197 pinch_zoom_active_ = false;
201 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
202 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
204 if (!pinch_zoom_active_ ||
205 (event->touchPointStates() & Qt::TouchPointPressed)) {
206 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
207 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
208 pinch_zoom_active_ = true;
211 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
213 double scale = (pinch_offset1_ - pinch_offset0_) / w;
216 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
218 view_.set_scale_offset(scale, offset);
221 if (event->touchPointStates() & Qt::TouchPointReleased) {
222 pinch_zoom_active_ = false;
224 if (touchPoint0.state() & Qt::TouchPointReleased) {
225 // Primary touch released
226 mouse_down_valid_ = false;
228 // Update the mouse down fields so that continued
229 // dragging with the primary touch will work correctly
230 mouse_down_point_ = touchPoint0.pos().toPoint();
231 mouse_down_offset_ = view_.offset();
232 mouse_down_valid_ = true;