-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledwidget.cpp
73 lines (60 loc) · 2.42 KB
/
ledwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/******************************************************************************
* Qt COM listener for XLAT, captures data, charts, interpolates metrics for deeper insights.
* Copyright (c) 2024 axaro1
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "ledwidget.h"
#include <QPainter>
#include <QLinearGradient>
#include <QFontMetrics>
LedWidget::LedWidget(QWidget *parent) : QWidget(parent), m_color(Qt::red), m_text("VCOM")
{
setFixedSize(81, 51); // size of the LED widget
}
void LedWidget::setColor(QColor color)
{
m_color = color;
update(); // Trigger a repaint of the widget
}
void LedWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QLinearGradient gradient(0, 0, width(), height());
gradient.setColorAt(0, QColor(70, 70, 70));
gradient.setColorAt(1, QColor(20, 20, 20));
painter.setBrush(gradient);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, width(), height());
painter.setPen(Qt::NoPen);
painter.setBrush(m_color);
painter.drawRect(2, 2, width() - 4, height() - 2);
QFont font("Droid Sans Mono", 8);
painter.setFont(font);
painter.setPen(Qt::black);
QFontMetrics fm(font);
int textWidth = fm.horizontalAdvance(m_text);
int textHeight = fm.height();
int x = (width() - textWidth) / 2;
int y = (height() + textHeight) / 2;
painter.drawText(x, y, m_text);
// Add glossy effect
QLinearGradient glossGradient(0, 0, width(), height());
glossGradient.setColorAt(0, QColor(255, 255, 255, 150));
glossGradient.setColorAt(1, QColor(255, 255, 255, 0));
painter.setBrush(glossGradient);
painter.drawRect(0, 0, width(), height());
}