1 #include "correlation_meter.h"
16 CorrelationMeter::CorrelationMeter(QWidget *parent)
21 void CorrelationMeter::resizeEvent(QResizeEvent *event)
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);
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);
44 void CorrelationMeter::paintEvent(QPaintEvent *event)
46 QPainter painter(this);
50 lock_guard<mutex> lock(correlation_mutex);
51 correlation = this->correlation;
55 correlation = std::min(std::max(correlation, -1.0f), 1.0f);
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());
61 painter.drawPixmap(off_rect, off_pixmap, off_rect);
62 painter.drawPixmap(on_rect, on_pixmap, on_rect);