From 58d6674897c90a69aea7bc370e429fbc9d73bcca Mon Sep 17 00:00:00 2001 From: Joshua Drake Date: Tue, 20 Jun 2023 10:15:47 -0500 Subject: Initial Commit --- gauge.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 gauge.cpp (limited to 'gauge.cpp') 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); +} -- cgit v1.2.3