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>
34 using std::shared_ptr;
40 Viewport::Viewport(View &parent) :
43 _mouse_down_valid(false),
44 _pinch_zoom_active(false)
46 setAttribute(Qt::WA_AcceptTouchEvents, true);
48 setMouseTracking(true);
49 setAutoFillBackground(true);
50 setBackgroundRole(QPalette::Base);
52 connect(&_view.session(), SIGNAL(signals_changed()),
53 this, SLOT(on_signals_changed()));
55 connect(&_view, SIGNAL(signals_moved()),
56 this, SLOT(on_signals_moved()));
58 // Trigger the initial event manually. The default device has signals
59 // which were created before this object came into being
63 int Viewport::get_total_height() const
66 const vector< shared_ptr<Trace> > traces(_view.get_traces());
67 for (const shared_ptr<Trace> t : traces) {
69 h = max(t->get_v_offset() + View::SignalHeight, h);
75 void Viewport::paintEvent(QPaintEvent*)
77 const vector< shared_ptr<Trace> > traces(_view.get_traces());
80 p.setRenderHint(QPainter::Antialiasing);
82 if (_view.cursors_shown())
83 _view.cursors().draw_viewport_background(p, rect());
86 for (const shared_ptr<Trace> t : traces)
89 t->paint_back(p, 0, width());
92 for (const shared_ptr<Trace> t : traces)
93 t->paint_mid(p, 0, width());
95 for (const shared_ptr<Trace> t : traces)
96 t->paint_fore(p, 0, width());
98 if (_view.cursors_shown())
99 _view.cursors().draw_viewport_foreground(p, rect());
104 bool Viewport::event(QEvent *event)
106 switch (event->type()) {
107 case QEvent::TouchBegin:
108 case QEvent::TouchUpdate:
109 case QEvent::TouchEnd:
110 if (touchEvent(static_cast<QTouchEvent *>(event)))
118 return QWidget::event(event);
121 void Viewport::mousePressEvent(QMouseEvent *event)
125 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)
137 _mouse_down_valid = false;
140 void Viewport::mouseMoveEvent(QMouseEvent *event)
144 if (event->buttons() & Qt::LeftButton) {
145 if (!_mouse_down_valid) {
146 _mouse_down_point = event->pos();
147 _mouse_down_offset = _view.offset();
148 _mouse_down_valid = true;
151 _view.set_scale_offset(_view.scale(),
153 (_mouse_down_point - event->pos()).x() *
158 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
162 if (event->buttons() & Qt::LeftButton)
163 _view.zoom(2.0, event->x());
164 else if (event->buttons() & Qt::RightButton)
165 _view.zoom(-2.0, event->x());
168 void Viewport::wheelEvent(QWheelEvent *event)
172 if (event->orientation() == Qt::Vertical) {
173 // Vertical scrolling is interpreted as zooming in/out
174 _view.zoom(event->delta() / 120, event->x());
175 } else if (event->orientation() == Qt::Horizontal) {
176 // Horizontal scrolling is interpreted as moving left/right
177 _view.set_scale_offset(_view.scale(),
178 event->delta() * _view.scale()
183 bool Viewport::touchEvent(QTouchEvent *event)
185 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
187 if (touchPoints.count() != 2) {
188 _pinch_zoom_active = false;
192 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
193 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
195 if (!_pinch_zoom_active ||
196 (event->touchPointStates() & Qt::TouchPointPressed)) {
197 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
198 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
199 _pinch_zoom_active = true;
202 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
204 double scale = (_pinch_offset1 - _pinch_offset0) / w;
207 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
209 _view.set_scale_offset(scale, offset);
212 if (event->touchPointStates() & Qt::TouchPointReleased) {
213 _pinch_zoom_active = false;
215 if (touchPoint0.state() & Qt::TouchPointReleased) {
216 // Primary touch released
217 _mouse_down_valid = false;
219 // Update the mouse down fields so that continued
220 // dragging with the primary touch will work correctly
221 _mouse_down_point = touchPoint0.pos().toPoint();
222 _mouse_down_offset = _view.offset();
223 _mouse_down_valid = true;
230 void Viewport::on_signals_changed()
232 const vector< shared_ptr<Trace> > traces(_view.get_traces());
233 for (shared_ptr<Trace> t : traces) {
235 connect(t.get(), SIGNAL(visibility_changed()),
236 this, SLOT(update()));
240 void Viewport::on_signals_moved()