From: Soeren Apel Date: Thu, 7 Apr 2016 20:03:05 +0000 (+0200) Subject: AnalogSignal: Use correct scaling factor for the grid to work X-Git-Tag: pulseview-0.4.0~317 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=834a4f1bb78a4098c59954500698bdd13060c219;p=pulseview.git AnalogSignal: Use correct scaling factor for the grid to work --- diff --git a/pv/view/analogsignal.cpp b/pv/view/analogsignal.cpp index b340de67..b92792d7 100644 --- a/pv/view/analogsignal.cpp +++ b/pv/view/analogsignal.cpp @@ -63,12 +63,14 @@ AnalogSignal::AnalogSignal( shared_ptr data) : Signal(session, channel), data_(data), - scale_index_(0), + scale_index_(4), // 20 per div scale_index_drag_offset_(0), div_height_(3 * QFontMetrics(QApplication::font()).height()), - vdivs_(1) + vdivs_(1), + resolution_(0) { set_colour(SignalColours[channel_->index() % countof(SignalColours)]); + update_scale(); } shared_ptr AnalogSignal::data() const @@ -101,11 +103,14 @@ void AnalogSignal::scale_handle_dragged(int offset) scale_index_ = scale_index_drag_offset_ - (offset + h / 2) / (h / 4); + + update_scale(); } void AnalogSignal::scale_handle_drag_release() { scale_index_drag_offset_ = scale_index_; + update_scale(); } void AnalogSignal::paint_back(QPainter &p, const ViewItemPaintParams &pp) @@ -188,7 +193,6 @@ void AnalogSignal::paint_trace(QPainter &p, int y, int left, const int64_t start, const int64_t end, const double pixels_offset, const double samples_per_pixel) { - const float scale = this->scale(); const int64_t sample_count = end - start; const float *const samples = segment->get_samples(start, end); @@ -203,7 +207,7 @@ void AnalogSignal::paint_trace(QPainter &p, const float x = (sample / samples_per_pixel - pixels_offset) + left; *point++ = QPointF(x, - y - samples[sample - start] * scale); + y - samples[sample - start] * scale_); } p.drawPolyline(points, point - points); @@ -219,8 +223,6 @@ void AnalogSignal::paint_envelope(QPainter &p, { using pv::data::AnalogSegment; - const float scale = this->scale(); - AnalogSegment::EnvelopeSection e; segment->get_envelope_section(e, start, end, samples_per_pixel); @@ -241,8 +243,8 @@ void AnalogSignal::paint_envelope(QPainter &p, // We overlap this sample with the next so that vertical // gaps do not appear during steep rising or falling edges - const float b = y - max(s->max, (s+1)->min) * scale; - const float t = y - min(s->min, (s+1)->max) * scale; + const float b = y - max(s->max, (s+1)->min) * scale_; + const float t = y - min(s->min, (s+1)->max) * scale_; float h = b - t; if (h >= 0.0f && h <= 1.0f) @@ -259,14 +261,17 @@ void AnalogSignal::paint_envelope(QPainter &p, delete[] e.samples; } -float AnalogSignal::scale() const +void AnalogSignal::update_scale() { const float seq[] = {1.0f, 2.0f, 5.0f}; + const int offset = std::numeric_limits::max() / (2 * countof(seq)); const std::div_t d = std::div( (int)(scale_index_ + countof(seq) * offset), countof(seq)); - return powf(10.0f, d.quot - offset) * seq[d.rem]; + + resolution_ = powf(10.0f, d.quot - offset) * seq[d.rem]; + scale_ = div_height_ / resolution_; } } // namespace view diff --git a/pv/view/analogsignal.hpp b/pv/view/analogsignal.hpp index dd2b31dc..7fdddf2d 100644 --- a/pv/view/analogsignal.hpp +++ b/pv/view/analogsignal.hpp @@ -103,18 +103,20 @@ private: const double pixels_offset, const double samples_per_pixel); /** - * Computes the scale factor from the scale index. + * Computes the scale factor from the scale index and vdiv settings. */ - float scale() const; + void update_scale(); private: std::shared_ptr data_; + float scale_; int scale_index_; int scale_index_drag_offset_; int div_height_; int vdivs_; // divs per positive/negative side + float resolution_; // e.g. 10 for 10 V/div }; } // namespace view