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