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 /gauge.cpp |
Diffstat (limited to 'gauge.cpp')
-rw-r--r-- | gauge.cpp | 85 |
1 files changed, 85 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); +} |