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>
31 #include <QFormLayout>
32 #include <QGridLayout>
36 #include "analogsignal.hpp"
37 #include "logicsignal.hpp"
40 #include "pv/data/analog.hpp"
41 #include "pv/data/analogsegment.hpp"
42 #include "pv/data/logic.hpp"
43 #include "pv/data/logicsegment.hpp"
44 #include "pv/data/signalbase.hpp"
45 #include "pv/globalsettings.hpp"
47 #include <libsigrokcxx/libsigrokcxx.hpp>
55 using std::numeric_limits;
57 using std::shared_ptr;
60 using pv::data::SignalBase;
66 const QColor AnalogSignal::SignalColours[4] = {
67 QColor(0xC4, 0xA0, 0x00), // Yellow
68 QColor(0x87, 0x20, 0x7A), // Magenta
69 QColor(0x20, 0x4A, 0x87), // Blue
70 QColor(0x4E, 0x9A, 0x06) // Green
73 const QPen AnalogSignal::AxisPen(QColor(0, 0, 0, 30 * 256 / 100), 2);
74 const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40 * 256 / 100);
75 const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20 * 256 / 100);
77 const QColor AnalogSignal::SamplingPointColour(0x77, 0x77, 0x77);
79 const QColor AnalogSignal::ThresholdColor = QColor(0, 0, 0, 30 * 256 / 100);
81 const int64_t AnalogSignal::TracePaintBlockSize = 1024 * 1024; // 4 MiB (due to float)
82 const float AnalogSignal::EnvelopeThreshold = 64.0f;
84 const int AnalogSignal::MaximumVDivs = 10;
85 const int AnalogSignal::MinScaleIndex = -6;
86 const int AnalogSignal::MaxScaleIndex = 7;
88 const int AnalogSignal::InfoTextMarginRight = 20;
89 const int AnalogSignal::InfoTextMarginBottom = 5;
91 AnalogSignal::AnalogSignal(
93 shared_ptr<data::SignalBase> base) :
94 Signal(session, base),
95 scale_index_(4), // 20 per div
96 scale_index_drag_offset_(0),
100 display_type_(DisplayBoth),
105 pv::data::Analog* analog_data =
106 dynamic_cast<pv::data::Analog*>(data().get());
108 connect(analog_data, SIGNAL(min_max_changed(float, float)),
109 this, SLOT(on_min_max_changed(float, float)));
112 div_height_ = gs.value(GlobalSettings::Key_View_DefaultDivHeight).toInt();
114 base_->set_colour(SignalColours[base_->index() % countof(SignalColours)]);
118 shared_ptr<pv::data::SignalData> AnalogSignal::data() const
120 return base_->analog_data();
123 void AnalogSignal::save_settings(QSettings &settings) const
125 settings.setValue("pos_vdivs", pos_vdivs_);
126 settings.setValue("neg_vdivs", neg_vdivs_);
127 settings.setValue("scale_index", scale_index_);
128 settings.setValue("display_type", display_type_);
129 settings.setValue("autoranging", autoranging_);
130 settings.setValue("div_height", div_height_);
133 void AnalogSignal::restore_settings(QSettings &settings)
135 if (settings.contains("pos_vdivs"))
136 pos_vdivs_ = settings.value("pos_vdivs").toInt();
138 if (settings.contains("neg_vdivs"))
139 neg_vdivs_ = settings.value("neg_vdivs").toInt();
141 if (settings.contains("scale_index")) {
142 scale_index_ = settings.value("scale_index").toInt();
146 if (settings.contains("display_type"))
147 display_type_ = (DisplayType)(settings.value("display_type").toInt());
149 if (settings.contains("autoranging"))
150 autoranging_ = settings.value("autoranging").toBool();
152 if (settings.contains("div_height")) {
153 const int old_height = div_height_;
154 div_height_ = settings.value("div_height").toInt();
156 if ((div_height_ != old_height) && owner_) {
157 // Call order is important, otherwise the lazy event handler won't work
158 owner_->extents_changed(false, true);
159 owner_->row_item_appearance_changed(false, true);
164 pair<int, int> AnalogSignal::v_extents() const
166 const int ph = pos_vdivs_ * div_height_;
167 const int nh = neg_vdivs_ * div_height_;
168 return make_pair(-ph, nh);
171 int AnalogSignal::scale_handle_offset() const
173 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
175 return ((scale_index_drag_offset_ - scale_index_) * h / 4) - h / 2;
178 void AnalogSignal::scale_handle_dragged(int offset)
180 const int h = (pos_vdivs_ + neg_vdivs_) * div_height_;
182 scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4);
187 void AnalogSignal::scale_handle_drag_release()
189 scale_index_drag_offset_ = scale_index_;
193 void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp)
195 if (base_->enabled()) {
196 Trace::paint_back(p, pp);
197 paint_axis(p, pp, get_visual_y());
201 void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp)
203 assert(base_->analog_data());
206 const int y = get_visual_y();
208 if (!base_->enabled())
211 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
212 paint_grid(p, y, pp.left(), pp.right());
214 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
215 base_->analog_data()->analog_segments();
216 if (segments.empty())
219 const shared_ptr<pv::data::AnalogSegment> &segment =
222 const double pixels_offset = pp.pixels_offset();
223 const double samplerate = max(1.0, segment->samplerate());
224 const pv::util::Timestamp& start_time = segment->start_time();
225 const int64_t last_sample = segment->get_sample_count() - 1;
226 const double samples_per_pixel = samplerate * pp.scale();
227 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
228 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
230 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
231 (int64_t)0), last_sample);
232 const int64_t end_sample = min(max((ceil(end) + 1).convert_to<int64_t>(),
233 (int64_t)0), last_sample);
235 if (samples_per_pixel < EnvelopeThreshold)
236 paint_trace(p, segment, y, pp.left(),
237 start_sample, end_sample,
238 pixels_offset, samples_per_pixel);
240 paint_envelope(p, segment, y, pp.left(),
241 start_sample, end_sample,
242 pixels_offset, samples_per_pixel);
245 const SignalBase::ConversionType conv_type = base_->get_conversion_type();
247 if (((conv_type == SignalBase::A2LConversionByThreshold) ||
248 (conv_type == SignalBase::A2LConversionBySchmittTrigger))) {
250 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth))
251 paint_conversion_thresholds(p, pp);
253 if ((display_type_ == DisplayConverted) || (display_type_ == DisplayBoth))
254 paint_logic_mid(p, pp);
258 void AnalogSignal::paint_fore(QPainter &p, ViewItemPaintParams &pp)
263 if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
264 const int y = get_visual_y();
266 // Show the info section on the right side of the trace
267 const QString infotext = QString("%1 V/div").arg(resolution_);
269 p.setPen(base_->colour());
270 p.setFont(QApplication::font());
272 const QRectF bounding_rect = QRectF(pp.left(),
273 y + v_extents().first,
274 pp.width() - InfoTextMarginRight,
275 v_extents().second - v_extents().first - InfoTextMarginBottom);
277 p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
281 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
283 p.setRenderHint(QPainter::Antialiasing, false);
285 GlobalSettings settings;
286 const bool show_analog_minor_grid =
287 settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
289 if (pos_vdivs_ > 0) {
290 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
291 for (int i = 1; i <= pos_vdivs_; i++) {
292 const float dy = i * div_height_;
293 p.drawLine(QLineF(left, y - dy, right, y - dy));
297 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
298 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
299 for (int i = 0; i < pos_vdivs_; i++) {
300 const float dy = i * div_height_;
301 const float dy25 = dy + (0.25 * div_height_);
302 const float dy50 = dy + (0.50 * div_height_);
303 const float dy75 = dy + (0.75 * div_height_);
304 p.drawLine(QLineF(left, y - dy25, right, y - dy25));
305 p.drawLine(QLineF(left, y - dy50, right, y - dy50));
306 p.drawLine(QLineF(left, y - dy75, right, y - dy75));
310 if (neg_vdivs_ > 0) {
311 p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
312 for (int i = 1; i <= neg_vdivs_; i++) {
313 const float dy = i * div_height_;
314 p.drawLine(QLineF(left, y + dy, right, y + dy));
318 if ((pos_vdivs_ > 0) && show_analog_minor_grid) {
319 p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
320 for (int i = 0; i < neg_vdivs_; i++) {
321 const float dy = i * div_height_;
322 const float dy25 = dy + (0.25 * div_height_);
323 const float dy50 = dy + (0.50 * div_height_);
324 const float dy75 = dy + (0.75 * div_height_);
325 p.drawLine(QLineF(left, y + dy25, right, y + dy25));
326 p.drawLine(QLineF(left, y + dy50, right, y + dy50));
327 p.drawLine(QLineF(left, y + dy75, right, y + dy75));
331 p.setRenderHint(QPainter::Antialiasing, true);
334 void AnalogSignal::paint_trace(QPainter &p,
335 const shared_ptr<pv::data::AnalogSegment> &segment,
336 int y, int left, const int64_t start, const int64_t end,
337 const double pixels_offset, const double samples_per_pixel)
342 // Calculate and paint the sampling points if enabled and useful
343 GlobalSettings settings;
344 const bool show_sampling_points =
345 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() &&
346 (samples_per_pixel < 0.25);
348 p.setPen(base_->colour());
350 const int64_t points_count = end - start;
352 QPointF *points = new QPointF[points_count];
353 QPointF *point = points;
355 QRectF *sampling_points = nullptr;
356 if (show_sampling_points)
357 sampling_points = new QRectF[points_count];
358 QRectF *sampling_point = sampling_points;
360 int64_t sample_count = min(points_count, TracePaintBlockSize);
361 int64_t block_sample = 0;
362 float *sample_block = new float[TracePaintBlockSize];
363 segment->get_samples(start, start + sample_count, sample_block);
366 for (int64_t sample = start; sample != end; sample++, block_sample++) {
368 if (block_sample == TracePaintBlockSize) {
370 sample_count = min(points_count - sample, TracePaintBlockSize);
371 segment->get_samples(sample, sample + sample_count, sample_block);
374 const float x = (sample / samples_per_pixel -
375 pixels_offset) + left;
377 *point++ = QPointF(x, y - sample_block[block_sample] * scale_);
379 if (show_sampling_points)
381 QRectF(x - (w / 2), y - sample_block[block_sample] * scale_ - (w / 2), w, w);
383 delete[] sample_block;
385 p.drawPolyline(points, points_count);
387 if (show_sampling_points) {
388 p.setPen(SamplingPointColour);
389 p.drawRects(sampling_points, points_count);
390 delete[] sampling_points;
396 void AnalogSignal::paint_envelope(QPainter &p,
397 const shared_ptr<pv::data::AnalogSegment> &segment,
398 int y, int left, const int64_t start, const int64_t end,
399 const double pixels_offset, const double samples_per_pixel)
401 using pv::data::AnalogSegment;
403 AnalogSegment::EnvelopeSection e;
404 segment->get_envelope_section(e, start, end, samples_per_pixel);
409 p.setPen(QPen(Qt::NoPen));
410 p.setBrush(base_->colour());
412 QRectF *const rects = new QRectF[e.length];
413 QRectF *rect = rects;
415 for (uint64_t sample = 0; sample < e.length - 1; sample++) {
416 const float x = ((e.scale * sample + e.start) /
417 samples_per_pixel - pixels_offset) + left;
418 const AnalogSegment::EnvelopeSample *const s =
421 // We overlap this sample with the next so that vertical
422 // gaps do not appear during steep rising or falling edges
423 const float b = y - max(s->max, (s + 1)->min) * scale_;
424 const float t = y - min(s->min, (s + 1)->max) * scale_;
427 if (h >= 0.0f && h <= 1.0f)
429 if (h <= 0.0f && h >= -1.0f)
432 *rect++ = QRectF(x, t, 1.0f, h);
435 p.drawRects(rects, e.length);
441 void AnalogSignal::paint_conversion_thresholds(QPainter &p,
442 ViewItemPaintParams &pp)
444 if (!base_->enabled() || !base_->logic_data())
447 // TODO Register a change handler instead of querying this with every repaint
448 GlobalSettings settings;
449 const bool show_conversion_thresholds =
450 settings.value(GlobalSettings::Key_View_ShowConversionThresholds).toBool();
452 if (!show_conversion_thresholds)
455 const vector<double> thresholds = base_->get_conversion_thresholds();
456 const int y = get_visual_y();
458 p.setRenderHint(QPainter::Antialiasing, false);
460 p.setPen(ThresholdColor);
462 if (thresholds.size() == 2) {
463 // Draw as hatched block because two thresholds denote lower/upper level
464 const double thr_y0 = y - thresholds[0] * scale_;
465 const double thr_y1 = y - thresholds[1] * scale_;
466 p.fillRect(QRect(pp.left(), thr_y0, pp.right(), thr_y1 - thr_y0),
467 QBrush(ThresholdColor, Qt::BDiagPattern));
469 // Draw as individual lines
470 for (const double thr : thresholds) {
471 const double thr_y = y - thr * scale_;
472 p.drawLine(QPointF(pp.left(), thr_y), QPointF(pp.right(), thr_y));
476 p.setRenderHint(QPainter::Antialiasing, true);
479 void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp)
483 vector< pair<int64_t, bool> > edges;
487 const int y = get_visual_y();
489 if (!base_->enabled() || !base_->logic_data())
492 const int signal_margin =
493 QFontMetrics(QApplication::font()).height() / 2;
495 const int ph = min(pos_vdivs_, 1) * div_height_;
496 const int nh = min(neg_vdivs_, 1) * div_height_;
497 const float high_offset = y - ph + signal_margin + 0.5f;
498 const float low_offset = y + nh - signal_margin - 0.5f;
500 const deque< shared_ptr<pv::data::LogicSegment> > &segments =
501 base_->logic_data()->logic_segments();
503 if (segments.empty())
506 const shared_ptr<pv::data::LogicSegment> &segment =
509 double samplerate = segment->samplerate();
511 // Show sample rate as 1Hz when it is unknown
512 if (samplerate == 0.0)
515 const double pixels_offset = pp.pixels_offset();
516 const pv::util::Timestamp& start_time = segment->start_time();
517 const int64_t last_sample = segment->get_sample_count() - 1;
518 const double samples_per_pixel = samplerate * pp.scale();
519 const double pixels_per_sample = 1 / samples_per_pixel;
520 const pv::util::Timestamp start = samplerate * (pp.offset() - start_time);
521 const pv::util::Timestamp end = start + samples_per_pixel * pp.width();
523 const int64_t start_sample = min(max(floor(start).convert_to<int64_t>(),
524 (int64_t)0), last_sample);
525 const uint64_t end_sample = min(max(ceil(end).convert_to<int64_t>(),
526 (int64_t)0), last_sample);
528 segment->get_subsampled_edges(edges, start_sample, end_sample,
529 samples_per_pixel / LogicSignal::Oversampling, 0);
530 assert(edges.size() >= 2);
532 // Check whether we need to paint the sampling points
533 GlobalSettings settings;
534 const bool show_sampling_points =
535 settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool() &&
536 (samples_per_pixel < 0.25);
538 vector<QRectF> sampling_points;
539 float sampling_point_x = 0.0f;
540 int64_t sampling_point_sample = start_sample;
543 if (show_sampling_points) {
544 sampling_points.reserve(end_sample - start_sample + 1);
545 sampling_point_x = (edges.cbegin()->first / samples_per_pixel - pixels_offset) + pp.left();
549 const unsigned int edge_count = edges.size() - 2;
550 QLineF *const edge_lines = new QLineF[edge_count];
553 for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) {
554 const float x = ((*i).first / samples_per_pixel -
555 pixels_offset) + pp.left();
556 *line++ = QLineF(x, high_offset, x, low_offset);
558 if (show_sampling_points)
559 while (sampling_point_sample < (*i).first) {
560 const float y = (*i).second ? low_offset : high_offset;
561 sampling_points.emplace_back(
562 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
563 sampling_point_sample++;
564 sampling_point_x += pixels_per_sample;
568 // Calculate the sample points from the last edge to the end of the trace
569 if (show_sampling_points)
570 while ((uint64_t)sampling_point_sample <= end_sample) {
571 // Signal changed after the last edge, so the level is inverted
572 const float y = (edges.cend() - 1)->second ? high_offset : low_offset;
573 sampling_points.emplace_back(
574 QRectF(sampling_point_x - (w / 2), y - (w / 2), w, w));
575 sampling_point_sample++;
576 sampling_point_x += pixels_per_sample;
579 p.setPen(LogicSignal::EdgeColour);
580 p.drawLines(edge_lines, edge_count);
584 const unsigned int max_cap_line_count = edges.size();
585 QLineF *const cap_lines = new QLineF[max_cap_line_count];
587 p.setPen(LogicSignal::HighColour);
588 paint_logic_caps(p, cap_lines, edges, true, samples_per_pixel,
589 pixels_offset, pp.left(), high_offset);
590 p.setPen(LogicSignal::LowColour);
591 paint_logic_caps(p, cap_lines, edges, false, samples_per_pixel,
592 pixels_offset, pp.left(), low_offset);
596 // Paint the sampling points
597 if (show_sampling_points) {
598 p.setPen(SamplingPointColour);
599 p.drawRects(sampling_points.data(), sampling_points.size());
603 void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines,
604 vector< pair<int64_t, bool> > &edges, bool level,
605 double samples_per_pixel, double pixels_offset, float x_offset,
608 QLineF *line = lines;
610 for (auto i = edges.begin(); i != (edges.end() - 1); i++)
611 if ((*i).second == level) {
613 ((*i).first / samples_per_pixel -
614 pixels_offset) + x_offset, y_offset,
615 ((*(i+1)).first / samples_per_pixel -
616 pixels_offset) + x_offset, y_offset);
619 p.drawLines(lines, line - lines);
622 float AnalogSignal::get_resolution(int scale_index)
624 const float seq[] = {1.0f, 2.0f, 5.0f};
626 const int offset = numeric_limits<int>::max() / (2 * countof(seq));
627 const div_t d = div((int)(scale_index + countof(seq) * offset),
630 return powf(10.0f, d.quot - offset) * seq[d.rem];
633 void AnalogSignal::update_scale()
635 resolution_ = get_resolution(scale_index_);
636 scale_ = div_height_ / resolution_;
639 void AnalogSignal::update_conversion_widgets()
641 SignalBase::ConversionType conv_type = base_->get_conversion_type();
643 // Enable or disable widgets depending on conversion state
644 conv_threshold_cb_->setEnabled(conv_type != SignalBase::NoConversion);
645 display_type_cb_->setEnabled(conv_type != SignalBase::NoConversion);
647 conv_threshold_cb_->clear();
649 vector < pair<QString, int> > presets = base_->get_conversion_presets();
651 // Prevent the combo box from firing the "edit text changed" signal
652 // as that would involuntarily select the first entry
653 conv_threshold_cb_->blockSignals(true);
655 // Set available options depending on chosen conversion
656 for (pair<QString, int> preset : presets)
657 conv_threshold_cb_->addItem(preset.first, preset.second);
659 map < QString, QVariant > options = base_->get_conversion_options();
661 if (conv_type == SignalBase::A2LConversionByThreshold) {
662 const vector<double> thresholds = base_->get_conversion_thresholds(
663 SignalBase::A2LConversionByThreshold, true);
664 conv_threshold_cb_->addItem(
665 QString("%1V").arg(QString::number(thresholds[0], 'f', 1)), -1);
668 if (conv_type == SignalBase::A2LConversionBySchmittTrigger) {
669 const vector<double> thresholds = base_->get_conversion_thresholds(
670 SignalBase::A2LConversionBySchmittTrigger, true);
671 conv_threshold_cb_->addItem(QString("%1V/%2V").arg(
672 QString::number(thresholds[0], 'f', 1),
673 QString::number(thresholds[1], 'f', 1)), -1);
676 int preset_id = base_->get_current_conversion_preset();
677 conv_threshold_cb_->setCurrentIndex(
678 conv_threshold_cb_->findData(preset_id));
680 conv_threshold_cb_->blockSignals(false);
683 void AnalogSignal::perform_autoranging(bool keep_divs, bool force_update)
685 const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
686 base_->analog_data()->analog_segments();
688 if (segments.empty())
691 static double prev_min = 0, prev_max = 0;
692 double min = 0, max = 0;
694 for (shared_ptr<pv::data::AnalogSegment> segment : segments) {
695 pair<double, double> mm = segment->get_min_max();
696 min = std::min(min, mm.first);
697 max = std::max(max, mm.second);
700 if ((min == prev_min) && (max == prev_max) && !force_update)
706 // If we're allowed to alter the div assignment...
708 // Use all divs for the positive range if there are no negative values
709 if ((min == 0) && (neg_vdivs_ > 0)) {
710 pos_vdivs_ += neg_vdivs_;
714 // Split up the divs if there are negative values but no negative divs
715 if ((min < 0) && (neg_vdivs_ == 0)) {
716 neg_vdivs_ = pos_vdivs_ / 2;
717 pos_vdivs_ -= neg_vdivs_;
721 // If there is still no positive div when we need it, add one
722 // (this can happen when pos_vdivs==neg_vdivs==0)
723 if ((max > 0) && (pos_vdivs_ == 0)) {
725 owner_->extents_changed(false, true);
728 // If there is still no negative div when we need it, add one
729 // (this can happen when pos_vdivs was 0 or 1 when trying to split)
730 if ((min < 0) && (neg_vdivs_ == 0)) {
732 owner_->extents_changed(false, true);
735 double min_value_per_div;
736 if ((pos_vdivs_ > 0) && (neg_vdivs_ > 0))
737 min_value_per_div = std::max(max / pos_vdivs_, -min / neg_vdivs_);
738 else if (pos_vdivs_ > 0)
739 min_value_per_div = max / pos_vdivs_;
741 min_value_per_div = -min / neg_vdivs_;
743 // Find first scale value that is bigger than the value we need
744 for (int i = MinScaleIndex; i < MaxScaleIndex; i++)
745 if (get_resolution(i) > min_value_per_div) {
753 void AnalogSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
755 // Add the standard options
756 Signal::populate_popup_form(parent, form);
758 QFormLayout *const layout = new QFormLayout;
760 // Add div-related settings
761 pvdiv_sb_ = new QSpinBox(parent);
762 pvdiv_sb_->setRange(0, MaximumVDivs);
763 pvdiv_sb_->setValue(pos_vdivs_);
764 connect(pvdiv_sb_, SIGNAL(valueChanged(int)),
765 this, SLOT(on_pos_vdivs_changed(int)));
766 layout->addRow(tr("Number of pos vertical divs"), pvdiv_sb_);
768 nvdiv_sb_ = new QSpinBox(parent);
769 nvdiv_sb_->setRange(0, MaximumVDivs);
770 nvdiv_sb_->setValue(neg_vdivs_);
771 connect(nvdiv_sb_, SIGNAL(valueChanged(int)),
772 this, SLOT(on_neg_vdivs_changed(int)));
773 layout->addRow(tr("Number of neg vertical divs"), nvdiv_sb_);
775 div_height_sb_ = new QSpinBox(parent);
776 div_height_sb_->setRange(20, 1000);
777 div_height_sb_->setSingleStep(5);
778 div_height_sb_->setSuffix(tr(" pixels"));
779 div_height_sb_->setValue(div_height_);
780 connect(div_height_sb_, SIGNAL(valueChanged(int)),
781 this, SLOT(on_div_height_changed(int)));
782 layout->addRow(tr("Div height"), div_height_sb_);
784 // Add the vertical resolution
785 resolution_cb_ = new QComboBox(parent);
787 for (int i = MinScaleIndex; i < MaxScaleIndex; i++) {
788 const QString label = QString("%1").arg(get_resolution(i));
789 resolution_cb_->insertItem(0, label, QVariant(i));
792 int cur_idx = resolution_cb_->findData(QVariant(scale_index_));
793 resolution_cb_->setCurrentIndex(cur_idx);
795 connect(resolution_cb_, SIGNAL(currentIndexChanged(int)),
796 this, SLOT(on_resolution_changed(int)));
798 QGridLayout *const vdiv_layout = new QGridLayout;
799 QLabel *const vdiv_unit = new QLabel(tr("V/div"));
800 vdiv_layout->addWidget(resolution_cb_, 0, 0);
801 vdiv_layout->addWidget(vdiv_unit, 0, 1);
803 layout->addRow(tr("Vertical resolution"), vdiv_layout);
805 // Add the autoranging checkbox
806 QCheckBox* autoranging_cb = new QCheckBox();
807 autoranging_cb->setCheckState(autoranging_ ? Qt::Checked : Qt::Unchecked);
809 connect(autoranging_cb, SIGNAL(stateChanged(int)),
810 this, SLOT(on_autoranging_changed(int)));
812 layout->addRow(tr("Autoranging"), autoranging_cb);
814 // Add the conversion type dropdown
815 conversion_cb_ = new QComboBox();
817 conversion_cb_->addItem(tr("none"),
818 SignalBase::NoConversion);
819 conversion_cb_->addItem(tr("to logic via threshold"),
820 SignalBase::A2LConversionByThreshold);
821 conversion_cb_->addItem(tr("to logic via schmitt-trigger"),
822 SignalBase::A2LConversionBySchmittTrigger);
824 cur_idx = conversion_cb_->findData(QVariant(base_->get_conversion_type()));
825 conversion_cb_->setCurrentIndex(cur_idx);
827 layout->addRow(tr("Conversion"), conversion_cb_);
829 connect(conversion_cb_, SIGNAL(currentIndexChanged(int)),
830 this, SLOT(on_conversion_changed(int)));
832 // Add the conversion threshold settings
833 conv_threshold_cb_ = new QComboBox();
834 conv_threshold_cb_->setEditable(true);
836 layout->addRow(tr("Conversion threshold(s)"), conv_threshold_cb_);
838 connect(conv_threshold_cb_, SIGNAL(currentIndexChanged(int)),
839 this, SLOT(on_conv_threshold_changed(int)));
840 connect(conv_threshold_cb_, SIGNAL(editTextChanged(const QString)),
841 this, SLOT(on_conv_threshold_changed())); // index will be -1
843 // Add the display type dropdown
844 display_type_cb_ = new QComboBox();
846 display_type_cb_->addItem(tr("analog"), DisplayAnalog);
847 display_type_cb_->addItem(tr("converted"), DisplayConverted);
848 display_type_cb_->addItem(tr("analog+converted"), DisplayBoth);
850 cur_idx = display_type_cb_->findData(QVariant(display_type_));
851 display_type_cb_->setCurrentIndex(cur_idx);
853 layout->addRow(tr("Show traces for"), display_type_cb_);
855 connect(display_type_cb_, SIGNAL(currentIndexChanged(int)),
856 this, SLOT(on_display_type_changed(int)));
858 // Update the conversion widget contents and states
859 update_conversion_widgets();
861 form->addRow(layout);
864 void AnalogSignal::on_min_max_changed(float min, float max)
870 perform_autoranging(false, false);
873 void AnalogSignal::on_pos_vdivs_changed(int vdivs)
875 if (vdivs == pos_vdivs_)
880 // There has to be at least one div, positive or negative
881 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
884 pvdiv_sb_->setValue(pos_vdivs_);
888 perform_autoranging(true, true);
890 // It could be that a positive or negative div was added, so update
892 pvdiv_sb_->setValue(pos_vdivs_);
893 nvdiv_sb_->setValue(neg_vdivs_);
898 // Call order is important, otherwise the lazy event handler won't work
899 owner_->extents_changed(false, true);
900 owner_->row_item_appearance_changed(false, true);
904 void AnalogSignal::on_neg_vdivs_changed(int vdivs)
906 if (vdivs == neg_vdivs_)
911 // There has to be at least one div, positive or negative
912 if ((neg_vdivs_ == 0) && (pos_vdivs_ == 0)) {
915 pvdiv_sb_->setValue(pos_vdivs_);
919 perform_autoranging(true, true);
921 // It could be that a positive or negative div was added, so update
923 pvdiv_sb_->setValue(pos_vdivs_);
924 nvdiv_sb_->setValue(neg_vdivs_);
929 // Call order is important, otherwise the lazy event handler won't work
930 owner_->extents_changed(false, true);
931 owner_->row_item_appearance_changed(false, true);
935 void AnalogSignal::on_div_height_changed(int height)
937 div_height_ = height;
941 // Call order is important, otherwise the lazy event handler won't work
942 owner_->extents_changed(false, true);
943 owner_->row_item_appearance_changed(false, true);
947 void AnalogSignal::on_resolution_changed(int index)
949 scale_index_ = resolution_cb_->itemData(index).toInt();
953 owner_->row_item_appearance_changed(false, true);
956 void AnalogSignal::on_autoranging_changed(int state)
958 autoranging_ = (state == Qt::Checked);
961 perform_autoranging(false, true);
964 // Call order is important, otherwise the lazy event handler won't work
965 owner_->extents_changed(false, true);
966 owner_->row_item_appearance_changed(false, true);
970 void AnalogSignal::on_conversion_changed(int index)
972 SignalBase::ConversionType old_conv_type = base_->get_conversion_type();
974 SignalBase::ConversionType conv_type =
975 (SignalBase::ConversionType)(conversion_cb_->itemData(index).toInt());
977 if (conv_type != old_conv_type) {
978 base_->set_conversion_type(conv_type);
979 update_conversion_widgets();
982 owner_->row_item_appearance_changed(false, true);
986 void AnalogSignal::on_conv_threshold_changed(int index)
988 SignalBase::ConversionType conv_type = base_->get_conversion_type();
990 // Note: index is set to -1 if the text in the combo box matches none of
991 // the entries in the combo box
993 if ((index == -1) && (conv_threshold_cb_->currentText().length() == 0))
996 // The combo box entry with the custom value has user_data set to -1
997 const int user_data = conv_threshold_cb_->findText(
998 conv_threshold_cb_->currentText());
1000 const bool use_custom_thr = (index == -1) || (user_data == -1);
1002 if (conv_type == SignalBase::A2LConversionByThreshold && use_custom_thr) {
1003 // Not one of the preset values, try to parse the combo box text
1004 // Note: Regex loosely based on
1005 // https://txt2re.com/index-c++.php3?s=0.1V&1&-13
1006 QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1007 QString re2 = "([a-zA-Z]*)"; // SI unit
1008 QRegExp regex(re1 + re2);
1010 const QString text = conv_threshold_cb_->currentText();
1011 if (!regex.exactMatch(text))
1012 return; // String doesn't match the regex
1014 QStringList tokens = regex.capturedTexts();
1016 // For now, we simply assume that the unit is volt without modifiers
1017 const double thr = tokens.at(1).toDouble();
1019 // Only restart the conversion if the threshold was updated.
1020 // We're starting a delayed conversion because the user may still be
1021 // typing and the UI would lag if we kept on restarting it immediately
1022 if (base_->set_conversion_option("threshold_value", thr))
1023 base_->start_conversion(true);
1026 if (conv_type == SignalBase::A2LConversionBySchmittTrigger && use_custom_thr) {
1027 // Not one of the preset values, try to parse the combo box text
1028 // Note: Regex loosely based on
1029 // https://txt2re.com/index-c++.php3?s=0.1V/0.2V&2&14&-22&3&15
1030 QString re1 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1031 QString re2 = "([a-zA-Z]*)"; // SI unit
1032 QString re3 = "\\/"; // Forward slash, not captured
1033 QString re4 = "([+-]?\\d*[\\.,]?\\d*)"; // Float value
1034 QString re5 = "([a-zA-Z]*)"; // SI unit
1035 QRegExp regex(re1 + re2 + re3 + re4 + re5);
1037 const QString text = conv_threshold_cb_->currentText();
1038 if (!regex.exactMatch(text))
1039 return; // String doesn't match the regex
1041 QStringList tokens = regex.capturedTexts();
1043 // For now, we simply assume that the unit is volt without modifiers
1044 const double low_thr = tokens.at(1).toDouble();
1045 const double high_thr = tokens.at(3).toDouble();
1047 // Only restart the conversion if one of the options was updated.
1048 // We're starting a delayed conversion because the user may still be
1049 // typing and the UI would lag if we kept on restarting it immediately
1050 bool o1 = base_->set_conversion_option("threshold_value_low", low_thr);
1051 bool o2 = base_->set_conversion_option("threshold_value_high", high_thr);
1053 base_->start_conversion(true); // Start delayed conversion
1056 base_->set_conversion_preset((SignalBase::ConversionPreset)index);
1058 // Immediately start the conversion if we're not using custom values
1059 // (i.e. we're using one of the presets)
1060 if (!use_custom_thr)
1061 base_->start_conversion();
1064 void AnalogSignal::on_delayed_conversion_starter()
1066 base_->start_conversion();
1069 void AnalogSignal::on_display_type_changed(int index)
1071 display_type_ = (DisplayType)(display_type_cb_->itemData(index).toInt());
1074 owner_->row_item_appearance_changed(false, true);
1077 } // namespace trace
1078 } // namespace views