summaryrefslogtreecommitdiff
path: root/gauge.cpp
blob: 9e47606e69463de582c359f1b015412e64b769ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
}