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 "viewitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
38 using std::shared_ptr;
39 using std::stable_sort;
45 Viewport::Viewport(View &parent) :
48 mouse_down_valid_(false),
49 pinch_zoom_active_(false)
51 setAttribute(Qt::WA_AcceptTouchEvents, true);
53 setMouseTracking(true);
54 setAutoFillBackground(true);
55 setBackgroundRole(QPalette::Base);
57 connect(&view_, SIGNAL(signals_moved()),
58 this, SLOT(on_signals_moved()));
61 void Viewport::paintEvent(QPaintEvent*)
63 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
64 assert(none_of(row_items.begin(), row_items.end(),
65 [](const shared_ptr<RowItem> &r) { return !r; }));
67 stable_sort(row_items.begin(), row_items.end(),
68 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
69 return a->visual_v_offset() < b->visual_v_offset(); });
71 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
72 assert(none_of(time_items.begin(), time_items.end(),
73 [](const shared_ptr<TimeItem> &t) { return !t; }));
76 p.setRenderHint(QPainter::Antialiasing);
78 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
80 for (const shared_ptr<TimeItem> t : time_items)
82 for (const shared_ptr<RowItem> r : row_items)
85 for (const shared_ptr<TimeItem> t : time_items)
87 for (const shared_ptr<RowItem> r : row_items)
90 for (const shared_ptr<RowItem> r : row_items)
92 for (const shared_ptr<TimeItem> t : time_items)
98 bool Viewport::event(QEvent *event)
100 switch (event->type()) {
101 case QEvent::TouchBegin:
102 case QEvent::TouchUpdate:
103 case QEvent::TouchEnd:
104 if (touchEvent(static_cast<QTouchEvent *>(event)))
112 return QWidget::event(event);
115 void Viewport::mousePressEvent(QMouseEvent *event)
119 if (event->button() == Qt::LeftButton) {
120 mouse_down_point_ = event->pos();
121 mouse_down_offset_ = view_.offset();
122 mouse_down_valid_ = true;
126 void Viewport::mouseReleaseEvent(QMouseEvent *event)
130 if (event->button() == Qt::LeftButton)
131 mouse_down_valid_ = false;
134 void Viewport::mouseMoveEvent(QMouseEvent *event)
138 if (event->buttons() & Qt::LeftButton) {
139 if (!mouse_down_valid_) {
140 mouse_down_point_ = event->pos();
141 mouse_down_offset_ = view_.offset();
142 mouse_down_valid_ = true;
145 view_.set_scale_offset(view_.scale(),
147 (mouse_down_point_ - event->pos()).x() *
152 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
156 if (event->buttons() & Qt::LeftButton)
157 view_.zoom(2.0, event->x());
158 else if (event->buttons() & Qt::RightButton)
159 view_.zoom(-2.0, event->x());
162 void Viewport::wheelEvent(QWheelEvent *event)
166 if (event->orientation() == Qt::Vertical) {
167 // Vertical scrolling is interpreted as zooming in/out
168 view_.zoom(event->delta() / 120, event->x());
169 } else if (event->orientation() == Qt::Horizontal) {
170 // Horizontal scrolling is interpreted as moving left/right
171 view_.set_scale_offset(view_.scale(),
172 event->delta() * view_.scale()
177 bool Viewport::touchEvent(QTouchEvent *event)
179 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
181 if (touchPoints.count() != 2) {
182 pinch_zoom_active_ = false;
186 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
187 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
189 if (!pinch_zoom_active_ ||
190 (event->touchPointStates() & Qt::TouchPointPressed)) {
191 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
192 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
193 pinch_zoom_active_ = true;
196 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
198 double scale = (pinch_offset1_ - pinch_offset0_) / w;
201 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
203 view_.set_scale_offset(scale, offset);
206 if (event->touchPointStates() & Qt::TouchPointReleased) {
207 pinch_zoom_active_ = false;
209 if (touchPoint0.state() & Qt::TouchPointReleased) {
210 // Primary touch released
211 mouse_down_valid_ = false;
213 // Update the mouse down fields so that continued
214 // dragging with the primary touch will work correctly
215 mouse_down_point_ = touchPoint0.pos().toPoint();
216 mouse_down_offset_ = view_.offset();
217 mouse_down_valid_ = true;
224 void Viewport::on_signals_moved()