]> git.sesse.net Git - nageru/blob - correlation_meter.cpp
Write 1.4.0 changelog.
[nageru] / correlation_meter.cpp
1 #include "correlation_meter.h"
2
3 #include <math.h>
4 #include <algorithm>
5
6 #include <QBrush>
7 #include <QColor>
8 #include <QPainter>
9 #include <QRect>
10
11 class QPaintEvent;
12 class QResizeEvent;
13
14 using namespace std;
15
16 CorrelationMeter::CorrelationMeter(QWidget *parent)
17         : QWidget(parent)
18 {
19 }
20
21 void CorrelationMeter::resizeEvent(QResizeEvent *event)
22 {
23         on_pixmap = QPixmap(width(), height());
24         QPainter on_painter(&on_pixmap);
25         QLinearGradient on(0, 0, width(), 0);
26         on.setColorAt(0.0f, QColor(255, 0, 0));
27         on.setColorAt(0.5f, QColor(255, 255, 0));
28         on.setColorAt(0.8f, QColor(0, 255, 0));
29         on.setColorAt(0.95f, QColor(255, 255, 0));
30         on_painter.fillRect(0, 0, width(), height(), Qt::black);
31         on_painter.fillRect(1, 1, width() - 2, height() - 2, on);
32
33         off_pixmap = QPixmap(width(), height());
34         QPainter off_painter(&off_pixmap);
35         QLinearGradient off(0, 0, width(), 0);
36         off.setColorAt(0.0f, QColor(127, 0, 0));
37         off.setColorAt(0.5f, QColor(127, 127, 0));
38         off.setColorAt(0.8f, QColor(0, 127, 0));
39         off.setColorAt(0.95f, QColor(127, 127, 0));
40         off_painter.fillRect(0, 0, width(), height(), Qt::black);
41         off_painter.fillRect(1, 1, width() - 2, height() - 2, off);
42 }
43
44 void CorrelationMeter::paintEvent(QPaintEvent *event)
45 {
46         QPainter painter(this);
47
48         float correlation;
49         {
50                 unique_lock<mutex> lock(correlation_mutex);
51                 correlation = this->correlation;
52         }
53
54         // Just in case.
55         correlation = std::min(std::max(correlation, -1.0f), 1.0f);
56
57         int pos = 3 + lrintf(0.5f * (correlation + 1.0f) * (width() - 6));
58         QRect off_rect(0, 0, width(), height());
59         QRect on_rect(pos - 2, 0, 5, height());
60
61         painter.drawPixmap(off_rect, off_pixmap, off_rect);
62         painter.drawPixmap(on_rect, on_pixmap, on_rect);
63 }