UIname added
[mardrone] / mardrone / gauges / gaugehorizon.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 "gaugehorizon.h"
23 #include <QDebug>
24 #include <math.h>
25
26 GaugeHorizon::GaugeHorizon(QGraphicsItem *parent) :
27     Gauge(parent)
28 {
29     m_horizonLineColor=QColor("white");
30     m_horizonSkyColor=QColor("darkBlue");
31     m_horizonGroundColor=QColor("darkRed");
32
33     m_ypos=0;
34
35     int xo=size().width()/2;
36     int yo=size().height()/2;
37
38 #define DR 3.141592653/180.0
39     int arcTable[]={30,45,60,70,80};
40
41     int i = 0;
42     int arc0=130;
43     int arc1=150;
44     fixo+=QLine(xo,yo-arc0,xo,yo-arc1);
45     for (i = 0; i < 5; i++) {
46         fixo+=QLine(xo+arc0*cos(arcTable[i]*DR),yo-arc0*sin(arcTable[i]*DR),xo+arc1*cos(arcTable[i]*DR),yo-arc1*sin(arcTable[i]*DR));
47         fixo+=QLine(xo-arc0*cos(arcTable[i]*DR),yo-arc0*sin(arcTable[i]*DR),xo-arc1*cos(arcTable[i]*DR),yo-arc1*sin(arcTable[i]*DR));
48     };
49     plane+=QPoint(-40,15);
50     plane+=QPoint(0,5);
51     plane+=QPoint(40,15);
52     plane+=QPoint(0,0);
53     plane+=QPoint(-40,15);
54
55     planel+=QPoint(-140,2);
56     planel+=QPoint(-75,2);
57     planel+=QPoint(-70,0);
58     planel+=QPoint(-75,-2);
59     planel+=QPoint(-140,-2);
60     planel+=QPoint(-140,2);
61
62     planer+=QPoint(140,2);
63     planer+=QPoint(75,2);
64     planer+=QPoint(70,0);
65     planer+=QPoint(75,-2);
66     planer+=QPoint(140,-2);
67     planer+=QPoint(140,2);
68
69     zeroTriangle+=QPoint(0,-arc1);
70     zeroTriangle+=QPoint(-8,-arc1-16);
71     zeroTriangle+=QPoint(8,-arc1-16);
72     zeroTriangle+=QPoint(0,-arc1);
73
74     angleTriangle+=QPoint(0,-arc1);
75     angleTriangle+=QPoint(-8,-arc1+16);
76     angleTriangle+=QPoint(8, -arc1+16);
77     angleTriangle+=QPoint(0,-arc1);
78 };
79
80
81
82 void GaugeHorizon::paint(QPainter *painter,const QStyleOptionGraphicsItem *option,
83                        QWidget *widget)
84 {
85     drawHorizon(painter,-1.0*m_value,5*m_value2);
86 };
87 //////////////////////////////////////////////////////////////////////////
88 void GaugeHorizon::drawHorizon(QPainter *painter, float roll, float pitch)
89     {
90       QRect update_rect;
91
92       int xo, yo;
93
94       painter->save();
95       xo=(size().width()/2)/m_scale;
96       if(m_ypos) yo=m_ypos/m_scale;
97         else yo=(size().height()/2)/m_scale;
98
99       painter->setClipRect(QRect(0,0, size().width(),size().height()));
100       painter->setTransform(QTransform().scale(m_scale,m_scale));
101       QTransform savematrix= painter->transform ();
102 //      painter->setClipRect(QRect(0,0, size().width()/m_scale,size().height()/m_scale));
103
104       painter->setTransform(QTransform().translate(xo+scenePos().x()/m_scale, yo+scenePos().y()/m_scale).rotate(roll).translate(0,pitch),true);
105 //      painter->setTransform(QTransform().translate(xo, yo).rotate(roll).translate(0,pitch),true);
106
107 //      qDebug("roll=%f pitch=%f xo=%d yo=%d x=%f y=%f",roll,pitch,xo,yo ,xo+scenePos().x(),yo+scenePos().y());
108
109       QBrush skyBrush(m_horizonSkyColor);
110       QBrush groundBrush(m_horizonGroundColor);
111       painter->fillRect(QRect(-500,0,size().width()*2/m_scale,size().height()*2/m_scale),groundBrush);
112       QBrush bbrush(m_backColor);
113       QBrush cbrush(m_color);
114
115       painter->setBrush(bbrush);
116       painter->fillRect(QRect(-500,0,size().width()*2/m_scale,-1*size().height()*2/m_scale),skyBrush);
117
118       QPen pen(m_color);
119       pen.setWidth(2);
120       painter->setPen(pen);
121       painter->save();
122       painter->setTransform(savematrix);
123       painter->setTransform(QTransform().translate(xo+scenePos().x()/m_scale, yo+scenePos().y()/m_scale).rotate(roll),true);
124
125       painter->setClipRect(QRect(-100,-105, 200,210));
126       painter->setTransform(QTransform().translate(0,pitch),true);
127
128       for(int i=0;i<90;i++) {
129           painter->drawLine(40,    50*i,-40,   50*i);
130           painter->drawLine(20,    50*i+25,-20,   50*i+25);
131           painter->drawText(43,50*i+5,QString("%1").arg(10*i));
132           painter->drawText(-63,50*i+5,QString("%1").arg(10*i));
133           painter->drawLine(40,    -50*i,-40,  -50*i);
134           painter->drawLine(20,    -50*i-25,-20,  -50*i-25);
135           painter->drawText(43,-50*i+5,QString("%1").arg(10*i));
136           painter->drawText(-63,-50*i+5,QString("%1").arg(10*i));
137       }
138
139       painter->restore();
140       painter->setTransform(savematrix);
141       painter->setTransform(QTransform().translate(xo+scenePos().x()/m_scale, yo+scenePos().y()/m_scale).rotate(roll),true);
142
143       painter->setBrush(cbrush);
144       painter->drawPolygon(angleTriangle);
145
146
147       painter->setTransform(savematrix);
148       painter->setTransform(QTransform().translate(xo+scenePos().x()/m_scale, yo+scenePos().y()/m_scale),true);
149       painter->setPen(pen);
150       painter->drawLines(fixo);
151       painter->drawConvexPolygon(plane);
152       painter->drawConvexPolygon(planel);
153       painter->drawConvexPolygon(planer);
154       painter->drawPolygon(zeroTriangle);
155       painter->drawArc(-150,-150,300,300,30*16,120*16);
156     //  painter->setTransform(savematrix);
157       painter->restore();
158
159 }
160