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 void Viewport::signals_updated()
61 for (shared_ptr<RowItem> r : _view) {
63 connect(r.get(), SIGNAL(appearance_changed()),
64 this, SLOT(update()));
68 void Viewport::paintEvent(QPaintEvent*)
70 vector< shared_ptr<RowItem> > row_items(_view.begin(), _view.end());
71 stable_sort(row_items.begin(), row_items.end(),
72 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
73 return a->visual_v_offset() < b->visual_v_offset(); });
76 p.setRenderHint(QPainter::Antialiasing);
78 if (_view.cursors_shown())
79 _view.cursors().draw_viewport_background(p, rect());
82 for (const shared_ptr<RowItem> r : row_items)
85 r->paint_back(p, 0, width());
88 for (const shared_ptr<RowItem> r : row_items)
89 r->paint_mid(p, 0, width());
91 for (const shared_ptr<RowItem> r : row_items)
92 r->paint_fore(p, 0, width());
94 if (_view.cursors_shown())
95 _view.cursors().draw_viewport_foreground(p, rect());
100 bool Viewport::event(QEvent *event)
102 switch (event->type()) {
103 case QEvent::TouchBegin:
104 case QEvent::TouchUpdate:
105 case QEvent::TouchEnd:
106 if (touchEvent(static_cast<QTouchEvent *>(event)))
114 return QWidget::event(event);
117 void Viewport::mousePressEvent(QMouseEvent *event)
121 if (event->button() == Qt::LeftButton) {
122 _mouse_down_point = event->pos();
123 _mouse_down_offset = _view.offset();
124 _mouse_down_valid = true;
128 void Viewport::mouseReleaseEvent(QMouseEvent *event)
132 if (event->button() == Qt::LeftButton)
133 _mouse_down_valid = false;
136 void Viewport::mouseMoveEvent(QMouseEvent *event)
140 if (event->buttons() & Qt::LeftButton) {
141 if (!_mouse_down_valid) {
142 _mouse_down_point = event->pos();
143 _mouse_down_offset = _view.offset();
144 _mouse_down_valid = true;
147 _view.set_scale_offset(_view.scale(),
149 (_mouse_down_point - event->pos()).x() *
154 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
158 if (event->buttons() & Qt::LeftButton)
159 _view.zoom(2.0, event->x());
160 else if (event->buttons() & Qt::RightButton)
161 _view.zoom(-2.0, event->x());
164 void Viewport::wheelEvent(QWheelEvent *event)
168 if (event->orientation() == Qt::Vertical) {
169 // Vertical scrolling is interpreted as zooming in/out
170 _view.zoom(event->delta() / 120, event->x());
171 } else if (event->orientation() == Qt::Horizontal) {
172 // Horizontal scrolling is interpreted as moving left/right
173 _view.set_scale_offset(_view.scale(),
174 event->delta() * _view.scale()
179 bool Viewport::touchEvent(QTouchEvent *event)
181 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
183 if (touchPoints.count() != 2) {
184 _pinch_zoom_active = false;
188 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
189 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
191 if (!_pinch_zoom_active ||
192 (event->touchPointStates() & Qt::TouchPointPressed)) {
193 _pinch_offset0 = _view.offset() + _view.scale() * touchPoint0.pos().x();
194 _pinch_offset1 = _view.offset() + _view.scale() * touchPoint1.pos().x();
195 _pinch_zoom_active = true;
198 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
200 double scale = (_pinch_offset1 - _pinch_offset0) / w;
203 double offset = _pinch_offset0 - touchPoint0.pos().x() * scale;
205 _view.set_scale_offset(scale, offset);
208 if (event->touchPointStates() & Qt::TouchPointReleased) {
209 _pinch_zoom_active = false;
211 if (touchPoint0.state() & Qt::TouchPointReleased) {
212 // Primary touch released
213 _mouse_down_valid = false;
215 // Update the mouse down fields so that continued
216 // dragging with the primary touch will work correctly
217 _mouse_down_point = touchPoint0.pos().toPoint();
218 _mouse_down_offset = _view.offset();
219 _mouse_down_valid = true;
226 void Viewport::on_signals_moved()