]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
971eb755228f978a1355677adbfb9d6f5668615d
[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
25 #include "signal.hpp"
26 #include "view.hpp"
27 #include "viewitempaintparams.hpp"
28 #include "viewport.hpp"
29
30 #include <pv/session.hpp>
31
32 #include <QMouseEvent>
33
34 using std::abs;
35 using std::back_inserter;
36 using std::copy;
37 using std::max;
38 using std::min;
39 using std::none_of;
40 using std::shared_ptr;
41 using std::stable_sort;
42 using std::vector;
43
44 namespace pv {
45 namespace view {
46
47 Viewport::Viewport(View &parent) :
48         ViewWidget(parent),
49         mouse_down_valid_(false),
50         pinch_zoom_active_(false)
51 {
52         setAutoFillBackground(true);
53         setBackgroundRole(QPalette::Base);
54 }
55
56 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
57 {
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))
62                         return *i;
63         return nullptr;
64 }
65
66 vector< shared_ptr<ViewItem> > Viewport::items()
67 {
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));
71         return items;
72 }
73
74 bool Viewport::touch_event(QTouchEvent *event)
75 {
76         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
77
78         if (touchPoints.count() != 2) {
79                 pinch_zoom_active_ = false;
80                 return false;
81         }
82
83         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
84         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
85
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;
91         }
92
93         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
94         if (abs(w) >= 1.0) {
95                 double scale = (pinch_offset1_ - pinch_offset0_) / w;
96                 if (scale < 0)
97                         scale = -scale;
98                 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
99                 if (scale > 0)
100                         view_.set_scale_offset(scale, offset);
101         }
102
103         if (event->touchPointStates() & Qt::TouchPointReleased) {
104                 pinch_zoom_active_ = false;
105
106                 if (touchPoint0.state() & Qt::TouchPointReleased) {
107                         // Primary touch released
108                         mouse_down_valid_ = false;
109                 } else {
110                         // Update the mouse down fields so that continued
111                         // dragging with the primary touch will work correctly
112                         mouse_down_point_ = touchPoint0.pos().toPoint();
113                         mouse_down_offset_ = view_.offset();
114                         mouse_down_valid_ = true;
115                 }
116         }
117
118         return true;
119 }
120
121 void Viewport::paintEvent(QPaintEvent*)
122 {
123         vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
124         assert(none_of(row_items.begin(), row_items.end(),
125                 [](const shared_ptr<RowItem> &r) { return !r; }));
126
127         stable_sort(row_items.begin(), row_items.end(),
128                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
129                         return a->visual_v_offset() < b->visual_v_offset(); });
130
131         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
132         assert(none_of(time_items.begin(), time_items.end(),
133                 [](const shared_ptr<TimeItem> &t) { return !t; }));
134
135         QPainter p(this);
136         p.setRenderHint(QPainter::Antialiasing);
137
138         const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
139
140         for (const shared_ptr<TimeItem> t : time_items)
141                 t->paint_back(p, pp);
142         for (const shared_ptr<RowItem> r : row_items)
143                 r->paint_back(p, pp);
144
145         for (const shared_ptr<TimeItem> t : time_items)
146                 t->paint_mid(p, pp);
147         for (const shared_ptr<RowItem> r : row_items)
148                 r->paint_mid(p, pp);
149
150         for (const shared_ptr<RowItem> r : row_items)
151                 r->paint_fore(p, pp);
152         for (const shared_ptr<TimeItem> t : time_items)
153                 t->paint_fore(p, pp);
154
155         p.end();
156 }
157
158 void Viewport::mousePressEvent(QMouseEvent *event)
159 {
160         assert(event);
161
162         if (event->button() == Qt::LeftButton) {
163                 mouse_down_point_ = event->pos();
164                 mouse_down_offset_ = view_.offset();
165                 mouse_down_valid_ = true;
166         }
167 }
168
169 void Viewport::mouseReleaseEvent(QMouseEvent *event)
170 {
171         assert(event);
172
173         if (event->button() == Qt::LeftButton)
174                 mouse_down_valid_ = false;
175 }
176
177 void Viewport::mouseMoveEvent(QMouseEvent *event)
178 {
179         assert(event);
180
181         if (event->buttons() & Qt::LeftButton) {
182                 if (!mouse_down_valid_) {
183                         mouse_down_point_ = event->pos();
184                         mouse_down_offset_ = view_.offset();
185                         mouse_down_valid_ = true;
186                 }
187
188                 view_.set_scale_offset(view_.scale(),
189                         mouse_down_offset_ +
190                         (mouse_down_point_ - event->pos()).x() *
191                         view_.scale());
192         }
193 }
194
195 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
196 {
197         assert(event);
198
199         if (event->buttons() & Qt::LeftButton)
200                 view_.zoom(2.0, event->x());
201         else if (event->buttons() & Qt::RightButton)
202                 view_.zoom(-2.0, event->x());
203 }
204
205 void Viewport::wheelEvent(QWheelEvent *event)
206 {
207         assert(event);
208
209         if (event->orientation() == Qt::Vertical) {
210                 // Vertical scrolling is interpreted as zooming in/out
211                 view_.zoom(event->delta() / 120, event->x());
212         } else if (event->orientation() == Qt::Horizontal) {
213                 // Horizontal scrolling is interpreted as moving left/right
214                 view_.set_scale_offset(view_.scale(),
215                                        event->delta() * view_.scale()
216                                        + view_.offset());
217         }
218 }
219
220 } // namespace view
221 } // namespace pv