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