0.2 version add desktop components multitouch, joystick, multicast
[mardrone] / mardrone / gaugelabel.cpp
1 /*==================================================================
2   !
3   !  mardrone application AR-Drone for MeeGo
4
5   ! Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6   ! All rights reserved.
7   !
8   !  Author:Kate Alhola  kate.alhola@nokia.com
9   !
10   ! GNU Lesser General Public License Usage
11   ! This file may be used under the terms of the GNU Lesser
12   ! General Public License version 2.1 as published by the Free Software
13   ! Foundation and appearing in the file LICENSE.LGPL included in the
14   ! packaging of this file.  Please review the following information to
15   ! ensure the GNU Lesser General Public License version 2.1 requirements
16   ! will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17   !
18   !
19   !
20   *===================================================================*/
21
22 #include "gaugelabel.h"
23 #include <QDebug>
24
25
26 GaugeLabel::GaugeLabel(QGraphicsItem *parent) :
27     Gauge(parent)
28 {
29 }
30
31 void GaugeLabel::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
32                        QWidget *widget)
33 {
34     drawNumLabel(painter);
35 };
36 //////////////////////////////////////////////////////////////////////////
37 void GaugeLabel::drawNumLabel(QPainter *painter)
38 {
39   int ld,ldo,ud,lo;
40   int split,uw,base;
41   char text[20] = {0};
42   int h=size().height();
43   int w=size().width();
44   int val=m_value;
45    painter->save();
46   qDebug() <<"drawnumlabel=" << m_name << " h=" << h << "w=" << w;
47   if(m_style&SCROLL) {
48     QFont sansFont("Helvetica [Cronyx]", 12);
49     QFontMetrics fm(sansFont);
50     painter->setFont(sansFont);
51
52     ld=abs(val%100);
53     ud=val/100;
54     ldo=ld%10;
55     printf("ld=%d ldo=%d lo=%d h=%d\n",ld,ldo,lo,h);
56     lo=(ldo*h)/-20;
57     drawLabel(painter, text, m_style);
58     sprintf(text, "%3d", ud);
59     split=w/2;
60     base=(h+fm.ascent())/2;
61     uw=fm.width(text);
62
63     painter->drawText(split-uw+6, base, text);
64     sprintf(text, "%02d", abs((ld-ldo-20)%100));
65     painter->drawText(split+ 7, base -h +lo, text);
66     sprintf(text, "%02d", abs((ld-ldo-10)%100));
67     painter->drawText(split+ 7, base -h/2 +lo, text);
68     sprintf(text, "%02d", abs((ld-ldo)%100));
69     painter->drawText(split +7, base +lo, text);
70     sprintf(text, "%02d", abs((ld-ldo+10)%100));
71     painter->drawText(split + 7,base + h/2 +lo , text);
72     sprintf(text, "%02d", abs((ld-ldo+20)%100));
73     painter->drawText(split + 7,base + h +lo , text);
74
75
76   } else {
77     sprintf(text, "%3d", val);
78     drawLabel(painter, text,m_style);
79   }
80   painter->restore();  // Restore and remove clicping rectabgle
81 }
82
83 ///////////////////////////////////////////////////////////////////////////
84 void GaugeLabel::drawLabel(QPainter *painter,char *text,int style)
85 {
86   int textlen = 0, i = 0;
87   int xplace=0;
88   int yplace=0;
89   int height=size().height();
90   int width=size().width();
91   int scrbox=(height*10)/30;
92
93
94   QVector<QPoint> nboxv;
95
96   QPen pen(Qt::white);
97   painter->setPen(pen);
98
99
100   if (style&7 == LEFT) xplace+=5;
101
102   nboxv.push_back(QPoint(xplace,yplace));
103
104
105
106   if (style&7 == UP) {
107     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace  ));
108     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace-5));
109     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace  ));
110   }
111
112   if (style&SCROLL) {
113     nboxv.push_back(QPoint(xplace+width/2,yplace));
114     nboxv.push_back(QPoint(xplace+width/2,yplace-scrbox));
115     nboxv.push_back(QPoint(xplace+width,yplace-scrbox  ));
116   } else
117     nboxv.push_back(QPoint(xplace+width,yplace  ));
118
119
120   if (style&7== RIGHT) {
121
122     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)-5));
123     nboxv.push_back(QPoint(xplace+width+5,yplace+(height/2)  ));
124     nboxv.push_back(QPoint(xplace+width  ,yplace+(height/2)+5));
125   }
126
127   if (style&SCROLL) {
128     nboxv.push_back(QPoint(xplace+width,yplace+height+scrbox));
129     nboxv.push_back(QPoint(xplace+width/2,yplace+height+scrbox));
130     nboxv.push_back(QPoint(xplace+width/2,yplace+height));
131   } else
132     nboxv.push_back(QPoint(xplace+width,yplace+height));
133
134   if (style&7== DOWN) {
135     nboxv.push_back(QPoint(xplace+(width/2)-5,yplace+height));
136     nboxv.push_back(QPoint(xplace+(width/2)  ,yplace+height+5));
137     nboxv.push_back(QPoint(xplace+(width/2)+5,yplace+height));
138   }
139
140   nboxv.push_back(QPoint(xplace,yplace+height));
141
142   if (style&7 == LEFT) {
143     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)+5));
144     nboxv.push_back(QPoint(xplace-5,yplace+(height/2)  ));
145     nboxv.push_back(QPoint(xplace  ,yplace+(height/2)-5));
146   }
147
148   nboxv.push_back(QPoint(xplace,yplace));
149
150   textlen = strlen(text) + 2;
151
152   //  pdrawable->draw_rectangle(pwidget->get_style()->get_black_gc(),
153   //                        true, xplace, yplace, width, height);
154
155   if (style&SCROLL)
156     painter->setClipRect(QRect(xplace-5,yplace-scrbox, width+10, height+2*scrbox+1));
157
158   QBrush brush(QColor(0,0,0,255));
159   //  QFont tapeFont("Helvetica [Cronyx]", 18);
160   //  painter->setFont(tapeFont);
161   //  painter->setPen(pen);
162   painter->setBrush(brush);
163
164   painter->drawPolygon(nboxv);
165
166   painter->drawLines(nboxv);
167
168   //  painter->setBrush(brush);
169   QFont sansFont("Helvetica [Cronyx]", 12);
170   QFontMetrics fm(sansFont);
171   painter->setFont(sansFont);
172
173   painter->drawText(xplace + 3, yplace + (height+fm.ascent())/2, text);
174 }
175