163 lines
5.1 KiB
C++
163 lines
5.1 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, 168);
|
|
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(12);
|
|
m_fieldLayout->setVerticalSpacing(6);
|
|
|
|
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: 18px;"
|
|
" font-weight: 600;"
|
|
"}"
|
|
"#singleTargetStatus {"
|
|
" color: rgb(255, 255, 255);"
|
|
" font-size: 14px;"
|
|
" font-weight: 600;"
|
|
" padding: 3px 10px;"
|
|
" border-radius: 4px;"
|
|
"}"
|
|
"#singleTargetEmpty {"
|
|
" color: rgb(172, 181, 194);"
|
|
" font-size: 16px;"
|
|
"}"
|
|
"#singleTargetFieldName {"
|
|
" color: rgb(153, 164, 178);"
|
|
" font-size: 14px;"
|
|
"}"
|
|
"#singleTargetFieldValue {"
|
|
" color: rgb(239, 242, 246);"
|
|
" font-size: 15px;"
|
|
" font-weight: 500;"
|
|
"}");
|
|
|
|
setTitle(QStringLiteral("目标结果"));
|
|
setStatus(QStringLiteral("正常"), false);
|
|
}
|
|
|
|
void SingleTargetResultWidget::setTitle(const QString& title)
|
|
{
|
|
m_titleLabel->setText(title.trimmed().isEmpty() ? QStringLiteral("目标结果") : title.trimmed());
|
|
}
|
|
|
|
void SingleTargetResultWidget::setStatus(const QString& text, bool warning)
|
|
{
|
|
m_statusLabel->setText(text.trimmed().isEmpty() ? QStringLiteral("未知") : text.trimmed());
|
|
m_statusLabel->setStyleSheet(warning
|
|
? QStringLiteral("background-color: rgb(190, 71, 61);")
|
|
: QStringLiteral("background-color: rgb(45, 137, 92);"));
|
|
}
|
|
|
|
void SingleTargetResultWidget::setFields(const QList<QPair<QString, QString>>& fields)
|
|
{
|
|
clearFieldRows();
|
|
m_emptyLabel->setVisible(fields.isEmpty());
|
|
|
|
const int columns = 2;
|
|
for (int i = 0; i < fields.size(); ++i) {
|
|
const int row = i / columns;
|
|
const int column = (i % columns) * 2;
|
|
|
|
QLabel* name = MakeTextLabel(QStringLiteral("singleTargetFieldName"), this);
|
|
name->setText(fields[i].first);
|
|
QLabel* value = MakeTextLabel(QStringLiteral("singleTargetFieldValue"), this);
|
|
value->setText(fields[i].second);
|
|
value->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
m_fieldLayout->addWidget(name, row, column);
|
|
m_fieldLayout->addWidget(value, row, column + 1);
|
|
m_fieldWidgets.push_back(name);
|
|
m_fieldWidgets.push_back(value);
|
|
}
|
|
}
|
|
|
|
void SingleTargetResultWidget::setEmptyText(const QString& text)
|
|
{
|
|
m_emptyLabel->setText(text.trimmed().isEmpty() ? QStringLiteral("暂无结果") : text.trimmed());
|
|
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 (QWidget* widget : m_fieldWidgets) {
|
|
m_fieldLayout->removeWidget(widget);
|
|
delete widget;
|
|
}
|
|
m_fieldWidgets.clear();
|
|
}
|