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