1 #include <QtCore/QFile>
2 #include <QtGui/QPainter>
6 #include "ameterwidget.h"
13 // Smoothing: 0 - none, 1 - light, 2 - medium, 3 - strong
15 qreal smoothing_k[] = {1, 0.5, 0.25, 0.125};
17 AMeterWidget::AMeterWidget(QWidget *parent)
20 // Reciprocal of gravity
22 r_a_max = 1.0 / a_max;
37 AMeterWidget::~AMeterWidget()
42 int AMeterWidget::setGravity(qreal g)
44 // Reciprocal of gravity
52 // This method is called on every update, keep division operations out
54 void AMeterWidget::paintEvent(QPaintEvent *e)
62 paint.drawImage(QPoint(0, 0), *background);
69 a = sqrt(ax * ax + ay * ay + az * az);
70 paint.setRenderHints(QPainter::Antialiasing);
80 paint.setPen(QPen(QBrush(QColor(255, 255, 255, 255)), 3));
81 paint.setBrush(QBrush(QColor(255, 255, 255, 255)));
82 paint.drawEllipse(QPointF(x1, y1), 3, 3);
83 paint.drawLine(QPointF(cx, cy), QPointF(x1, y1));
86 #define DEG2RAD (M_PI / 180.0)
88 void AMeterWidget::resizeEvent(QResizeEvent *e)
93 void AMeterWidget::drawScale()
97 qreal cx, cy, rx, ry, dx, dy;
99 qreal id = 1.0 / divisions;
108 r_a_max = 1.0 / a_max;
110 background = new QImage(size(), QImage::Format_ARGB32);
111 paint = new QPainter(background);
112 paint->setRenderHints(QPainter::Antialiasing);
126 r = sqrt(cx * cx + cy * cy);
128 paint->setPen(QPen(QBrush(QColor(64, 64, 64, 255)), 3));
130 paint->drawLine(QPointF(cx, 0), QPointF(cx, cy + cy));
131 paint->drawLine(QPointF(0, cy), QPointF(cx + cx, cy));
133 for (i = 0; i * rx < cx; i++)
135 paint->setPen(QPen(QBrush(QColor(64, 64, 64, 255)), 3));
138 paint->drawEllipse(QPointF(cx, cy), rx * i, ry * i);
141 paint->setPen(QPen(QBrush(QColor(64, 64, 64, 255)), 1));
142 for (j = 0; j < divisions; j++)
144 paint->drawEllipse(QPointF(cx, cy), rx * (i + id * j), ry * (i + id * j));
147 str = QString::number(i) + "g";
148 paint->setPen(QPen(QBrush(QColor(192, 192, 192, 255)), 1));
149 paint->drawText(QPointF(cx - rx * i, cy), str);
150 paint->drawText(QPointF(cx + rx * i, cy), str);
151 paint->drawText(QPointF(cx, cy - ry * i), str);
152 paint->drawText(QPointF(cx, cy + ry * i), str);
156 for (i = 0; i < 360; i += angle_step)
161 paint->drawLine(QPointF(cx, cy), QPointF(cx + dx, cy + dy));
167 bool AMeterWidget::filter(QAccelerometerReading *reading)
174 ax = reading->x() * r_g;
175 ay = reading->y() * r_g;
176 az = reading->z() * r_g;
178 if (!in_reset && smoothing > 0 && smoothing < 4)
180 k = smoothing_k[smoothing];
181 ax = ax * k + bx * (1.0 - k);
182 ay = ay * k + by * (1.0 - k);
183 az = az * k + bz * (1.0 - k);
192 void AMeterWidget::reset()