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