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