]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
Added ViewWidget as a common ancestor of all view widgets
[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::max;
36 using std::min;
37 using std::none_of;
38 using std::shared_ptr;
39 using std::stable_sort;
40 using std::vector;
41
42 namespace pv {
43 namespace view {
44
45 Viewport::Viewport(View &parent) :
46         ViewWidget(parent),
47         mouse_down_valid_(false),
48         pinch_zoom_active_(false)
49 {
50         setAttribute(Qt::WA_AcceptTouchEvents, true);
51
52         setAutoFillBackground(true);
53         setBackgroundRole(QPalette::Base);
54
55         connect(&view_, SIGNAL(signals_moved()),
56                 this, SLOT(on_signals_moved()));
57 }
58
59 void Viewport::paintEvent(QPaintEvent*)
60 {
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; }));
64
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(); });
68
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; }));
72
73         QPainter p(this);
74         p.setRenderHint(QPainter::Antialiasing);
75
76         const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
77
78         for (const shared_ptr<TimeItem> t : time_items)
79                 t->paint_back(p, pp);
80         for (const shared_ptr<RowItem> r : row_items)
81                 r->paint_back(p, pp);
82
83         for (const shared_ptr<TimeItem> t : time_items)
84                 t->paint_mid(p, pp);
85         for (const shared_ptr<RowItem> r : row_items)
86                 r->paint_mid(p, pp);
87
88         for (const shared_ptr<RowItem> r : row_items)
89                 r->paint_fore(p, pp);
90         for (const shared_ptr<TimeItem> t : time_items)
91                 t->paint_fore(p, pp);
92
93         p.end();
94 }
95
96 bool Viewport::event(QEvent *event)
97 {
98         switch (event->type()) {
99         case QEvent::TouchBegin:
100         case QEvent::TouchUpdate:
101         case QEvent::TouchEnd:
102                 if (touchEvent(static_cast<QTouchEvent *>(event)))
103                         return true;
104                 break;
105
106         default:
107                 break;
108         }
109
110         return QWidget::event(event);
111 }
112
113 void Viewport::mousePressEvent(QMouseEvent *event)
114 {
115         assert(event);
116
117         if (event->button() == Qt::LeftButton) {
118                 mouse_down_point_ = event->pos();
119                 mouse_down_offset_ = view_.offset();
120                 mouse_down_valid_ = true;
121         }
122 }
123
124 void Viewport::mouseReleaseEvent(QMouseEvent *event)
125 {
126         assert(event);
127
128         if (event->button() == Qt::LeftButton)
129                 mouse_down_valid_ = false;
130 }
131
132 void Viewport::mouseMoveEvent(QMouseEvent *event)
133 {
134         assert(event);
135
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;
141                 }
142
143                 view_.set_scale_offset(view_.scale(),
144                         mouse_down_offset_ +
145                         (mouse_down_point_ - event->pos()).x() *
146                         view_.scale());
147         }
148 }
149
150 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
151 {
152         assert(event);
153
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());
158 }
159
160 void Viewport::wheelEvent(QWheelEvent *event)
161 {
162         assert(event);
163
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()
171                                        + view_.offset());
172         }
173 }
174
175 bool Viewport::touchEvent(QTouchEvent *event)
176 {
177         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
178
179         if (touchPoints.count() != 2) {
180                 pinch_zoom_active_ = false;
181                 return false;
182         }
183
184         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
185         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
186
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;
192         }
193
194         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
195         if (abs(w) >= 1.0) {
196                 double scale = (pinch_offset1_ - pinch_offset0_) / w;
197                 if (scale < 0)
198                         scale = -scale;
199                 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
200                 if (scale > 0)
201                         view_.set_scale_offset(scale, offset);
202         }
203
204         if (event->touchPointStates() & Qt::TouchPointReleased) {
205                 pinch_zoom_active_ = false;
206
207                 if (touchPoint0.state() & Qt::TouchPointReleased) {
208                         // Primary touch released
209                         mouse_down_valid_ = false;
210                 } else {
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;
216                 }
217         }
218
219         return true;
220 }
221
222 void Viewport::on_signals_moved()
223 {
224         update();
225 }
226
227 } // namespace view
228 } // namespace pv