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>
38 using std::shared_ptr;
39 using std::stable_sort;
45 Viewport::Viewport(View &parent) :
47 mouse_down_valid_(false),
48 pinch_zoom_active_(false)
50 setAttribute(Qt::WA_AcceptTouchEvents, true);
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
56 void Viewport::paintEvent(QPaintEvent*)
58 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
59 assert(none_of(row_items.begin(), row_items.end(),
60 [](const shared_ptr<RowItem> &r) { return !r; }));
62 stable_sort(row_items.begin(), row_items.end(),
63 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
64 return a->visual_v_offset() < b->visual_v_offset(); });
66 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
67 assert(none_of(time_items.begin(), time_items.end(),
68 [](const shared_ptr<TimeItem> &t) { return !t; }));
71 p.setRenderHint(QPainter::Antialiasing);
73 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
75 for (const shared_ptr<TimeItem> t : time_items)
77 for (const shared_ptr<RowItem> r : row_items)
80 for (const shared_ptr<TimeItem> t : time_items)
82 for (const shared_ptr<RowItem> r : row_items)
85 for (const shared_ptr<RowItem> r : row_items)
87 for (const shared_ptr<TimeItem> t : time_items)
93 bool Viewport::event(QEvent *event)
95 switch (event->type()) {
96 case QEvent::TouchBegin:
97 case QEvent::TouchUpdate:
98 case QEvent::TouchEnd:
99 if (touchEvent(static_cast<QTouchEvent *>(event)))
107 return QWidget::event(event);
110 void Viewport::mousePressEvent(QMouseEvent *event)
114 if (event->button() == Qt::LeftButton) {
115 mouse_down_point_ = event->pos();
116 mouse_down_offset_ = view_.offset();
117 mouse_down_valid_ = true;
121 void Viewport::mouseReleaseEvent(QMouseEvent *event)
125 if (event->button() == Qt::LeftButton)
126 mouse_down_valid_ = false;
129 void Viewport::mouseMoveEvent(QMouseEvent *event)
133 if (event->buttons() & Qt::LeftButton) {
134 if (!mouse_down_valid_) {
135 mouse_down_point_ = event->pos();
136 mouse_down_offset_ = view_.offset();
137 mouse_down_valid_ = true;
140 view_.set_scale_offset(view_.scale(),
142 (mouse_down_point_ - event->pos()).x() *
147 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
151 if (event->buttons() & Qt::LeftButton)
152 view_.zoom(2.0, event->x());
153 else if (event->buttons() & Qt::RightButton)
154 view_.zoom(-2.0, event->x());
157 void Viewport::wheelEvent(QWheelEvent *event)
161 if (event->orientation() == Qt::Vertical) {
162 // Vertical scrolling is interpreted as zooming in/out
163 view_.zoom(event->delta() / 120, event->x());
164 } else if (event->orientation() == Qt::Horizontal) {
165 // Horizontal scrolling is interpreted as moving left/right
166 view_.set_scale_offset(view_.scale(),
167 event->delta() * view_.scale()
172 bool Viewport::touchEvent(QTouchEvent *event)
174 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
176 if (touchPoints.count() != 2) {
177 pinch_zoom_active_ = false;
181 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
182 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
184 if (!pinch_zoom_active_ ||
185 (event->touchPointStates() & Qt::TouchPointPressed)) {
186 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
187 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
188 pinch_zoom_active_ = true;
191 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
193 double scale = (pinch_offset1_ - pinch_offset0_) / w;
196 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
198 view_.set_scale_offset(scale, offset);
201 if (event->touchPointStates() & Qt::TouchPointReleased) {
202 pinch_zoom_active_ = false;
204 if (touchPoint0.state() & Qt::TouchPointReleased) {
205 // Primary touch released
206 mouse_down_valid_ = false;
208 // Update the mouse down fields so that continued
209 // dragging with the primary touch will work correctly
210 mouse_down_point_ = touchPoint0.pos().toPoint();
211 mouse_down_offset_ = view_.offset();
212 mouse_down_valid_ = true;