]>
Commit | Line | Data |
---|---|---|
adb4b10c | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
adb4b10c JH |
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> | |
f25770e2 | 22 | #include <limits.h> |
adb4b10c JH |
23 | #include <math.h> |
24 | ||
25 | #include <boost/foreach.hpp> | |
26 | ||
27 | #include <QEvent> | |
cbd80f64 | 28 | #include <QMouseEvent> |
adb4b10c JH |
29 | #include <QScrollBar> |
30 | ||
1d8dca91 | 31 | #include "header.h" |
ccdd3ef5 | 32 | #include "ruler.h" |
2e575351 | 33 | #include "signal.h" |
cdf7bea7 JH |
34 | #include "view.h" |
35 | #include "viewport.h" | |
adb4b10c | 36 | |
51e77110 JH |
37 | #include "../logicdata.h" |
38 | #include "../logicdatasnapshot.h" | |
39 | #include "../sigsession.h" | |
adb4b10c JH |
40 | |
41 | using namespace boost; | |
42 | using namespace std; | |
43 | ||
cdf7bea7 JH |
44 | namespace pv { |
45 | namespace view { | |
adb4b10c | 46 | |
cdf7bea7 JH |
47 | const double View::MaxScale = 1e9; |
48 | const double View::MinScale = 1e-15; | |
adb4b10c | 49 | |
cdf7bea7 JH |
50 | const int View::LabelMarginWidth = 70; |
51 | const int View::RulerHeight = 30; | |
52 | ||
f25770e2 JH |
53 | const int View::MaxScrollValue = INT_MAX / 2; |
54 | ||
1d8dca91 JH |
55 | const int View::SignalHeight = 50; |
56 | ||
f76af637 JH |
57 | const QColor View::CursorAreaColour(220, 231, 243); |
58 | ||
2e04f9bd JH |
59 | const QSizeF View::LabelPadding(4, 0); |
60 | ||
cdf7bea7 | 61 | View::View(SigSession &session, QWidget *parent) : |
adb4b10c JH |
62 | QAbstractScrollArea(parent), |
63 | _session(session), | |
cdf7bea7 | 64 | _viewport(new Viewport(*this)), |
ccdd3ef5 | 65 | _ruler(new Ruler(*this)), |
1d8dca91 | 66 | _header(new Header(*this)), |
adb4b10c JH |
67 | _data_length(0), |
68 | _scale(1e-6), | |
69 | _offset(0), | |
cbd80f64 | 70 | _v_offset(0), |
f76af637 JH |
71 | _show_cursors(false), |
72 | _cursors(pair<Cursor, Cursor>(Cursor(*this, 0.0), | |
73 | Cursor(*this, 1.0))), | |
cbd80f64 | 74 | _hover_point(-1, -1) |
adb4b10c JH |
75 | { |
76 | connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), | |
77 | this, SLOT(h_scroll_value_changed(int))); | |
78 | connect(verticalScrollBar(), SIGNAL(valueChanged(int)), | |
79 | this, SLOT(v_scroll_value_changed(int))); | |
80 | connect(&_session, SIGNAL(data_updated()), | |
81 | this, SLOT(data_updated())); | |
1d8dca91 | 82 | |
ca4ec3ea JH |
83 | connect(&_cursors.first, SIGNAL(time_changed()), |
84 | this, SLOT(marker_time_changed())); | |
85 | connect(&_cursors.second, SIGNAL(time_changed()), | |
86 | this, SLOT(marker_time_changed())); | |
87 | ||
ccdd3ef5 | 88 | setViewportMargins(LabelMarginWidth, RulerHeight, 0, 0); |
adb4b10c | 89 | setViewport(_viewport); |
cbd80f64 JH |
90 | |
91 | _viewport->installEventFilter(this); | |
92 | _ruler->installEventFilter(this); | |
93 | _header->installEventFilter(this); | |
adb4b10c JH |
94 | } |
95 | ||
1d19ef83 JH |
96 | SigSession& View::session() |
97 | { | |
98 | return _session; | |
99 | } | |
100 | ||
cdf7bea7 | 101 | double View::scale() const |
adb4b10c JH |
102 | { |
103 | return _scale; | |
104 | } | |
105 | ||
cdf7bea7 | 106 | double View::offset() const |
adb4b10c JH |
107 | { |
108 | return _offset; | |
109 | } | |
110 | ||
cdf7bea7 | 111 | int View::v_offset() const |
adb4b10c JH |
112 | { |
113 | return _v_offset; | |
114 | } | |
115 | ||
cdf7bea7 | 116 | void View::zoom(double steps) |
adb4b10c JH |
117 | { |
118 | zoom(steps, (width() - LabelMarginWidth) / 2); | |
119 | } | |
120 | ||
17c0f398 JH |
121 | void View::zoom(double steps, int offset) |
122 | { | |
123 | const double cursor_offset = _offset + _scale * offset; | |
124 | _scale *= pow(3.0/2.0, -steps); | |
125 | _scale = max(min(_scale, MaxScale), MinScale); | |
126 | _offset = cursor_offset - _scale * offset; | |
127 | ||
128 | _ruler->update(); | |
129 | _viewport->update(); | |
130 | update_scroll(); | |
131 | } | |
132 | ||
133 | ||
cdf7bea7 | 134 | void View::set_scale_offset(double scale, double offset) |
adb4b10c JH |
135 | { |
136 | _scale = scale; | |
137 | _offset = offset; | |
ccdd3ef5 | 138 | |
adb4b10c | 139 | update_scroll(); |
ccdd3ef5 | 140 | _ruler->update(); |
adb4b10c JH |
141 | _viewport->update(); |
142 | } | |
143 | ||
f76af637 JH |
144 | bool View::cursors_shown() const |
145 | { | |
146 | return _show_cursors; | |
147 | } | |
148 | ||
149 | void View::show_cursors(bool show) | |
150 | { | |
151 | _show_cursors = show; | |
152 | _ruler->update(); | |
153 | _viewport->update(); | |
154 | } | |
155 | ||
156 | std::pair<Cursor, Cursor>& View::cursors() | |
157 | { | |
158 | return _cursors; | |
159 | } | |
160 | ||
cbd80f64 JH |
161 | const QPoint& View::hover_point() const |
162 | { | |
163 | return _hover_point; | |
164 | } | |
165 | ||
f25770e2 JH |
166 | void View::get_scroll_layout(double &length, double &offset) const |
167 | { | |
168 | const shared_ptr<SignalData> sig_data = _session.get_data(); | |
169 | if(!sig_data) | |
170 | return; | |
171 | ||
172 | length = _data_length / (sig_data->get_samplerate() * _scale); | |
173 | offset = _offset / _scale; | |
174 | } | |
175 | ||
cdf7bea7 | 176 | void View::update_scroll() |
adb4b10c JH |
177 | { |
178 | assert(_viewport); | |
179 | ||
180 | const QSize areaSize = _viewport->size(); | |
181 | ||
182 | // Set the horizontal scroll bar | |
183 | double length = 0, offset = 0; | |
f25770e2 JH |
184 | get_scroll_layout(length, offset); |
185 | length = max(length - areaSize.width(), 0.0); | |
adb4b10c JH |
186 | |
187 | horizontalScrollBar()->setPageStep(areaSize.width()); | |
f25770e2 JH |
188 | |
189 | if(length < MaxScrollValue) { | |
190 | horizontalScrollBar()->setRange(0, length); | |
191 | horizontalScrollBar()->setSliderPosition(offset); | |
192 | } else { | |
193 | horizontalScrollBar()->setRange(0, MaxScrollValue); | |
194 | horizontalScrollBar()->setSliderPosition( | |
195 | _offset * MaxScrollValue / (_scale * length)); | |
196 | } | |
adb4b10c JH |
197 | |
198 | // Set the vertical scrollbar | |
199 | verticalScrollBar()->setPageStep(areaSize.height()); | |
200 | verticalScrollBar()->setRange(0, | |
201 | _viewport->get_total_height() - areaSize.height()); | |
202 | } | |
203 | ||
2e575351 JH |
204 | void View::reset_signal_layout() |
205 | { | |
206 | int offset = 0; | |
207 | vector< shared_ptr<Signal> > &sigs = _session.get_signals(); | |
208 | BOOST_FOREACH(shared_ptr<Signal> s, sigs) { | |
209 | s->set_v_offset(offset); | |
210 | offset += SignalHeight; | |
211 | } | |
212 | } | |
213 | ||
cbd80f64 JH |
214 | bool View::eventFilter(QObject *object, QEvent *event) |
215 | { | |
216 | const QEvent::Type type = event->type(); | |
217 | if(type == QEvent::MouseMove) { | |
218 | ||
219 | const QMouseEvent *const mouse_event = (QMouseEvent*)event; | |
220 | if(object == _viewport) | |
221 | _hover_point = mouse_event->pos(); | |
222 | else if(object == _ruler) | |
223 | _hover_point = QPoint(mouse_event->x(), 0); | |
224 | else if(object == _header) | |
225 | _hover_point = QPoint(0, mouse_event->y()); | |
226 | else | |
227 | _hover_point = QPoint(-1, -1); | |
228 | ||
229 | hover_point_changed(); | |
230 | ||
231 | } else if(type == QEvent::Leave) { | |
232 | _hover_point = QPoint(-1, -1); | |
233 | hover_point_changed(); | |
234 | } | |
235 | ||
236 | return QObject::eventFilter(object, event); | |
237 | } | |
238 | ||
cdf7bea7 | 239 | bool View::viewportEvent(QEvent *e) |
adb4b10c JH |
240 | { |
241 | switch(e->type()) { | |
242 | case QEvent::Paint: | |
243 | case QEvent::MouseButtonPress: | |
244 | case QEvent::MouseButtonRelease: | |
245 | case QEvent::MouseButtonDblClick: | |
246 | case QEvent::MouseMove: | |
247 | case QEvent::Wheel: | |
248 | return false; | |
249 | ||
250 | default: | |
251 | return QAbstractScrollArea::viewportEvent(e); | |
252 | } | |
253 | } | |
254 | ||
cdf7bea7 | 255 | void View::resizeEvent(QResizeEvent *e) |
adb4b10c | 256 | { |
ccdd3ef5 JH |
257 | _ruler->setGeometry(_viewport->x(), 0, |
258 | _viewport->width(), _viewport->y()); | |
259 | _header->setGeometry(0, _viewport->y(), | |
1d8dca91 | 260 | _viewport->x(), _viewport->height()); |
adb4b10c JH |
261 | update_scroll(); |
262 | } | |
263 | ||
cdf7bea7 | 264 | void View::h_scroll_value_changed(int value) |
adb4b10c | 265 | { |
f25770e2 JH |
266 | const int range = horizontalScrollBar()->maximum(); |
267 | if(range < MaxScrollValue) | |
268 | _offset = _scale * value; | |
269 | else { | |
270 | double length = 0, offset; | |
271 | get_scroll_layout(length, offset); | |
272 | _offset = _scale * length * value / MaxScrollValue; | |
273 | } | |
274 | ||
ccdd3ef5 | 275 | _ruler->update(); |
adb4b10c JH |
276 | _viewport->update(); |
277 | } | |
278 | ||
cdf7bea7 | 279 | void View::v_scroll_value_changed(int value) |
adb4b10c JH |
280 | { |
281 | _v_offset = value; | |
1d8dca91 | 282 | _header->update(); |
adb4b10c JH |
283 | _viewport->update(); |
284 | } | |
285 | ||
cdf7bea7 | 286 | void View::data_updated() |
adb4b10c JH |
287 | { |
288 | // Get the new data length | |
289 | _data_length = 0; | |
290 | shared_ptr<LogicData> sig_data = _session.get_data(); | |
291 | if(sig_data) { | |
292 | deque< shared_ptr<LogicDataSnapshot> > &snapshots = | |
293 | sig_data->get_snapshots(); | |
294 | BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots) | |
295 | if(s) | |
296 | _data_length = max(_data_length, | |
297 | s->get_sample_count()); | |
298 | } | |
299 | ||
300 | // Update the scroll bars | |
301 | update_scroll(); | |
302 | ||
303 | // Repaint the view | |
304 | _viewport->update(); | |
2e575351 JH |
305 | |
306 | /// @todo: Call this only once when the signals are first created. | |
307 | reset_signal_layout(); | |
adb4b10c | 308 | } |
cdf7bea7 | 309 | |
ca4ec3ea JH |
310 | void View::marker_time_changed() |
311 | { | |
312 | _ruler->update(); | |
313 | _viewport->update(); | |
314 | } | |
315 | ||
cdf7bea7 JH |
316 | } // namespace view |
317 | } // namespace pv |