]> sigrok.org Git - pulseview.git/blob - pv/view/view.cpp
eca6b8eabce92c05575cbefeb1473ea95e424028
[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 "signal.h"
34 #include "view.h"
35 #include "viewport.h"
36
37 #include "pv/sigsession.h"
38 #include "pv/data/logic.h"
39 #include "pv/data/logicsnapshot.h"
40
41 using namespace boost;
42 using namespace std;
43
44 namespace pv {
45 namespace view {
46
47 const double View::MaxScale = 1e9;
48 const double View::MinScale = 1e-15;
49
50 const int View::LabelMarginWidth = 70;
51 const int View::RulerHeight = 30;
52
53 const int View::MaxScrollValue = INT_MAX / 2;
54
55 const int View::SignalHeight = 30;
56 const int View::SignalMargin = 10;
57 const int View::SignalSnapGridSize = 10;
58
59 const QColor View::CursorAreaColour(220, 231, 243);
60
61 const QSizeF View::LabelPadding(4, 0);
62
63 View::View(SigSession &session, QWidget *parent) :
64         QAbstractScrollArea(parent),
65         _session(session),
66         _viewport(new Viewport(*this)),
67         _ruler(new Ruler(*this)),
68         _header(new Header(*this)),
69         _data_length(0),
70         _scale(1e-6),
71         _offset(0),
72         _v_offset(0),
73         _updating_scroll(false),
74         _show_cursors(false),
75         _cursors(*this),
76         _hover_point(-1, -1)
77 {
78         connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
79                 this, SLOT(h_scroll_value_changed(int)));
80         connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
81                 this, SLOT(v_scroll_value_changed(int)));
82
83         connect(&_session, SIGNAL(signals_changed()),
84                 this, SLOT(signals_changed()));
85         connect(&_session, SIGNAL(data_updated()),
86                 this, SLOT(data_updated()));
87
88         connect(_cursors.first().get(), SIGNAL(time_changed()),
89                 this, SLOT(marker_time_changed()));
90         connect(_cursors.second().get(), SIGNAL(time_changed()),
91                 this, SLOT(marker_time_changed()));
92
93         connect(_header, SIGNAL(signals_moved()),
94                 this, SLOT(on_signals_moved()));
95
96         connect(_header, SIGNAL(selection_changed()),
97                 _ruler, SLOT(clear_selection()));
98         connect(_ruler, SIGNAL(selection_changed()),
99                 _header, SLOT(clear_selection()));
100
101         connect(_header, SIGNAL(selection_changed()),
102                 this, SIGNAL(selection_changed()));
103         connect(_ruler, SIGNAL(selection_changed()),
104                 this, SIGNAL(selection_changed()));
105
106         setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0);
107         setViewport(_viewport);
108
109         _viewport->installEventFilter(this);
110         _ruler->installEventFilter(this);
111         _header->installEventFilter(this);
112 }
113
114 SigSession& View::session()
115 {
116         return _session;
117 }
118
119 double View::scale() const
120 {
121         return _scale;
122 }
123
124 double View::offset() const
125 {
126         return _offset;
127 }
128
129 int View::v_offset() const
130 {
131         return _v_offset;
132 }
133
134 void View::zoom(double steps)
135 {
136         zoom(steps, (width() - LabelMarginWidth) / 2);
137 }
138
139 void View::zoom(double steps, int offset)
140 {
141         const double cursor_offset = _offset + _scale * offset;
142         _scale *= pow(3.0/2.0, -steps);
143         _scale = max(min(_scale, MaxScale), MinScale);
144         _offset = cursor_offset - _scale * offset;
145
146         _ruler->update();
147         _viewport->update();
148         update_scroll();
149 }
150
151
152 void View::set_scale_offset(double scale, double offset)
153 {
154         _scale = scale;
155         _offset = offset;
156
157         update_scroll();
158         _ruler->update();
159         _viewport->update();
160 }
161
162 bool View::cursors_shown() const
163 {
164         return _show_cursors;
165 }
166
167 void View::show_cursors(bool show)
168 {
169         _show_cursors = show;
170         _ruler->update();
171         _viewport->update();
172 }
173
174 void View::centre_cursors()
175 {
176         const double time_width = _scale * _viewport->width();
177         _cursors.first()->set_time(_offset + time_width * 0.4);
178         _cursors.second()->set_time(_offset + time_width * 0.6);
179         _ruler->update();
180         _viewport->update();
181 }
182
183 CursorPair& View::cursors()
184 {
185         return _cursors;
186 }
187
188 const CursorPair& View::cursors() const
189 {
190         return _cursors;
191 }
192
193 const QPoint& View::hover_point() const
194 {
195         return _hover_point;
196 }
197
198 void View::normalize_layout()
199 {
200         const vector< shared_ptr<Signal> > sigs(_session.get_signals());
201
202         int v_min = INT_MAX;
203         BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
204                 v_min = min(s->get_v_offset(), v_min);
205
206         const int delta = -min(v_min, 0);
207         BOOST_FOREACH(shared_ptr<Signal> s, sigs)
208                 s->set_v_offset(s->get_v_offset() + delta);
209
210         verticalScrollBar()->setSliderPosition(_v_offset + delta);
211         v_scroll_value_changed(verticalScrollBar()->sliderPosition());
212 }
213
214 void View::get_scroll_layout(double &length, double &offset) const
215 {
216         const shared_ptr<data::SignalData> sig_data = _session.get_data();
217         if (!sig_data)
218                 return;
219
220         length = _data_length / (sig_data->get_samplerate() * _scale);
221         offset = _offset / _scale;
222 }
223
224 void View::update_scroll()
225 {
226         assert(_viewport);
227
228         const QSize areaSize = _viewport->size();
229
230         // Set the horizontal scroll bar
231         double length = 0, offset = 0;
232         get_scroll_layout(length, offset);
233         length = max(length - areaSize.width(), 0.0);
234
235         horizontalScrollBar()->setPageStep(areaSize.width() / 2);
236
237         _updating_scroll = true;
238
239         if (length < MaxScrollValue) {
240                 horizontalScrollBar()->setRange(0, length);
241                 horizontalScrollBar()->setSliderPosition(offset);
242         } else {
243                 horizontalScrollBar()->setRange(0, MaxScrollValue);
244                 horizontalScrollBar()->setSliderPosition(
245                         _offset * MaxScrollValue / (_scale * length));
246         }
247
248         _updating_scroll = false;
249
250         // Set the vertical scrollbar
251         verticalScrollBar()->setPageStep(areaSize.height());
252         verticalScrollBar()->setRange(0,
253                 _viewport->get_total_height() + SignalMargin -
254                 areaSize.height());
255 }
256
257 void View::reset_signal_layout()
258 {
259         int offset = SignalMargin + SignalHeight;
260         const vector< shared_ptr<Signal> > sigs(_session.get_signals());
261         BOOST_FOREACH(shared_ptr<Signal> s, sigs) {
262                 s->set_v_offset(offset);
263                 offset += SignalHeight + 2 * SignalMargin;
264         }
265
266         normalize_layout();
267 }
268
269 bool View::eventFilter(QObject *object, QEvent *event)
270 {
271         const QEvent::Type type = event->type();
272         if (type == QEvent::MouseMove) {
273
274                 const QMouseEvent *const mouse_event = (QMouseEvent*)event;
275                 if (object == _viewport)
276                         _hover_point = mouse_event->pos();
277                 else if (object == _ruler)
278                         _hover_point = QPoint(mouse_event->x(), 0);
279                 else if (object == _header)
280                         _hover_point = QPoint(0, mouse_event->y());
281                 else
282                         _hover_point = QPoint(-1, -1);
283
284                 hover_point_changed();
285
286         } else if (type == QEvent::Leave) {
287                 _hover_point = QPoint(-1, -1);
288                 hover_point_changed();
289         }
290
291         return QObject::eventFilter(object, event);
292 }
293
294 bool View::viewportEvent(QEvent *e)
295 {
296         switch(e->type()) {
297         case QEvent::Paint:
298         case QEvent::MouseButtonPress:
299         case QEvent::MouseButtonRelease:
300         case QEvent::MouseButtonDblClick:
301         case QEvent::MouseMove:
302         case QEvent::Wheel:
303                 return false;
304
305         default:
306                 return QAbstractScrollArea::viewportEvent(e);
307         }
308 }
309
310 void View::resizeEvent(QResizeEvent*)
311 {
312         _ruler->setGeometry(_viewport->x(), 0,
313                 _viewport->width(), _viewport->y());
314         _header->setGeometry(0, _viewport->y(),
315                 _viewport->x(), _viewport->height());
316         update_scroll();
317 }
318
319 void View::h_scroll_value_changed(int value)
320 {
321         if (_updating_scroll)
322                 return;
323
324         const int range = horizontalScrollBar()->maximum();
325         if (range < MaxScrollValue)
326                 _offset = _scale * value;
327         else {
328                 double length = 0, offset;
329                 get_scroll_layout(length, offset);
330                 _offset = _scale * length * value / MaxScrollValue;
331         }
332
333         _ruler->update();
334         _viewport->update();
335 }
336
337 void View::v_scroll_value_changed(int value)
338 {
339         _v_offset = value;
340         _header->update();
341         _viewport->update();
342 }
343
344 void View::signals_changed()
345 {
346         reset_signal_layout();
347 }
348
349 void View::data_updated()
350 {
351         // Get the new data length
352         _data_length = 0;
353         shared_ptr<data::Logic> sig_data = _session.get_data();
354         if (sig_data) {
355                 deque< shared_ptr<data::LogicSnapshot> > &snapshots =
356                         sig_data->get_snapshots();
357                 BOOST_FOREACH(shared_ptr<data::LogicSnapshot> s, snapshots)
358                         if (s)
359                                 _data_length = max(_data_length,
360                                         s->get_sample_count());
361         }
362
363         // Update the scroll bars
364         update_scroll();
365
366         // Repaint the view
367         _viewport->update();
368 }
369
370 void View::marker_time_changed()
371 {
372         _ruler->update();
373         _viewport->update();
374 }
375
376 void View::on_signals_moved()
377 {
378         update_scroll();
379         signals_moved();
380 }
381
382 } // namespace view
383 } // namespace pv