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