Fixed GaugeArc
[mardrone] / mardrone / gauges / gaugearc.cpp
1 #include "gaugearc.h"
2
3 #include "gaugetape.h"
4 #include <stdio.h>
5 #include <math.h>
6 #include <qdebug.h>
7
8 GaugeArc::GaugeArc(QGraphicsItem *parent) :
9     Gauge(parent)
10 {
11     QPainterPath scale;
12     int xo=size().width()/2;
13     int yo=size().height()/2;
14
15 #define DR 3.141592653/180.0
16 #if 0
17     for (i = 0; i < 5; i++) {
18         fixo+=QLine(xo+arc0*cos(alpha*DR),yo-arc0*sin(alpha*DR),xo+arc1*cos(alpha*DR),yo-arc1*sin(alpha*DR));
19
20         scale.moveTo(xo+arc0*cos(alpha*DR),yo-arc0*sin(alpha*DR));
21         scale.lineTo(xo+arc1*cos(alpha*DR),yo-arc1*sin(alpha*DR));
22
23         fixo+=QLine(xo+arc0*cos(alpha*DR),yo-arc0*sin(alpha*DR),xo+arc1*cos(alpha*DR),yo-arc1*sin(alpha*DR));
24     };
25 #endif
26 }
27
28
29 void GaugeArc::paintArcScale(QPainter *painter,int width,int height)
30 {
31     float alpha;
32     int xo=(width/2);
33     int yo=(height/2);
34     int arcsize=xo-5;
35      painter->setTransform(QTransform().translate(scenePos().x()+width/2,scenePos().y()+height/2).scale(m_scale,m_scale));
36     QTransform savematrix= painter->transform ();
37     QPen pen(m_color);
38     pen.setWidth(m_lineWidth);
39
40     QPen nopen(Qt::NoPen);
41     QBrush cbrush(m_color);
42     QBrush nbrush(Qt::NoBrush);
43     QBrush backBrush(m_backColor);
44    // QBrush cbrush(m_color);
45     painter->setBrush(cbrush);
46     alpha=0;
47     int val=m_low;
48     painter->setFont(m_font);
49     QFontMetrics fm(m_font);
50     QPolygon needle;
51
52     //painter->setTransform(QTransform().translate(xo+scenePos().x()/m_scale, yo+scenePos().y()/m_scale),true);
53     painter->setBrush(backBrush);
54     painter->setPen(nopen);
55     painter->drawChord(QRect(-xo,-xo,2*xo,2*xo),0,360*16);
56     painter->setBrush(cbrush);
57     painter->setPen(pen);
58     painter->setTransform(QTransform().rotate(m_lowPosition),true);
59     painter->drawArc(-arcsize,-arcsize,2*arcsize,2*arcsize,(int(270)%360)*-16,(int(m_highPosition)%360)*-16);
60     while(alpha<=m_highPosition) {
61         QTransform rotT;
62         rotT.rotate(alpha+m_lowPosition);
63         QString label;
64         label=QString("%1").arg(val);
65         int labelWidth=fm.width(label);
66         int labelHeight=fm.height();
67         painter->setTransform(savematrix);
68
69         QPoint labelP=QPoint(0,-xo+15);
70         labelP=rotT.map(labelP);
71         QPoint lineInP=QPoint(0,-xo+2);
72         lineInP=rotT.map(lineInP);
73         QPoint lineOutP=QPoint(0,-xo+12);
74         lineOutP=rotT.map(lineOutP);
75         QPoint labelPo=QPoint(labelWidth*((-1-sin(3.1415926/180*alpha))/2),labelHeight*((1+cos(3.1415926/180*alpha))/2));
76         QPoint labelPd=labelP+labelPo;
77    //      qDebug() << alpha << labelP << labelPd << labelPo << labelWidth << labelHeight;
78         painter->drawText(labelPd,label);
79         painter->drawLine(lineInP,lineOutP);
80         alpha+=m_tickSpacing?m_tickSpacing:10;
81         val+=m_tickIncrement;
82     }
83     needle+=QPoint(-5,0);
84     needle+=QPoint(0,-xo);
85     needle+=QPoint(5,0);
86     needle+=QPoint(-5,0);
87     painter->setTransform(QTransform().rotate(m_lowPosition+(m_value*1.0*m_tickSpacing/m_tickIncrement)),true);
88     painter->drawPolygon(needle);
89 }
90
91 void GaugeArc::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
92                            QWidget *widget)
93 {
94         paintArcScale(painter,size().width(),size().height());
95     //    paintNeedle(painter,val,size().width(),size().height());
96 };
97