2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <QApplication>
32 #include <QFormLayout>
33 #include <QGridLayout>
37 #include "analogsignal.hpp"
38 #include "logicsignal.hpp"
41 #include "pv/data/analog.hpp"
42 #include "pv/data/analogsegment.hpp"
43 #include "pv/data/logic.hpp"
44 #include "pv/data/logicsegment.hpp"
45 #include "pv/data/signalbase.hpp"
46 #include "pv/globalsettings.hpp"
48 #include <libsigrokcxx/libsigrokcxx.hpp>
56 using std::numeric_limits;
57 using std::out_of_range;
59 using std::shared_ptr;
62 using pv::data::SignalBase;
68 const QColor AnalogSignal::SignalColors[4] = {
69 QColor(0xC4, 0xA0, 0x00), // Yellow
70 QColor(0x87, 0x20, 0x7A), // Magenta
71 QColor(0x20, 0x4A, 0x87), // Blue
72 QColor(0x4E, 0x9A, 0x06) // Green
75 const QPen AnalogSignal::AxisPen(QColor(0, 0, 0, 30 * 256 / 100), 2);
76 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100);
77 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100);
79 const QColor AnalogSignal::SamplingPointColor(0x77, 0x77, 0x77);
80 const QColor AnalogSignal::SamplingPointColorLo = QColor(200, 0, 0, 80 * 256 / 100);
81 const QColor AnalogSignal::SamplingPointColorNe = QColor(0, 0, 0, 80 * 256 / 100);
82 const QColor AnalogSignal::SamplingPointColorHi = QColor(0, 200, 0, 80 * 256 / 100);
84 const QColor AnalogSignal::ThresholdColor = QColor(0, 0, 0, 30 * 256 / 100);
85 const QColor AnalogSignal::ThresholdColorLo = QColor(255, 0, 0, 8 * 256 / 100);
86 const QColor AnalogSignal::ThresholdColorNe = QColor(0, 0, 0, 10 * 256 / 100);
87 const QColor AnalogSignal::ThresholdColorHi = QColor(0, 255, 0, 8 * 256 / 100);
89 const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float)
90 const float AnalogSignal::EnvelopeThreshold = 64.0f;
92 const int AnalogSignal::MaximumVDivs = 10;
93 const int AnalogSignal::MinScaleIndex = -6;
94 const int AnalogSignal::MaxScaleIndex = 7;
96 const int AnalogSignal::InfoTextMarginRight = 20;
97 const int AnalogSignal::InfoTextMarginBottom = 5;
99 AnalogSignal::AnalogSignal(
100 pv::Session &session,
101 shared_ptr<data::SignalBase> base) :
102 Signal(session, base),
103 scale_index_(4), // 20 per div
104 scale_index_drag_offset_(0),
108 display_type_(DisplayBoth),
113 pv::data::Analog* analog_data =
114 dynamic_cast<pv::data::Analog*>(data().get());
116 connect(analog_data, SIGNAL(min_max_changed(float, float)),
117 this, SLOT(on_min_max_changed(float, float)));
119 GlobalSettings::add_change_handler(this);
122 conversion_threshold_disp_mode_ =
123 gs.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt();
125 div_height_ = gs.value(GlobalSettings::Key_View_DefaultDivHeight).toInt();
127 base_->set_color(SignalColors[base_->index() % countof(SignalColors)]);
131 AnalogSignal::~AnalogSignal()
133 GlobalSettings::remove_change_handler(this);
136 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
138 return base_->analog_data();
141 void AnalogSignal::save_settings(QSettings &settings) const
143 settings.setValue("pos_vdivs", pos_vdivs_);
144 settings.setValue("neg_vdivs", neg_vdivs_);
145 settings.setValue("scale_index", scale_index_);
146 settings.setValue("display_type", display_type_);
147 settings.setValue("autoranging", autoranging_);
148 settings.setValue("div_height", div_height_);
151 void AnalogSignal::restore_settings(QSettings &settings)
153 if (settings.contains("pos_vdivs"))
154 pos_vdivs_ = settings.value("pos_vdivs").toInt();
156 if (settings.contains("neg_vdivs"))
157 neg_vdivs_ = settings.value("neg_vdivs").toInt();
159 if (settings.contains("scale_index")) {
160 scale_index_ = settings.value("scale_index").toInt();
164 if (settings.contains("display_type"))
165 display_type_ = (DisplayType)(settings.value("display_type").toInt());
167 if (settings.contains("autoranging"))
168 autoranging_ = settings.value("autoranging").toBool();
170 if (settings.contains("div_height")) {
171 const int old_height = div_height_;
172 div_height_ = settings.value("div_height").toInt();
174 if ((div_height_ != old_height) && owner_) {
175 // Call order is important, otherwise the lazy event handler won't work
176 owner_->extents_changed(false, true);
177 owner_->row_item_appearance_changed(false, true);
182 pair<int, int> AnalogSignal::v_extents() const
184 const int ph = pos_vdivs_ * div_height_;
185 const int nh = neg_vdivs_ * div_height_;
186 return make_pair(-ph, nh);
189 int AnalogSignal::scale_handle_offset() const
191 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
193 return ((scale_index_drag_offset_ - scale_index_) * h / 4) - h / 2;
196 void AnalogSignal::scale_handle_dragged(int offset)
198 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
200 scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4);
205 void AnalogSignal::scale_handle_drag_release()
207 scale_index_drag_offset_ = scale_index_;
211 void AnalogSignal::on_setting_changed(const QString &key, const QVariant &value)
213 if (key == GlobalSettings::Key_View_ConversionThresholdDispMode)
214 on_settingViewConversionThresholdDispMode_changed(value);
217 void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp)
219 if (!base_->enabled())
223 conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Background;
225 const vector<double> thresholds = base_->get_conversion_thresholds();
227 // Only display thresholds if we have some and we show analog samples
228 if ((thresholds.size() > 0) && paint_thr_bg &&
229 ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth))) {
231 const int visual_y = get_visual_y();
232 const pair<int, int> extents = v_extents();
233 const int top = visual_y + extents.first;
234 const int btm = visual_y + extents.second;
236 // Draw high/neutral/low areas
237 if (thresholds.size() == 2) {
238 int thr_lo = visual_y - thresholds[0] * scale_;
239 int thr_hi = visual_y - thresholds[1] * scale_;
240 thr_lo = min(max(thr_lo, top), btm);
241 thr_hi = min(max(thr_hi, top), btm);
243 p.fillRect(QRectF(pp.left(), top, pp.width(), thr_hi - top),
244 QBrush(ThresholdColorHi));
245 p.fillRect(QRectF(pp.left(), thr_hi, pp.width(), thr_lo - thr_hi),
246 QBrush(ThresholdColorNe));
247 p.fillRect(QRectF(pp.left(), thr_lo, pp.width(), btm - thr_lo),
248 QBrush(ThresholdColorLo));
250 int thr = visual_y - thresholds[0] * scale_;
251 thr = min(max(thr, top), btm);
253 p.fillRect(QRectF(pp.left(), top, pp.width(), thr - top),
254 QBrush(ThresholdColorHi));
255 p.fillRect(QRectF(pp.left(), thr, pp.width(), btm - thr),
256 QBrush(ThresholdColorLo));
259 paint_axis(p, pp, get_visual_y());
261 Trace::paint_back(p, pp);
262 paint_axis(p, pp, get_visual_y());
266 void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp)
268 assert(base_->analog_data());
271 const int y = get_visual_y();
273 if (!base_->enabled())
276 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
277 paint_grid(p, y, pp.left(), pp.right());
279 shared_ptr<pv::data::AnalogSegment> segment = get_analog_segment_to_paint();
280 if (!segment || (segment->get_sample_count() == 0))
283 const double pixels_offset = pp.pixels_offset();
284 const double samplerate = max(1.0, segment->samplerate());
285 const pv::util::Timestamp& start_time = segment->start_time();
286 const int64_t last_sample = (int64_t)segment->get_sample_count() - 1;
287 const double samples_per_pixel = samplerate * pp.scale();
288 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
289 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
291 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
292 (int64_t)0), last_sample);
293 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
294 (int64_t)0), last_sample);
296 if (samples_per_pixel < EnvelopeThreshold)
297 paint_trace(p, segment, y, pp.left(),
298 start_sample, end_sample,
299 pixels_offset, samples_per_pixel);
301 paint_envelope(p, segment, y, pp.left(),
302 start_sample, end_sample,
303 pixels_offset, samples_per_pixel);
306 if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth))
307 paint_logic_mid(p, pp);
310 void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp)
315 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
316 const int y = get_visual_y();
318 // Show the info section on the right side of the trace
319 const QString infotext = QString("%1 V/div").arg(resolution_);
321 p.setPen(base_->color());
322 p.setFont(QApplication::font());
324 const QRectF bounding_rect = QRectF(pp.left(),
325 y + v_extents().first,
326 pp.width() - InfoTextMarginRight,
327 v_extents().second - v_extents().first - InfoTextMarginBottom);
329 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
333 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
335 p.setRenderHint(QPainter::Antialiasing, false);
337 GlobalSettings settings;
338 const bool show_analog_minor_grid =
339 settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
341 if (pos_vdivs_ > 0) {
342 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
343 for (int i = 1; i <= pos_vdivs_; i++) {
344 const float dy = i * div_height_;
345 p.drawLine(QLineF(left, y - dy, right, y - dy));
349 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
350 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
351 for (int i = 0; i < pos_vdivs_; i++) {
352 const float dy = i * div_height_;
353 const float dy25 = dy + (0.25 * div_height_);
354 const float dy50 = dy + (0.50 * div_height_);
355 const float dy75 = dy + (0.75 * div_height_);
356 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
357 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
358 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
362 if (neg_vdivs_ > 0) {
363 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
364 for (int i = 1; i <= neg_vdivs_; i++) {
365 const float dy = i * div_height_;
366 p.drawLine(QLineF(left, y + dy, right, y + dy));
370 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
371 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
372 for (int i = 0; i < neg_vdivs_; i++) {
373 const float dy = i * div_height_;
374 const float dy25 = dy + (0.25 * div_height_);
375 const float dy50 = dy + (0.50 * div_height_);
376 const float dy75 = dy + (0.75 * div_height_);
377 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
378 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
379 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
383 p.setRenderHint(QPainter::Antialiasing, true);
386 void AnalogSignal::paint_trace(QPainter &p,
387 const shared_ptr<pv::data::AnalogSegment> &segment,
388 int y, int left, const int64_t start, const int64_t end,
389 const double pixels_offset, const double samples_per_pixel)
394 bool paint_thr_dots =
395 (base_->get_conversion_type() != data::SignalBase::NoConversion) &&
396 (conversion_threshold_disp_mode_ == GlobalSettings::ConvThrDispMode_Dots);
398 vector<double> thresholds;
400 thresholds = base_->get_conversion_thresholds();
402 // Calculate and paint the sampling points if enabled and useful
403 GlobalSettings settings;
404 const bool show_sampling_points =
405 (settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() ||
406 paint_thr_dots) && (samples_per_pixel < 0.25);
408 p.setPen(base_->color());
410 const int64_t points_count = end - start + 1;
412 QPointF *points = new QPointF[points_count];
413 QPointF *point = points;
415 vector<QRectF> sampling_points[3];
417 int64_t sample_count = min(points_count, TracePaintBlockSize);
418 int64_t block_sample = 0;
419 float *sample_block = new float[TracePaintBlockSize];
420 segment->get_samples(start, start + sample_count, sample_block);
423 for (int64_t sample = start; sample <= end; sample++, block_sample++) {
425 if (block_sample == TracePaintBlockSize) {
427 sample_count = min(points_count - sample, TracePaintBlockSize);
428 segment->get_samples(sample, sample + sample_count, sample_block);
431 const float x = left + (sample / samples_per_pixel - pixels_offset);
433 *point++ = QPointF(x, y - sample_block[block_sample] * scale_);
435 if (show_sampling_points) {
436 int idx = 0; // Neutral
438 if (paint_thr_dots) {
439 if (thresholds.size() == 1)
440 idx = (sample_block[block_sample] >= thresholds[0]) ? 2 : 1;
441 else if (thresholds.size() == 2) {
442 if (sample_block[block_sample] > thresholds[1])
444 else if (sample_block[block_sample] < thresholds[0])
449 sampling_points[idx].emplace_back(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w);
452 delete[] sample_block;
454 p.drawPolyline(points, points_count);
456 if (show_sampling_points) {
457 if (paint_thr_dots) {
458 p.setPen(SamplingPointColorNe);
459 p.drawRects(sampling_points[0].data(), sampling_points[0].size());
460 p.setPen(SamplingPointColorLo);
461 p.drawRects(sampling_points[1].data(), sampling_points[1].size());
462 p.setPen(SamplingPointColorHi);
463 p.drawRects(sampling_points[2].data(), sampling_points[2].size());
465 p.setPen(SamplingPointColor);
466 p.drawRects(sampling_points[0].data(), sampling_points[0].size());
473 void AnalogSignal::paint_envelope(QPainter &p,
474 const shared_ptr<pv::data::AnalogSegment> &segment,
475 int y, int left, const int64_t start, const int64_t end,
476 const double pixels_offset, const double samples_per_pixel)
478 using pv::data::AnalogSegment;
480 AnalogSegment::EnvelopeSection e;
481 segment->get_envelope_section(e, start, end, samples_per_pixel);
486 p.setPen(QPen(Qt::NoPen));
487 p.setBrush(base_->color());
489 QRectF *const rects = new QRectF[e.length];
490 QRectF *rect = rects;
492 for (uint64_t sample = 0; sample < e.length - 1; sample++) {
493 const float x = ((e.scale * sample + e.start) /
494 samples_per_pixel - pixels_offset) + left;
495 const AnalogSegment::EnvelopeSample *const s =
498 // We overlap this sample with the next so that vertical
499 // gaps do not appear during steep rising or falling edges
500 const float b = y - max(s->max, (s + 1)->min) * scale_;
501 const float t = y - min(s->min, (s + 1)->max) * scale_;
504 if (h >= 0.0f && h <= 1.0f)
506 if (h <= 0.0f && h >= -1.0f)
509 *rect++ = QRectF(x, t, 1.0f, h);
512 p.drawRects(rects, e.length);
518 void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp)
522 vector< pair<int64_t, bool> > edges;
526 const int y = get_visual_y();
528 if (!base_->enabled() || !base_->logic_data())
531 const int signal_margin =
532 QFontMetrics(QApplication::font()).height() / 2;
534 const int ph = min(pos_vdivs_, 1) * div_height_;
535 const int nh = min(neg_vdivs_, 1) * div_height_;
536 const float high_offset = y - ph + signal_margin + 0.5f;
537 const float low_offset = y + nh - signal_margin - 0.5f;
539 shared_ptr<pv::data::LogicSegment> segment = get_logic_segment_to_paint();
540 if (!segment || (segment->get_sample_count() == 0))
543 double samplerate = segment->samplerate();
545 // Show sample rate as 1Hz when it is unknown
546 if (samplerate == 0.0)
549 const double pixels_offset = pp.pixels_offset();
550 const pv::util::Timestamp& start_time = segment->start_time();
551 const int64_t last_sample = (int64_t)segment->get_sample_count() - 1;
552 const double samples_per_pixel = samplerate * pp.scale();
553 const double pixels_per_sample = 1 / samples_per_pixel;
554 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
555 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
557 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
558 (int64_t)0), last_sample);
559 const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(),
560 (int64_t)0), last_sample);
562 segment->get_subsampled_edges(edges, start_sample, end_sample,
563 samples_per_pixel / LogicSignal::Oversampling, 0);
564 assert(edges.size() >= 2);
566 // Check whether we need to paint the sampling points
567 GlobalSettings settings;
568 const bool show_sampling_points =
569 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() &&
570 (samples_per_pixel < 0.25);
572 vector<QRectF> sampling_points;
573 float sampling_point_x = 0.0f;
574 int64_t sampling_point_sample = start_sample;
577 if (show_sampling_points) {
578 sampling_points.reserve(end_sample - start_sample + 1);
579 sampling_point_x = (edges.cbegin()->first / samples_per_pixel - pixels_offset) + pp.left();
583 const unsigned int edge_count = edges.size() - 2;
584 QLineF *const edge_lines = new QLineF[edge_count];
587 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
588 const float x = ((*i).first / samples_per_pixel -
589 pixels_offset) + pp.left();
590 *line++ = QLineF(x, high_offset, x, low_offset);
592 if (show_sampling_points)
593 while (sampling_point_sample < (*i).first) {
594 const float y = (*i).second ? low_offset : high_offset;
595 sampling_points.emplace_back(
596 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
597 sampling_point_sample++;
598 sampling_point_x += pixels_per_sample;
602 // Calculate the sample points from the last edge to the end of the trace
603 if (show_sampling_points)
604 while ((uint64_t)sampling_point_sample <= end_sample) {
605 // Signal changed after the last edge, so the level is inverted
606 const float y = (edges.cend() - 1)->second ? high_offset : low_offset;
607 sampling_points.emplace_back(
608 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
609 sampling_point_sample++;
610 sampling_point_x += pixels_per_sample;
613 p.setPen(LogicSignal::EdgeColor);
614 p.drawLines(edge_lines, edge_count);
618 const unsigned int max_cap_line_count = edges.size();
619 QLineF *const cap_lines = new QLineF[max_cap_line_count];
621 p.setPen(LogicSignal::HighColor);
622 paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel,
623 pixels_offset, pp.left(), high_offset);
624 p.setPen(LogicSignal::LowColor);
625 paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel,
626 pixels_offset, pp.left(), low_offset);
630 // Paint the sampling points
631 if (show_sampling_points) {
632 p.setPen(SamplingPointColor);
633 p.drawRects(sampling_points.data(), sampling_points.size());
637 void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines,
638 vector< pair<int64_t, bool> > &edges, bool level,
639 double samples_per_pixel, double pixels_offset, float x_offset,
642 QLineF *line = lines;
644 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
645 if ((*i).second == level) {
647 ((*i).first / samples_per_pixel -
648 pixels_offset) + x_offset, y_offset,
649 ((*(i+1)).first / samples_per_pixel -
650 pixels_offset) + x_offset, y_offset);
653 p.drawLines(lines, line - lines);
656 shared_ptr<pv::data::AnalogSegment> AnalogSignal::get_analog_segment_to_paint() const
658 shared_ptr<pv::data::AnalogSegment> segment;
660 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
661 base_->analog_data()->analog_segments();
663 if (!segments.empty()) {
664 if (segment_display_mode_ == ShowLastSegmentOnly)
665 segment = segments.back();
667 if ((segment_display_mode_ == ShowSingleSegmentOnly) ||
668 (segment_display_mode_ == ShowLastCompleteSegmentOnly)) {
670 segment = segments.at(current_segment_);
671 } catch (out_of_range&) {
672 qDebug() << "Current analog segment out of range for signal" << base_->name() << ":" << current_segment_;
680 shared_ptr<pv::data::LogicSegment> AnalogSignal::get_logic_segment_to_paint() const
682 shared_ptr<pv::data::LogicSegment> segment;
684 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
685 base_->logic_data()->logic_segments();
687 if (!segments.empty()) {
688 if (segment_display_mode_ == ShowLastSegmentOnly)
689 segment = segments.back();
691 if ((segment_display_mode_ == ShowSingleSegmentOnly) ||
692 (segment_display_mode_ == ShowLastCompleteSegmentOnly)) {
694 segment = segments.at(current_segment_);
695 } catch (out_of_range&) {
696 qDebug() << "Current logic segment out of range for signal" << base_->name() << ":" << current_segment_;
704 float AnalogSignal::get_resolution(int scale_index)
706 const float seq[] = {1.0f, 2.0f, 5.0f};
708 const int offset = numeric_limits<int>::max() / (2 * countof(seq));
709 const div_t d = div((int)(scale_index + countof(seq) * offset),
712 return powf(10.0f, d.quot - offset) * seq[d.rem];
715 void AnalogSignal::update_scale()
717 resolution_ = get_resolution(scale_index_);
718 scale_ = div_height_ / resolution_;
721 void AnalogSignal::update_conversion_widgets()
723 SignalBase::ConversionType conv_type = base_->get_conversion_type();
725 // Enable or disable widgets depending on conversion state
726 conv_threshold_cb_->setEnabled(conv_type != SignalBase::NoConversion);
727 display_type_cb_->setEnabled(conv_type != SignalBase::NoConversion);
729 conv_threshold_cb_->clear();
731 vector < pair<QString, int> > presets = base_->get_conversion_presets();
733 // Prevent the combo box from firing the "edit text changed" signal
734 // as that would involuntarily select the first entry
735 conv_threshold_cb_->blockSignals(true);
737 // Set available options depending on chosen conversion
738 for (pair<QString, int> preset : presets)
739 conv_threshold_cb_->addItem(preset.first, preset.second);
741 map < QString, QVariant > options = base_->get_conversion_options();
743 if (conv_type == SignalBase::A2LConversionByThreshold) {
744 const vector<double> thresholds = base_->get_conversion_thresholds(
745 SignalBase::A2LConversionByThreshold, true);
746 conv_threshold_cb_->addItem(
747 QString("%1V").arg(QString::number(thresholds[0], 'f', 1)), -1);
750 if (conv_type == SignalBase::A2LConversionBySchmittTrigger) {
751 const vector<double> thresholds = base_->get_conversion_thresholds(
752 SignalBase::A2LConversionBySchmittTrigger, true);
753 conv_threshold_cb_->addItem(QString("%1V/%2V").arg(
754 QString::number(thresholds[0], 'f', 1),
755 QString::number(thresholds[1], 'f', 1)), -1);
758 int preset_id = base_->get_current_conversion_preset();
759 conv_threshold_cb_->setCurrentIndex(
760 conv_threshold_cb_->findData(preset_id));
762 conv_threshold_cb_->blockSignals(false);
765 void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update)
767 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
768 base_->analog_data()->analog_segments();
770 if (segments.empty())
773 static double prev_min = 0, prev_max = 0;
774 double min = 0, max = 0;
776 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
777 pair<double, double> mm = segment->get_min_max();
778 min = std::min(min, mm.first);
779 max = std::max(max, mm.second);
782 if ((min == prev_min) && (max == prev_max) && !force_update)
788 // If we're allowed to alter the div assignment...
790 // Use all divs for the positive range if there are no negative values
791 if ((min == 0) && (neg_vdivs_ > 0)) {
792 pos_vdivs_ += neg_vdivs_;
796 // Split up the divs if there are negative values but no negative divs
797 if ((min < 0) && (neg_vdivs_ == 0)) {
798 neg_vdivs_ = pos_vdivs_ / 2;
799 pos_vdivs_ -= neg_vdivs_;
803 // If there is still no positive div when we need it, add one
804 // (this can happen when pos_vdivs==neg_vdivs==0)
805 if ((max > 0) && (pos_vdivs_ == 0)) {
807 owner_->extents_changed(false, true);
810 // If there is still no negative div when we need it, add one
811 // (this can happen when pos_vdivs was 0 or 1 when trying to split)
812 if ((min < 0) && (neg_vdivs_ == 0)) {
814 owner_->extents_changed(false, true);
817 double min_value_per_div;
818 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
819 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
820 else if (pos_vdivs_ > 0)
821 min_value_per_div = max / pos_vdivs_;
823 min_value_per_div = -min / neg_vdivs_;
825 // Find first scale value that is bigger than the value we need
826 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
827 if (get_resolution(i) > min_value_per_div) {
835 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
837 // Add the standard options
838 Signal::populate_popup_form(parent, form);
840 QFormLayout *const layout = new QFormLayout;
842 // Add div-related settings
843 pvdiv_sb_ = new QSpinBox(parent);
844 pvdiv_sb_->setRange(0, MaximumVDivs);
845 pvdiv_sb_->setValue(pos_vdivs_);
846 connect(pvdiv_sb_, SIGNAL(valueChanged(int)),
847 this, SLOT(on_pos_vdivs_changed(int)));
848 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_);
850 nvdiv_sb_ = new QSpinBox(parent);
851 nvdiv_sb_->setRange(0, MaximumVDivs);
852 nvdiv_sb_->setValue(neg_vdivs_);
853 connect(nvdiv_sb_, SIGNAL(valueChanged(int)),
854 this, SLOT(on_neg_vdivs_changed(int)));
855 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_);
857 div_height_sb_ = new QSpinBox(parent);
858 div_height_sb_->setRange(20, 1000);
859 div_height_sb_->setSingleStep(5);
860 div_height_sb_->setSuffix(tr(" pixels"));
861 div_height_sb_->setValue(div_height_);
862 connect(div_height_sb_, SIGNAL(valueChanged(int)),
863 this, SLOT(on_div_height_changed(int)));
864 layout->addRow(tr("Div height"), div_height_sb_);
866 // Add the vertical resolution
867 resolution_cb_ = new QComboBox(parent);
869 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
870 const QString label = QString("%1").arg(get_resolution(i));
871 resolution_cb_->insertItem(0, label, QVariant(i));
874 int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
875 resolution_cb_->setCurrentIndex(cur_idx);
877 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
878 this, SLOT(on_resolution_changed(int)));
880 QGridLayout *const vdiv_layout = new QGridLayout;
881 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
882 vdiv_layout->addWidget(resolution_cb_, 0, 0);
883 vdiv_layout->addWidget(vdiv_unit, 0, 1);
885 layout->addRow(tr("Vertical resolution"), vdiv_layout);
887 // Add the autoranging checkbox
888 QCheckBox* autoranging_cb = new QCheckBox();
889 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
891 connect(autoranging_cb, SIGNAL(stateChanged(int)),
892 this, SLOT(on_autoranging_changed(int)));
894 layout->addRow(tr("Autoranging"), autoranging_cb);
896 // Add the conversion type dropdown
897 conversion_cb_ = new QComboBox();
899 conversion_cb_->addItem(tr("none"),
900 SignalBase::NoConversion);
901 conversion_cb_->addItem(tr("to logic via threshold"),
902 SignalBase::A2LConversionByThreshold);
903 conversion_cb_->addItem(tr("to logic via schmitt-trigger"),
904 SignalBase::A2LConversionBySchmittTrigger);
906 cur_idx = conversion_cb_->findData(QVariant(base_->get_conversion_type()));
907 conversion_cb_->setCurrentIndex(cur_idx);
909 layout->addRow(tr("Conversion"), conversion_cb_);
911 connect(conversion_cb_, SIGNAL(currentIndexChanged(int)),
912 this, SLOT(on_conversion_changed(int)));
914 // Add the conversion threshold settings
915 conv_threshold_cb_ = new QComboBox();
916 conv_threshold_cb_->setEditable(true);
918 layout->addRow(tr("Conversion threshold(s)"), conv_threshold_cb_);
920 connect(conv_threshold_cb_, SIGNAL(currentIndexChanged(int)),
921 this, SLOT(on_conv_threshold_changed(int)));
922 connect(conv_threshold_cb_, SIGNAL(editTextChanged(const QString)),
923 this, SLOT(on_conv_threshold_changed())); // index will be -1
925 // Add the display type dropdown
926 display_type_cb_ = new QComboBox();
928 display_type_cb_->addItem(tr("analog"), DisplayAnalog);
929 display_type_cb_->addItem(tr("converted"), DisplayConverted);
930 display_type_cb_->addItem(tr("analog+converted"), DisplayBoth);
932 cur_idx = display_type_cb_->findData(QVariant(display_type_));
933 display_type_cb_->setCurrentIndex(cur_idx);
935 layout->addRow(tr("Show traces for"), display_type_cb_);
937 connect(display_type_cb_, SIGNAL(currentIndexChanged(int)),
938 this, SLOT(on_display_type_changed(int)));
940 // Update the conversion widget contents and states
941 update_conversion_widgets();
943 form->addRow(layout);
946 void AnalogSignal::on_min_max_changed(float min, float max)
952 perform_autoranging(false, false);
955 void AnalogSignal::on_pos_vdivs_changed(int vdivs)
957 if (vdivs == pos_vdivs_)
962 // There has to be at least one div, positive or negative
963 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
966 pvdiv_sb_->setValue(pos_vdivs_);
970 perform_autoranging(true, true);
972 // It could be that a positive or negative div was added, so update
974 pvdiv_sb_->setValue(pos_vdivs_);
975 nvdiv_sb_->setValue(neg_vdivs_);
980 // Call order is important, otherwise the lazy event handler won't work
981 owner_->extents_changed(false, true);
982 owner_->row_item_appearance_changed(false, true);
986 void AnalogSignal::on_neg_vdivs_changed(int vdivs)
988 if (vdivs == neg_vdivs_)
993 // There has to be at least one div, positive or negative
994 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
997 pvdiv_sb_->setValue(pos_vdivs_);
1001 perform_autoranging(true, true);
1003 // It could be that a positive or negative div was added, so update
1005 pvdiv_sb_->setValue(pos_vdivs_);
1006 nvdiv_sb_->setValue(neg_vdivs_);
1011 // Call order is important, otherwise the lazy event handler won't work
1012 owner_->extents_changed(false, true);
1013 owner_->row_item_appearance_changed(false, true);
1017 void AnalogSignal::on_div_height_changed(int height)
1019 div_height_ = height;
1023 // Call order is important, otherwise the lazy event handler won't work
1024 owner_->extents_changed(false, true);
1025 owner_->row_item_appearance_changed(false, true);
1029 void AnalogSignal::on_resolution_changed(int index)
1031 scale_index_ = resolution_cb_->itemData(index).toInt();
1035 owner_->row_item_appearance_changed(false, true);
1038 void AnalogSignal::on_autoranging_changed(int state)
1040 autoranging_ = (state == Qt::Checked);
1043 perform_autoranging(false, true);
1046 // Call order is important, otherwise the lazy event handler won't work
1047 owner_->extents_changed(false, true);
1048 owner_->row_item_appearance_changed(false, true);
1052 void AnalogSignal::on_conversion_changed(int index)
1054 SignalBase::ConversionType old_conv_type = base_->get_conversion_type();
1056 SignalBase::ConversionType conv_type =
1057 (SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt());
1059 if (conv_type != old_conv_type) {
1060 base_->set_conversion_type(conv_type);
1061 update_conversion_widgets();
1064 owner_->row_item_appearance_changed(false, true);
1068 void AnalogSignal::on_conv_threshold_changed(int index)
1070 SignalBase::ConversionType conv_type = base_->get_conversion_type();
1072 // Note: index is set to -1 if the text in the combo box matches none of
1073 // the entries in the combo box
1075 if ((index == -1) && (conv_threshold_cb_->currentText().length() == 0))
1078 // The combo box entry with the custom value has user_data set to -1
1079 const int user_data = conv_threshold_cb_->findText(
1080 conv_threshold_cb_->currentText());
1082 const bool use_custom_thr = (index == -1) || (user_data == -1);
1084 if (conv_type == SignalBase::A2LConversionByThreshold && use_custom_thr) {
1085 // Not one of the preset values, try to parse the combo box text
1086 // Note: Regex loosely based on
1087 // https://txt2re.com/index-c++.php3?s=0.1V&1&-13
1088 QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1089 QString re2 = "([a-zA-Z]*)"; // SI unit
1090 QRegExp regex(re1 + re2);
1092 const QString text = conv_threshold_cb_->currentText();
1093 if (!regex.exactMatch(text))
1094 return; // String doesn't match the regex
1096 QStringList tokens = regex.capturedTexts();
1098 // For now, we simply assume that the unit is volt without modifiers
1099 const double thr = tokens.at(1).toDouble();
1101 // Only restart the conversion if the threshold was updated.
1102 // We're starting a delayed conversion because the user may still be
1103 // typing and the UI would lag if we kept on restarting it immediately
1104 if (base_->set_conversion_option("threshold_value", thr))
1105 base_->start_conversion(true);
1108 if (conv_type == SignalBase::A2LConversionBySchmittTrigger && use_custom_thr) {
1109 // Not one of the preset values, try to parse the combo box text
1110 // Note: Regex loosely based on
1111 // https://txt2re.com/index-c++.php3?s=0.1V/0.2V&2&14&-22&3&15
1112 QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1113 QString re2 = "([a-zA-Z]*)"; // SI unit
1114 QString re3 = "\\/"; // Forward slash, not captured
1115 QString re4 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1116 QString re5 = "([a-zA-Z]*)"; // SI unit
1117 QRegExp regex(re1 + re2 + re3 + re4 + re5);
1119 const QString text = conv_threshold_cb_->currentText();
1120 if (!regex.exactMatch(text))
1121 return; // String doesn't match the regex
1123 QStringList tokens = regex.capturedTexts();
1125 // For now, we simply assume that the unit is volt without modifiers
1126 const double low_thr = tokens.at(1).toDouble();
1127 const double high_thr = tokens.at(3).toDouble();
1129 // Only restart the conversion if one of the options was updated.
1130 // We're starting a delayed conversion because the user may still be
1131 // typing and the UI would lag if we kept on restarting it immediately
1132 bool o1 = base_->set_conversion_option("threshold_value_low", low_thr);
1133 bool o2 = base_->set_conversion_option("threshold_value_high", high_thr);
1135 base_->start_conversion(true); // Start delayed conversion
1138 base_->set_conversion_preset((SignalBase::ConversionPreset)index);
1140 // Immediately start the conversion if we're not using custom values
1141 // (i.e. we're using one of the presets)
1142 if (!use_custom_thr)
1143 base_->start_conversion();
1146 void AnalogSignal::on_delayed_conversion_starter()
1148 base_->start_conversion();
1151 void AnalogSignal::on_display_type_changed(int index)
1153 display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt());
1156 owner_->row_item_appearance_changed(false, true);
1159 void AnalogSignal::on_settingViewConversionThresholdDispMode_changed(const QVariant new_value)
1161 conversion_threshold_disp_mode_ = new_value.toInt();
1164 owner_->row_item_appearance_changed(false, true);
1167 } // namespace trace
1168 } // namespace views