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