]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
Viewport: Implemented on-canvas drag
[pulseview.git] / pv / view / viewport.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <cassert>
22 #include <cmath>
23 #include <algorithm>
24 #include <limits>
25
26 #include "signal.hpp"
27 #include "view.hpp"
28 #include "viewitempaintparams.hpp"
29 #include "viewport.hpp"
30
31 #include <pv/session.hpp>
32
33 #include <QMouseEvent>
34
35 using std::abs;
36 using std::back_inserter;
37 using std::copy;
38 using std::max;
39 using std::min;
40 using std::none_of;
41 using std::numeric_limits;
42 using std::shared_ptr;
43 using std::stable_sort;
44 using std::vector;
45
46 namespace pv {
47 namespace view {
48
49 Viewport::Viewport(View &parent) :
50         ViewWidget(parent),
51         drag_offset_(numeric_limits<double>::signaling_NaN()),
52         pinch_zoom_active_(false)
53 {
54         setAutoFillBackground(true);
55         setBackgroundRole(QPalette::Base);
56 }
57
58 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
59 {
60         const vector< shared_ptr<ViewItem> > items(this->items());
61         for (auto i = items.rbegin(); i != items.rend(); i++)
62                 if ((*i)->enabled() &&
63                         (*i)->hit_box_rect(rect()).contains(pt))
64                         return *i;
65         return nullptr;
66 }
67
68 void Viewport::drag()
69 {
70         drag_offset_ = view_.offset();
71 }
72
73 void Viewport::drag_by(const QPoint &delta)
74 {
75         if (isnan(drag_offset_))
76                 return;
77
78         view_.set_scale_offset(view_.scale(), drag_offset_ -
79                 delta.x() * view_.scale());
80 }
81
82 void Viewport::drag_release()
83 {
84         drag_offset_ = numeric_limits<double>::signaling_NaN();
85 }
86
87 vector< shared_ptr<ViewItem> > Viewport::items()
88 {
89         vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
90         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
91         copy(time_items.begin(), time_items.end(), back_inserter(items));
92         return items;
93 }
94
95 bool Viewport::touch_event(QTouchEvent *event)
96 {
97         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
98
99         if (touchPoints.count() != 2) {
100                 pinch_zoom_active_ = false;
101                 return false;
102         }
103
104         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
105         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
106
107         if (!pinch_zoom_active_ ||
108             (event->touchPointStates() & Qt::TouchPointPressed)) {
109                 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
110                 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
111                 pinch_zoom_active_ = true;
112         }
113
114         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
115         if (abs(w) >= 1.0) {
116                 const double scale =
117                         fabs((pinch_offset1_ - pinch_offset0_) / w);
118                 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
119                 if (scale > 0)
120                         view_.set_scale_offset(scale, offset);
121         }
122
123         if (event->touchPointStates() & Qt::TouchPointReleased) {
124                 pinch_zoom_active_ = false;
125
126                 if (touchPoint0.state() & Qt::TouchPointReleased) {
127                         // Primary touch released
128                         drag_release();
129                 } else {
130                         // Update the mouse down fields so that continued
131                         // dragging with the primary touch will work correctly
132                         mouse_down_point_ = touchPoint0.pos().toPoint();
133                         drag();
134                 }
135         }
136
137         return true;
138 }
139
140 void Viewport::paintEvent(QPaintEvent*)
141 {
142         vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
143         assert(none_of(row_items.begin(), row_items.end(),
144                 [](const shared_ptr<RowItem> &r) { return !r; }));
145
146         stable_sort(row_items.begin(), row_items.end(),
147                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
148                         return a->visual_v_offset() < b->visual_v_offset(); });
149
150         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
151         assert(none_of(time_items.begin(), time_items.end(),
152                 [](const shared_ptr<TimeItem> &t) { return !t; }));
153
154         QPainter p(this);
155         p.setRenderHint(QPainter::Antialiasing);
156
157         const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
158
159         for (const shared_ptr<TimeItem> t : time_items)
160                 t->paint_back(p, pp);
161         for (const shared_ptr<RowItem> r : row_items)
162                 r->paint_back(p, pp);
163
164         for (const shared_ptr<TimeItem> t : time_items)
165                 t->paint_mid(p, pp);
166         for (const shared_ptr<RowItem> r : row_items)
167                 r->paint_mid(p, pp);
168
169         for (const shared_ptr<RowItem> r : row_items)
170                 r->paint_fore(p, pp);
171         for (const shared_ptr<TimeItem> t : time_items)
172                 t->paint_fore(p, pp);
173
174         p.end();
175 }
176
177 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
178 {
179         assert(event);
180
181         if (event->buttons() & Qt::LeftButton)
182                 view_.zoom(2.0, event->x());
183         else if (event->buttons() & Qt::RightButton)
184                 view_.zoom(-2.0, event->x());
185 }
186
187 void Viewport::wheelEvent(QWheelEvent *event)
188 {
189         assert(event);
190
191         if (event->orientation() == Qt::Vertical) {
192                 // Vertical scrolling is interpreted as zooming in/out
193                 view_.zoom(event->delta() / 120, event->x());
194         } else if (event->orientation() == Qt::Horizontal) {
195                 // Horizontal scrolling is interpreted as moving left/right
196                 view_.set_scale_offset(view_.scale(),
197                                        event->delta() * view_.scale()
198                                        + view_.offset());
199         }
200 }
201
202 } // namespace view
203 } // namespace pv