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