]>
Commit | Line | Data |
---|---|---|
aba1dd16 JH |
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
aba1dd16 JH |
18 | */ |
19 | ||
20 | #include <extdef.h> | |
21 | ||
3b68d03d JH |
22 | #include <cassert> |
23 | #include <cmath> | |
8a2fafcc JH |
24 | #include <cstdlib> |
25 | #include <limits> | |
a8fd2759 | 26 | #include <vector> |
aba1dd16 | 27 | |
37b9fed4 | 28 | #include <QApplication> |
73e377fe | 29 | #include <QCheckBox> |
368a37c2 | 30 | #include <QComboBox> |
4cffac16 | 31 | #include <QFormLayout> |
368a37c2 SA |
32 | #include <QGridLayout> |
33 | #include <QLabel> | |
03cc651d | 34 | #include <QString> |
37b9fed4 | 35 | |
2acdb232 | 36 | #include "analogsignal.hpp" |
1573bf16 SA |
37 | #include "logicsignal.hpp" |
38 | #include "view.hpp" | |
39 | ||
2acdb232 | 40 | #include "pv/data/analog.hpp" |
f3d66e52 | 41 | #include "pv/data/analogsegment.hpp" |
a8fd2759 SA |
42 | #include "pv/data/logic.hpp" |
43 | #include "pv/data/logicsegment.hpp" | |
bf0edd2b | 44 | #include "pv/data/signalbase.hpp" |
8de1e1b2 | 45 | #include "pv/globalsettings.hpp" |
aba1dd16 | 46 | |
fe3a1c21 | 47 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 48 | |
2601ce96 | 49 | using std::bind; |
6f925ba9 UH |
50 | using std::deque; |
51 | using std::div; | |
52 | using std::div_t; | |
819f4c25 | 53 | using std::max; |
a5d93c27 | 54 | using std::make_pair; |
819f4c25 | 55 | using std::min; |
6f925ba9 UH |
56 | using std::numeric_limits; |
57 | using std::pair; | |
2601ce96 | 58 | using std::placeholders::_1; |
f9abf97e | 59 | using std::shared_ptr; |
a8fd2759 | 60 | using std::vector; |
aba1dd16 | 61 | |
2b6e7a72 SA |
62 | using pv::data::SignalBase; |
63 | ||
aba1dd16 | 64 | namespace pv { |
f4e57597 | 65 | namespace views { |
1573bf16 | 66 | namespace trace { |
aba1dd16 | 67 | |
568b90d4 JH |
68 | const QColor AnalogSignal::SignalColours[4] = { |
69 | QColor(0xC4, 0xA0, 0x00), // Yellow | |
70 | QColor(0x87, 0x20, 0x7A), // Magenta | |
71 | QColor(0x20, 0x4A, 0x87), // Blue | |
72 | QColor(0x4E, 0x9A, 0x06) // Green | |
73 | }; | |
74 | ||
561ba3ae | 75 | const QPen AnalogSignal::AxisPen(QColor(0, 0, 0, 30 * 256 / 100), 2); |
c063290a UH |
76 | const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100); |
77 | const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100); | |
37b9fed4 | 78 | |
8de1e1b2 | 79 | const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77); |
d37ff80d SA |
80 | const QColor AnalogSignal::SamplingPointColourLo = QColor(200, 0, 0, 80 * 256 / 100); |
81 | const QColor AnalogSignal::SamplingPointColourNe = QColor(0, 0, 0, 80 * 256 / 100); | |
82 | const QColor AnalogSignal::SamplingPointColourHi = QColor(0, 200, 0, 80 * 256 / 100); | |
8de1e1b2 | 83 | |
4433246b | 84 | const QColor AnalogSignal::ThresholdColor = QColor(0, 0, 0, 30 * 256 / 100); |
1d150cd6 | 85 | const QColor AnalogSignal::ThresholdColorLo = QColor(255, 0, 0, 8 * 256 / 100); |
78bf7ce5 | 86 | const QColor AnalogSignal::ThresholdColorNe = QColor(0, 0, 0, 10 * 256 / 100); |
1d150cd6 | 87 | const QColor AnalogSignal::ThresholdColorHi = QColor(0, 255, 0, 8 * 256 / 100); |
4433246b | 88 | |
fdfd5b3e SA |
89 | const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float) |
90 | const float AnalogSignal::EnvelopeThreshold = 64.0f; | |
7c68ddae | 91 | |
4cffac16 | 92 | const int AnalogSignal::MaximumVDivs = 10; |
368a37c2 SA |
93 | const int AnalogSignal::MinScaleIndex = -6; |
94 | const int AnalogSignal::MaxScaleIndex = 7; | |
4cffac16 | 95 | |
03cc651d SA |
96 | const int AnalogSignal::InfoTextMarginRight = 20; |
97 | const int AnalogSignal::InfoTextMarginBottom = 5; | |
98 | ||
b86aa8f4 | 99 | AnalogSignal::AnalogSignal( |
2b81ae46 | 100 | pv::Session &session, |
cbd2a2de | 101 | shared_ptr<data::SignalBase> base) : |
73a25a6e | 102 | Signal(session, base), |
834a4f1b | 103 | scale_index_(4), // 20 per div |
37b9fed4 | 104 | scale_index_drag_offset_(0), |
459db2c5 SA |
105 | pos_vdivs_(1), |
106 | neg_vdivs_(1), | |
73e377fe | 107 | resolution_(0), |
a970015f | 108 | display_type_(DisplayBoth), |
1f1d55ce | 109 | autoranging_(true) |
aba1dd16 | 110 | { |
561ba3ae SA |
111 | axis_pen_ = AxisPen; |
112 | ||
85715407 SA |
113 | pv::data::Analog* analog_data = |
114 | dynamic_cast<pv::data::Analog*>(data().get()); | |
115 | ||
c6d9cf65 SA |
116 | connect(analog_data, SIGNAL(min_max_changed(float, float)), |
117 | this, SLOT(on_min_max_changed(float, float))); | |
85715407 | 118 | |
90ee1ed9 SA |
119 | GlobalSettings::register_change_handler(GlobalSettings::Key_View_ConversionThresholdDispMode, |
120 | bind(&AnalogSignal::on_settingViewConversionThresholdDispMode_changed, this, _1)); | |
2601ce96 | 121 | |
cbb50547 | 122 | GlobalSettings gs; |
90ee1ed9 SA |
123 | conversion_threshold_disp_mode_ = |
124 | gs.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt(); | |
2601ce96 | 125 | |
cbb50547 SA |
126 | div_height_ = gs.value(GlobalSettings::Key_View_DefaultDivHeight).toInt(); |
127 | ||
73a25a6e | 128 | base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]); |
834a4f1b | 129 | update_scale(); |
aba1dd16 JH |
130 | } |
131 | ||
3009b5b3 JH |
132 | shared_ptr<pv::data::SignalData> AnalogSignal::data() const |
133 | { | |
cbd2a2de | 134 | return base_->analog_data(); |
9a0cd293 JH |
135 | } |
136 | ||
3a21afa6 SA |
137 | void AnalogSignal::save_settings(QSettings &settings) const |
138 | { | |
459db2c5 SA |
139 | settings.setValue("pos_vdivs", pos_vdivs_); |
140 | settings.setValue("neg_vdivs", neg_vdivs_); | |
3a21afa6 | 141 | settings.setValue("scale_index", scale_index_); |
a970015f | 142 | settings.setValue("display_type", display_type_); |
73e377fe | 143 | settings.setValue("autoranging", autoranging_); |
cbb50547 | 144 | settings.setValue("div_height", div_height_); |
3a21afa6 SA |
145 | } |
146 | ||
147 | void AnalogSignal::restore_settings(QSettings &settings) | |
148 | { | |
459db2c5 SA |
149 | if (settings.contains("pos_vdivs")) |
150 | pos_vdivs_ = settings.value("pos_vdivs").toInt(); | |
151 | ||
152 | if (settings.contains("neg_vdivs")) | |
153 | neg_vdivs_ = settings.value("neg_vdivs").toInt(); | |
3a21afa6 SA |
154 | |
155 | if (settings.contains("scale_index")) { | |
156 | scale_index_ = settings.value("scale_index").toInt(); | |
157 | update_scale(); | |
158 | } | |
73e377fe | 159 | |
a970015f SA |
160 | if (settings.contains("display_type")) |
161 | display_type_ = (DisplayType)(settings.value("display_type").toInt()); | |
162 | ||
73e377fe SA |
163 | if (settings.contains("autoranging")) |
164 | autoranging_ = settings.value("autoranging").toBool(); | |
cbb50547 SA |
165 | |
166 | if (settings.contains("div_height")) { | |
167 | const int old_height = div_height_; | |
168 | div_height_ = settings.value("div_height").toInt(); | |
169 | ||
170 | if ((div_height_ != old_height) && owner_) { | |
171 | // Call order is important, otherwise the lazy event handler won't work | |
172 | owner_->extents_changed(false, true); | |
173 | owner_->row_item_appearance_changed(false, true); | |
174 | } | |
175 | } | |
3a21afa6 SA |
176 | } |
177 | ||
6f925ba9 | 178 | pair<int, int> AnalogSignal::v_extents() const |
a5d93c27 | 179 | { |
459db2c5 SA |
180 | const int ph = pos_vdivs_ * div_height_; |
181 | const int nh = neg_vdivs_ * div_height_; | |
182 | return make_pair(-ph, nh); | |
a5d93c27 JH |
183 | } |
184 | ||
214470fc JH |
185 | int AnalogSignal::scale_handle_offset() const |
186 | { | |
459db2c5 | 187 | const int h = (pos_vdivs_ + neg_vdivs_) * div_height_; |
37b9fed4 | 188 | |
c063290a | 189 | return ((scale_index_drag_offset_ - scale_index_) * h / 4) - h / 2; |
214470fc JH |
190 | } |
191 | ||
192 | void AnalogSignal::scale_handle_dragged(int offset) | |
193 | { | |
459db2c5 | 194 | const int h = (pos_vdivs_ + neg_vdivs_) * div_height_; |
37b9fed4 | 195 | |
c063290a | 196 | scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4); |
834a4f1b SA |
197 | |
198 | update_scale(); | |
8a2fafcc JH |
199 | } |
200 | ||
201 | void AnalogSignal::scale_handle_drag_release() | |
202 | { | |
203 | scale_index_drag_offset_ = scale_index_; | |
834a4f1b | 204 | update_scale(); |
214470fc JH |
205 | } |
206 | ||
60938e04 | 207 | void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp) |
fe08b6e8 | 208 | { |
1d150cd6 SA |
209 | if (!base_->enabled()) |
210 | return; | |
211 | ||
90ee1ed9 SA |
212 | bool paint_thr_bg = |
213 | conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Background; | |
214 | ||
1d150cd6 SA |
215 | const vector<double> thresholds = base_->get_conversion_thresholds(); |
216 | ||
217 | // Only display thresholds if we have some and we show analog samples | |
90ee1ed9 | 218 | if ((thresholds.size() > 0) && paint_thr_bg && |
1d150cd6 SA |
219 | ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth))) { |
220 | ||
221 | const int visual_y = get_visual_y(); | |
222 | const pair<int, int> extents = v_extents(); | |
223 | const int top = visual_y + extents.first; | |
224 | const int btm = visual_y + extents.second; | |
225 | ||
226 | // Draw high/neutral/low areas | |
227 | if (thresholds.size() == 2) { | |
03d2a4f8 SA |
228 | int thr_lo = visual_y - thresholds[0] * scale_; |
229 | int thr_hi = visual_y - thresholds[1] * scale_; | |
230 | thr_lo = min(max(thr_lo, top), btm); | |
231 | thr_hi = min(max(thr_hi, top), btm); | |
1d150cd6 SA |
232 | |
233 | p.fillRect(QRectF(pp.left(), top, pp.width(), thr_hi - top), | |
234 | QBrush(ThresholdColorHi)); | |
235 | p.fillRect(QRectF(pp.left(), thr_hi, pp.width(), thr_lo - thr_hi), | |
236 | QBrush(ThresholdColorNe)); | |
237 | p.fillRect(QRectF(pp.left(), thr_lo, pp.width(), btm - thr_lo), | |
238 | QBrush(ThresholdColorLo)); | |
239 | } else { | |
03d2a4f8 SA |
240 | int thr = visual_y - thresholds[0] * scale_; |
241 | thr = min(max(thr, top), btm); | |
1d150cd6 SA |
242 | |
243 | p.fillRect(QRectF(pp.left(), top, pp.width(), thr - top), | |
244 | QBrush(ThresholdColorHi)); | |
245 | p.fillRect(QRectF(pp.left(), thr, pp.width(), btm - thr), | |
246 | QBrush(ThresholdColorLo)); | |
247 | } | |
248 | ||
249 | paint_axis(p, pp, get_visual_y()); | |
250 | } else { | |
99af6802 | 251 | Trace::paint_back(p, pp); |
97904bf7 | 252 | paint_axis(p, pp, get_visual_y()); |
99af6802 | 253 | } |
fe08b6e8 JH |
254 | } |
255 | ||
60938e04 | 256 | void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp) |
aba1dd16 | 257 | { |
cbd2a2de | 258 | assert(base_->analog_data()); |
8dbbc7f0 | 259 | assert(owner_); |
a8acb46e | 260 | |
be9e7b4b | 261 | const int y = get_visual_y(); |
01fd3263 | 262 | |
73a25a6e | 263 | if (!base_->enabled()) |
cec48d16 JH |
264 | return; |
265 | ||
a8fd2759 SA |
266 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
267 | paint_grid(p, y, pp.left(), pp.right()); | |
268 | ||
269 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
270 | base_->analog_data()->analog_segments(); | |
271 | if (segments.empty()) | |
272 | return; | |
273 | ||
274 | const shared_ptr<pv::data::AnalogSegment> &segment = | |
275 | segments.front(); | |
276 | ||
277 | const double pixels_offset = pp.pixels_offset(); | |
278 | const double samplerate = max(1.0, segment->samplerate()); | |
279 | const pv::util::Timestamp& start_time = segment->start_time(); | |
280 | const int64_t last_sample = segment->get_sample_count() - 1; | |
281 | const double samples_per_pixel = samplerate * pp.scale(); | |
282 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); | |
283 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
284 | ||
285 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
286 | (int64_t)0), last_sample); | |
287 | const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(), | |
288 | (int64_t)0), last_sample); | |
289 | ||
290 | if (samples_per_pixel < EnvelopeThreshold) | |
291 | paint_trace(p, segment, y, pp.left(), | |
292 | start_sample, end_sample, | |
293 | pixels_offset, samples_per_pixel); | |
294 | else | |
295 | paint_envelope(p, segment, y, pp.left(), | |
296 | start_sample, end_sample, | |
297 | pixels_offset, samples_per_pixel); | |
298 | } | |
a8acb46e | 299 | |
1d150cd6 SA |
300 | if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth)) |
301 | paint_logic_mid(p, pp); | |
7c68ddae JH |
302 | } |
303 | ||
60938e04 | 304 | void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp) |
03cc651d SA |
305 | { |
306 | if (!enabled()) | |
307 | return; | |
308 | ||
b8c4a95b SA |
309 | if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) { |
310 | const int y = get_visual_y(); | |
03cc651d | 311 | |
b8c4a95b SA |
312 | // Show the info section on the right side of the trace |
313 | const QString infotext = QString("%1 V/div").arg(resolution_); | |
03cc651d | 314 | |
b8c4a95b SA |
315 | p.setPen(base_->colour()); |
316 | p.setFont(QApplication::font()); | |
03cc651d | 317 | |
b8c4a95b SA |
318 | const QRectF bounding_rect = QRectF(pp.left(), |
319 | y + v_extents().first, | |
320 | pp.width() - InfoTextMarginRight, | |
321 | v_extents().second - v_extents().first - InfoTextMarginBottom); | |
03cc651d | 322 | |
b8c4a95b SA |
323 | p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext); |
324 | } | |
03cc651d SA |
325 | } |
326 | ||
37b9fed4 SA |
327 | void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right) |
328 | { | |
46a0cadc SA |
329 | p.setRenderHint(QPainter::Antialiasing, false); |
330 | ||
8ad61f40 UH |
331 | GlobalSettings settings; |
332 | const bool show_analog_minor_grid = | |
333 | settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool(); | |
334 | ||
459db2c5 SA |
335 | if (pos_vdivs_ > 0) { |
336 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
337 | for (int i = 1; i <= pos_vdivs_; i++) { | |
338 | const float dy = i * div_height_; | |
339 | p.drawLine(QLineF(left, y - dy, right, y - dy)); | |
340 | } | |
8ad61f40 | 341 | } |
459db2c5 | 342 | |
8ad61f40 | 343 | if ((pos_vdivs_ > 0) && show_analog_minor_grid) { |
459db2c5 SA |
344 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
345 | for (int i = 0; i < pos_vdivs_; i++) { | |
346 | const float dy = i * div_height_; | |
347 | const float dy25 = dy + (0.25 * div_height_); | |
348 | const float dy50 = dy + (0.50 * div_height_); | |
349 | const float dy75 = dy + (0.75 * div_height_); | |
350 | p.drawLine(QLineF(left, y - dy25, right, y - dy25)); | |
351 | p.drawLine(QLineF(left, y - dy50, right, y - dy50)); | |
352 | p.drawLine(QLineF(left, y - dy75, right, y - dy75)); | |
353 | } | |
37b9fed4 SA |
354 | } |
355 | ||
459db2c5 SA |
356 | if (neg_vdivs_ > 0) { |
357 | p.setPen(QPen(GridMajorColor, 1, Qt::DashLine)); | |
358 | for (int i = 1; i <= neg_vdivs_; i++) { | |
359 | const float dy = i * div_height_; | |
360 | p.drawLine(QLineF(left, y + dy, right, y + dy)); | |
361 | } | |
8ad61f40 | 362 | } |
459db2c5 | 363 | |
8ad61f40 | 364 | if ((pos_vdivs_ > 0) && show_analog_minor_grid) { |
459db2c5 SA |
365 | p.setPen(QPen(GridMinorColor, 1, Qt::DashLine)); |
366 | for (int i = 0; i < neg_vdivs_; i++) { | |
367 | const float dy = i * div_height_; | |
368 | const float dy25 = dy + (0.25 * div_height_); | |
369 | const float dy50 = dy + (0.50 * div_height_); | |
370 | const float dy75 = dy + (0.75 * div_height_); | |
371 | p.drawLine(QLineF(left, y + dy25, right, y + dy25)); | |
372 | p.drawLine(QLineF(left, y + dy50, right, y + dy50)); | |
373 | p.drawLine(QLineF(left, y + dy75, right, y + dy75)); | |
374 | } | |
37b9fed4 | 375 | } |
46a0cadc SA |
376 | |
377 | p.setRenderHint(QPainter::Antialiasing, true); | |
37b9fed4 SA |
378 | } |
379 | ||
7c68ddae | 380 | void AnalogSignal::paint_trace(QPainter &p, |
f3d66e52 | 381 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
382 | int y, int left, const int64_t start, const int64_t end, |
383 | const double pixels_offset, const double samples_per_pixel) | |
384 | { | |
fdfd5b3e SA |
385 | if (end <= start) |
386 | return; | |
387 | ||
d37ff80d SA |
388 | bool paint_thr_dots = |
389 | (base_->get_conversion_type() != data::SignalBase::NoConversion) && | |
390 | (conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Dots); | |
391 | ||
392 | vector<double> thresholds; | |
393 | if (paint_thr_dots) | |
394 | thresholds = base_->get_conversion_thresholds(); | |
395 | ||
fdfd5b3e SA |
396 | // Calculate and paint the sampling points if enabled and useful |
397 | GlobalSettings settings; | |
398 | const bool show_sampling_points = | |
d37ff80d SA |
399 | (settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() || |
400 | paint_thr_dots) && (samples_per_pixel < 0.25); | |
fdfd5b3e | 401 | |
73a25a6e | 402 | p.setPen(base_->colour()); |
7c68ddae | 403 | |
76ce6c7a UH |
404 | const int64_t points_count = end - start; |
405 | ||
406 | QPointF *points = new QPointF[points_count]; | |
a8acb46e JH |
407 | QPointF *point = points; |
408 | ||
d37ff80d | 409 | vector<QRectF> sampling_points[3]; |
8de1e1b2 | 410 | |
fdfd5b3e SA |
411 | int64_t sample_count = min(points_count, TracePaintBlockSize); |
412 | int64_t block_sample = 0; | |
b82243f7 SA |
413 | float *sample_block = new float[TracePaintBlockSize]; |
414 | segment->get_samples(start, start + sample_count, sample_block); | |
26a883ed | 415 | |
8de1e1b2 | 416 | const int w = 2; |
fdfd5b3e SA |
417 | for (int64_t sample = start; sample != end; sample++, block_sample++) { |
418 | ||
419 | if (block_sample == TracePaintBlockSize) { | |
420 | block_sample = 0; | |
fdfd5b3e | 421 | sample_count = min(points_count - sample, TracePaintBlockSize); |
b82243f7 | 422 | segment->get_samples(sample, sample + sample_count, sample_block); |
fdfd5b3e SA |
423 | } |
424 | ||
a8acb46e | 425 | const float x = (sample / samples_per_pixel - |
2658961b | 426 | pixels_offset) + left; |
26a883ed | 427 | |
fdfd5b3e | 428 | *point++ = QPointF(x, y - sample_block[block_sample] * scale_); |
8de1e1b2 | 429 | |
d37ff80d SA |
430 | if (show_sampling_points) { |
431 | int idx = 0; // Neutral | |
432 | ||
433 | if (paint_thr_dots) { | |
434 | if (thresholds.size() == 1) | |
435 | idx = (sample_block[block_sample] >= thresholds[0]) ? 2 : 1; | |
436 | else if (thresholds.size() == 2) { | |
437 | if (sample_block[block_sample] > thresholds[1]) | |
438 | idx = 2; // High | |
439 | else if (sample_block[block_sample] < thresholds[0]) | |
440 | idx = 1; // Low | |
441 | } | |
442 | } | |
443 | ||
444 | sampling_points[idx].push_back( | |
445 | QRectF(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w)); | |
446 | } | |
a8acb46e | 447 | } |
fdfd5b3e | 448 | delete[] sample_block; |
a8acb46e | 449 | |
76ce6c7a | 450 | p.drawPolyline(points, points_count); |
a8acb46e | 451 | |
fdfd5b3e | 452 | if (show_sampling_points) { |
d37ff80d SA |
453 | if (paint_thr_dots) { |
454 | p.setPen(SamplingPointColourNe); | |
455 | p.drawRects(sampling_points[0].data(), sampling_points[0].size()); | |
456 | p.setPen(SamplingPointColourLo); | |
457 | p.drawRects(sampling_points[1].data(), sampling_points[1].size()); | |
458 | p.setPen(SamplingPointColourHi); | |
459 | p.drawRects(sampling_points[2].data(), sampling_points[2].size()); | |
460 | } else { | |
461 | p.setPen(SamplingPointColour); | |
462 | p.drawRects(sampling_points[0].data(), sampling_points[0].size()); | |
463 | } | |
8de1e1b2 UH |
464 | } |
465 | ||
a8acb46e | 466 | delete[] points; |
aba1dd16 JH |
467 | } |
468 | ||
7c68ddae | 469 | void AnalogSignal::paint_envelope(QPainter &p, |
f3d66e52 | 470 | const shared_ptr<pv::data::AnalogSegment> &segment, |
7c68ddae JH |
471 | int y, int left, const int64_t start, const int64_t end, |
472 | const double pixels_offset, const double samples_per_pixel) | |
473 | { | |
f3d66e52 | 474 | using pv::data::AnalogSegment; |
7c68ddae | 475 | |
f3d66e52 JH |
476 | AnalogSegment::EnvelopeSection e; |
477 | segment->get_envelope_section(e, start, end, samples_per_pixel); | |
7c68ddae JH |
478 | |
479 | if (e.length < 2) | |
480 | return; | |
481 | ||
819f4c25 | 482 | p.setPen(QPen(Qt::NoPen)); |
73a25a6e | 483 | p.setBrush(base_->colour()); |
7c68ddae JH |
484 | |
485 | QRectF *const rects = new QRectF[e.length]; | |
486 | QRectF *rect = rects; | |
487 | ||
c063290a | 488 | for (uint64_t sample = 0; sample < e.length - 1; sample++) { |
7c68ddae JH |
489 | const float x = ((e.scale * sample + e.start) / |
490 | samples_per_pixel - pixels_offset) + left; | |
f3d66e52 | 491 | const AnalogSegment::EnvelopeSample *const s = |
7c68ddae JH |
492 | e.samples + sample; |
493 | ||
494 | // We overlap this sample with the next so that vertical | |
495 | // gaps do not appear during steep rising or falling edges | |
c063290a UH |
496 | const float b = y - max(s->max, (s + 1)->min) * scale_; |
497 | const float t = y - min(s->min, (s + 1)->max) * scale_; | |
7c68ddae JH |
498 | |
499 | float h = b - t; | |
f3290553 | 500 | if (h >= 0.0f && h <= 1.0f) |
7c68ddae | 501 | h = 1.0f; |
f3290553 | 502 | if (h <= 0.0f && h >= -1.0f) |
7c68ddae JH |
503 | h = -1.0f; |
504 | ||
505 | *rect++ = QRectF(x, t, 1.0f, h); | |
506 | } | |
507 | ||
508 | p.drawRects(rects, e.length); | |
509 | ||
510 | delete[] rects; | |
511 | delete[] e.samples; | |
512 | } | |
513 | ||
60938e04 | 514 | void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp) |
a8fd2759 SA |
515 | { |
516 | QLineF *line; | |
517 | ||
518 | vector< pair<int64_t, bool> > edges; | |
519 | ||
520 | assert(base_); | |
521 | ||
522 | const int y = get_visual_y(); | |
523 | ||
6c0df7c4 | 524 | if (!base_->enabled() || !base_->logic_data()) |
a8fd2759 SA |
525 | return; |
526 | ||
527 | const int signal_margin = | |
528 | QFontMetrics(QApplication::font()).height() / 2; | |
529 | ||
530 | const int ph = min(pos_vdivs_, 1) * div_height_; | |
531 | const int nh = min(neg_vdivs_, 1) * div_height_; | |
532 | const float high_offset = y - ph + signal_margin + 0.5f; | |
533 | const float low_offset = y + nh - signal_margin - 0.5f; | |
534 | ||
535 | const deque< shared_ptr<pv::data::LogicSegment> > &segments = | |
536 | base_->logic_data()->logic_segments(); | |
6c0df7c4 | 537 | |
a8fd2759 SA |
538 | if (segments.empty()) |
539 | return; | |
540 | ||
541 | const shared_ptr<pv::data::LogicSegment> &segment = | |
542 | segments.front(); | |
543 | ||
544 | double samplerate = segment->samplerate(); | |
545 | ||
546 | // Show sample rate as 1Hz when it is unknown | |
547 | if (samplerate == 0.0) | |
548 | samplerate = 1.0; | |
549 | ||
550 | const double pixels_offset = pp.pixels_offset(); | |
551 | const pv::util::Timestamp& start_time = segment->start_time(); | |
552 | const int64_t last_sample = segment->get_sample_count() - 1; | |
553 | const double samples_per_pixel = samplerate * pp.scale(); | |
124be547 | 554 | const double pixels_per_sample = 1 / samples_per_pixel; |
a8fd2759 SA |
555 | const pv::util::Timestamp start = samplerate * (pp.offset() - start_time); |
556 | const pv::util::Timestamp end = start + samples_per_pixel * pp.width(); | |
557 | ||
558 | const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(), | |
559 | (int64_t)0), last_sample); | |
560 | const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(), | |
561 | (int64_t)0), last_sample); | |
562 | ||
563 | segment->get_subsampled_edges(edges, start_sample, end_sample, | |
564 | samples_per_pixel / LogicSignal::Oversampling, 0); | |
565 | assert(edges.size() >= 2); | |
566 | ||
124be547 SA |
567 | // Check whether we need to paint the sampling points |
568 | GlobalSettings settings; | |
569 | const bool show_sampling_points = | |
570 | settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() && | |
571 | (samples_per_pixel < 0.25); | |
572 | ||
573 | vector<QRectF> sampling_points; | |
574 | float sampling_point_x = 0.0f; | |
575 | int64_t sampling_point_sample = start_sample; | |
576 | const int w = 2; | |
577 | ||
578 | if (show_sampling_points) { | |
579 | sampling_points.reserve(end_sample - start_sample + 1); | |
580 | sampling_point_x = (edges.cbegin()->first / samples_per_pixel - pixels_offset) + pp.left(); | |
581 | } | |
582 | ||
a8fd2759 SA |
583 | // Paint the edges |
584 | const unsigned int edge_count = edges.size() - 2; | |
585 | QLineF *const edge_lines = new QLineF[edge_count]; | |
586 | line = edge_lines; | |
587 | ||
588 | for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { | |
589 | const float x = ((*i).first / samples_per_pixel - | |
590 | pixels_offset) + pp.left(); | |
591 | *line++ = QLineF(x, high_offset, x, low_offset); | |
124be547 SA |
592 | |
593 | if (show_sampling_points) | |
594 | while (sampling_point_sample < (*i).first) { | |
595 | const float y = (*i).second ? low_offset : high_offset; | |
596 | sampling_points.emplace_back( | |
597 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
598 | sampling_point_sample++; | |
599 | sampling_point_x += pixels_per_sample; | |
600 | }; | |
a8fd2759 SA |
601 | } |
602 | ||
124be547 SA |
603 | // Calculate the sample points from the last edge to the end of the trace |
604 | if (show_sampling_points) | |
605 | while ((uint64_t)sampling_point_sample <= end_sample) { | |
606 | // Signal changed after the last edge, so the level is inverted | |
607 | const float y = (edges.cend() - 1)->second ? high_offset : low_offset; | |
608 | sampling_points.emplace_back( | |
609 | QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w)); | |
610 | sampling_point_sample++; | |
611 | sampling_point_x += pixels_per_sample; | |
612 | }; | |
613 | ||
a8fd2759 SA |
614 | p.setPen(LogicSignal::EdgeColour); |
615 | p.drawLines(edge_lines, edge_count); | |
616 | delete[] edge_lines; | |
617 | ||
618 | // Paint the caps | |
619 | const unsigned int max_cap_line_count = edges.size(); | |
620 | QLineF *const cap_lines = new QLineF[max_cap_line_count]; | |
621 | ||
622 | p.setPen(LogicSignal::HighColour); | |
623 | paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel, | |
624 | pixels_offset, pp.left(), high_offset); | |
625 | p.setPen(LogicSignal::LowColour); | |
626 | paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel, | |
627 | pixels_offset, pp.left(), low_offset); | |
628 | ||
629 | delete[] cap_lines; | |
630 | ||
a8fd2759 | 631 | // Paint the sampling points |
124be547 SA |
632 | if (show_sampling_points) { |
633 | p.setPen(SamplingPointColour); | |
634 | p.drawRects(sampling_points.data(), sampling_points.size()); | |
a8fd2759 | 635 | } |
a8fd2759 SA |
636 | } |
637 | ||
638 | void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines, | |
639 | vector< pair<int64_t, bool> > &edges, bool level, | |
640 | double samples_per_pixel, double pixels_offset, float x_offset, | |
641 | float y_offset) | |
642 | { | |
643 | QLineF *line = lines; | |
644 | ||
645 | for (auto i = edges.begin(); i != (edges.end() - 1); i++) | |
646 | if ((*i).second == level) { | |
647 | *line++ = QLineF( | |
648 | ((*i).first / samples_per_pixel - | |
649 | pixels_offset) + x_offset, y_offset, | |
650 | ((*(i+1)).first / samples_per_pixel - | |
651 | pixels_offset) + x_offset, y_offset); | |
652 | } | |
653 | ||
654 | p.drawLines(lines, line - lines); | |
655 | } | |
656 | ||
368a37c2 | 657 | float AnalogSignal::get_resolution(int scale_index) |
8a2fafcc JH |
658 | { |
659 | const float seq[] = {1.0f, 2.0f, 5.0f}; | |
834a4f1b | 660 | |
6f925ba9 UH |
661 | const int offset = numeric_limits<int>::max() / (2 * countof(seq)); |
662 | const div_t d = div((int)(scale_index + countof(seq) * offset), | |
8a2fafcc | 663 | countof(seq)); |
834a4f1b | 664 | |
368a37c2 SA |
665 | return powf(10.0f, d.quot - offset) * seq[d.rem]; |
666 | } | |
667 | ||
668 | void AnalogSignal::update_scale() | |
669 | { | |
670 | resolution_ = get_resolution(scale_index_); | |
834a4f1b | 671 | scale_ = div_height_ / resolution_; |
8a2fafcc JH |
672 | } |
673 | ||
52c900ac SA |
674 | void AnalogSignal::update_conversion_widgets() |
675 | { | |
2b6e7a72 | 676 | SignalBase::ConversionType conv_type = base_->get_conversion_type(); |
52c900ac SA |
677 | |
678 | // Enable or disable widgets depending on conversion state | |
2b6e7a72 SA |
679 | conv_threshold_cb_->setEnabled(conv_type != SignalBase::NoConversion); |
680 | display_type_cb_->setEnabled(conv_type != SignalBase::NoConversion); | |
52c900ac SA |
681 | |
682 | conv_threshold_cb_->clear(); | |
683 | ||
684 | vector < pair<QString, int> > presets = base_->get_conversion_presets(); | |
685 | ||
686 | // Prevent the combo box from firing the "edit text changed" signal | |
687 | // as that would involuntarily select the first entry | |
688 | conv_threshold_cb_->blockSignals(true); | |
689 | ||
690 | // Set available options depending on chosen conversion | |
691 | for (pair<QString, int> preset : presets) | |
692 | conv_threshold_cb_->addItem(preset.first, preset.second); | |
693 | ||
694 | map < QString, QVariant > options = base_->get_conversion_options(); | |
695 | ||
b9cdbe03 | 696 | if (conv_type == SignalBase::A2LConversionByThreshold) { |
52c900ac | 697 | const vector<double> thresholds = base_->get_conversion_thresholds( |
b9cdbe03 | 698 | SignalBase::A2LConversionByThreshold, true); |
52c900ac SA |
699 | conv_threshold_cb_->addItem( |
700 | QString("%1V").arg(QString::number(thresholds[0], 'f', 1)), -1); | |
701 | } | |
702 | ||
2b6e7a72 | 703 | if (conv_type == SignalBase::A2LConversionBySchmittTrigger) { |
52c900ac | 704 | const vector<double> thresholds = base_->get_conversion_thresholds( |
2b6e7a72 | 705 | SignalBase::A2LConversionBySchmittTrigger, true); |
52c900ac SA |
706 | conv_threshold_cb_->addItem(QString("%1V/%2V").arg( |
707 | QString::number(thresholds[0], 'f', 1), | |
708 | QString::number(thresholds[1], 'f', 1)), -1); | |
709 | } | |
710 | ||
711 | int preset_id = base_->get_current_conversion_preset(); | |
712 | conv_threshold_cb_->setCurrentIndex( | |
713 | conv_threshold_cb_->findData(preset_id)); | |
714 | ||
715 | conv_threshold_cb_->blockSignals(false); | |
716 | } | |
717 | ||
430b94aa | 718 | void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update) |
73e377fe SA |
719 | { |
720 | const deque< shared_ptr<pv::data::AnalogSegment> > &segments = | |
721 | base_->analog_data()->analog_segments(); | |
722 | ||
723 | if (segments.empty()) | |
724 | return; | |
725 | ||
726 | static double prev_min = 0, prev_max = 0; | |
727 | double min = 0, max = 0; | |
728 | ||
729 | for (shared_ptr<pv::data::AnalogSegment> segment : segments) { | |
6f925ba9 | 730 | pair<double, double> mm = segment->get_min_max(); |
73e377fe SA |
731 | min = std::min(min, mm.first); |
732 | max = std::max(max, mm.second); | |
733 | } | |
734 | ||
735 | if ((min == prev_min) && (max == prev_max) && !force_update) | |
736 | return; | |
737 | ||
738 | prev_min = min; | |
739 | prev_max = max; | |
740 | ||
430b94aa SA |
741 | // If we're allowed to alter the div assignment... |
742 | if (!keep_divs) { | |
743 | // Use all divs for the positive range if there are no negative values | |
744 | if ((min == 0) && (neg_vdivs_ > 0)) { | |
745 | pos_vdivs_ += neg_vdivs_; | |
746 | neg_vdivs_ = 0; | |
747 | } | |
73e377fe | 748 | |
430b94aa SA |
749 | // Split up the divs if there are negative values but no negative divs |
750 | if ((min < 0) && (neg_vdivs_ == 0)) { | |
751 | neg_vdivs_ = pos_vdivs_ / 2; | |
752 | pos_vdivs_ -= neg_vdivs_; | |
753 | } | |
681b6d5a SA |
754 | } |
755 | ||
430b94aa SA |
756 | // If there is still no positive div when we need it, add one |
757 | // (this can happen when pos_vdivs==neg_vdivs==0) | |
17c7f31c | 758 | if ((max > 0) && (pos_vdivs_ == 0)) { |
430b94aa | 759 | pos_vdivs_ = 1; |
17c7f31c SA |
760 | owner_->extents_changed(false, true); |
761 | } | |
430b94aa SA |
762 | |
763 | // If there is still no negative div when we need it, add one | |
764 | // (this can happen when pos_vdivs was 0 or 1 when trying to split) | |
17c7f31c | 765 | if ((min < 0) && (neg_vdivs_ == 0)) { |
430b94aa | 766 | neg_vdivs_ = 1; |
17c7f31c SA |
767 | owner_->extents_changed(false, true); |
768 | } | |
430b94aa | 769 | |
73e377fe SA |
770 | double min_value_per_div; |
771 | if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0)) | |
772 | min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_); | |
773 | else if (pos_vdivs_ > 0) | |
774 | min_value_per_div = max / pos_vdivs_; | |
775 | else | |
776 | min_value_per_div = -min / neg_vdivs_; | |
777 | ||
778 | // Find first scale value that is bigger than the value we need | |
779 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) | |
780 | if (get_resolution(i) > min_value_per_div) { | |
781 | scale_index_ = i; | |
782 | break; | |
783 | } | |
784 | ||
785 | update_scale(); | |
786 | } | |
787 | ||
4cffac16 SA |
788 | void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form) |
789 | { | |
790 | // Add the standard options | |
791 | Signal::populate_popup_form(parent, form); | |
792 | ||
368a37c2 SA |
793 | QFormLayout *const layout = new QFormLayout; |
794 | ||
cbb50547 | 795 | // Add div-related settings |
430b94aa SA |
796 | pvdiv_sb_ = new QSpinBox(parent); |
797 | pvdiv_sb_->setRange(0, MaximumVDivs); | |
798 | pvdiv_sb_->setValue(pos_vdivs_); | |
799 | connect(pvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 800 | this, SLOT(on_pos_vdivs_changed(int))); |
430b94aa | 801 | layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_); |
459db2c5 | 802 | |
430b94aa SA |
803 | nvdiv_sb_ = new QSpinBox(parent); |
804 | nvdiv_sb_->setRange(0, MaximumVDivs); | |
805 | nvdiv_sb_->setValue(neg_vdivs_); | |
806 | connect(nvdiv_sb_, SIGNAL(valueChanged(int)), | |
459db2c5 | 807 | this, SLOT(on_neg_vdivs_changed(int))); |
430b94aa | 808 | layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_); |
368a37c2 | 809 | |
cbb50547 SA |
810 | div_height_sb_ = new QSpinBox(parent); |
811 | div_height_sb_->setRange(20, 1000); | |
812 | div_height_sb_->setSingleStep(5); | |
813 | div_height_sb_->setSuffix(tr(" pixels")); | |
814 | div_height_sb_->setValue(div_height_); | |
815 | connect(div_height_sb_, SIGNAL(valueChanged(int)), | |
816 | this, SLOT(on_div_height_changed(int))); | |
817 | layout->addRow(tr("Div height"), div_height_sb_); | |
818 | ||
368a37c2 SA |
819 | // Add the vertical resolution |
820 | resolution_cb_ = new QComboBox(parent); | |
821 | ||
822 | for (int i = MinScaleIndex; i < MaxScaleIndex; i++) { | |
823 | const QString label = QString("%1").arg(get_resolution(i)); | |
824 | resolution_cb_->insertItem(0, label, QVariant(i)); | |
825 | } | |
826 | ||
a970015f | 827 | int cur_idx = resolution_cb_->findData(QVariant(scale_index_)); |
368a37c2 SA |
828 | resolution_cb_->setCurrentIndex(cur_idx); |
829 | ||
830 | connect(resolution_cb_, SIGNAL(currentIndexChanged(int)), | |
831 | this, SLOT(on_resolution_changed(int))); | |
832 | ||
833 | QGridLayout *const vdiv_layout = new QGridLayout; | |
834 | QLabel *const vdiv_unit = new QLabel(tr("V/div")); | |
835 | vdiv_layout->addWidget(resolution_cb_, 0, 0); | |
836 | vdiv_layout->addWidget(vdiv_unit, 0, 1); | |
837 | ||
838 | layout->addRow(tr("Vertical resolution"), vdiv_layout); | |
839 | ||
73e377fe SA |
840 | // Add the autoranging checkbox |
841 | QCheckBox* autoranging_cb = new QCheckBox(); | |
842 | autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked); | |
843 | ||
844 | connect(autoranging_cb, SIGNAL(stateChanged(int)), | |
845 | this, SLOT(on_autoranging_changed(int))); | |
846 | ||
847 | layout->addRow(tr("Autoranging"), autoranging_cb); | |
848 | ||
a970015f SA |
849 | // Add the conversion type dropdown |
850 | conversion_cb_ = new QComboBox(); | |
851 | ||
2b6e7a72 SA |
852 | conversion_cb_->addItem(tr("none"), |
853 | SignalBase::NoConversion); | |
854 | conversion_cb_->addItem(tr("to logic via threshold"), | |
b9cdbe03 | 855 | SignalBase::A2LConversionByThreshold); |
2b6e7a72 SA |
856 | conversion_cb_->addItem(tr("to logic via schmitt-trigger"), |
857 | SignalBase::A2LConversionBySchmittTrigger); | |
a970015f | 858 | |
06b6ce26 | 859 | cur_idx = conversion_cb_->findData(QVariant(base_->get_conversion_type())); |
a970015f SA |
860 | conversion_cb_->setCurrentIndex(cur_idx); |
861 | ||
7c84c074 | 862 | layout->addRow(tr("Conversion"), conversion_cb_); |
a970015f SA |
863 | |
864 | connect(conversion_cb_, SIGNAL(currentIndexChanged(int)), | |
865 | this, SLOT(on_conversion_changed(int))); | |
866 | ||
52c900ac SA |
867 | // Add the conversion threshold settings |
868 | conv_threshold_cb_ = new QComboBox(); | |
869 | conv_threshold_cb_->setEditable(true); | |
870 | ||
871 | layout->addRow(tr("Conversion threshold(s)"), conv_threshold_cb_); | |
872 | ||
873 | connect(conv_threshold_cb_, SIGNAL(currentIndexChanged(int)), | |
874 | this, SLOT(on_conv_threshold_changed(int))); | |
875 | connect(conv_threshold_cb_, SIGNAL(editTextChanged(const QString)), | |
876 | this, SLOT(on_conv_threshold_changed())); // index will be -1 | |
877 | ||
a970015f SA |
878 | // Add the display type dropdown |
879 | display_type_cb_ = new QComboBox(); | |
880 | ||
c6b017fb SA |
881 | display_type_cb_->addItem(tr("analog"), DisplayAnalog); |
882 | display_type_cb_->addItem(tr("converted"), DisplayConverted); | |
883 | display_type_cb_->addItem(tr("analog+converted"), DisplayBoth); | |
a970015f SA |
884 | |
885 | cur_idx = display_type_cb_->findData(QVariant(display_type_)); | |
886 | display_type_cb_->setCurrentIndex(cur_idx); | |
887 | ||
c6b017fb | 888 | layout->addRow(tr("Show traces for"), display_type_cb_); |
a970015f | 889 | |
b8c4a95b SA |
890 | connect(display_type_cb_, SIGNAL(currentIndexChanged(int)), |
891 | this, SLOT(on_display_type_changed(int))); | |
892 | ||
52c900ac SA |
893 | // Update the conversion widget contents and states |
894 | update_conversion_widgets(); | |
895 | ||
368a37c2 | 896 | form->addRow(layout); |
4cffac16 SA |
897 | } |
898 | ||
c6d9cf65 | 899 | void AnalogSignal::on_min_max_changed(float min, float max) |
85715407 | 900 | { |
c6d9cf65 SA |
901 | (void)min; |
902 | (void)max; | |
903 | ||
904 | if (autoranging_) | |
905 | perform_autoranging(false, false); | |
85715407 SA |
906 | } |
907 | ||
459db2c5 SA |
908 | void AnalogSignal::on_pos_vdivs_changed(int vdivs) |
909 | { | |
430b94aa SA |
910 | if (vdivs == pos_vdivs_) |
911 | return; | |
912 | ||
459db2c5 SA |
913 | pos_vdivs_ = vdivs; |
914 | ||
80067e49 SA |
915 | // There has to be at least one div, positive or negative |
916 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
917 | pos_vdivs_ = 1; | |
918 | if (pvdiv_sb_) | |
919 | pvdiv_sb_->setValue(pos_vdivs_); | |
920 | } | |
921 | ||
430b94aa SA |
922 | if (autoranging_) { |
923 | perform_autoranging(true, true); | |
924 | ||
925 | // It could be that a positive or negative div was added, so update | |
926 | if (pvdiv_sb_) { | |
927 | pvdiv_sb_->setValue(pos_vdivs_); | |
928 | nvdiv_sb_->setValue(neg_vdivs_); | |
929 | } | |
930 | } | |
73e377fe | 931 | |
459db2c5 SA |
932 | if (owner_) { |
933 | // Call order is important, otherwise the lazy event handler won't work | |
934 | owner_->extents_changed(false, true); | |
935 | owner_->row_item_appearance_changed(false, true); | |
936 | } | |
937 | } | |
938 | ||
939 | void AnalogSignal::on_neg_vdivs_changed(int vdivs) | |
4cffac16 | 940 | { |
430b94aa SA |
941 | if (vdivs == neg_vdivs_) |
942 | return; | |
943 | ||
459db2c5 | 944 | neg_vdivs_ = vdivs; |
4cffac16 | 945 | |
80067e49 SA |
946 | // There has to be at least one div, positive or negative |
947 | if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) { | |
948 | pos_vdivs_ = 1; | |
949 | if (pvdiv_sb_) | |
950 | pvdiv_sb_->setValue(pos_vdivs_); | |
951 | } | |
952 | ||
430b94aa SA |
953 | if (autoranging_) { |
954 | perform_autoranging(true, true); | |
955 | ||
956 | // It could be that a positive or negative div was added, so update | |
957 | if (pvdiv_sb_) { | |
958 | pvdiv_sb_->setValue(pos_vdivs_); | |
959 | nvdiv_sb_->setValue(neg_vdivs_); | |
960 | } | |
961 | } | |
73e377fe | 962 | |
75d0779e SA |
963 | if (owner_) { |
964 | // Call order is important, otherwise the lazy event handler won't work | |
4cffac16 | 965 | owner_->extents_changed(false, true); |
75d0779e SA |
966 | owner_->row_item_appearance_changed(false, true); |
967 | } | |
4cffac16 SA |
968 | } |
969 | ||
cbb50547 SA |
970 | void AnalogSignal::on_div_height_changed(int height) |
971 | { | |
972 | div_height_ = height; | |
973 | update_scale(); | |
974 | ||
975 | if (owner_) { | |
976 | // Call order is important, otherwise the lazy event handler won't work | |
977 | owner_->extents_changed(false, true); | |
978 | owner_->row_item_appearance_changed(false, true); | |
979 | } | |
980 | } | |
981 | ||
368a37c2 SA |
982 | void AnalogSignal::on_resolution_changed(int index) |
983 | { | |
984 | scale_index_ = resolution_cb_->itemData(index).toInt(); | |
985 | update_scale(); | |
986 | ||
987 | if (owner_) | |
988 | owner_->row_item_appearance_changed(false, true); | |
989 | } | |
4cffac16 | 990 | |
73e377fe SA |
991 | void AnalogSignal::on_autoranging_changed(int state) |
992 | { | |
993 | autoranging_ = (state == Qt::Checked); | |
994 | ||
995 | if (autoranging_) | |
430b94aa | 996 | perform_autoranging(false, true); |
73e377fe | 997 | |
85715407 SA |
998 | if (owner_) { |
999 | // Call order is important, otherwise the lazy event handler won't work | |
1000 | owner_->extents_changed(false, true); | |
73e377fe | 1001 | owner_->row_item_appearance_changed(false, true); |
85715407 | 1002 | } |
73e377fe SA |
1003 | } |
1004 | ||
a970015f SA |
1005 | void AnalogSignal::on_conversion_changed(int index) |
1006 | { | |
2b6e7a72 | 1007 | SignalBase::ConversionType old_conv_type = base_->get_conversion_type(); |
a8fd2759 | 1008 | |
2b6e7a72 SA |
1009 | SignalBase::ConversionType conv_type = |
1010 | (SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt()); | |
a8fd2759 | 1011 | |
06b6ce26 SA |
1012 | if (conv_type != old_conv_type) { |
1013 | base_->set_conversion_type(conv_type); | |
52c900ac | 1014 | update_conversion_widgets(); |
06b6ce26 SA |
1015 | |
1016 | if (owner_) | |
1017 | owner_->row_item_appearance_changed(false, true); | |
a8fd2759 | 1018 | } |
a970015f SA |
1019 | } |
1020 | ||
52c900ac SA |
1021 | void AnalogSignal::on_conv_threshold_changed(int index) |
1022 | { | |
2b6e7a72 | 1023 | SignalBase::ConversionType conv_type = base_->get_conversion_type(); |
52c900ac SA |
1024 | |
1025 | // Note: index is set to -1 if the text in the combo box matches none of | |
1026 | // the entries in the combo box | |
1027 | ||
1028 | if ((index == -1) && (conv_threshold_cb_->currentText().length() == 0)) | |
1029 | return; | |
1030 | ||
1031 | // The combo box entry with the custom value has user_data set to -1 | |
1032 | const int user_data = conv_threshold_cb_->findText( | |
1033 | conv_threshold_cb_->currentText()); | |
1034 | ||
1035 | const bool use_custom_thr = (index == -1) || (user_data == -1); | |
1036 | ||
b9cdbe03 | 1037 | if (conv_type == SignalBase::A2LConversionByThreshold && use_custom_thr) { |
52c900ac SA |
1038 | // Not one of the preset values, try to parse the combo box text |
1039 | // Note: Regex loosely based on | |
1040 | // https://txt2re.com/index-c++.php3?s=0.1V&1&-13 | |
1041 | QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1042 | QString re2 = "([a-zA-Z]*)"; // SI unit | |
1043 | QRegExp regex(re1 + re2); | |
1044 | ||
1045 | const QString text = conv_threshold_cb_->currentText(); | |
1046 | if (!regex.exactMatch(text)) | |
1047 | return; // String doesn't match the regex | |
1048 | ||
1049 | QStringList tokens = regex.capturedTexts(); | |
1050 | ||
1051 | // For now, we simply assume that the unit is volt without modifiers | |
1052 | const double thr = tokens.at(1).toDouble(); | |
1053 | ||
932bc246 SA |
1054 | // Only restart the conversion if the threshold was updated. |
1055 | // We're starting a delayed conversion because the user may still be | |
1056 | // typing and the UI would lag if we kept on restarting it immediately | |
52c900ac | 1057 | if (base_->set_conversion_option("threshold_value", thr)) |
932bc246 | 1058 | base_->start_conversion(true); |
52c900ac SA |
1059 | } |
1060 | ||
2b6e7a72 | 1061 | if (conv_type == SignalBase::A2LConversionBySchmittTrigger && use_custom_thr) { |
52c900ac SA |
1062 | // Not one of the preset values, try to parse the combo box text |
1063 | // Note: Regex loosely based on | |
1064 | // https://txt2re.com/index-c++.php3?s=0.1V/0.2V&2&14&-22&3&15 | |
1065 | QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1066 | QString re2 = "([a-zA-Z]*)"; // SI unit | |
1067 | QString re3 = "\\/"; // Forward slash, not captured | |
1068 | QString re4 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value | |
1069 | QString re5 = "([a-zA-Z]*)"; // SI unit | |
1070 | QRegExp regex(re1 + re2 + re3 + re4 + re5); | |
1071 | ||
1072 | const QString text = conv_threshold_cb_->currentText(); | |
1073 | if (!regex.exactMatch(text)) | |
1074 | return; // String doesn't match the regex | |
1075 | ||
1076 | QStringList tokens = regex.capturedTexts(); | |
1077 | ||
1078 | // For now, we simply assume that the unit is volt without modifiers | |
1079 | const double low_thr = tokens.at(1).toDouble(); | |
1080 | const double high_thr = tokens.at(3).toDouble(); | |
1081 | ||
932bc246 SA |
1082 | // Only restart the conversion if one of the options was updated. |
1083 | // We're starting a delayed conversion because the user may still be | |
1084 | // typing and the UI would lag if we kept on restarting it immediately | |
52c900ac SA |
1085 | bool o1 = base_->set_conversion_option("threshold_value_low", low_thr); |
1086 | bool o2 = base_->set_conversion_option("threshold_value_high", high_thr); | |
1087 | if (o1 || o2) | |
932bc246 | 1088 | base_->start_conversion(true); // Start delayed conversion |
52c900ac SA |
1089 | } |
1090 | ||
f0f9c856 | 1091 | base_->set_conversion_preset((SignalBase::ConversionPreset)index); |
52c900ac | 1092 | |
932bc246 SA |
1093 | // Immediately start the conversion if we're not using custom values |
1094 | // (i.e. we're using one of the presets) | |
1095 | if (!use_custom_thr) | |
52c900ac SA |
1096 | base_->start_conversion(); |
1097 | } | |
1098 | ||
1099 | void AnalogSignal::on_delayed_conversion_starter() | |
1100 | { | |
1101 | base_->start_conversion(); | |
1102 | } | |
1103 | ||
b8c4a95b SA |
1104 | void AnalogSignal::on_display_type_changed(int index) |
1105 | { | |
1106 | display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt()); | |
1107 | ||
1108 | if (owner_) | |
1109 | owner_->row_item_appearance_changed(false, true); | |
1110 | } | |
1111 | ||
90ee1ed9 | 1112 | void AnalogSignal::on_settingViewConversionThresholdDispMode_changed(const QVariant new_value) |
2601ce96 | 1113 | { |
90ee1ed9 | 1114 | conversion_threshold_disp_mode_ = new_value.toInt(); |
2601ce96 SA |
1115 | |
1116 | if (owner_) | |
1117 | owner_->row_item_appearance_changed(false, true); | |
1118 | } | |
1119 | ||
1573bf16 | 1120 | } // namespace trace |
f4e57597 | 1121 | } // namespace views |
aba1dd16 | 1122 | } // namespace pv |