UIname added
[mardrone] / mardrone / gauges / gaugelabel.cpp
1 #include "gaugelabel.h"
2 #include <QDebug>
3 #include <math.h>
4
5 GaugeLabel::GaugeLabel(QGraphicsItem *parent) :
6     Gauge(parent)
7 {
8     m_textSize=20;
9 }
10
11 void GaugeLabel::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
12                        QWidget *widget)
13 {
14     drawNumLabel(painter);
15 };
16 //////////////////////////////////////////////////////////////////////////
17 void GaugeLabel::drawNumLabel(QPainter *painter)
18 {
19   int ld,ldo,ud,fh;
20   int lDigits,lWidth;
21   int vsplit,hsplit,uw,base;
22   char text[50]="";
23   QString  utext;
24   int h=size().height();
25   int w=size().width();
26   int val=m_value;
27   painter->save();
28   painter->setTransform(QTransform().translate(scenePos().x(),scenePos().y()).scale(m_scale,m_scale));
29  // qDebug() <<"drawnumlabel=" << m_name << " x=" << scenePos().x() << " y=" << scenePos().y() <<  " h=" << h << " w=" << w;
30   QFontMetrics fm(m_font);
31   painter->setFont(m_font);
32   if(m_style&SCROLL) {
33       lDigits=m_tickMinor;
34       int lIncr=(lDigits&1?10:1)*(lDigits&2?100:1)*(lDigits&4?10000:1);
35       lWidth=fm.width(QString().number(lIncr-1));
36
37
38     ld=abs(val%(lIncr));
39     ud=val/(lIncr);
40     ldo=lDigits>1?ld%10:0;
41
42
43
44     utext=QString("%1").arg(ud,3);
45     hsplit=lWidth+10;
46
47     uw=fm.width(utext);
48     fh=fm.ascent();
49     float lo=(fmod(m_value,lIncr/10)/lIncr)*fh*10;
50     vsplit=(h-fm.ascent()-8)/2;
51     base=fm.ascent()+vsplit;
52     drawFrame(painter, m_style,hsplit,vsplit);
53 //    qDebug("val=%f ld=%d ldo=%d lo=%f h=%d lDigits=%d lIncr=%d\n",m_value,ld,ldo,lo,h,lDigits,lIncr);
54     if((ld*10/lIncr)==9) {
55         ud=(val+lIncr/2)/(lIncr);
56         painter->drawText(w-hsplit- uw, base+lo,   QString("%1").arg(abs(ud-1),3));
57         painter->drawText(w-hsplit- uw, base-fh+lo,QString("%1").arg(abs(ud  ),3) );
58     } else painter->drawText(w-hsplit- uw, base,utext );
59     painter->drawText(w-hsplit, base +2*fh+lo, QString("%1").arg(abs(ld-ldo-2*lIncr/10+lIncr)%lIncr,lDigits,10,QLatin1Char('0')));
60     painter->drawText(w-hsplit, base +fh  +lo, QString("%1").arg(abs(ld-ldo-  lIncr/10+lIncr)%lIncr,lDigits,10,QLatin1Char('0')));
61     painter->drawText(w-hsplit, base      +lo, QString("%1").arg(abs(ld-ldo),lDigits,10,QLatin1Char('0')));
62     painter->drawText(w-hsplit, base -fh  +lo ,QString("%1").arg(abs((ld-ldo+  lIncr/10)%lIncr),lDigits,10,QLatin1Char('0')));
63     painter->drawText(w-hsplit, base -2*fh+lo ,QString("%1").arg(abs((ld-ldo+2*lIncr/10)%lIncr),lDigits,10,QLatin1Char('0')));
64
65
66   } else {
67     sprintf(text, "%3d", val);
68     painter->drawText(7,base + fh , text);
69   //  drawLabel(painter, text,m_style);
70   }
71   painter->restore();  // Restore and remove clicping rectabgle
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 void GaugeLabel::drawFrame(QPainter *painter,int style,int hsplit,int vsplit)
76 {
77   int textlen = 0, i = 0;
78   int xplace=0;
79   int yplace=0;
80   int height=size().height();
81   int width=size().width();
82
83  // qDebug() << "height=" << height << " width=" << width << " hsplit=" << hsplit << " vsplit=" << vsplit;
84
85   QVector<QPoint> nboxv;
86
87   QPen pen(m_color);
88   painter->setPen(pen);
89
90
91   if (style&7 == LEFT) xplace+=5;
92
93   nboxv.push_back(QPoint(xplace,yplace+vsplit));
94
95
96
97   if (style&7 == UP) {
98     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace  ));
99     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace-5));
100     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace  ));
101   }
102
103   if (style&SCROLL) {
104     nboxv.push_back(QPoint(xplace+width-hsplit,yplace+vsplit));
105     nboxv.push_back(QPoint(xplace+width-hsplit,yplace));
106     nboxv.push_back(QPoint(xplace+width,yplace));
107   } else
108     nboxv.push_back(QPoint(xplace+width,yplace  ));
109
110
111   if (style&7== RIGHT) {
112
113     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)-5));
114     nboxv.push_back(QPoint(xplace+width+5,yplace+(height/2)  ));
115     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)+5));
116   }
117
118   if (style&SCROLL) {
119     nboxv.push_back(QPoint(xplace+width,yplace+height));
120     nboxv.push_back(QPoint(xplace+width-hsplit,yplace+height));
121     nboxv.push_back(QPoint(xplace+width-hsplit,yplace+height-vsplit));
122   } else
123     nboxv.push_back(QPoint(xplace,yplace+height-vsplit));
124
125   if (style&7== DOWN) {
126     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace+height));
127     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace+height+5));
128     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace+height));
129   }
130
131   nboxv.push_back(QPoint(xplace,yplace+height-vsplit));
132
133   if (style&7 == LEFT) {
134     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)+5));
135     nboxv.push_back(QPoint(xplace-5,yplace+(height/2)  ));
136     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)-5));
137   }
138
139   nboxv.push_back(QPoint(xplace,yplace+vsplit));
140
141   //  pdrawable->draw_rectangle(pwidget->get_style()->get_black_gc(),
142   //                        true, xplace, yplace, width, height);
143
144   if (style&SCROLL) {
145
146         painter->setClipRect(QRect(xplace+width-hsplit,yplace, width+10, height+1));
147         painter->setClipRect(QRect(xplace-5,yplace+vsplit, width+10, height-2*vsplit+1),Qt::UniteClip);
148     //  painter->drawRect(QRect(xplace-5,yplace, width+10, height));
149    }
150
151   //  QFont tapeFont("Helvetica [Cronyx]", 18);
152   //  painter->setFont(tapeFont);
153   //  painter->setPen(pen);
154   QBrush brush(m_backColor);
155   painter->setBrush(brush);
156
157   painter->drawPolygon(nboxv);
158
159   painter->drawLines(nboxv);
160  /* QPen redpen(Qt::red);
161   painter->setPen(redpen);
162   int ii=0;
163  foreach (QPoint p,nboxv) {
164       qDebug() << ii << p;
165       painter->drawText(p, QString().number(ii++));
166
167   }*/
168
169 }
170