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 "../sigsession.h"
29 #include <QMouseEvent>
33 using std::shared_ptr;
39 Viewport::Viewport(View &parent) :
42 _mouse_down_valid(false),
43 _pinch_zoom_active(false)
45 setAttribute(Qt::WA_AcceptTouchEvents, true);
47 setMouseTracking(true);
48 setAutoFillBackground(true);
49 setBackgroundRole(QPalette::Base);
51 connect(&_view.session(), SIGNAL(signals_changed()),
52 this, SLOT(on_signals_changed()));
54 connect(&_view, SIGNAL(signals_moved()),
55 this, SLOT(on_signals_moved()));
57 // Trigger the initial event manually. The default device has signals
58 // which were created before this object came into being
62 int Viewport::get_total_height() const
65 const vector< shared_ptr<Trace> > traces(_view.get_traces());
66 for (const shared_ptr<Trace> t : traces) {
68 h = max(t->get_v_offset() + View::SignalHeight, h);
74 void Viewport::paintEvent(QPaintEvent*)
76 const vector< shared_ptr<Trace> > traces(_view.get_traces());
79 p.setRenderHint(QPainter::Antialiasing);
81 if (_view.cursors_shown())
82 _view.cursors().draw_viewport_background(p, rect());
85 for (const shared_ptr<Trace> t : traces)
88 t->paint_back(p, 0, width());
91 for (const shared_ptr<Trace> t : traces)
92 t->paint_mid(p, 0, width());
94 for (const shared_ptr<Trace> t : traces)
95 t->paint_fore(p, 0, width());
97 if (_view.cursors_shown())
98 _view.cursors().draw_viewport_foreground(p, rect());
103 bool Viewport::event(QEvent *event)
105 switch(event->type()) {
106 case QEvent::TouchBegin:
107 case QEvent::TouchUpdate:
108 case QEvent::TouchEnd:
109 if (touchEvent(static_cast<QTouchEvent *>(event)))
117 return QWidget::event(event);
120 void Viewport::mousePressEvent(QMouseEvent *event)
124 if (event->button() == Qt::LeftButton)
126 _mouse_down_point = event->pos();
127 _mouse_down_offset = _view.offset();
128 _mouse_down_valid = true;
132 void Viewport::mouseReleaseEvent(QMouseEvent *event)
136 if (event->button() == Qt::LeftButton)
138 _mouse_down_valid = false;
142 void Viewport::mouseMoveEvent(QMouseEvent *event)
146 if (event->buttons() & Qt::LeftButton)
148 if (! _mouse_down_valid) {
149 _mouse_down_point = event->pos();
150 _mouse_down_offset = _view.offset();
151 _mouse_down_valid = true;
154 _view.set_scale_offset(_view.scale(),
156 (_mouse_down_point - event->pos()).x() *
161 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
165 if (event->buttons() & Qt::LeftButton)
166 _view.zoom(2.0, event->x());
167 else if (event->buttons() & Qt::RightButton)
168 _view.zoom(-2.0, event->x());
171 void Viewport::wheelEvent(QWheelEvent *event)
175 if (event->orientation() == Qt::Vertical) {
176 // Vertical scrolling is interpreted as zooming in/out
177 _view.zoom(event->delta() / 120, event->x());
178 } else if (event->orientation() == Qt::Horizontal) {
179 // Horizontal scrolling is interpreted as moving left/right
180 _view.set_scale_offset(_view.scale(),
181 event->delta() * _view.scale()
186 bool Viewport::touchEvent(QTouchEvent *event)
188 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
190 if (touchPoints.count() != 2) {
191 _pinch_zoom_active = false;
195 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
196 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
198 if (!_pinch_zoom_active ||
199 (event->touchPointStates() & Qt::TouchPointPressed)) {
200 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
201 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
202 _pinch_zoom_active = true;
205 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
207 double scale = (_pinch_offset1 - _pinch_offset0) / w;
210 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
212 _view.set_scale_offset(scale, offset);
215 if (event->touchPointStates() & Qt::TouchPointReleased) {
216 _pinch_zoom_active = false;
218 if (touchPoint0.state() & Qt::TouchPointReleased) {
219 // Primary touch released
220 _mouse_down_valid = false;
222 // Update the mouse down fields so that continued
223 // dragging with the primary touch will work correctly
224 _mouse_down_point = touchPoint0.pos().toPoint();
225 _mouse_down_offset = _view.offset();
226 _mouse_down_valid = true;
233 void Viewport::on_signals_changed()
235 const vector< shared_ptr<Trace> > traces(_view.get_traces());
236 for (shared_ptr<Trace> t : traces) {
238 connect(t.get(), SIGNAL(visibility_changed()),
239 this, SLOT(update()));
243 void Viewport::on_signals_moved()