]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
Added cursor dragging
[pulseview.git] / pv / view / view.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 <assert.h>
22 #include <limits.h>
23 #include <math.h>
24
25 #include <boost/foreach.hpp>
26
27 #include <QEvent>
28 #include <QMouseEvent>
29 #include <QScrollBar>
30
31 #include "header.h"
32 #include "ruler.h"
33 #include "view.h"
34 #include "viewport.h"
35
36 #include "../logicdata.h"
37 #include "../logicdatasnapshot.h"
38 #include "../sigsession.h"
39
40 using namespace boost;
41 using namespace std;
42
43 namespace pv {
44 namespace view {
45
46 const double View::MaxScale = 1e9;
47 const double View::MinScale = 1e-15;
48
49 const int View::LabelMarginWidth = 70;
50 const int View::RulerHeight = 30;
51
52 const int View::MaxScrollValue = INT_MAX / 2;
53
54 const int View::SignalHeight = 50;
55
56 const QColor View::CursorAreaColour(220, 231, 243);
57
58 View::View(SigSession &session, QWidget *parent) :
59         QAbstractScrollArea(parent),
60         _session(session),
61         _viewport(new Viewport(*this)),
62         _ruler(new Ruler(*this)),
63         _header(new Header(*this)),
64         _data_length(0),
65         _scale(1e-6),
66         _offset(0),
67         _v_offset(0),
68         _show_cursors(false),
69         _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0),
70                 Cursor(*this, 1.0))),
71         _hover_point(-1, -1)
72 {
73         connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
74                 this, SLOT(h_scroll_value_changed(int)));
75         connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
76                 this, SLOT(v_scroll_value_changed(int)));
77         connect(&_session, SIGNAL(data_updated()),
78                 this, SLOT(data_updated()));
79
80         connect(&_cursors.first, SIGNAL(time_changed()),
81                 this, SLOT(marker_time_changed()));
82         connect(&_cursors.second, SIGNAL(time_changed()),
83                 this, SLOT(marker_time_changed()));
84
85         setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
86         setViewport(_viewport);
87
88         _viewport->installEventFilter(this);
89         _ruler->installEventFilter(this);
90         _header->installEventFilter(this);
91 }
92
93 SigSession& View::session()
94 {
95         return _session;
96 }
97
98 double View::scale() const
99 {
100         return _scale;
101 }
102
103 double View::offset() const
104 {
105         return _offset;
106 }
107
108 int View::v_offset() const
109 {
110         return _v_offset;
111 }
112
113 void View::zoom(double steps)
114 {
115         zoom(steps, (width() - LabelMarginWidth) / 2);
116 }
117
118 void View::zoom(double steps, int offset)
119 {
120         const double cursor_offset = _offset + _scale * offset;
121         _scale *= pow(3.0/2.0, -steps);
122         _scale = max(min(_scale, MaxScale), MinScale);
123         _offset = cursor_offset - _scale * offset;
124
125         _ruler->update();
126         _viewport->update();
127         update_scroll();
128 }
129
130
131 void View::set_scale_offset(double scale, double offset)
132 {
133         _scale = scale;
134         _offset = offset;
135
136         update_scroll();
137         _ruler->update();
138         _viewport->update();
139 }
140
141 bool View::cursors_shown() const
142 {
143         return _show_cursors;
144 }
145
146 void View::show_cursors(bool show)
147 {
148         _show_cursors = show;
149         _ruler->update();
150         _viewport->update();
151 }
152
153 std::pair<Cursor, Cursor>& View::cursors()
154 {
155         return _cursors;
156 }
157
158 const QPoint& View::hover_point() const
159 {
160         return _hover_point;
161 }
162
163 void View::get_scroll_layout(double &length, double &offset) const
164 {
165         const shared_ptr<SignalData> sig_data = _session.get_data();
166         if(!sig_data)
167                 return;
168
169         length = _data_length / (sig_data->get_samplerate() * _scale);
170         offset = _offset / _scale;
171 }
172
173 void View::update_scroll()
174 {
175         assert(_viewport);
176
177         const QSize areaSize = _viewport->size();
178
179         // Set the horizontal scroll bar
180         double length = 0, offset = 0;
181         get_scroll_layout(length, offset);
182         length = max(length - areaSize.width(), 0.0);
183
184         horizontalScrollBar()->setPageStep(areaSize.width());
185
186         if(length < MaxScrollValue) {
187                 horizontalScrollBar()->setRange(0, length);
188                 horizontalScrollBar()->setSliderPosition(offset);
189         } else {
190                 horizontalScrollBar()->setRange(0, MaxScrollValue);
191                 horizontalScrollBar()->setSliderPosition(
192                         _offset * MaxScrollValue / (_scale * length));
193         }
194
195         // Set the vertical scrollbar
196         verticalScrollBar()->setPageStep(areaSize.height());
197         verticalScrollBar()->setRange(0,
198                 _viewport->get_total_height() - areaSize.height());
199 }
200
201 bool View::eventFilter(QObject *object, QEvent *event)
202 {
203         const QEvent::Type type = event->type();
204         if(type == QEvent::MouseMove) {
205
206                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
207                 if(object == _viewport)
208                         _hover_point = mouse_event->pos();
209                 else if(object == _ruler)
210                         _hover_point = QPoint(mouse_event->x(), 0);
211                 else if(object == _header)
212                         _hover_point = QPoint(0, mouse_event->y());
213                 else
214                         _hover_point = QPoint(-1, -1);
215
216                 hover_point_changed();
217
218         } else if(type == QEvent::Leave) {
219                 _hover_point = QPoint(-1, -1);
220                 hover_point_changed();
221         }
222
223         return QObject::eventFilter(object, event);
224 }
225
226 bool View::viewportEvent(QEvent *e)
227 {
228         switch(e->type()) {
229         case QEvent::Paint:
230         case QEvent::MouseButtonPress:
231         case QEvent::MouseButtonRelease:
232         case QEvent::MouseButtonDblClick:
233         case QEvent::MouseMove:
234         case QEvent::Wheel:
235                 return false;
236
237         default:
238                 return QAbstractScrollArea::viewportEvent(e);
239         }
240 }
241
242 void View::resizeEvent(QResizeEvent *e)
243 {
244         _ruler->setGeometry(_viewport->x(), 0,
245                 _viewport->width(), _viewport->y());
246         _header->setGeometry(0, _viewport->y(),
247                 _viewport->x(), _viewport->height());
248         update_scroll();
249 }
250
251 void View::h_scroll_value_changed(int value)
252 {
253         const int range = horizontalScrollBar()->maximum();
254         if(range < MaxScrollValue)
255                 _offset = _scale * value;
256         else {
257                 double length = 0, offset;
258                 get_scroll_layout(length, offset);
259                 _offset = _scale * length * value / MaxScrollValue;
260         }
261
262         _ruler->update();
263         _viewport->update();
264 }
265
266 void View::v_scroll_value_changed(int value)
267 {
268         _v_offset = value;
269         _header->update();
270         _viewport->update();
271 }
272
273 void View::data_updated()
274 {
275         // Get the new data length
276         _data_length = 0;
277         shared_ptr<LogicData> sig_data = _session.get_data();
278         if(sig_data) {
279                 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
280                         sig_data->get_snapshots();
281                 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
282                         if(s)
283                                 _data_length = max(_data_length,
284                                         s->get_sample_count());
285         }
286
287         // Update the scroll bars
288         update_scroll();
289
290         // Repaint the view
291         _viewport->update();
292 }
293
294 void View::marker_time_changed()
295 {
296         _ruler->update();
297         _viewport->update();
298 }
299
300 } // namespace view
301 } // namespace pv