38d6046c9291e75d4aeecc5bc7b4da50dc16c32b
[mardrone] / mardrone / gaugelabel.cpp
1 #include "gaugelabel.h"
2 #include <QDebug>
3
4
5 GaugeLabel::GaugeLabel(QGraphicsItem *parent) :
6     Gauge(parent)
7 {
8 }
9
10 void GaugeLabel::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
11                        QWidget *widget)
12 {
13     drawNumLabel(painter);
14 };
15 //////////////////////////////////////////////////////////////////////////
16 void GaugeLabel::drawNumLabel(QPainter *painter)
17 {
18   int ld,ldo,ud,lo;
19   int split,uw,base;
20   char text[20] = {0};
21   int h=size().height();
22   int w=size().width();
23   int val=m_value;
24    painter->save();
25   qDebug() <<"drawnumlabel=" << m_name << " h=" << h << "w=" << w;
26   if(m_style&SCROLL) {
27     QFont sansFont("Helvetica [Cronyx]", 12);
28     QFontMetrics fm(sansFont);
29     painter->setFont(sansFont);
30
31     ld=abs(val%100);
32     ud=val/100;
33     ldo=ld%10;
34     printf("ld=%d ldo=%d lo=%d h=%d\n",ld,ldo,lo,h);
35     lo=(ldo*h)/-20;
36     drawLabel(painter, text, m_style);
37     sprintf(text, "%3d", ud);
38     split=w/2;
39     base=(h+fm.ascent())/2;
40     uw=fm.width(text);
41
42     painter->drawText(split-uw+6, base, text);
43     sprintf(text, "%02d", abs((ld-ldo-20)%100));
44     painter->drawText(split+ 7, base -h +lo, text);
45     sprintf(text, "%02d", abs((ld-ldo-10)%100));
46     painter->drawText(split+ 7, base -h/2 +lo, text);
47     sprintf(text, "%02d", abs((ld-ldo)%100));
48     painter->drawText(split +7, base +lo, text);
49     sprintf(text, "%02d", abs((ld-ldo+10)%100));
50     painter->drawText(split + 7,base + h/2 +lo , text);
51     sprintf(text, "%02d", abs((ld-ldo+20)%100));
52     painter->drawText(split + 7,base + h +lo , text);
53
54
55   } else {
56     sprintf(text, "%3d", val);
57     drawLabel(painter, text,m_style);
58   }
59   painter->restore();  // Restore and remove clicping rectabgle
60 }
61
62 ///////////////////////////////////////////////////////////////////////////
63 void GaugeLabel::drawLabel(QPainter *painter,char *text,int style)
64 {
65   int textlen = 0, i = 0;
66   int xplace=0;
67   int yplace=0;
68   int height=size().height();
69   int width=size().width();
70   int scrbox=(height*10)/30;
71
72
73   QVector<QPoint> nboxv;
74
75   QPen pen(Qt::white);
76   painter->setPen(pen);
77
78
79   if (style&7 == LEFT) xplace+=5;
80
81   nboxv.push_back(QPoint(xplace,yplace));
82
83
84
85   if (style&7 == UP) {
86     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace  ));
87     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace-5));
88     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace  ));
89   }
90
91   if (style&SCROLL) {
92     nboxv.push_back(QPoint(xplace+width/2,yplace));
93     nboxv.push_back(QPoint(xplace+width/2,yplace-scrbox));
94     nboxv.push_back(QPoint(xplace+width,yplace-scrbox  ));
95   } else
96     nboxv.push_back(QPoint(xplace+width,yplace  ));
97
98
99   if (style&7== RIGHT) {
100
101     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)-5));
102     nboxv.push_back(QPoint(xplace+width+5,yplace+(height/2)  ));
103     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)+5));
104   }
105
106   if (style&SCROLL) {
107     nboxv.push_back(QPoint(xplace+width,yplace+height+scrbox));
108     nboxv.push_back(QPoint(xplace+width/2,yplace+height+scrbox));
109     nboxv.push_back(QPoint(xplace+width/2,yplace+height));
110   } else
111     nboxv.push_back(QPoint(xplace+width,yplace+height));
112
113   if (style&7== DOWN) {
114     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace+height));
115     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace+height+5));
116     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace+height));
117   }
118
119   nboxv.push_back(QPoint(xplace,yplace+height));
120
121   if (style&7 == LEFT) {
122     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)+5));
123     nboxv.push_back(QPoint(xplace-5,yplace+(height/2)  ));
124     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)-5));
125   }
126
127   nboxv.push_back(QPoint(xplace,yplace));
128
129   textlen = strlen(text) + 2;
130
131   //  pdrawable->draw_rectangle(pwidget->get_style()->get_black_gc(),
132   //                        true, xplace, yplace, width, height);
133
134   if (style&SCROLL)
135     painter->setClipRect(QRect(xplace-5,yplace-scrbox, width+10, height+2*scrbox+1));
136
137   QBrush brush(QColor(0,0,0,255));
138   //  QFont tapeFont("Helvetica [Cronyx]", 18);
139   //  painter->setFont(tapeFont);
140   //  painter->setPen(pen);
141   painter->setBrush(brush);
142
143   painter->drawPolygon(nboxv);
144
145   painter->drawLines(nboxv);
146
147   //  painter->setBrush(brush);
148   QFont sansFont("Helvetica [Cronyx]", 12);
149   QFontMetrics fm(sansFont);
150   painter->setFont(sansFont);
151
152   painter->drawText(xplace + 3, yplace + (height+fm.ascent())/2, text);
153 }
154