]> sigrok.org Git - pulseview.git/blobdiff - pv/views/trace/analogsignal.cpp
View: Fixes related to multi-segment display
[pulseview.git] / pv / views / trace / analogsignal.cpp
index 2c5c82a9d1d18c5609d9d7965d2a10f35a6e2149..cb4c0325e3e83fd42fbd7335d09cf77faee4bef0 100644 (file)
@@ -28,6 +28,7 @@
 #include <QApplication>
 #include <QCheckBox>
 #include <QComboBox>
+#include <QDebug>
 #include <QFormLayout>
 #include <QGridLayout>
 #include <QLabel>
@@ -54,6 +55,7 @@ using std::max;
 using std::make_pair;
 using std::min;
 using std::numeric_limits;
+using std::out_of_range;
 using std::pair;
 using std::placeholders::_1;
 using std::shared_ptr;
@@ -225,8 +227,10 @@ void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp)
 
                // Draw high/neutral/low areas
                if (thresholds.size() == 2) {
-                       const double thr_lo = visual_y - thresholds[0] * scale_;
-                       const double thr_hi = visual_y - thresholds[1] * scale_;
+                       int thr_lo = visual_y - thresholds[0] * scale_;
+                       int thr_hi = visual_y - thresholds[1] * scale_;
+                       thr_lo = min(max(thr_lo, top), btm);
+                       thr_hi = min(max(thr_hi, top), btm);
 
                        p.fillRect(QRectF(pp.left(), top, pp.width(), thr_hi - top),
                                QBrush(ThresholdColorHi));
@@ -235,7 +239,8 @@ void AnalogSignal::paint_back(QPainter &p, ViewItemPaintParams &pp)
                        p.fillRect(QRectF(pp.left(), thr_lo, pp.width(), btm - thr_lo),
                                QBrush(ThresholdColorLo));
                } else {
-                       const double thr = visual_y - thresholds[0] * scale_;
+                       int thr = visual_y - thresholds[0] * scale_;
+                       thr = min(max(thr, top), btm);
 
                        p.fillRect(QRectF(pp.left(), top, pp.width(), thr - top),
                                QBrush(ThresholdColorHi));
@@ -263,14 +268,10 @@ void AnalogSignal::paint_mid(QPainter &p, ViewItemPaintParams &pp)
        if ((display_type_ == DisplayAnalog) || (display_type_ == DisplayBoth)) {
                paint_grid(p, y, pp.left(), pp.right());
 
-               const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
-                       base_->analog_data()->analog_segments();
-               if (segments.empty())
+               shared_ptr<pv::data::AnalogSegment> segment = get_analog_segment_to_paint();
+               if (!segment)
                        return;
 
-               const shared_ptr<pv::data::AnalogSegment> &segment =
-                       segments.front();
-
                const double pixels_offset = pp.pixels_offset();
                const double samplerate = max(1.0, segment->samplerate());
                const pv::util::Timestamp& start_time = segment->start_time();
@@ -529,15 +530,10 @@ void AnalogSignal::paint_logic_mid(QPainter &p, ViewItemPaintParams &pp)
        const float high_offset = y - ph + signal_margin + 0.5f;
        const float low_offset = y + nh - signal_margin - 0.5f;
 
-       const deque< shared_ptr<pv::data::LogicSegment> > &segments =
-               base_->logic_data()->logic_segments();
-
-       if (segments.empty())
+       shared_ptr<pv::data::LogicSegment> segment = get_logic_segment_to_paint();
+       if (!segment)
                return;
 
-       const shared_ptr<pv::data::LogicSegment> &segment =
-               segments.front();
-
        double samplerate = segment->samplerate();
 
        // Show sample rate as 1Hz when it is unknown
@@ -651,6 +647,52 @@ void AnalogSignal::paint_logic_caps(QPainter &p, QLineF *const lines,
        p.drawLines(lines, line - lines);
 }
 
+shared_ptr<pv::data::AnalogSegment> AnalogSignal::get_analog_segment_to_paint() const
+{
+       shared_ptr<pv::data::AnalogSegment> segment;
+
+       const deque< shared_ptr<pv::data::AnalogSegment> > &segments =
+               base_->analog_data()->analog_segments();
+
+       if (!segments.empty()) {
+               if (segment_display_mode_ == ShowLastSegmentOnly)
+                       segment = segments.back();
+
+               if (segment_display_mode_ == ShowSingleSegmentOnly) {
+                       try {
+                               segment = segments.at(current_segment_);
+                       } catch (out_of_range) {
+                               qDebug() << "Current analog segment out of range for signal" << base_->name() << ":" << current_segment_;
+                       }
+               }
+       }
+
+       return segment;
+}
+
+shared_ptr<pv::data::LogicSegment> AnalogSignal::get_logic_segment_to_paint() const
+{
+       shared_ptr<pv::data::LogicSegment> segment;
+
+       const deque< shared_ptr<pv::data::LogicSegment> > &segments =
+               base_->logic_data()->logic_segments();
+
+       if (!segments.empty()) {
+               if (segment_display_mode_ == ShowLastSegmentOnly)
+                       segment = segments.back();
+
+               if (segment_display_mode_ == ShowSingleSegmentOnly) {
+                       try {
+                               segment = segments.at(current_segment_);
+                       } catch (out_of_range) {
+                               qDebug() << "Current logic segment out of range for signal" << base_->name() << ":" << current_segment_;
+                       }
+               }
+       }
+
+       return segment;
+}
+
 float AnalogSignal::get_resolution(int scale_index)
 {
        const float seq[] = {1.0f, 2.0f, 5.0f};