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