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 "../sigsession.h"
30 #include <QMouseEvent>
35 using std::shared_ptr;
41 Viewport::Viewport(View &parent) :
44 _mouse_down_valid(false),
45 _pinch_zoom_active(false)
47 setAttribute(Qt::WA_AcceptTouchEvents, true);
49 setMouseTracking(true);
50 setAutoFillBackground(true);
51 setBackgroundRole(QPalette::Base);
53 connect(&_view.session(), SIGNAL(signals_changed()),
54 this, SLOT(on_signals_changed()));
56 connect(&_view, SIGNAL(signals_moved()),
57 this, SLOT(on_signals_moved()));
59 // Trigger the initial event manually. The default device has signals
60 // which were created before this object came into being
64 int Viewport::get_total_height() const
67 const vector< shared_ptr<Trace> > traces(_view.get_traces());
68 for (const shared_ptr<Trace> t : traces) {
70 h = max(t->get_v_offset() + View::SignalHeight, h);
76 void Viewport::paintEvent(QPaintEvent*)
78 const vector< shared_ptr<Trace> > traces(_view.get_traces());
81 p.setRenderHint(QPainter::Antialiasing);
83 if (_view.cursors_shown())
84 _view.cursors().draw_viewport_background(p, rect());
87 for (const shared_ptr<Trace> t : traces)
90 t->paint_back(p, 0, width());
93 for (const shared_ptr<Trace> t : traces)
94 t->paint_mid(p, 0, width());
96 for (const shared_ptr<Trace> t : traces)
97 t->paint_fore(p, 0, width());
99 if (_view.cursors_shown())
100 _view.cursors().draw_viewport_foreground(p, rect());
105 bool Viewport::event(QEvent *event)
107 switch (event->type()) {
108 case QEvent::TouchBegin:
109 case QEvent::TouchUpdate:
110 case QEvent::TouchEnd:
111 if (touchEvent(static_cast<QTouchEvent *>(event)))
119 return QWidget::event(event);
122 void Viewport::mousePressEvent(QMouseEvent *event)
126 if (event->button() == Qt::LeftButton) {
127 _mouse_down_point = event->pos();
128 _mouse_down_offset = _view.offset();
129 _mouse_down_valid = true;
133 void Viewport::mouseReleaseEvent(QMouseEvent *event)
137 if (event->button() == Qt::LeftButton)
138 _mouse_down_valid = false;
141 void Viewport::mouseMoveEvent(QMouseEvent *event)
145 if (event->buttons() & Qt::LeftButton) {
146 if (!_mouse_down_valid) {
147 _mouse_down_point = event->pos();
148 _mouse_down_offset = _view.offset();
149 _mouse_down_valid = true;
152 _view.set_scale_offset(_view.scale(),
154 (_mouse_down_point - event->pos()).x() *
159 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
163 if (event->buttons() & Qt::LeftButton)
164 _view.zoom(2.0, event->x());
165 else if (event->buttons() & Qt::RightButton)
166 _view.zoom(-2.0, event->x());
169 void Viewport::wheelEvent(QWheelEvent *event)
173 if (event->orientation() == Qt::Vertical) {
174 // Vertical scrolling is interpreted as zooming in/out
175 _view.zoom(event->delta() / 120, event->x());
176 } else if (event->orientation() == Qt::Horizontal) {
177 // Horizontal scrolling is interpreted as moving left/right
178 _view.set_scale_offset(_view.scale(),
179 event->delta() * _view.scale()
184 bool Viewport::touchEvent(QTouchEvent *event)
186 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
188 if (touchPoints.count() != 2) {
189 _pinch_zoom_active = false;
193 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
194 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
196 if (!_pinch_zoom_active ||
197 (event->touchPointStates() & Qt::TouchPointPressed)) {
198 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
199 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
200 _pinch_zoom_active = true;
203 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
205 double scale = (_pinch_offset1 - _pinch_offset0) / w;
208 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
210 _view.set_scale_offset(scale, offset);
213 if (event->touchPointStates() & Qt::TouchPointReleased) {
214 _pinch_zoom_active = false;
216 if (touchPoint0.state() & Qt::TouchPointReleased) {
217 // Primary touch released
218 _mouse_down_valid = false;
220 // Update the mouse down fields so that continued
221 // dragging with the primary touch will work correctly
222 _mouse_down_point = touchPoint0.pos().toPoint();
223 _mouse_down_offset = _view.offset();
224 _mouse_down_valid = true;
231 void Viewport::on_signals_changed()
233 const vector< shared_ptr<Trace> > traces(_view.get_traces());
234 for (shared_ptr<Trace> t : traces) {
236 connect(t.get(), SIGNAL(visibility_changed()),
237 this, SLOT(update()));
241 void Viewport::on_signals_moved()