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>
35 using std::back_inserter;
40 using std::shared_ptr;
41 using std::stable_sort;
47 Viewport::Viewport(View &parent) :
49 mouse_down_valid_(false),
50 pinch_zoom_active_(false)
52 setAutoFillBackground(true);
53 setBackgroundRole(QPalette::Base);
56 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
58 const vector< shared_ptr<ViewItem> > items(this->items());
59 for (auto i = items.rbegin(); i != items.rend(); i++)
60 if ((*i)->enabled() &&
61 (*i)->hit_box_rect(rect()).contains(pt))
66 vector< shared_ptr<ViewItem> > Viewport::items()
68 vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
69 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
70 copy(time_items.begin(), time_items.end(), back_inserter(items));
74 bool Viewport::touch_event(QTouchEvent *event)
76 QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
78 if (touchPoints.count() != 2) {
79 pinch_zoom_active_ = false;
83 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
84 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
86 if (!pinch_zoom_active_ ||
87 (event->touchPointStates() & Qt::TouchPointPressed)) {
88 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
89 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
90 pinch_zoom_active_ = true;
93 double w = touchPoint1.pos().x() - touchPoint0.pos().x();
96 fabs((pinch_offset1_ - pinch_offset0_) / w);
97 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
99 view_.set_scale_offset(scale, offset);
102 if (event->touchPointStates() & Qt::TouchPointReleased) {
103 pinch_zoom_active_ = false;
105 if (touchPoint0.state() & Qt::TouchPointReleased) {
106 // Primary touch released
107 mouse_down_valid_ = false;
109 // Update the mouse down fields so that continued
110 // dragging with the primary touch will work correctly
111 mouse_down_point_ = touchPoint0.pos().toPoint();
112 mouse_down_offset_ = view_.offset();
113 mouse_down_valid_ = true;
120 void Viewport::paintEvent(QPaintEvent*)
122 vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
123 assert(none_of(row_items.begin(), row_items.end(),
124 [](const shared_ptr<RowItem> &r) { return !r; }));
126 stable_sort(row_items.begin(), row_items.end(),
127 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
128 return a->visual_v_offset() < b->visual_v_offset(); });
130 const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
131 assert(none_of(time_items.begin(), time_items.end(),
132 [](const shared_ptr<TimeItem> &t) { return !t; }));
135 p.setRenderHint(QPainter::Antialiasing);
137 const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
139 for (const shared_ptr<TimeItem> t : time_items)
140 t->paint_back(p, pp);
141 for (const shared_ptr<RowItem> r : row_items)
142 r->paint_back(p, pp);
144 for (const shared_ptr<TimeItem> t : time_items)
146 for (const shared_ptr<RowItem> r : row_items)
149 for (const shared_ptr<RowItem> r : row_items)
150 r->paint_fore(p, pp);
151 for (const shared_ptr<TimeItem> t : time_items)
152 t->paint_fore(p, pp);
157 void Viewport::mousePressEvent(QMouseEvent *event)
161 if (event->button() == Qt::LeftButton) {
162 mouse_down_point_ = event->pos();
163 mouse_down_offset_ = view_.offset();
164 mouse_down_valid_ = true;
168 void Viewport::mouseReleaseEvent(QMouseEvent *event)
172 if (event->button() == Qt::LeftButton)
173 mouse_down_valid_ = false;
176 void Viewport::mouseMoveEvent(QMouseEvent *event)
180 if (event->buttons() & Qt::LeftButton) {
181 if (!mouse_down_valid_) {
182 mouse_down_point_ = event->pos();
183 mouse_down_offset_ = view_.offset();
184 mouse_down_valid_ = true;
187 view_.set_scale_offset(view_.scale(),
189 (mouse_down_point_ - event->pos()).x() *
194 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
198 if (event->buttons() & Qt::LeftButton)
199 view_.zoom(2.0, event->x());
200 else if (event->buttons() & Qt::RightButton)
201 view_.zoom(-2.0, event->x());
204 void Viewport::wheelEvent(QWheelEvent *event)
208 if (event->orientation() == Qt::Vertical) {
209 // Vertical scrolling is interpreted as zooming in/out
210 view_.zoom(event->delta() / 120, event->x());
211 } else if (event->orientation() == Qt::Horizontal) {
212 // Horizontal scrolling is interpreted as moving left/right
213 view_.set_scale_offset(view_.scale(),
214 event->delta() * view_.scale()