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