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