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
29 #include "../sigsession.h"
31 #include <QMouseEvent>
36 using std::shared_ptr;
37 using std::stable_sort;
43 Viewport::Viewport(View &parent) :
46 _mouse_down_valid(false),
47 _pinch_zoom_active(false)
49 setAttribute(Qt::WA_AcceptTouchEvents, true);
51 setMouseTracking(true);
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
55 connect(&_view, SIGNAL(signals_moved()),
56 this, SLOT(on_signals_moved()));
59 int Viewport::get_total_height() const
63 h = max(i->v_offset() + View::SignalHeight, h);
67 void Viewport::signals_updated()
69 for (shared_ptr<RowItem> r : _view) {
71 connect(r.get(), SIGNAL(appearance_changed()),
72 this, SLOT(update()));
76 void Viewport::paintEvent(QPaintEvent*)
78 vector< shared_ptr<RowItem> > row_items(_view.begin(), _view.end());
79 stable_sort(row_items.begin(), row_items.end(),
80 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
81 return a->v_offset() < b->v_offset(); });
84 p.setRenderHint(QPainter::Antialiasing);
86 if (_view.cursors_shown())
87 _view.cursors().draw_viewport_background(p, rect());
90 for (const shared_ptr<RowItem> r : row_items)
93 r->paint_back(p, 0, width());
96 for (const shared_ptr<RowItem> r : row_items)
97 r->paint_mid(p, 0, width());
99 for (const shared_ptr<RowItem> r : row_items)
100 r->paint_fore(p, 0, width());
102 if (_view.cursors_shown())
103 _view.cursors().draw_viewport_foreground(p, rect());
108 bool Viewport::event(QEvent *event)
110 switch (event->type()) {
111 case QEvent::TouchBegin:
112 case QEvent::TouchUpdate:
113 case QEvent::TouchEnd:
114 if (touchEvent(static_cast<QTouchEvent *>(event)))
122 return QWidget::event(event);
125 void Viewport::mousePressEvent(QMouseEvent *event)
129 if (event->button() == Qt::LeftButton) {
130 _mouse_down_point = event->pos();
131 _mouse_down_offset = _view.offset();
132 _mouse_down_valid = true;
136 void Viewport::mouseReleaseEvent(QMouseEvent *event)
140 if (event->button() == Qt::LeftButton)
141 _mouse_down_valid = false;
144 void Viewport::mouseMoveEvent(QMouseEvent *event)
148 if (event->buttons() & Qt::LeftButton) {
149 if (!_mouse_down_valid) {
150 _mouse_down_point = event->pos();
151 _mouse_down_offset = _view.offset();
152 _mouse_down_valid = true;
155 _view.set_scale_offset(_view.scale(),
157 (_mouse_down_point - event->pos()).x() *
162 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
166 if (event->buttons() & Qt::LeftButton)
167 _view.zoom(2.0, event->x());
168 else if (event->buttons() & Qt::RightButton)
169 _view.zoom(-2.0, event->x());
172 void Viewport::wheelEvent(QWheelEvent *event)
176 if (event->orientation() == Qt::Vertical) {
177 // Vertical scrolling is interpreted as zooming in/out
178 _view.zoom(event->delta() / 120, event->x());
179 } else if (event->orientation() == Qt::Horizontal) {
180 // Horizontal scrolling is interpreted as moving left/right
181 _view.set_scale_offset(_view.scale(),
182 event->delta() * _view.scale()
187 bool Viewport::touchEvent(QTouchEvent *event)
189 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
191 if (touchPoints.count() != 2) {
192 _pinch_zoom_active = false;
196 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
197 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
199 if (!_pinch_zoom_active ||
200 (event->touchPointStates() & Qt::TouchPointPressed)) {
201 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
202 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
203 _pinch_zoom_active = true;
206 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
208 double scale = (_pinch_offset1 - _pinch_offset0) / w;
211 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
213 _view.set_scale_offset(scale, offset);
216 if (event->touchPointStates() & Qt::TouchPointReleased) {
217 _pinch_zoom_active = false;
219 if (touchPoint0.state() & Qt::TouchPointReleased) {
220 // Primary touch released
221 _mouse_down_valid = false;
223 // Update the mouse down fields so that continued
224 // dragging with the primary touch will work correctly
225 _mouse_down_point = touchPoint0.pos().toPoint();
226 _mouse_down_offset = _view.offset();
227 _mouse_down_valid = true;
234 void Viewport::on_signals_moved()