Files
esa-remote-lite/PanelsPanel.cpp

172 lines
5.6 KiB
C++

#include "PanelsPanel.h"
#include <QLabel>
#include <QPainter>
#include <QPainterPath>
#include <QTimer>
#include <QVBoxLayout>
#include <QtMath>
PanelsPanel::PanelsPanel(QWidget *parent) : QWidget(parent)
{
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(4, 4, 4, 4);
m_statusLabel = new QLabel("Connect to load panels...", this);
m_statusLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
m_statusLabel->setStyleSheet("color: #888; font-style: italic;");
layout->addWidget(m_statusLabel);
layout->addStretch(1);
setMinimumSize(200, 200);
}
void PanelsPanel::setRnpCount(int count)
{
m_count = count;
m_colors.clear();
m_hits.clear();
m_statusLabel->setText(count > 0
? QString("%1 panel%2").arg(count).arg(count == 1 ? "" : "s")
: "No panels reported");
update();
}
void PanelsPanel::setPanelColor(int index, const QColor &color)
{
m_colors[index] = color;
update();
}
void PanelsPanel::impactPanel(int index, int power)
{
m_hits[index] = power;
update();
QTimer::singleShot(3000, this, [this, index]() {
m_hits.remove(index);
update();
});
}
void PanelsPanel::reset()
{
m_count = 0;
m_colors.clear();
m_hits.clear();
m_statusLabel->setText("Connect to load panels...");
update();
}
// Returns a circle radius multiplier (0.0-1.0) based on 3 power tiers
static double bubbleScale(int power)
{
if (power < 50) return 0.18; // small
if (power < 100) return 0.30; // medium
return 0.85; // large
}
void PanelsPanel::paintEvent(QPaintEvent *)
{
if (m_count <= 0) return;
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
const QRectF bounds = rect();
const QRectF drawArea = bounds.adjusted(0, 28, 0, 0);
const double side = qMin(drawArea.width(), drawArea.height()) * 0.88;
const double cx = drawArea.center().x();
const double cy = drawArea.center().y();
const double outerR = side / 2.0;
const double innerR = outerR * 0.44;
const QRectF outerRect(cx - outerR, cy - outerR, side, side);
const QRectF innerRect(cx - innerR, cy - innerR, innerR * 2, innerR * 2);
const double gapDeg = (m_count > 1) ? 3.0 : 0.0;
const double segSpan = (360.0 - m_count * gapDeg) / m_count;
// Max bubble radius constrained to fit inside the segment band
const double maxCircR = qMin((outerR - innerR) * 0.48,
(outerR + innerR) / 2.0 * qSin(qDegreesToRadians(segSpan / 2.0)) * 0.88);
static const QColor kDefault(100, 140, 180);
static const QColor kHit(220, 40, 40);
for (int i = 0; i < m_count; ++i) {
const bool isHit = m_hits.contains(i);
const double startAngle = 90.0 - i * (segSpan + gapDeg);
const double sweep = -segSpan;
// --- Segment path ---
QPainterPath path;
path.arcMoveTo(outerRect, startAngle);
path.arcTo(outerRect, startAngle, sweep);
path.arcTo(innerRect, startAngle + sweep, -sweep);
path.closeSubpath();
p.setBrush(isHit ? kHit : m_colors.value(i, kDefault));
p.setPen(QPen(Qt::white, isHit ? 3 : 2));
p.drawPath(path);
// --- Midpoint for label / hit circle ---
const double midAngle = startAngle + sweep / 2.0;
const double midAngleRad = qDegreesToRadians(midAngle);
const double midR = (outerR + innerR) / 2.0;
const QPointF mid(cx + midR * qCos(midAngleRad),
cy - midR * qSin(midAngleRad));
if (isHit) {
const int power = m_hits[i];
const double circR = maxCircR * bubbleScale(power);
// Tier label for legend (small/medium/large)
// Glow halo
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QColor(255, 80, 80, 110), 5));
p.drawPath(path);
// Drop shadow
const QRectF circRect(mid.x() - circR, mid.y() - circR, circR * 2, circR * 2);
p.setBrush(QColor(0, 0, 0, 55));
p.setPen(Qt::NoPen);
p.drawEllipse(circRect.adjusted(2, 3, 2, 3));
// Radial gradient fill
QRadialGradient grad(mid, circR,
QPointF(mid.x() - circR * 0.3, mid.y() - circR * 0.3));
grad.setColorAt(0.0, QColor(255, 100, 100));
grad.setColorAt(1.0, QColor(160, 10, 10));
p.setBrush(grad);
p.setPen(QPen(QColor(255, 200, 200), 1.5));
p.drawEllipse(circRect);
// Power value text — scale font to circle size
QFont pf = p.font();
pf.setBold(true);
pf.setPointSize(qMax(6, qMin(12, static_cast<int>(circR * 0.68))));
p.setFont(pf);
p.setPen(Qt::white);
p.drawText(circRect, Qt::AlignCenter, QString::number(power));
} else {
// Normal index label
QFont font = p.font();
font.setBold(true);
font.setPointSize(qMax(7, qMin(12, static_cast<int>(outerR / (m_count * 0.55 + 2)))));
p.setFont(font);
p.setPen(Qt::white);
p.drawText(QRectF(mid.x() - 22, mid.y() - 11, 44, 22),
Qt::AlignCenter, QString::number(i));
}
}
// --- Centre hole: total count ---
QFont cf = p.font();
cf.setBold(true);
cf.setPointSize(qMax(8, static_cast<int>(innerR * 0.45)));
p.setFont(cf);
p.setPen(palette().text().color());
p.drawText(innerRect, Qt::AlignCenter, QString::number(m_count));
}