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