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