diff options
author | Joshua Drake <joshua.drake@ditchwitch.com> | 2023-06-20 10:15:47 -0500 |
---|---|---|
committer | Joshua Drake <joshua.drake@ditchwitch.com> | 2023-06-20 10:15:47 -0500 |
commit | 58d6674897c90a69aea7bc370e429fbc9d73bcca (patch) | |
tree | ab40f2c477c3af60435f54f4646fd24cee78ac04 |
-rw-r--r-- | gauge.cpp | 85 | ||||
-rw-r--r-- | gauge.h | 51 |
2 files changed, 136 insertions, 0 deletions
diff --git a/gauge.cpp b/gauge.cpp new file mode 100644 index 0000000..9e47606 --- /dev/null +++ b/gauge.cpp @@ -0,0 +1,85 @@ +#include "gauge.h" + +Gauge::Gauge(QWidget *parent) : QWidget (parent) , + m_gauge_percentage(0), + m_needle_horizontal_offset(0), + m_needle_origin(0,0), + m_start_angle(180), + m_end_angle(0) +{ + setMinimumHeight(32); + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); +} + +void Gauge::setOrigin(int x,int y) //Set a real origin based on image if available, otherwise store desired value for later use. +{ + if(!m_needle.isNull()) + { + m_draw_point = QPoint(x-m_needle_horizontal_offset-(m_needle.width()/2),y-m_needle.height()); + m_needle_origin = QPoint(x,y); + update(); + } + else + { + m_needle_origin = QPoint(x,y); + } +} +void Gauge::setOrigin(QPoint o) //Set a real origin based on image if available, otherwise store desired value for later use. +{ + if(!m_needle.isNull()) + { + m_draw_point = QPoint(o.x()-m_needle_horizontal_offset-(m_needle.width()/2),o.y()-m_needle.height()); + m_needle_origin = o; + update(); + } + else + { + m_needle_origin = o; + } +} +//void Gauge::obtainLength() +//{ +// if(m_needle.isNull())return; + +// if(m_needle.height() > m_needle.width()) +// m_needle_length = m_needle.height(); +// else +// m_needle_length = m_needle.width(); +// update(); +//} + +void Gauge::setNeedleImage(QString path) +{ + m_needle.load(path); + //obtainLength(); + update(); +} + +void Gauge::setEndAngle(qreal a) +{ + if(a > 0) m_end_angle = a - 360.0; + else m_end_angle = a; + update(); +} + +void Gauge::setStartAngle(qreal a) +{ + if(a < 0) m_start_angle = a + 360.0; + else m_start_angle = a; + update(); +} + +void Gauge::paintEvent(QPaintEvent *) +{ + if(m_needle.isNull())return; + QPainter p(this); + QTransform tran; + double angle = (DEFUALT_IMAGE_ANGLE - m_start_angle)-((m_end_angle-m_start_angle)/(100.0)*m_gauge_percentage); + + tran.translate(m_needle_origin.x(),m_needle_origin.y()); + tran.rotate(angle); + tran.translate(-m_needle_origin.x(),-m_needle_origin.y()); + + p.setTransform(tran); + p.drawPixmap(m_draw_point,m_needle); +} @@ -0,0 +1,51 @@ +/* + * JD 12/9/2022 +*/ +#ifndef GAUGE_H +#define GAUGE_H + +#include <QWidget> +#include <QPainter> +#include <QString> +#include <QPixmap> +#include <QTransform> +#include <QPixmap> +#include <QFontMetrics> + +#define DEFUALT_IMAGE_ANGLE 90 + +class Gauge : public QWidget +{ + Q_OBJECT +public: + explicit Gauge(QWidget *parent = nullptr); + void paintEvent(QPaintEvent *); + +signals: + +public slots: + void setNeedleImage(QPixmap p) {m_needle = p; update();} + void setNeedleImage(QString path); + //void obtainLength(); + void setOrigin(int x,int y); + void setOrigin(QPoint o); + void setHorizontalOffset(int o) {m_needle_horizontal_offset = o; update();} + void setStartAngle(qreal a); + void setEndAngle(qreal a); + void setGaugePercentage(qreal p) {m_gauge_percentage = p; update();} + + +protected: + +private: + //double m_needle_length; + double m_gauge_percentage; + int m_needle_horizontal_offset; //Use if needle isn't centered within the image. + QPoint m_needle_origin; + QPoint m_draw_point; + QPixmap m_needle; + qreal m_start_angle; + qreal m_end_angle; +}; + +#endif // GAUGE_H |