]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/display-qt/displayqt.cpp
displa-qt: Match timing display background color set according to status.
[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         ui->matchTime->display(time);
123 }
124
125 // 0-modra
126 // 1-cervena
127 void DisplayQT::setTeamColor(char color)
128 {
129         if(color==0){
130                 color=0;
131                 ui->our_color->setStyleSheet("background-color: rgb(213, 40, 24)");
132         }else if(color==1){
133                 color=1;
134                 ui->our_color->setStyleSheet("background-color: blue");
135         }
136 }
137
138 void DisplayQT::setPosition(double x, double y, double phi)
139 {
140         this->pos.x=x;
141         this->pos.y=y;
142         this->pos.phi=phi;
143
144         //prevod z radianu na stupne a uprava
145         pos.phi = RAD2DEG(pos.phi);
146         pos.phi = fmod(pos.phi, 360);
147         if ( pos.phi < 0 )
148                 pos.phi += 360;
149
150         ui->position_x->setText("x: "+QString::number(pos.x, 10, 3)+" m");
151         ui->position_y->setText("y: "+QString::number(pos.y, 10, 3)+" m");
152         ui->position_phi->setText("phi: "+QString::number(pos.phi, 10, 1)+" deg");
153
154         pos.positionIsActual=true;
155         emit repaintCompass();
156 }
157
158 void DisplayQT::display_status(UDE_component_t c, UDE_hw_status_t s)
159 {
160         switch(c){
161                 case 0:break;
162                 case MOT:
163                         if(s==STATUS_OK)
164                                 ui->comp_MOT->setStyleSheet(GREEN);
165                         else if(s==STATUS_FAILED)
166                                 ui->comp_MOT->setStyleSheet(RED);
167                         else
168                                 ui->comp_MOT->setStyleSheet(YELLOW);
169                 break;
170                 case ODO:
171                         if(s==STATUS_OK)
172                                 ui->comp_ODO->setStyleSheet(GREEN);
173                         else if(s==STATUS_FAILED)
174                                 ui->comp_ODO->setStyleSheet(RED);
175                         else
176                                 ui->comp_ODO->setStyleSheet(YELLOW);
177                 break;
178                 case JAW:
179                         if(s==STATUS_OK)
180                                 ui->comp_JAW->setStyleSheet(GREEN);
181                         else if(s==STATUS_FAILED)
182                                 ui->comp_JAW->setStyleSheet(RED);
183                         else
184                                 ui->comp_JAW->setStyleSheet(YELLOW);
185                         break;
186                 case PWR:
187                         if(s==STATUS_OK)
188                                 ui->comp_PWR->setStyleSheet(GREEN);
189                         else if(s==STATUS_FAILED){
190                                 ui->comp_PWR->setStyleSheet(RED);
191
192                                 ui->voltage_33->setText("v.33 = ?");
193                                 ui->voltage_50->setText("v.50 = ?");
194                                 ui->voltage_80->setText("v.80 = ?");
195                                 ui->voltage_BAT->setText("v.BAT = ?");
196
197                                 ui->voltage_33->setStyleSheet(YELLOW);
198                                 ui->voltage_50->setStyleSheet(YELLOW);
199                                 ui->voltage_80->setStyleSheet(YELLOW);
200                                 ui->voltage_BAT->setStyleSheet(YELLOW);
201                         }
202                         else
203                                 ui->comp_PWR->setStyleSheet(YELLOW);
204                 break;
205                 case HOK:
206                         if(s==STATUS_OK)
207                                 ui->comp_HOK->setStyleSheet(GREEN);
208                         else if(s==STATUS_FAILED)
209                                 ui->comp_HOK->setStyleSheet(RED);
210                         else
211                                 ui->comp_HOK->setStyleSheet(YELLOW);
212                         break;
213                 case APP:
214                         if(s==STATUS_OK)
215                                 ui->comp_APP->setStyleSheet(GREEN);
216                         else if(s==STATUS_FAILED){
217                                 ui->comp_APP->setStyleSheet(RED);
218
219                                 pos.positionIsActual=false;
220                                 ui->position_x->setText("x: ?");
221                                 ui->position_y->setText("y: ?");
222                                 ui->position_phi->setText("phi: ?");
223                                 emit repaintCompass();
224                         }
225                         else
226                                 ui->comp_APP->setStyleSheet(YELLOW);
227                         break;
228                 case LFT:
229                         if(s==STATUS_OK)
230                                 ui->comp_LFT->setStyleSheet(GREEN);
231                         else if(s==STATUS_FAILED)
232                                 ui->comp_LFT->setStyleSheet(RED);
233                         else
234                                 ui->comp_LFT->setStyleSheet(YELLOW);
235                         break;
236                 case STA:
237                         if(s==STATUS_OK)
238                                 ui->comp_STA->setStyleSheet(GREEN);
239                         else if(s==STATUS_FAILED)
240                                 ui->comp_STA->setStyleSheet(RED);
241                         else
242                                 ui->comp_STA->setStyleSheet(YELLOW);
243                         break;
244                 case TIM:
245                         if(s==STATUS_OK)
246                                 ui->matchTime->setStyleSheet(GREEN);
247                         else if(s==STATUS_FAILED)
248                                 ui->matchTime->setStyleSheet(RED);
249                         else
250                                 ui->matchTime->setStyleSheet(YELLOW);
251                         break;
252         }
253 }
254
255 void DisplayQT::display_fsm(UDE_fsm_t fsm, QString state){
256
257         switch(fsm){
258                 case FSM_MAIN:
259                         ui->fsm_main->setText(state);
260                 break;
261                 case FSM_MOVE:
262                         ui->fsm_move->setText(state);
263                 break;
264                 case FSM_ACT:
265                         ui->fsm_act->setText(state);
266                 break;
267         }
268 }
269
270 void DisplayQT::display_voltage(double voltage33, double voltage50, double voltage80, double voltageBAT){
271         ui->voltage_33->setText("v.33 = "+QString::number(voltage33, 10, 2)+" V");
272         ui->voltage_50->setText("v.50 = "+QString::number(voltage50, 10, 2)+" V");
273         ui->voltage_80->setText("v.80 = "+QString::number(voltage80, 10, 2)+" V");
274         ui->voltage_BAT->setText("v.BAT = "+QString::number(voltageBAT, 10, 2)+" V");
275
276
277         if( voltageBAT < WARNING_VOLTAGEBAT && voltageBAT > TRESHOLDS_VOLTAGEBAT )
278                 ui->voltage_BAT->setStyleSheet(YELLOW);
279         else if( voltageBAT < TRESHOLDS_VOLTAGEBAT )
280                 ui->voltage_BAT->setStyleSheet(RED);
281         else
282                 ui->voltage_BAT->setStyleSheet(GREEN);
283
284         if( voltage33 < TRESHOLDS_VOLTAGE33 )
285                 ui->voltage_33->setStyleSheet(YELLOW);
286         else
287                 ui->voltage_33->setStyleSheet(GREEN);
288
289         if( voltage50 < TRESHOLDS_VOLTAGE50 )
290                 ui->voltage_50->setStyleSheet(YELLOW);
291         else
292                 ui->voltage_50->setStyleSheet(GREEN);
293
294         if( voltage80 < TRESHOLDS_VOLTAGE80 )
295                 ui->voltage_80->setStyleSheet(YELLOW);
296         else
297                 ui->voltage_80->setStyleSheet(GREEN);
298 }