Files
Qt_keyboard/APP/APP_KeyButton.cpp

172 lines
4.1 KiB
C++
Raw Permalink Normal View History

2026-04-03 09:26:10 +08:00
#include "APP/APP_KeyButton.h"
#include "APP/APP_Theme.h"
#include <QtCore/QtMath>
#include <QtGui/QPainter>
namespace {
QColor App_Func_MixColor(const QColor& Left, const QColor& Right, qreal Value)
{
const qreal Rate = qBound(0.0, Value, 1.0);
return QColor(
qRound(Left.red() + (Right.red() - Left.red()) * Rate),
qRound(Left.green() + (Right.green() - Left.green()) * Rate),
qRound(Left.blue() + (Right.blue() - Left.blue()) * Rate),
qRound(Left.alpha() + (Right.alpha() - Left.alpha()) * Rate));
}
} // namespace
namespace APP {
APP_KeyButton::APP_KeyButton(const APP_KeyInfo& KeyInfo, QWidget* parent)
: QPushButton(parent),
appKeyInfo(KeyInfo)
{
appHintText = appKeyInfo.hint;
setCursor(Qt::PointingHandCursor);
setFlat(true);
setMinimumSize(78, 78);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
void APP_KeyButton::App_Func_SetLatched(bool IsLatched)
{
if (appIsLatched == IsLatched)
{
return;
}
appIsLatched = IsLatched;
update();
}
void APP_KeyButton::App_Func_SetPressed(bool IsPressed)
{
if (appIsPressed == IsPressed)
{
return;
}
appIsPressed = IsPressed;
update();
}
void APP_KeyButton::App_Func_SetHintText(const QString& HintText)
{
if (appHintText == HintText)
{
return;
}
appHintText = HintText;
update();
}
void APP_KeyButton::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
const QRectF ButtonRect = rect().adjusted(6.0, 6.0, -6.0, -6.0);
const qreal Radius = 14.0;
QColor FillColor = App_Func_GetBackgroundColor();
QColor OutlineColor = App_Func_GetBorderColor();
if (isDown())
{
FillColor = FillColor.darker(108);
OutlineColor = OutlineColor.darker(112);
}
QPainter Painter(this);
Painter.setRenderHint(QPainter::Antialiasing, true);
Painter.setRenderHint(QPainter::TextAntialiasing, true);
Painter.setPen(QPen(OutlineColor, 1.2));
Painter.setBrush(FillColor);
Painter.drawRoundedRect(ButtonRect, Radius, Radius);
if (!appHintText.isEmpty())
{
Painter.setFont(APP_Theme::App_Func_GetKeyHintFont());
Painter.setPen(App_Func_GetTextColor().lighter(115));
Painter.drawText(
ButtonRect.adjusted(10.0, 8.0, -10.0, -8.0),
Qt::AlignLeft | Qt::AlignTop,
appHintText.toUpper());
}
QFont LabelFont = APP_Theme::App_Func_GetKeyLabelFont();
if (appKeyInfo.label.size() > 2)
{
LabelFont.setPointSize(LabelFont.pointSize() - 4);
}
else if (appKeyInfo.label.size() == 2)
{
LabelFont.setPointSize(LabelFont.pointSize() - 2);
}
Painter.setFont(LabelFont);
Painter.setPen(App_Func_GetTextColor());
Painter.drawText(ButtonRect, Qt::AlignCenter, appKeyInfo.label);
}
QColor APP_KeyButton::App_Func_GetAccentColor() const
{
switch (appKeyInfo.tone)
{
case APP_KeyTone::Aqua:
return QColor(72, 184, 162);
case APP_KeyTone::Amber:
return QColor(224, 172, 76);
case APP_KeyTone::Blue:
return QColor(103, 146, 224);
case APP_KeyTone::Normal:
default:
return QColor(150, 168, 196);
}
}
QColor APP_KeyButton::App_Func_GetBackgroundColor() const
{
const QColor BaseColor(55, 61, 70);
if (appIsPressed)
{
return App_Func_MixColor(BaseColor, App_Func_GetAccentColor(), 0.56);
}
if (appKeyInfo.tone != APP_KeyTone::Normal || appIsLatched)
{
return App_Func_MixColor(BaseColor, App_Func_GetAccentColor(), appIsLatched ? 0.35 : 0.18);
}
return BaseColor;
}
QColor APP_KeyButton::App_Func_GetBorderColor() const
{
const QColor BaseColor(104, 114, 126);
if (appIsPressed)
{
return App_Func_MixColor(BaseColor, App_Func_GetAccentColor(), 0.72);
}
if (appKeyInfo.tone != APP_KeyTone::Normal || appIsLatched)
{
return App_Func_MixColor(BaseColor, App_Func_GetAccentColor(), appIsLatched ? 0.45 : 0.25);
}
return BaseColor;
}
QColor APP_KeyButton::App_Func_GetTextColor() const
{
return QColor(238, 242, 247);
}
} // namespace APP