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