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