]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/display-qt/displayqt.cpp
c59c138ebe5ba3e4d04c5d4dac7e6ac80cd95aaf
[eurobot/public.git] / src / display-qt / displayqt.cpp
1 #include "displayqt.h"
2 #include "ui_displayqt.h"
3 #include <QtGui>
4 #include <QObject>
5 #include <QTimer>
6 #include <QString>
7 #include <robomath.h>
8
9 //backround colors for labels
10 #define GREEN "background-color:rgb(27, 255, 11)"
11 #define RED "background-color:red"
12 #define YELLOW "background-color:yellow"
13
14 DisplayQT::DisplayQT(QWidget *parent) :
15         QWidget(parent),
16         ui(new Ui::DisplayQT)
17 {
18         ui->setupUi(this);
19
20         /* create the window without the title bar */
21         Qt::WindowFlags flags = this->windowFlags();
22         flags |= Qt::FramelessWindowHint;
23         this->setWindowFlags(flags);
24
25         //cursor will be hidden
26         setCursor(QCursor(Qt::BlankCursor));
27
28         //na zacatku nazname polohu
29         this->pos.positionIsActual=false;
30
31         /*ASCII tocitko
32         pomoci timeru je kazdych 500 ms
33         vyvolan signal pro pohnuti tocitka
34         */
35
36         QTimer *timer = new QTimer(this);
37         timer->start(500);
38         connect(timer, SIGNAL(timeout()), this, SLOT(alive()));
39     ///***///
40
41     //premalovavani kompasu
42     connect(this, SIGNAL(repaintCompass()), this, SLOT(update()));
43 }
44
45 //sipka od uhlu natoceni
46 void DisplayQT::paintEvent(QPaintEvent *)
47 {
48         //umisteni stredu kompasu
49         int const x=410;
50         int const y=95;
51         int const dimension=80;
52
53         static const QPoint arrow[3] = {
54                 QPoint(4, 4),
55                 QPoint(-4, 4),
56                 QPoint(0, -((dimension/2)-15))
57     };
58
59
60         //barvy pro malovani
61         QColor prvniColor(127, 0, 127);
62         QColor druhaColor(0, 127, 127, 191);
63         QColor pozadi(240, 240, 240);
64
65         QPainter painter(this);
66
67         //background
68         painter.fillRect(x-dimension/2, y-dimension/2, dimension, dimension, pozadi);
69
70         painter.setRenderHint(QPainter::Antialiasing);
71         painter.translate(x,y);
72
73         painter.setPen(Qt::NoPen);
74         painter.setBrush(prvniColor);
75         painter.save();
76
77     //namalovani sipky p.t.k. je aktualni pozice
78         if(pos.positionIsActual){
79                 painter.rotate(90-pos.phi);
80                 painter.drawConvexPolygon(arrow, 3);
81         }
82         painter.restore();
83
84     //namaluju 4 cary po 90 stupnich
85         painter.setPen(prvniColor);
86         for(int i=0; i<4; i++){
87                 painter.drawLine((dimension/2)-10, 0, (dimension/2)-5, 0);
88                 painter.rotate(90.0);
89         }
90
91         //namaluju mensi cary mezi ty predchozi
92         painter.setPen(druhaColor);
93
94         for(int j=0; j<12; j++){
95                 if(j%3)
96                         painter.drawLine((dimension/2)-12, 0, (dimension/2)-7, 0);
97                 painter.rotate(30.0);
98         }
99 }
100
101 DisplayQT::~DisplayQT()
102 {
103         delete ui;
104 }
105
106 void DisplayQT::alive(void)
107 {
108         static char aliveState=0;
109         if(++aliveState==4)
110                 aliveState=0;
111
112         switch(aliveState){
113                 case 0: ui->ziju->setText("|");break;
114                 case 1: ui->ziju->setText("/");break;
115                 case 2: ui->ziju->setText("-");break;
116                 case 3: ui->ziju->setText("\\");break;
117         }
118 }
119
120 void DisplayQT::display_time(double time) {
121
122         this->match_time = 90 - time;
123
124         ui->matchTime->display(match_time);
125 }
126
127 // 0-modra
128 // 1-cervena
129 void DisplayQT::setTeamColor(char color)
130 {
131         if(color==0){
132                 color=0;
133                 ui->our_color->setStyleSheet("background-color: rgb(213, 40, 24)");
134         }else if(color==1){
135                 color=1;
136                 ui->our_color->setStyleSheet("background-color: blue");
137         }
138 }
139
140 void DisplayQT::setPosition(double x, double y, double phi)
141 {
142         this->pos.x=x;
143         this->pos.y=y;
144         this->pos.phi=phi;
145
146         //prevod z radianu na stupne a uprava
147         pos.phi = RAD2DEG(pos.phi);
148         pos.phi = fmod(pos.phi, 360);
149         if ( pos.phi < 0 )
150                 pos.phi += 360;
151
152         ui->position_x->setText("x: "+QString::number(pos.x, 10, 3)+" m");
153         ui->position_y->setText("y: "+QString::number(pos.y, 10, 3)+" m");
154         ui->position_phi->setText("phi: "+QString::number(pos.phi, 10, 1)+" deg");
155
156         pos.positionIsActual=true;
157         emit repaintCompass();
158 }
159
160 void DisplayQT::display_status(UDE_component_t c, UDE_hw_status_t s)
161 {
162         switch(c){
163                 case 0:break;
164                 case MOT:
165                         if(s==STATUS_OK)
166                                 ui->comp_MOT->setStyleSheet(GREEN);
167                         else if(s==STATUS_FAILED)
168                                 ui->comp_MOT->setStyleSheet(RED);
169                         else
170                                 ui->comp_MOT->setStyleSheet(YELLOW);
171                 break;
172                 case ODO:
173                         if(s==STATUS_OK)
174                                 ui->comp_ODO->setStyleSheet(GREEN);
175                         else if(s==STATUS_FAILED)
176                                 ui->comp_ODO->setStyleSheet(RED);
177                         else
178                                 ui->comp_ODO->setStyleSheet(YELLOW);
179                 break;
180                 case JAW:
181                         if(s==STATUS_OK)
182                                 ui->comp_JAW->setStyleSheet(GREEN);
183                         else if(s==STATUS_FAILED)
184                                 ui->comp_JAW->setStyleSheet(RED);
185                         else
186                                 ui->comp_JAW->setStyleSheet(YELLOW);
187                         break;
188                 case PWR:
189                         if(s==STATUS_OK)
190                                 ui->comp_PWR->setStyleSheet(GREEN);
191                         else if(s==STATUS_FAILED){
192                                 ui->comp_PWR->setStyleSheet(RED);
193
194                                 ui->voltage_33->setText("v.33 = ?");
195                                 ui->voltage_50->setText("v.50 = ?");
196                                 ui->voltage_80->setText("v.80 = ?");
197                                 ui->voltage_BAT->setText("v.BAT = ?");
198
199                                 ui->voltage_33->setStyleSheet(YELLOW);
200                                 ui->voltage_50->setStyleSheet(YELLOW);
201                                 ui->voltage_80->setStyleSheet(YELLOW);
202                                 ui->voltage_BAT->setStyleSheet(YELLOW);
203                         }
204                         else
205                                 ui->comp_PWR->setStyleSheet(YELLOW);
206                 break;
207                 case HOK:
208                         if(s==STATUS_OK)
209                                 ui->comp_HOK->setStyleSheet(GREEN);
210                         else if(s==STATUS_FAILED)
211                                 ui->comp_HOK->setStyleSheet(RED);
212                         else
213                                 ui->comp_HOK->setStyleSheet(YELLOW);
214                         break;
215                 case APP:
216                         if(s==STATUS_OK)
217                                 ui->comp_APP->setStyleSheet(GREEN);
218                         else if(s==STATUS_FAILED){
219                                 ui->comp_APP->setStyleSheet(RED);
220
221                                 pos.positionIsActual=false;
222                                 ui->position_x->setText("x: ?");
223                                 ui->position_y->setText("y: ?");
224                                 ui->position_phi->setText("phi: ?");
225                                 emit repaintCompass();
226                         }
227                         else
228                                 ui->comp_APP->setStyleSheet(YELLOW);
229                         break;
230                 case LFT:
231                         if(s==STATUS_OK)
232                                 ui->comp_LFT->setStyleSheet(GREEN);
233                         else if(s==STATUS_FAILED)
234                                 ui->comp_LFT->setStyleSheet(RED);
235                         else
236                                 ui->comp_LFT->setStyleSheet(YELLOW);
237                         break;
238                 case STA:
239                         if(s==STATUS_OK)
240                                 ui->comp_STA->setStyleSheet(GREEN);
241                         else if(s==STATUS_FAILED)
242                                 ui->comp_STA->setStyleSheet(RED);
243                         else
244                                 ui->comp_STA->setStyleSheet(YELLOW);
245                 break;
246         }
247 }
248
249 void DisplayQT::display_fsm(UDE_fsm_t fsm, QString state){
250
251         switch(fsm){
252                 case FSM_MAIN:
253                         ui->fsm_main->setText(state);
254                 break;
255                 case FSM_MOVE:
256                         ui->fsm_move->setText(state);
257                 break;
258                 case FSM_ACT:
259                         ui->fsm_act->setText(state);
260                 break;
261         }
262 }
263
264 void DisplayQT::display_voltage(double voltage33, double voltage50, double voltage80, double voltageBAT){
265         ui->voltage_33->setText("v.33 = "+QString::number(voltage33, 10, 2)+" V");
266         ui->voltage_50->setText("v.50 = "+QString::number(voltage50, 10, 2)+" V");
267         ui->voltage_80->setText("v.80 = "+QString::number(voltage80, 10, 2)+" V");
268         ui->voltage_BAT->setText("v.BAT = "+QString::number(voltageBAT, 10, 2)+" V");
269
270
271         if( voltageBAT < WARNING_VOLTAGEBAT && voltageBAT > TRESHOLDS_VOLTAGEBAT )
272                 ui->voltage_BAT->setStyleSheet(YELLOW);
273         else if( voltageBAT < TRESHOLDS_VOLTAGEBAT )
274                 ui->voltage_BAT->setStyleSheet(RED);
275         else
276                 ui->voltage_BAT->setStyleSheet(GREEN);
277
278         if( voltage33 < TRESHOLDS_VOLTAGE33 )
279                 ui->voltage_33->setStyleSheet(YELLOW);
280         else
281                 ui->voltage_33->setStyleSheet(GREEN);
282
283         if( voltage50 < TRESHOLDS_VOLTAGE50 )
284                 ui->voltage_50->setStyleSheet(YELLOW);
285         else
286                 ui->voltage_50->setStyleSheet(GREEN);
287
288         if( voltage80 < TRESHOLDS_VOLTAGE80 )
289                 ui->voltage_80->setStyleSheet(YELLOW);
290         else
291                 ui->voltage_80->setStyleSheet(GREEN);
292 }