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