]> sigrok.org Git - pulseview.git/commitdiff
Initial implementation of labels
authorJoel Holdsworth <redacted>
Sat, 23 Jun 2012 10:32:07 +0000 (11:32 +0100)
committerJoel Holdsworth <redacted>
Mon, 3 Sep 2012 12:59:05 +0000 (13:59 +0100)
logicsignal.cpp
logicsignal.h
signal.cpp
signal.h
sigview.cpp
sigview.h

index 67d72bef8b7c741190cad954f84fddd9f42103ed..486c002d7a1ed1179e9979377a4195d15f816c76 100644 (file)
@@ -24,6 +24,8 @@
 
 #include <math.h>
 
+#include "extdef.h"
+
 #include "logicdata.h"
 #include "logicdatasnapshot.h"
 #include "logicsignal.h"
@@ -39,6 +41,19 @@ const float LogicSignal::EdgeColour[3] =     {0.50f, 0.50f, 0.50f};
 const float LogicSignal::HighColour[3] =       {0.00f, 0.75f, 0.00f};
 const float LogicSignal::LowColour[3] =        {0.75f, 0.00f, 0.00f};
 
+const QColor LogicSignal::LogicSignalColours[10] = {
+       QColor(0x16, 0x19, 0x1A),       // Black
+       QColor(0x8F, 0x52, 0x02),       // Brown
+       QColor(0xCC, 0x00, 0x00),       // Red
+       QColor(0xF5, 0x79, 0x00),       // Orange
+       QColor(0xED, 0xD4, 0x00),       // Yellow
+       QColor(0x73, 0xD2, 0x16),       // Green
+       QColor(0x34, 0x65, 0xA4),       // Blue
+       QColor(0x75, 0x50, 0x7B),       // Violet
+       QColor(0x88, 0x8A, 0x85),       // Grey
+       QColor(0xEE, 0xEE, 0xEC),       // White
+};
+
 LogicSignal::LogicSignal(QString name, shared_ptr<LogicData> data,
        int probe_index) :
        Signal(name),
@@ -166,3 +181,13 @@ void LogicSignal::paint_lines(Point2F *points, int count)
 
        glDeleteBuffers(1, &vbo_id);
 }
+
+QColor LogicSignal::get_colour() const
+{
+       return LogicSignalColours[_probe_index % countof(LogicSignalColours)];
+}
+
+int LogicSignal::get_nominal_offset(const QRect &rect) const
+{
+       return rect.bottom() - Margin;
+}
index 60250b1e4bcac7669d00d0c9dbeb55bc56f5bbe0..8677afe3c03e30e9c5851ca952f8a91296593a7e 100644 (file)
@@ -39,6 +39,8 @@ private:
        static const float HighColour[3];
        static const float LowColour[3];
 
+       static const QColor LogicSignalColours[10];
+
 public:
        LogicSignal(QString name,
                boost::shared_ptr<LogicData> data,
@@ -64,6 +66,17 @@ private:
 
        static void paint_lines(Point2F *points, int count);
 
+       /**
+        * Get the colour of the logic signal
+        */
+       QColor get_colour() const;
+
+       /**
+        * When painting into the rectangle, calculate the y
+        * offset of the zero point.
+        **/
+       int get_nominal_offset(const QRect &rect) const;
+
 private:
        int _probe_index;
        boost::shared_ptr<LogicData> _data;
index d35be6bfed5dfecae1382525f2885732ec9ff206..24d1e5236e2002db28c1352c6328269997e6af76 100644 (file)
@@ -20,7 +20,9 @@
 
 #include "signal.h"
 
-#include <memory.h>
+#include "extdef.h"
+
+const QSizeF Signal::LabelPadding(4, 0);
 
 Signal::Signal(QString name) :
        _name(name)
@@ -31,3 +33,41 @@ QString Signal::get_name() const
 {
        return _name;
 }
+
+void Signal::paint_label(QPainter &p, const QRect &rect)
+{
+       p.setBrush(get_colour());
+
+       const QString text(_name);
+       const QColor colour = get_colour();
+
+       const QSizeF text_size = p.boundingRect(
+               QRectF(0, 0, rect.width(), 0), 0, text).size();
+
+       const float nominal_offset = get_nominal_offset(rect);
+       const QSizeF label_size(
+               text_size.width() + LabelPadding.width() * 2,
+               text_size.height() + LabelPadding.height() * 2);
+       const float label_arrow_length = label_size.height() / 2;
+       const QRectF label_rect(
+               rect.right() - label_arrow_length - label_size.width(),
+               nominal_offset - label_size.height() / 2,
+               label_size.width(), label_size.height());
+
+       // Paint the label
+       const QPointF points[] = {
+               label_rect.topLeft(),
+               label_rect.topRight(),
+               QPointF(rect.right(), nominal_offset),
+               label_rect.bottomRight(),
+               label_rect.bottomLeft()
+       };
+
+       p.setPen(Qt::black);
+       p.setBrush(colour);
+       p.drawPolygon(points, countof(points));
+
+       // Paint the text
+       p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
+       p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
+}
index ac23a3fa214a81255855934d13cb2f64430e9b07..edab476deb7d31c22f0b16929c2221e82e05cb1a 100644 (file)
--- a/signal.h
+++ b/signal.h
@@ -21,6 +21,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include <QGLWidget>
+#include <QPainter>
 #include <QRect>
 #include <QString>
 
@@ -30,6 +31,9 @@ class SignalData;
 
 class Signal
 {
+private:
+       static const QSizeF LabelPadding;
+
 protected:
        Signal(QString name);
 
@@ -47,6 +51,26 @@ public:
        virtual void paint(QGLWidget &widget, const QRect &rect,
                double scale, double offset) = 0;
 
+       /**
+        * Paints the signal label into a QGLWidget.
+        * @param p the QPainter to paint into.
+        * @param rect the rectangular area to draw the label into.
+        */
+       virtual void paint_label(QPainter &p, const QRect &rect);
+
+protected:
+
+       /**
+        * Get the colour of the logic signal
+        */
+       virtual QColor get_colour() const = 0;
+
+       /**
+        * When painting into the rectangle, calculate the y
+        * offset of the zero point.
+        **/
+       virtual int get_nominal_offset(const QRect &rect) const = 0;
+
 protected:
        QString _name;
 };
index 4097ae5c25fe1f316b4cd9359dbe44b79bd8a8cf..cf0983d90ba2b0cefbb798f1edcc9dda697f4f39 100644 (file)
@@ -31,6 +31,7 @@ using namespace boost;
 using namespace std;
 
 const int SigView::SignalHeight = 50;
+const int SigView::LabelMarginWidth = 70;
 
 SigView::SigView(SigSession &session, QWidget *parent) :
        QGLWidget(parent),
@@ -42,41 +43,70 @@ SigView::SigView(SigSession &session, QWidget *parent) :
                this, SLOT(dataUpdated()));
 
        setMouseTracking(true);
