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) :
47 mouse_down_valid_(false),
48 pinch_zoom_active_(false)
50 setAttribute(Qt::WA_AcceptTouchEvents, true);
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
55 connect(&view_, SIGNAL(signals_moved()),
56 this, SLOT(on_signals_moved()));
59 void Viewport::paintEvent(QPaintEvent*)
61 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
62 assert(none_of(row_items.begin(), row_items.end(),
63 [](const shared_ptr<RowItem> &r) { return !r; }));
65 stable_sort(row_items.begin(), row_items.end(),
66 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
67 return a->visual_v_offset() < b->visual_v_offset(); });
69 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
70 assert(none_of(time_items.begin(), time_items.end(),
71 [](const shared_ptr<TimeItem> &t) { return !t; }));
74 p.setRenderHint(QPainter::Antialiasing);
76 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
78 for (const shared_ptr<TimeItem> t : time_items)
80 for (const shared_ptr<RowItem> r : row_items)
83 for (const shared_ptr<TimeItem> t : time_items)
85 for (const shared_ptr<RowItem> r : row_items)
88 for (const shared_ptr<RowItem> r : row_items)
90 for (const shared_ptr<TimeItem> t : time_items)
96 bool Viewport::event(QEvent *event)
98 switch (event->type()) {
99 case QEvent::TouchBegin:
100 case QEvent::TouchUpdate:
101 case QEvent::TouchEnd:
102 if (touchEvent(static_cast<QTouchEvent *>(event)))
110 return QWidget::event(event);
113 void Viewport::mousePressEvent(QMouseEvent *event)
117 if (event->button() == Qt::LeftButton) {
118 mouse_down_point_ = event->pos();
119 mouse_down_offset_ = view_.offset();
120 mouse_down_valid_ = true;
124 void Viewport::mouseReleaseEvent(QMouseEvent *event)
128 if (event->button() == Qt::LeftButton)
129 mouse_down_valid_ = false;
132 void Viewport::mouseMoveEvent(QMouseEvent *event)
136 if (event->buttons() & Qt::LeftButton) {
137 if (!mouse_down_valid_) {
138 mouse_down_point_ = event->pos();
139 mouse_down_offset_ = view_.offset();
140 mouse_down_valid_ = true;
143 view_.set_scale_offset(view_.scale(),
145 (mouse_down_point_ - event->pos()).x() *
150 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
154 if (event->buttons() & Qt::LeftButton)
155 view_.zoom(2.0, event->x());
156 else if (event->buttons() & Qt::RightButton)
157 view_.zoom(-2.0, event->x());
160 void Viewport::wheelEvent(QWheelEvent *event)
164 if (event->orientation() == Qt::Vertical) {
165 // Vertical scrolling is interpreted as zooming in/out
166 view_.zoom(event->delta() / 120, event->x());
167 } else if (event->orientation() == Qt::Horizontal) {
168 // Horizontal scrolling is interpreted as moving left/right
169 view_.set_scale_offset(view_.scale(),
170 event->delta() * view_.scale()
175 bool Viewport::touchEvent(QTouchEvent *event)
177 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
179 if (touchPoints.count() != 2) {
180 pinch_zoom_active_ = false;
184 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
185 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
187 if (!pinch_zoom_active_ ||
188 (event->touchPointStates() & Qt::TouchPointPressed)) {
189 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
190 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
191 pinch_zoom_active_ = true;
194 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
196 double scale = (pinch_offset1_ - pinch_offset0_) / w;
199 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
201 view_.set_scale_offset(scale, offset);
204 if (event->touchPointStates() & Qt::TouchPointReleased) {
205 pinch_zoom_active_ = false;
207 if (touchPoint0.state() & Qt::TouchPointReleased) {
208 // Primary touch released
209 mouse_down_valid_ = false;
211 // Update the mouse down fields so that continued
212 // dragging with the primary touch will work correctly
213 mouse_down_point_ = touchPoint0.pos().toPoint();
214 mouse_down_offset_ = view_.offset();
215 mouse_down_valid_ = true;
222 void Viewport::on_signals_moved()