214 lines
6.6 KiB
C++
214 lines
6.6 KiB
C++
#include "SingleTargetResultWidget.h"
|
|
|
|
#include <QAbstractItemView>
|
|
#include <QGridLayout>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QListView>
|
|
#include <QListWidget>
|
|
#include <QSizePolicy>
|
|
#include <QVBoxLayout>
|
|
|
|
namespace {
|
|
|
|
QLabel* MakeTextLabel(const QString& objectName, QWidget* parent)
|
|
{
|
|
QLabel* label = new QLabel(parent);
|
|
label->setObjectName(objectName);
|
|
label->setTextFormat(Qt::PlainText);
|
|
label->setWordWrap(false);
|
|
return label;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
SingleTargetResultWidget::SingleTargetResultWidget(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
setObjectName(QStringLiteral("singleTargetResultWidget"));
|
|
setMinimumSize(360, 360);
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
m_titleLabel = MakeTextLabel(QStringLiteral("singleTargetTitle"), this);
|
|
m_statusLabel = MakeTextLabel(QStringLiteral("singleTargetStatus"), this);
|
|
m_statusLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
auto* headerLayout = new QHBoxLayout();
|
|
headerLayout->setContentsMargins(0, 0, 0, 0);
|
|
headerLayout->setSpacing(8);
|
|
headerLayout->addWidget(m_titleLabel, 1);
|
|
headerLayout->addWidget(m_statusLabel, 0);
|
|
|
|
m_emptyLabel = MakeTextLabel(QStringLiteral("singleTargetEmpty"), this);
|
|
m_emptyLabel->setAlignment(Qt::AlignCenter);
|
|
m_emptyLabel->setVisible(false);
|
|
|
|
m_fieldLayout = new QGridLayout();
|
|
m_fieldLayout->setContentsMargins(0, 0, 0, 0);
|
|
m_fieldLayout->setHorizontalSpacing(16);
|
|
m_fieldLayout->setVerticalSpacing(10);
|
|
m_fieldLayout->setColumnStretch(0, 0);
|
|
m_fieldLayout->setColumnStretch(1, 1);
|
|
|
|
auto* mainLayout = new QVBoxLayout(this);
|
|
mainLayout->setContentsMargins(14, 12, 14, 12);
|
|
mainLayout->setSpacing(10);
|
|
mainLayout->addLayout(headerLayout);
|
|
mainLayout->addWidget(m_emptyLabel, 1);
|
|
mainLayout->addLayout(m_fieldLayout);
|
|
|
|
setStyleSheet(
|
|
"#singleTargetResultWidget {"
|
|
" background-color: rgb(31, 36, 45);"
|
|
" border: 1px solid rgb(68, 78, 92);"
|
|
" border-radius: 6px;"
|
|
"}"
|
|
"#singleTargetTitle {"
|
|
" color: rgb(245, 247, 250);"
|
|
" font-size: 22px;"
|
|
" font-weight: 600;"
|
|
"}"
|
|
"#singleTargetStatus {"
|
|
" color: rgb(255, 255, 255);"
|
|
" font-size: 18px;"
|
|
" font-weight: 600;"
|
|
" padding: 4px 12px;"
|
|
" border-radius: 4px;"
|
|
"}"
|
|
"#singleTargetEmpty {"
|
|
" color: rgb(172, 181, 194);"
|
|
" font-size: 22px;"
|
|
"}"
|
|
"#singleTargetFieldName {"
|
|
" color: rgb(153, 164, 178);"
|
|
" font-size: 18px;"
|
|
"}"
|
|
"#singleTargetFieldValue {"
|
|
" color: rgb(239, 242, 246);"
|
|
" font-size: 20px;"
|
|
" font-weight: 500;"
|
|
"}");
|
|
|
|
setTitle(QStringLiteral("目标结果"));
|
|
setStatus(QStringLiteral("未知"), false);
|
|
}
|
|
|
|
void SingleTargetResultWidget::setTitle(const QString& title)
|
|
{
|
|
const QString normalized = title.trimmed().isEmpty()
|
|
? QStringLiteral("目标结果")
|
|
: title.trimmed();
|
|
if (m_titleLabel->text() != normalized) {
|
|
m_titleLabel->setText(normalized);
|
|
}
|
|
}
|
|
|
|
void SingleTargetResultWidget::setStatus(const QString& text, bool warning)
|
|
{
|
|
const QString normalized = text.trimmed().isEmpty()
|
|
? QStringLiteral("未知")
|
|
: text.trimmed();
|
|
if (m_statusLabel->text() != normalized) {
|
|
m_statusLabel->setText(normalized);
|
|
}
|
|
if (!m_statusStyleInitialized || m_statusWarning != warning) {
|
|
m_statusWarning = warning;
|
|
m_statusStyleInitialized = true;
|
|
m_statusLabel->setStyleSheet(warning
|
|
? QStringLiteral("background-color: rgb(150, 55, 55);")
|
|
: QStringLiteral("background-color: rgb(73, 84, 99);"));
|
|
}
|
|
}
|
|
|
|
void SingleTargetResultWidget::setFields(const QList<QPair<QString, QString>>& fields)
|
|
{
|
|
if (fields.isEmpty()) {
|
|
if (m_emptyLabel->isHidden()) {
|
|
m_emptyLabel->setVisible(true);
|
|
}
|
|
} else if (!m_emptyLabel->isHidden()) {
|
|
m_emptyLabel->setVisible(false);
|
|
}
|
|
|
|
for (int i = 0; i < fields.size(); ++i) {
|
|
if (i >= m_fieldRows.size()) {
|
|
FieldRow row;
|
|
row.nameLabel = MakeTextLabel(
|
|
QStringLiteral("singleTargetFieldName"), this);
|
|
row.valueLabel = MakeTextLabel(
|
|
QStringLiteral("singleTargetFieldValue"), this);
|
|
row.valueLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
|
m_fieldLayout->addWidget(row.nameLabel, i, 0);
|
|
m_fieldLayout->addWidget(row.valueLabel, i, 1);
|
|
m_fieldRows.push_back(row);
|
|
}
|
|
|
|
FieldRow& row = m_fieldRows[i];
|
|
if (row.nameLabel->text() != fields[i].first) {
|
|
row.nameLabel->setText(fields[i].first);
|
|
}
|
|
if (row.valueLabel->text() != fields[i].second) {
|
|
row.valueLabel->setText(fields[i].second);
|
|
}
|
|
if (row.nameLabel->isHidden()) {
|
|
row.nameLabel->setVisible(true);
|
|
}
|
|
if (row.valueLabel->isHidden()) {
|
|
row.valueLabel->setVisible(true);
|
|
}
|
|
}
|
|
|
|
for (int i = fields.size(); i < m_fieldRows.size(); ++i) {
|
|
if (!m_fieldRows[i].nameLabel->isHidden()) {
|
|
m_fieldRows[i].nameLabel->setVisible(false);
|
|
}
|
|
if (!m_fieldRows[i].valueLabel->isHidden()) {
|
|
m_fieldRows[i].valueLabel->setVisible(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SingleTargetResultWidget::setEmptyText(const QString& text)
|
|
{
|
|
const QString normalized = text.trimmed().isEmpty()
|
|
? QStringLiteral("暂无结果")
|
|
: text.trimmed();
|
|
if (m_emptyLabel->text() != normalized) {
|
|
m_emptyLabel->setText(normalized);
|
|
}
|
|
if (m_emptyLabel->isHidden()) {
|
|
m_emptyLabel->setVisible(true);
|
|
}
|
|
clearFieldRows();
|
|
}
|
|
|
|
void SingleTargetResultWidget::setupListWidget(QListWidget* listWidget, const QSize& itemSize)
|
|
{
|
|
if (!listWidget) {
|
|
return;
|
|
}
|
|
|
|
listWidget->setViewMode(QListView::IconMode);
|
|
listWidget->setResizeMode(QListView::Adjust);
|
|
listWidget->setMovement(QListView::Static);
|
|
listWidget->setSelectionMode(QAbstractItemView::NoSelection);
|
|
listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
listWidget->setFlow(QListView::LeftToRight);
|
|
listWidget->setWrapping(false);
|
|
listWidget->setSpacing(0);
|
|
listWidget->setGridSize(itemSize);
|
|
}
|
|
|
|
void SingleTargetResultWidget::clearFieldRows()
|
|
{
|
|
for (FieldRow& row : m_fieldRows) {
|
|
if (!row.nameLabel->isHidden()) {
|
|
row.nameLabel->setVisible(false);
|
|
}
|
|
if (!row.valueLabel->isHidden()) {
|
|
row.valueLabel->setVisible(false);
|
|
}
|
|
}
|
|
}
|