]> sigrok.org Git - pulseview.git/blob - pv/view/viewport.cpp
Fix an isnan() issue on (at least) MinGW and Mac OS X.
[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::dynamic_pointer_cast;
39 using std::max;
40 using std::min;
41 using std::none_of;
42 using std::numeric_limits;
43 using std::shared_ptr;
44 using std::stable_sort;
45 using std::vector;
46
47 namespace pv {
48 namespace view {
49
50 Viewport::Viewport(View &parent) :
51         ViewWidget(parent),
52         drag_offset_(numeric_limits<double>::signaling_NaN()),
53         pinch_zoom_active_(false)
54 {
55         setAutoFillBackground(true);
56         setBackgroundRole(QPalette::Base);
57 }
58
59 shared_ptr<ViewItem> Viewport::get_mouse_over_item(const QPoint &pt)
60 {
61         const vector< shared_ptr<ViewItem> > items(this->items());
62         for (auto i = items.rbegin(); i != items.rend(); i++)
63                 if ((*i)->enabled() &&
64                         (*i)->hit_box_rect(rect()).contains(pt))
65                         return *i;
66         return nullptr;
67 }
68
69 void Viewport::item_hover(const shared_ptr<ViewItem> &item)
70 {
71         if (item)
72                 setCursor(dynamic_pointer_cast<RowItem>(item) ?
73                         Qt::SizeVerCursor : Qt::SizeHorCursor);
74         else
75                 unsetCursor();
76 }
77
78 void Viewport::drag()
79 {
80         drag_offset_ = view_.offset();
81 }
82
83 void Viewport::drag_by(const QPoint &delta)
84 {
85         // Use std::isnan() instead of isnan(), the latter can cause issues.
86         if (std::isnan(drag_offset_))
87                 return;
88
89         view_.set_scale_offset(view_.scale(), drag_offset_ -
90                 delta.x() * view_.scale());
91 }
92
93 void Viewport::drag_release()
94 {
95         drag_offset_ = numeric_limits<double>::signaling_NaN();
96 }
97
98 vector< shared_ptr<ViewItem> > Viewport::items()
99 {
100         vector< shared_ptr<ViewItem> > items(view_.begin(), view_.end());
101         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
102         copy(time_items.begin(), time_items.end(), back_inserter(items));
103         return items;
104 }
105
106 bool Viewport::touch_event(QTouchEvent *event)
107 {
108         QList<QTouchEvent::TouchPoint> touchPoints = event->touchPoints();
109
110         if (touchPoints.count() != 2) {
111                 pinch_zoom_active_ = false;
112                 return false;
113         }
114
115         const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
116         const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
117
118         if (!pinch_zoom_active_ ||
119             (event->touchPointStates() & Qt::TouchPointPressed)) {
120                 pinch_offset0_ = view_.offset() + view_.scale() * touchPoint0.pos().x();
121                 pinch_offset1_ = view_.offset() + view_.scale() * touchPoint1.pos().x();
122                 pinch_zoom_active_ = true;
123         }
124
125         double w = touchPoint1.pos().x() - touchPoint0.pos().x();
126         if (abs(w) >= 1.0) {
127                 const double scale =
128                         fabs((pinch_offset1_ - pinch_offset0_) / w);
129                 double offset = pinch_offset0_ - touchPoint0.pos().x() * scale;
130                 if (scale > 0)
131                         view_.set_scale_offset(scale, offset);
132         }
133
134         if (event->touchPointStates() & Qt::TouchPointReleased) {
135                 pinch_zoom_active_ = false;
136
137                 if (touchPoint0.state() & Qt::TouchPointReleased) {
138                         // Primary touch released
139                         drag_release();
140                 } else {
141                         // Update the mouse down fields so that continued
142                         // dragging with the primary touch will work correctly
143                         mouse_down_point_ = touchPoint0.pos().toPoint();
144                         drag();
145                 }
146         }
147
148         return true;
149 }
150
151 void Viewport::paintEvent(QPaintEvent*)
152 {
153         vector< shared_ptr<RowItem> > row_items(view_.begin(), view_.end());
154         assert(none_of(row_items.begin(), row_items.end(),
155                 [](const shared_ptr<RowItem> &r) { return !r; }));
156
157         stable_sort(row_items.begin(), row_items.end(),
158                 [](const shared_ptr<RowItem> &a, const shared_ptr<RowItem> &b) {
159                         return a->visual_v_offset() < b->visual_v_offset(); });
160
161         const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
162         assert(none_of(time_items.begin(), time_items.end(),
163                 [](const shared_ptr<TimeItem> &t) { return !t; }));
164
165         QPainter p(this);
166         p.setRenderHint(QPainter::Antialiasing);
167
168         const ViewItemPaintParams pp(rect(), view_.scale(), view_.offset());
169
170         for (const shared_ptr<TimeItem> t : time_items)
171                 t->paint_back(p, pp);
172         for (const shared_ptr<RowItem> r : row_items)
173                 r->paint_back(p, pp);
174
175         for (const shared_ptr<TimeItem> t : time_items)
176                 t->paint_mid(p, pp);
177         for (const shared_ptr<RowItem> r : row_items)
178                 r->paint_mid(p, pp);
179
180         for (const shared_ptr<RowItem> r : row_items)
181                 r->paint_fore(p, pp);
182         for (const shared_ptr<TimeItem> t : time_items)
183                 t->paint_fore(p, pp);
184
185         p.end();
186 }
187
188 void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
189 {
190         assert(event);
191
192         if (event->buttons() & Qt::LeftButton)
193                 view_.zoom(2.0, event->x());
194         else if (event->buttons() & Qt::RightButton)
195                 view_.zoom(-2.0, event->x());
196 }
197
198 void Viewport::wheelEvent(QWheelEvent *e)
199 {
200         assert(e);
201
202         if (e->orientation() == Qt::Vertical)
203         {
204                 if (e->modifiers() & Qt::ControlModifier) {
205                         // Vertical scrolling with the control key pressed
206                         // is intrepretted as vertical scrolling
207                         view_.set_v_offset(-view_.owner_visual_v_offset() -
208                                 (e->delta() * height()) / (8 * 120));
209                 } else {
210                         // Vertical scrolling is interpreted as zooming in/out
211                         view_.zoom(e->delta() / 120, e->x());
212                 }
213         }
214         else if (e->orientation() == Qt::Horizontal)
215         {
216                 // Horizontal scrolling is interpreted as moving left/right
217                 view_.set_scale_offset(view_.scale(),
218                         e->delta() * view_.scale() + view_.offset());
219         }
220 }
221
222 } // namespace view
223 } // namespace pv