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