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