Files
Qt_keyboard/DEBUG/Debug_Panel.cpp

207 lines
7.7 KiB
C++
Raw Normal View History

2026-04-03 09:26:10 +08:00
#include "DEBUG/Debug_Panel.h"
#include "APP/APP_Theme.h"
#include <QtCore/QRegularExpression>
#include <QtGui/QFontDatabase>
#include <QtGui/QRegularExpressionValidator>
#include <QtWidgets/QFrame>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QVBoxLayout>
namespace DEBUG {
namespace
{
void Debug_Func_SetLabelTextColor(QLabel* p_Label, const QColor& Color)
{
QPalette Palette = p_Label->palette();
Palette.setColor(QPalette::WindowText, Color);
p_Label->setPalette(Palette);
}
bool Debug_Func_ParseHexField(QString Text, quint16* p_Value)
{
Text = Text.trimmed();
if (Text.startsWith(QStringLiteral("0x"), Qt::CaseInsensitive))
{
Text.remove(0, 2);
}
if (Text.isEmpty())
{
return false;
}
bool IsOk = false;
const quint16 Value = static_cast<quint16>(Text.toUShort(&IsOk, 16));
if (IsOk)
{
*p_Value = Value;
}
return IsOk;
}
QLabel* Debug_Func_CreateLabel(QWidget* parent, const QString& Text)
{
QLabel* p_Label = new QLabel(Text, parent);
p_Label->setWordWrap(true);
p_Label->setFont(APP::APP_Theme::App_Func_GetBodyFont());
p_Label->setAttribute(Qt::WA_TranslucentBackground, true);
return p_Label;
}
} // namespace
Debug_Panel::Debug_Panel(QWidget* parent)
: APP::APP_GlassCard(parent)
{
QVBoxLayout* p_RootLayout = new QVBoxLayout(this);
p_RootLayout->setContentsMargins(20, 18, 20, 20);
p_RootLayout->setSpacing(12);
QHBoxLayout* p_HeaderLayout = new QHBoxLayout();
p_HeaderLayout->setContentsMargins(0, 0, 0, 0);
p_HeaderLayout->setSpacing(10);
QLabel* p_TitleLabel = new QLabel(QStringLiteral("设备协议调试窗口"), this);
p_TitleLabel->setWordWrap(true);
p_TitleLabel->setFont(APP::APP_Theme::App_Func_GetMetricFont());
p_TitleLabel->setAttribute(Qt::WA_TranslucentBackground, true);
p_HeaderLayout->addWidget(p_TitleLabel, 1);
debugButtonRefresh = new QPushButton(QStringLiteral("刷新设备"), this);
debugButtonClear = new QPushButton(QStringLiteral("清空日志"), this);
p_HeaderLayout->addWidget(debugButtonRefresh);
p_HeaderLayout->addWidget(debugButtonClear);
p_RootLayout->addLayout(p_HeaderLayout);
const QRegularExpression HexPattern(QStringLiteral("^(?:0[xX])?[0-9A-Fa-f]{0,4}$"));
QHBoxLayout* p_ConfigLayout = new QHBoxLayout();
p_ConfigLayout->setContentsMargins(0, 0, 0, 0);
p_ConfigLayout->setSpacing(8);
p_ConfigLayout->addWidget(Debug_Func_CreateLabel(this, QStringLiteral("目标 VID")));
debugEditVendorId = new QLineEdit(this);
debugEditVendorId->setMaxLength(6);
debugEditVendorId->setClearButtonEnabled(true);
debugEditVendorId->setMinimumWidth(92);
debugEditVendorId->setAlignment(Qt::AlignCenter);
debugEditVendorId->setValidator(new QRegularExpressionValidator(HexPattern, debugEditVendorId));
debugEditVendorId->setPlaceholderText(QStringLiteral("1209"));
p_ConfigLayout->addWidget(debugEditVendorId);
p_ConfigLayout->addWidget(Debug_Func_CreateLabel(this, QStringLiteral("目标 PID")));
debugEditProductId = new QLineEdit(this);
debugEditProductId->setMaxLength(6);
debugEditProductId->setClearButtonEnabled(true);
debugEditProductId->setMinimumWidth(92);
debugEditProductId->setAlignment(Qt::AlignCenter);
debugEditProductId->setValidator(new QRegularExpressionValidator(HexPattern, debugEditProductId));
debugEditProductId->setPlaceholderText(QStringLiteral("0001"));
p_ConfigLayout->addWidget(debugEditProductId);
debugButtonApplyConfig = new QPushButton(QStringLiteral("应用目标"), this);
p_ConfigLayout->addWidget(debugButtonApplyConfig);
p_ConfigLayout->addStretch(1);
p_RootLayout->addLayout(p_ConfigLayout);
QHBoxLayout* p_CommandLayout = new QHBoxLayout();
p_CommandLayout->setContentsMargins(0, 0, 0, 0);
p_CommandLayout->setSpacing(8);
debugButtonSyncTime = new QPushButton(QStringLiteral("时间同步"), this);
debugButtonModeSwitch = new QPushButton(QStringLiteral("切换颜色"), this);
p_CommandLayout->addWidget(debugButtonSyncTime);
p_CommandLayout->addWidget(debugButtonModeSwitch);
p_CommandLayout->addStretch(1);
p_RootLayout->addLayout(p_CommandLayout);
debugLabelConfigStatus = new QLabel(QStringLiteral("当前目标由上层初始化。"), this);
debugLabelConfigStatus->setWordWrap(true);
debugLabelConfigStatus->setFont(APP::APP_Theme::App_Func_GetBodyFont());
debugLabelConfigStatus->setAttribute(Qt::WA_TranslucentBackground, true);
p_RootLayout->addWidget(debugLabelConfigStatus);
debugLabelConnection = new QLabel(QStringLiteral("未连接。"), this);
debugLabelConnection->setWordWrap(true);
debugLabelConnection->setFont(APP::APP_Theme::App_Func_GetBodyFont());
debugLabelConnection->setAttribute(Qt::WA_TranslucentBackground, true);
p_RootLayout->addWidget(debugLabelConnection);
QLabel* p_LogTitleLabel = new QLabel(QStringLiteral("调试日志(原始包 + 语义解析)"), this);
p_LogTitleLabel->setWordWrap(true);
p_LogTitleLabel->setFont(APP::APP_Theme::App_Func_GetTitleFont());
p_LogTitleLabel->setAttribute(Qt::WA_TranslucentBackground, true);
p_RootLayout->addWidget(p_LogTitleLabel);
debugEditLog = new QPlainTextEdit(this);
debugEditLog->setReadOnly(true);
debugEditLog->setMinimumHeight(300);
debugEditLog->setLineWrapMode(QPlainTextEdit::NoWrap);
debugEditLog->setFrameShape(QFrame::NoFrame);
debugEditLog->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
debugEditLog->setPlainText(QStringLiteral("等待收到输入包。"));
p_RootLayout->addWidget(debugEditLog, 1);
}
QPushButton* Debug_Panel::Debug_Func_GetRefreshButton() const { return debugButtonRefresh; }
QPushButton* Debug_Panel::Debug_Func_GetClearButton() const { return debugButtonClear; }
QPushButton* Debug_Panel::Debug_Func_GetApplyConfigButton() const { return debugButtonApplyConfig; }
QPushButton* Debug_Panel::Debug_Func_GetSyncTimeButton() const { return debugButtonSyncTime; }
QPushButton* Debug_Panel::Debug_Func_GetModeSwitchButton() const { return debugButtonModeSwitch; }
void Debug_Panel::Debug_Func_SetDeviceConfigText(
quint16 VendorId,
quint16 ProductId)
{
debugEditVendorId->setText(QStringLiteral("%1").arg(VendorId, 4, 16, QLatin1Char('0')).toUpper());
debugEditProductId->setText(QStringLiteral("%1").arg(ProductId, 4, 16, QLatin1Char('0')).toUpper());
}
bool Debug_Panel::Debug_Func_TryGetDeviceConfig(
quint16* p_VendorId,
quint16* p_ProductId) const
{
return Debug_Func_ParseHexField(debugEditVendorId->text(), p_VendorId) &&
Debug_Func_ParseHexField(debugEditProductId->text(), p_ProductId);
}
void Debug_Panel::Debug_Func_SetConfigStatusText(const QString& Text, bool IsOk)
{
debugLabelConfigStatus->setText(Text);
Debug_Func_SetLabelTextColor(
debugLabelConfigStatus,
IsOk ? QColor(98, 198, 164) : QColor(214, 110, 102));
}
void Debug_Panel::Debug_Func_SetConnectionText(const QString& Text, bool IsConnected)
{
debugLabelConnection->setText(Text);
Debug_Func_SetLabelTextColor(
debugLabelConnection,
IsConnected ? QColor(98, 198, 164) : QColor(214, 110, 102));
}
void Debug_Panel::Debug_Func_SetLogText(const QString& Text)
{
const QString DisplayText = Text.isEmpty()
? QStringLiteral("等待收到输入包。")
: Text;
if (debugEditLog->toPlainText() == DisplayText)
{
return;
}
debugEditLog->setPlainText(DisplayText);
debugEditLog->verticalScrollBar()->setValue(debugEditLog->verticalScrollBar()->maximum());
}
} // namespace DEBUG