speedometer.cpp
Go to the documentation of this file.
1 #include "speedometer.h"
2 #include <QPainter>
3 #include <QTimer>
4 #include <QTime>
5 
6 #include <iostream>
7 
8 using namespace std;
9 
10 Speedometer::Speedometer(QWidget *parent) :
11  QWidget(parent),
12  value(0)
13 {
14  resize(200,125);
15 }
16 
17 void Speedometer::setValue(float value)
18 {
19  this->value = value;
20  update();
21 }
22 
23 void Speedometer::paintEvent(QPaintEvent *event)
24 {
25  static const QPoint needle[4] = {
26  QPoint(4,8),
27  QPoint(-4,8),
28  QPoint(-1,-90),
29  QPoint(1,-90)
30  };
31 
32  auto text = tr((to_string(value) + " m/s").c_str());
33 
34  QColor needleColor(0,0,0);
35 
36  auto side = 200;
37  auto time = QTime::currentTime();
38 
39  QPainter painter(this);
40  painter.setRenderHint(QPainter::Antialiasing);
41  painter.translate(width() / 2, height()-25);
42  painter.scale(side / 200.0, side / 200.0);
43 
44  painter.setPen(Qt::NoPen);
45  painter.setBrush(needleColor);
46 
47  painter.save();
48  painter.rotate( (value * (180./6.)) - 90.);
49 
50  painter.drawConvexPolygon(needle, 4);
51  painter.restore();
52 
53  painter.setPen(Qt::black);
54  auto text_width = painter.fontMetrics().width(text);
55  painter.drawText(QRectF(-text_width/2., -40, 100, 20), text);
56 
57  for (int j = 0; j < 30; ++j) {
58  if ((j % 5) != 0) {
59  painter.setPen(QColor(255 - ((255. / 30.) * j), (255. / 30.) * j, 0));
60  painter.drawLine(92, 0, 96, 0);
61  } else {
62  painter.setPen(QColor(0,0,0));
63  painter.drawLine(84, 0, 96, 0);
64  }
65  painter.rotate(-6.0);
66  }
67 }
Speedometer(QWidget *parent=0)
Definition: speedometer.cpp:10
void paintEvent(QPaintEvent *event) override
Definition: speedometer.cpp:23
float value
Definition: speedometer.h:19
void setValue(float value)
Definition: speedometer.cpp:17


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45