]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/AnglesHistogramPainter.cpp
robofsm: Strategy
[eurobot/public.git] / src / robomon / AnglesHistogramPainter.cpp
1 /*
2  * AnglesHistogramPainter.cpp                           08/01/28
3  *
4  * Angles histogram painter.
5  *
6  * Copyright: (c) 2008 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
9  * License: GNU GPL v.2
10  */
11
12 #include <QtGui>
13 #include <robomath.h>
14 #include "AnglesHistogramPainter.h"
15
16 #define ANGLES_HISTOGRAM_HEIGHT 70
17
18 AnglesHistogramPainter::AnglesHistogramPainter
19         (struct angles_freq *angles, int count)
20 {
21         background = QBrush(QColor(220, 220, 220));
22         this->angles = angles;
23         this->count = count;
24         setSize(count, ANGLES_HISTOGRAM_HEIGHT);
25 }
26
27 AnglesHistogramPainter::~AnglesHistogramPainter()
28 {
29 }
30
31 void AnglesHistogramPainter::paint(QPainter *painter, QPaintEvent *event)
32 {
33         int max;
34         int e = 3;
35         int cor = 0;
36         QRect rect = event->rect();
37
38         painter->fillRect(rect, background);
39         painter->setPen(QColor(20, 20, 20));
40
41         /* the first one is with highest frequency, use to calibrate 
42            columns height */
43         max = angles[0].frequency;
44         for (int i=0; i<count; i++) {
45                 /* skip angles with frequency = zero */
46                 if ((int)(angles[i].frequency) == 0)
47                         continue;
48
49                 /* description for three angles with highest frequency*/
50                 if (e-- > 0) {
51                         /* correction to show text inside the view */
52                         if ((int)(angles[i].angle) > 310)
53                                 cor = -(int)(angles[i].angle-320)-10;
54
55                         painter->setPen(QColor(255, 20, 20));
56                         painter->drawText(
57                                 (int)(angles[i].angle)+cor,
58                                 height-(int)(
59                                         (double)angles[i].frequency /
60                                         (double)max*(height-20)+1),
61                                 QString("%1(%2)")
62                                 .arg(angles[i].angle)
63                                 .arg(angles[i].frequency));
64                         painter->setPen(QColor(20, 20, 20));
65                 }
66
67                 /* draw a column */
68                 painter->drawLine((int)(angles[i].angle), 
69                         height, 
70                         (int)(angles[i].angle), 
71                         height-(int)(
72                                 (double)angles[i].frequency /
73                                 (double)max*(height-20)));
74         }
75 }
76
77 void AnglesHistogramPainter::setCount(int count)
78 {
79         this->count = count;
80         setSize(count, ANGLES_HISTOGRAM_HEIGHT);
81 }
82
83 void AnglesHistogramPainter::setSize(int width, int height)
84 {
85         this->width = width;
86         this->height = height;
87 }
88
89 QSize AnglesHistogramPainter::size()
90 {
91         return QSize(width, height);
92 }