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
25 #include "rowitempaintparams.hpp"
28 #include "viewport.hpp"
30 #include <pv/session.hpp>
32 #include <QMouseEvent>
37 using std::shared_ptr;
38 using std::stable_sort;
44 Viewport::Viewport(View &parent) :
47 mouse_down_valid_(false),
48 pinch_zoom_active_(false)
50 setAttribute(Qt::WA_AcceptTouchEvents, true);
52 setMouseTracking(true);
53 setAutoFillBackground(true);
54 setBackgroundRole(QPalette::Base);
56 connect(&view_, SIGNAL(signals_moved()),
57 this, SLOT(on_signals_moved()));
60 void Viewport::paintEvent(QPaintEvent*)
62 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
63 stable_sort(row_items.begin(), row_items.end(),
64 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
65 return a->visual_v_offset() < b->visual_v_offset(); });
68 p.setRenderHint(QPainter::Antialiasing);
70 if (view_.cursors_shown())
71 view_.cursors()->draw_viewport_background(p, rect());
73 const RowItemPaintParams pp(0, width(), view_.scale(), view_.offset());
76 for (const shared_ptr<RowItem> r : row_items)
82 for (const shared_ptr<RowItem> r : row_items)
85 for (const shared_ptr<RowItem> r : row_items)
88 if (view_.cursors_shown())
89 view_.cursors()->draw_viewport_foreground(p, rect());
94 bool Viewport::event(QEvent *event)
96 switch (event->type()) {
97 case QEvent::TouchBegin:
98 case QEvent::TouchUpdate:
99 case QEvent::TouchEnd:
100 if (touchEvent(static_cast<QTouchEvent *>(event)))
108 return QWidget::event(event);
111 void Viewport::mousePressEvent(QMouseEvent *event)
115 if (event->button() == Qt::LeftButton) {
116 mouse_down_point_ = event->pos();
117 mouse_down_offset_ = view_.offset();
118 mouse_down_valid_ = true;
122 void Viewport::mouseReleaseEvent(QMouseEvent *event)
126 if (event->button() == Qt::LeftButton)
127 mouse_down_valid_ = false;
130 void Viewport::mouseMoveEvent(QMouseEvent *event)
134 if (event->buttons() & Qt::LeftButton) {
135 if (!mouse_down_valid_) {
136 mouse_down_point_ = event->pos();
137 mouse_down_offset_ = view_.offset();
138 mouse_down_valid_ = true;
141 view_.set_scale_offset(view_.scale(),
143 (mouse_down_point_ - event->pos()).x() *
148 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
152 if (event->buttons() & Qt::LeftButton)
153 view_.zoom(2.0, event->x());
154 else if (event->buttons() & Qt::RightButton)
155 view_.zoom(-2.0, event->x());
158 void Viewport::wheelEvent(QWheelEvent *event)
162 if (event->orientation() == Qt::Vertical) {
163 // Vertical scrolling is interpreted as zooming in/out
164 view_.zoom(event->delta() / 120, event->x());
165 } else if (event->orientation() == Qt::Horizontal) {
166 // Horizontal scrolling is interpreted as moving left/right
167 view_.set_scale_offset(view_.scale(),
168 event->delta() * view_.scale()
173 bool Viewport::touchEvent(QTouchEvent *event)
175 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
177 if (touchPoints.count() != 2) {
178 pinch_zoom_active_ = false;
182 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
183 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
185 if (!pinch_zoom_active_ ||
186 (event->touchPointStates() & Qt::TouchPointPressed)) {
187 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
188 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
189 pinch_zoom_active_ = true;
192 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
194 double scale = (pinch_offset1_ - pinch_offset0_) / w;
197 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
199 view_.set_scale_offset(scale, offset);
202 if (event->touchPointStates() & Qt::TouchPointReleased) {
203 pinch_zoom_active_ = false;
205 if (touchPoint0.state() & Qt::TouchPointReleased) {
206 // Primary touch released
207 mouse_down_valid_ = false;
209 // Update the mouse down fields so that continued
210 // dragging with the primary touch will work correctly
211 mouse_down_point_ = touchPoint0.pos().toPoint();
212 mouse_down_offset_ = view_.offset();
213 mouse_down_valid_ = true;
220 void Viewport::on_signals_moved()