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