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