]> sigrok.org Git - pulseview.git/blame - pv/view/view.cpp
Exposed View::_session with accessor function
[pulseview.git] / pv / view / view.cpp
CommitLineData
adb4b10c
JH
1/*
2 * This file is part of the sigrok 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 <math.h>
23
24#include <boost/foreach.hpp>
25
26#include <QEvent>
27#include <QScrollBar>
28
cdf7bea7
JH
29#include "view.h"
30#include "viewport.h"
adb4b10c 31
cdf7bea7
JH
32#include "../../logicdata.h"
33#include "../../logicdatasnapshot.h"
34#include "../../sigsession.h"
adb4b10c
JH
35
36using namespace boost;
37using namespace std;
38
cdf7bea7
JH
39namespace pv {
40namespace view {
adb4b10c 41
cdf7bea7
JH
42const double View::MaxScale = 1e9;
43const double View::MinScale = 1e-15;
adb4b10c 44
cdf7bea7
JH
45const int View::LabelMarginWidth = 70;
46const int View::RulerHeight = 30;
47
48View::View(SigSession &session, QWidget *parent) :
adb4b10c
JH
49 QAbstractScrollArea(parent),
50 _session(session),
cdf7bea7 51 _viewport(new Viewport(*this)),
adb4b10c
JH
52 _data_length(0),
53 _scale(1e-6),
54 _offset(0),
55 _v_offset(0)
56{
57 connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
58 this, SLOT(h_scroll_value_changed(int)));
59 connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
60 this, SLOT(v_scroll_value_changed(int)));
61 connect(&_session, SIGNAL(data_updated()),
62 this, SLOT(data_updated()));
63 setViewport(_viewport);
64}
65
1d19ef83
JH
66SigSession& View::session()
67{
68 return _session;
69}
70
cdf7bea7 71double View::scale() const
adb4b10c
JH
72{
73 return _scale;
74}
75
cdf7bea7 76double View::offset() const
adb4b10c
JH
77{
78 return _offset;
79}
80
cdf7bea7 81int View::v_offset() const
adb4b10c
JH
82{
83 return _v_offset;
84}
85
cdf7bea7 86void View::zoom(double steps)
adb4b10c
JH
87{
88 zoom(steps, (width() - LabelMarginWidth) / 2);
89}
90
cdf7bea7 91void View::set_scale_offset(double scale, double offset)
adb4b10c
JH
92{
93 _scale = scale;
94 _offset = offset;
95 update_scroll();
96 _viewport->update();
97}
98
cdf7bea7 99void View::update_scroll()
adb4b10c
JH
100{
101 assert(_viewport);
102
103 const QSize areaSize = _viewport->size();
104
105 // Set the horizontal scroll bar
106 double length = 0, offset = 0;
107 const shared_ptr<SignalData> sig_data = _session.get_data();
108 if(sig_data) {
109 length = _data_length /
110 (sig_data->get_samplerate() * _scale);
111 offset = _offset / _scale;
112 }
113
114 horizontalScrollBar()->setPageStep(areaSize.width());
115 horizontalScrollBar()->setRange(0,
116 max((int)(length - areaSize.width()), 0));
117 horizontalScrollBar()->setSliderPosition(offset);
118
119 // Set the vertical scrollbar
120 verticalScrollBar()->setPageStep(areaSize.height());
121 verticalScrollBar()->setRange(0,
122 _viewport->get_total_height() - areaSize.height());
123}
124
cdf7bea7 125void View::zoom(double steps, int offset)
adb4b10c
JH
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 _viewport->update();
132 update_scroll();
133}
134
cdf7bea7 135bool View::viewportEvent(QEvent *e)
adb4b10c
JH
136{
137 switch(e->type()) {
138 case QEvent::Paint:
139 case QEvent::MouseButtonPress:
140 case QEvent::MouseButtonRelease:
141 case QEvent::MouseButtonDblClick:
142 case QEvent::MouseMove:
143 case QEvent::Wheel:
144 return false;
145
146 default:
147 return QAbstractScrollArea::viewportEvent(e);
148 }
149}
150
cdf7bea7 151void View::resizeEvent(QResizeEvent *e)
adb4b10c
JH
152{
153 update_scroll();
154}
155
cdf7bea7 156void View::h_scroll_value_changed(int value)
adb4b10c
JH
157{
158 _offset = _scale * value;
159 _viewport->update();
160}
161
cdf7bea7 162void View::v_scroll_value_changed(int value)
adb4b10c
JH
163{
164 _v_offset = value;
165 _viewport->update();
166}
167
cdf7bea7 168void View::data_updated()
adb4b10c
JH
169{
170 // Get the new data length
171 _data_length = 0;
172 shared_ptr<LogicData> sig_data = _session.get_data();
173 if(sig_data) {
174 deque< shared_ptr<LogicDataSnapshot> > &snapshots =
175 sig_data->get_snapshots();
176 BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
177 if(s)
178 _data_length = max(_data_length,
179 s->get_sample_count());
180 }
181
182 // Update the scroll bars
183 update_scroll();
184
185 // Repaint the view
186 _viewport->update();
187}
cdf7bea7
JH
188
189} // namespace view
190} // namespace pv