+       setAutoFillBackground(false);
 }
 
 void SigView::initializeGL()
 {
-       glDisable(GL_TEXTURE_2D);
-       glDisable(GL_DEPTH_TEST);
-       glDisable(GL_COLOR_MATERIAL);
-       glEnable(GL_BLEND);
-       glEnable(GL_POLYGON_SMOOTH);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glClearColor(1.0, 1.0, 1.0, 0);
 }
 
 void SigView::resizeGL(int width, int height)
 {
-       glViewport(0, 0, (GLint)width, (GLint)height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, width, height, 0, -1, 1);
-       glMatrixMode(GL_MODELVIEW);
+       setupViewport(width, height);
 }
 
-void SigView::paintGL()
+void SigView::paintEvent(QPaintEvent *event)
 {
-       glClear(GL_COLOR_BUFFER_BIT);
+       int offset;
 
-       QRect rect(0, 0, width(), SignalHeight);
        const vector< shared_ptr<Signal> > &sigs =
                _session.get_signals();
+
+       // Prepare for OpenGL rendering
+       makeCurrent();
+       glMatrixMode(GL_MODELVIEW);
+       glPushMatrix();
+
+       setupViewport(width(), height());
+
+       qglClearColor(Qt::white);
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       // Plot the signal
+       offset = 0;
+       BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
+       {
+               assert(s);
+
+               const QRect signal_rect(LabelMarginWidth, offset,
+                       width() - LabelMarginWidth, SignalHeight);
+
+               s->paint(*this, signal_rect, _scale, _offset);
+
+               offset += SignalHeight;
+       }
+
+       // Prepare for QPainter rendering
+       glMatrixMode(GL_MODELVIEW);
+       glPopMatrix();
+
+       QPainter painter(this);
+       painter.setRenderHint(QPainter::Antialiasing);
+
+       // Paint the label
+       offset = 0;
        BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
        {
                assert(s);
-               s->paint(*this, rect, _scale, _offset);
-               rect.translate(0, SignalHeight);
+
+               const QRect label_rect(0, offset,
+                       LabelMarginWidth, SignalHeight);
+               s->paint_label(painter, label_rect);
+
+               offset += SignalHeight;
        }
+
+       painter.end();
 }
 
 void SigView::dataUpdated()
@@ -113,6 +143,14 @@ void SigView::mouseReleaseEvent(QMouseEvent *event)
 
        _offset = cursor_offset - _scale * (double)event->x();
 
-       updateGL();
+       update();
 }
 
+void SigView::setupViewport(int width, int height)
+{
+       glViewport(0, 0, (GLint)width, (GLint)height);
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, width, height, 0, -1, 1);
+       glMatrixMode(GL_MODELVIEW);
+}
index c4ddc15b527cbd294b1d6d0f413f5cabd684e521..e2d9543a452d5a7d5cfce2b62ebc542275b41f14 100644 (file)
--- a/sigview.h
+++ b/sigview.h
@@ -24,6 +24,7 @@
 #include <QtOpenGL/QGLWidget>
 #include <QTimer>
 
+class QPaintEvent;
 class SigSession;
 
 class SigView : public QGLWidget
@@ -32,6 +33,7 @@ class SigView : public QGLWidget
 
 private:
        static const int SignalHeight;
+       static const int LabelMarginWidth;
 
 public:
        explicit SigView(SigSession &session, QWidget *parent = 0);
@@ -42,13 +44,16 @@ protected:
 
        void resizeGL(int width, int height);
 
-       void paintGL();
+       void paintEvent(QPaintEvent *event);
 
 private:
        void mouseMoveEvent(QMouseEvent *event);
        void mousePressEvent(QMouseEvent *event);
        void mouseReleaseEvent(QMouseEvent *event);
 
+private:
+       void setupViewport(int width, int height);
+
 private slots:
        void dataUpdated();