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