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