Introduce pv::data::SignalBase
[pulseview.git] / pv / view / analogsignal.cpp
index 6703ce9ed8b2c1b4e40cab8b905231b284cf450e..8ccad390eb4730ee8436d34007f133833579d329 100644 (file)
 #include <QGridLayout>
 #include <QLabel>
 #include <QSpinBox>
+#include <QString>
 
 #include "analogsignal.hpp"
 #include "pv/data/analog.hpp"
 #include "pv/data/analogsegment.hpp"
+#include "pv/data/signalbase.hpp"
 #include "pv/view/view.hpp"
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
@@ -45,8 +47,6 @@ using std::min;
 using std::shared_ptr;
 using std::deque;
 
-using sigrok::Channel;
-
 namespace pv {
 namespace view {
 
@@ -57,8 +57,8 @@ const QColor AnalogSignal::SignalColours[4] = {
        QColor(0x4E, 0x9A, 0x06)        // Green
 };
 
-const QColor AnalogSignal::GridMajorColor = QColor(0xB0, 0xB0, 0xB0);
-const QColor AnalogSignal::GridMinorColor = QColor(0xD0, 0xD0, 0xD0);
+const QColor AnalogSignal::GridMajorColor = QColor(0, 0, 0, 40*256/100);
+const QColor AnalogSignal::GridMinorColor = QColor(0, 0, 0, 20*256/100);
 
 const float AnalogSignal::EnvelopeThreshold = 256.0f;
 
@@ -66,9 +66,12 @@ const int AnalogSignal::MaximumVDivs = 10;
 const int AnalogSignal::MinScaleIndex = -6;
 const int AnalogSignal::MaxScaleIndex = 7;
 
+const int AnalogSignal::InfoTextMarginRight = 20;
+const int AnalogSignal::InfoTextMarginBottom = 5;
+
 AnalogSignal::AnalogSignal(
        pv::Session &session,
-       shared_ptr<Channel> channel,
+       shared_ptr<data::SignalBase> channel,
        shared_ptr<data::Analog> data) :
        Signal(session, channel),
        data_(data),
@@ -78,7 +81,7 @@ AnalogSignal::AnalogSignal(
        vdivs_(1),
        resolution_(0)
 {
-       set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
+       channel_->set_colour(SignalColours[channel_->index() % countof(SignalColours)]);
        update_scale();
 }
 
@@ -173,18 +176,41 @@ void AnalogSignal::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
                        pixels_offset, samples_per_pixel);
 }
 
+void AnalogSignal::paint_fore(QPainter &p, const ViewItemPaintParams &pp)
+{
+       if (!enabled())
+               return;
+
+       const int y = get_visual_y();
+
+       // Show the info section on the right side of the trace
+       const QString infotext = QString("%1 V/div").arg(resolution_);
+
+       p.setPen(channel_->colour());
+       p.setFont(QApplication::font());
+
+       const QRectF bounding_rect = QRectF(pp.left(),
+                       y + v_extents().first,
+                       pp.width() - InfoTextMarginRight,
+                       v_extents().second - v_extents().first - InfoTextMarginBottom);
+
+       p.drawText(bounding_rect, Qt::AlignRight | Qt::AlignBottom, infotext);
+}
+
 void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
 {
-       p.setPen(QPen(GridMajorColor, 0.5, Qt::DashLine));
+       p.setRenderHint(QPainter::Antialiasing, false);
+
+       p.setPen(QPen(GridMajorColor, 1, Qt::DashLine));
        for (int i = 1; i <= vdivs_; i++) {
-               const int dy = i * div_height_;
+               const float dy = i * div_height_;
                p.drawLine(QLineF(left, y - dy, right, y - dy));
                p.drawLine(QLineF(left, y + dy, right, y + dy));
        }
 
-       p.setPen(QPen(GridMinorColor, 0.5, Qt::DashLine));
+       p.setPen(QPen(GridMinorColor, 1, Qt::DashLine));
        for (int i = 0; i < vdivs_; i++) {
-               const int dy = i * div_height_;
+               const float dy = i * div_height_;
                const float dy25 = dy + (0.25 * div_height_);
                const float dy50 = dy + (0.50 * div_height_);
                const float dy75 = dy + (0.75 * div_height_);
@@ -195,6 +221,8 @@ void AnalogSignal::paint_grid(QPainter &p, int y, int left, int right)
                p.drawLine(QLineF(left, y - dy75, right, y - dy75));
                p.drawLine(QLineF(left, y + dy75, right, y + dy75));
        }
+
+       p.setRenderHint(QPainter::Antialiasing, true);
 }
 
 void AnalogSignal::paint_trace(QPainter &p,
@@ -207,7 +235,7 @@ void AnalogSignal::paint_trace(QPainter &p,
        const float *const samples = segment->get_samples(start, end);
        assert(samples);
 
-       p.setPen(colour_);
+       p.setPen(channel_->colour());
 
        QPointF *points = new QPointF[sample_count];
        QPointF *point = points;
@@ -239,7 +267,7 @@ void AnalogSignal::paint_envelope(QPainter &p,
                return;
 
        p.setPen(QPen(Qt::NoPen));
-       p.setBrush(colour_);
+       p.setBrush(channel_->colour());
 
        QRectF *const rects = new QRectF[e.length];
        QRectF *rect = rects;
@@ -331,8 +359,11 @@ void AnalogSignal::on_vdivs_changed(int vdivs)
 {
        vdivs_ = vdivs;
 
-       if (owner_)
+       if (owner_) {
+               // Call order is important, otherwise the lazy event handler won't work
                owner_->extents_changed(false, true);
+               owner_->row_item_appearance_changed(false, true);
+       }
 }
 
 void AnalogSignal::on_resolution_changed(int index)