]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
ViewItem: Use drag_point() with drag_by()
[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         QWidget(&parent),
47         view_(parent),
48         mouse_down_valid_(false),
49         pinch_zoom_active_(false)
50 {
51         setAttribute(Qt::WA_AcceptTouchEvents, true);
52
53         setMouseTracking(true);
54         setAutoFillBackground(true);
55         setBackgroundRole(QPalette::Base);
56
57         connect(&view_, SIGNAL(signals_moved()),
58                 this, SLOT(on_signals_moved()));
59 }
60
61 void Viewport::paintEvent(QPaintEvent*)
62 {
63         vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
64         assert(none_of(row_items.begin(), row_items.end(),
65                 [](const shared_ptr<RowItem> &r) { return !r; }));
66
67         stable_sort(row_items.begin(), row_items.end(),
68                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
69                         return a->visual_v_offset() < b->visual_v_offset(); });
70
71         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
72         assert(none_of(time_items.begin(), time_items.end(),
73                 [](const shared_ptr<TimeItem> &t) { return !t; }));
74
75         QPainter p(this);
76         p.setRenderHint(QPainter::Antialiasing);
77
78         const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
79
80         for (const shared_ptr<TimeItem> t : time_items)
81                 t->paint_back(p, pp);
82         for (const shared_ptr<RowItem> r : row_items)
83                 r->paint_back(p, pp);
84
85         for (const shared_ptr<TimeItem> t : time_items)
86                 t->paint_mid(p, pp);
87         for (const shared_ptr<RowItem> r : row_items)
88                 r->paint_mid(p, pp);
89
90         for (const shared_ptr<RowItem> r : row_items)
91                 r->paint_fore(p, pp);
92         for (const shared_ptr<TimeItem> t : time_items)
93                 t->paint_fore(p, pp);
94
95         p.end();
96 }
97
98 bool Viewport::event(QEvent *event)
99 {
100         switch (event->type()) {
101         case QEvent::TouchBegin:
102         case QEvent::TouchUpdate:
103         case QEvent::TouchEnd:
104                 if (touchEvent(static_cast<QTouchEvent *>(event)))
105                         return true;
106                 break;
107
108         default:
109                 break;
110         }
111
112         return QWidget::event(event);
113 }
114
115 void Viewport::mousePressEvent(QMouseEvent *event)
116 {
117         assert(event);
118
119         if (event->button() == Qt::LeftButton) {
120                 mouse_down_point_ = event->pos();
121                 mouse_down_offset_ = view_.offset();
122                 mouse_down_valid_ = true;
123         }
124 }
125
126 void Viewport::mouseReleaseEvent(QMouseEvent *event)
127 {
128         assert(event);
129
130         if (event->button() == Qt::LeftButton)
131                 mouse_down_valid_ = false;
132 }
133
134 void Viewport::mouseMoveEvent(QMouseEvent *event)
135 {
136         assert(event);
137
138         if (event->buttons() & Qt::LeftButton) {
139                 if (!mouse_down_valid_) {
140                         mouse_down_point_ = event->pos();
141                         mouse_down_offset_ = view_.offset();
142                         mouse_down_valid_ = true;
143                 }
144
145                 view_.set_scale_offset(view_.scale(),
146                         mouse_down_offset_ +
147                         (mouse_down_point_ - event->pos()).x() *
148                         view_.scale());
149         }
150 }
151
152 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
153 {
154         assert(event);
155
156         if (event->buttons() & Qt::LeftButton)
157                 view_.zoom(2.0, event->x());
158         else if (event->buttons() & Qt::RightButton)
159                 view_.zoom(-2.0, event->x());
160 }
161
162 void Viewport::wheelEvent(QWheelEvent *event)
163 {
164         assert(event);
165
166         if (event->orientation() == Qt::Vertical) {
167                 // Vertical scrolling is interpreted as zooming in/out
168                 view_.zoom(event->delta() / 120, event->x());
169         } else if (event->orientation() == Qt::Horizontal) {
170                 // Horizontal scrolling is interpreted as moving left/right
171                 view_.set_scale_offset(view_.scale(),
172                                        event->delta() * view_.scale()
173                                        + view_.offset());
174         }
175 }
176
177 bool Viewport::touchEvent(QTouchEvent *event)
178 {
179         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
180
181         if (touchPoints.count() != 2) {
182                 pinch_zoom_active_ = false;
183                 return false;
184         }
185
186         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
187         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
188
189         if (!pinch_zoom_active_ ||
190             (event->touchPointStates() & Qt::TouchPointPressed)) {
191                 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
192                 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
193                 pinch_zoom_active_ = true;
194         }
195
196         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
197         if (abs(w) >= 1.0) {
198                 double scale = (pinch_offset1_ - pinch_offset0_) / w;
199                 if (scale < 0)
200                         scale = -scale;
201                 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
202                 if (scale > 0)
203                         view_.set_scale_offset(scale, offset);
204         }
205
206         if (event->touchPointStates() & Qt::TouchPointReleased) {
207                 pinch_zoom_active_ = false;
208
209                 if (touchPoint0.state() & Qt::TouchPointReleased) {
210                         // Primary touch released
211                         mouse_down_valid_ = false;
212                 } else {
213                         // Update the mouse down fields so that continued
214                         // dragging with the primary touch will work correctly
215                         mouse_down_point_ = touchPoint0.pos().toPoint();
216                         mouse_down_offset_ = view_.offset();
217                         mouse_down_valid_ = true;
218                 }
219         }
220
221         return true;
222 }
223
224 void Viewport::on_signals_moved()
225 {
226         update();
227 }
228
229 } // namespace view
230 } // namespace pv