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