2026-08-18 09:40:43 +08:00
|
|
|
#include "uihelpers.h"
|
|
|
|
|
|
|
|
|
|
#include <QAbstractItemView>
|
2026-11-05 14:14:29 +08:00
|
|
|
#include <QEvent>
|
2026-08-18 09:40:43 +08:00
|
|
|
#include <QFrame>
|
2026-11-05 14:14:29 +08:00
|
|
|
#include <QFontMetrics>
|
2026-08-18 09:40:43 +08:00
|
|
|
#include <QHeaderView>
|
|
|
|
|
#include <QLabel>
|
2026-11-05 14:14:29 +08:00
|
|
|
#include <QObject>
|
2026-08-18 09:40:43 +08:00
|
|
|
#include <QSize>
|
|
|
|
|
#include <QSizePolicy>
|
|
|
|
|
#include <QTableWidget>
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
|
|
namespace UiHelpers {
|
|
|
|
|
|
|
|
|
|
class SquareLabel : public QLabel
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit SquareLabel(const QString &text) : QLabel(text) {}
|
|
|
|
|
|
|
|
|
|
bool hasHeightForWidth() const override { return true; }
|
|
|
|
|
int heightForWidth(int width) const override { return width; }
|
|
|
|
|
|
|
|
|
|
QSize sizeHint() const override
|
|
|
|
|
{
|
|
|
|
|
const int side = qMax(QLabel::sizeHint().width(), QLabel::sizeHint().height());
|
|
|
|
|
return QSize(side, side);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-05 14:14:29 +08:00
|
|
|
class ResponsiveTableColumns : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ResponsiveTableColumns(QTableWidget *table, const QList<int> &baseWidths,
|
|
|
|
|
int cappedColumn, int maximumSharePercent)
|
|
|
|
|
: QObject(table)
|
|
|
|
|
, m_table(table)
|
|
|
|
|
, m_baseWidths(baseWidths)
|
|
|
|
|
, m_cappedColumn(cappedColumn)
|
|
|
|
|
, m_maximumSharePercent(qBound(1, maximumSharePercent, 100))
|
|
|
|
|
{
|
|
|
|
|
table->installEventFilter(this);
|
|
|
|
|
table->viewport()->installEventFilter(this);
|
|
|
|
|
applyWidths();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool eventFilter(QObject *watched, QEvent *event) override
|
|
|
|
|
{
|
|
|
|
|
if ((watched == m_table || watched == m_table->viewport())
|
|
|
|
|
&& (event->type() == QEvent::Resize
|
|
|
|
|
|| event->type() == QEvent::Show
|
|
|
|
|
|| event->type() == QEvent::LayoutRequest)) {
|
|
|
|
|
applyWidths();
|
|
|
|
|
}
|
|
|
|
|
return QObject::eventFilter(watched, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void applyWidths()
|
|
|
|
|
{
|
|
|
|
|
if (m_applying || !m_table || m_baseWidths.size() != m_table->columnCount()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const int availableWidth = m_table->viewport()->width();
|
|
|
|
|
if (availableWidth <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int baseTotal = 0;
|
|
|
|
|
for (int width : m_baseWidths) {
|
|
|
|
|
baseTotal += qMax(1, width);
|
|
|
|
|
}
|
|
|
|
|
const int layoutWidth = qMax(availableWidth, baseTotal);
|
|
|
|
|
QList<int> widths;
|
|
|
|
|
int allocated = 0;
|
|
|
|
|
for (int width : m_baseWidths) {
|
|
|
|
|
const int scaled = qMax(1, int(qint64(qMax(1, width)) * layoutWidth / baseTotal));
|
|
|
|
|
widths.append(scaled);
|
|
|
|
|
allocated += scaled;
|
|
|
|
|
}
|
|
|
|
|
for (int column = 0; allocated < layoutWidth; ++column, ++allocated) {
|
|
|
|
|
widths[column % widths.size()] += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_cappedColumn >= 0 && m_cappedColumn < widths.size()) {
|
|
|
|
|
const int maximumWidth = layoutWidth * m_maximumSharePercent / 100;
|
|
|
|
|
const int surplus = qMax(0, widths.at(m_cappedColumn) - maximumWidth);
|
|
|
|
|
const int otherBaseTotal = baseTotal - qMax(1, m_baseWidths.at(m_cappedColumn));
|
|
|
|
|
if (surplus > 0 && otherBaseTotal > 0) {
|
|
|
|
|
widths[m_cappedColumn] = maximumWidth;
|
|
|
|
|
int distributed = 0;
|
|
|
|
|
for (int column = 0; column < widths.size(); ++column) {
|
|
|
|
|
if (column == m_cappedColumn) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const int addition = int(
|
|
|
|
|
qint64(surplus) * qMax(1, m_baseWidths.at(column)) / otherBaseTotal);
|
|
|
|
|
widths[column] += addition;
|
|
|
|
|
distributed += addition;
|
|
|
|
|
}
|
|
|
|
|
for (int column = 0; distributed < surplus; ++column) {
|
|
|
|
|
const int target = column % widths.size();
|
|
|
|
|
if (target == m_cappedColumn) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
widths[target] += 1;
|
|
|
|
|
++distributed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_applying = true;
|
|
|
|
|
QHeaderView *header = m_table->horizontalHeader();
|
|
|
|
|
for (int column = 0; column < widths.size(); ++column) {
|
|
|
|
|
if (header->sectionSize(column) != widths.at(column)) {
|
|
|
|
|
header->resizeSection(column, widths.at(column));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_applying = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTableWidget *m_table;
|
|
|
|
|
QList<int> m_baseWidths;
|
|
|
|
|
int m_cappedColumn;
|
|
|
|
|
int m_maximumSharePercent;
|
|
|
|
|
bool m_applying = false;
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-18 09:40:43 +08:00
|
|
|
QFrame *makeCard(const QString &title)
|
|
|
|
|
{
|
|
|
|
|
QFrame *card = new QFrame;
|
|
|
|
|
card->setObjectName("card");
|
|
|
|
|
card->setFrameShape(QFrame::StyledPanel);
|
|
|
|
|
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(card);
|
|
|
|
|
layout->setContentsMargins(12, 10, 12, 12);
|
|
|
|
|
layout->setSpacing(8);
|
|
|
|
|
|
|
|
|
|
QLabel *titleLabel = new QLabel(title);
|
|
|
|
|
titleLabel->setObjectName("cardTitle");
|
|
|
|
|
layout->addWidget(titleLabel);
|
|
|
|
|
return card;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLabel *makeBadge(const QString &text, const QString &color)
|
|
|
|
|
{
|
|
|
|
|
QLabel *label = new QLabel(text);
|
|
|
|
|
label->setAlignment(Qt::AlignCenter);
|
|
|
|
|
label->setMinimumHeight(26);
|
|
|
|
|
label->setStyleSheet(QString("QLabel { color: white; background: %1; border-radius: 4px; padding: 4px 10px; }").arg(color));
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLabel *makeStatusCell(const QString &text, const QString &color)
|
|
|
|
|
{
|
|
|
|
|
QLabel *label = new QLabel(text);
|
|
|
|
|
label->setAlignment(Qt::AlignCenter);
|
|
|
|
|
label->setMinimumSize(34, 30);
|
|
|
|
|
label->setStyleSheet(QString("QLabel { background: %1; border: 1px solid #ffffff; border-radius: 3px; color: white; font-size: 11px; }").arg(color));
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QLabel *makeSquareStatusCell(const QString &text, const QString &color)
|
|
|
|
|
{
|
|
|
|
|
QLabel *label = new SquareLabel(text);
|
|
|
|
|
label->setAlignment(Qt::AlignCenter);
|
|
|
|
|
label->setMinimumSize(60, 60);
|
|
|
|
|
label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
|
label->setStyleSheet(QString("QLabel { background: %1; border: 1px solid #ffffff; border-radius: 3px; color: white; font-size: 11px; }").arg(color));
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTableWidget *makeTable(const QStringList &headers)
|
|
|
|
|
{
|
|
|
|
|
QTableWidget *table = new QTableWidget(0, headers.size());
|
|
|
|
|
table->setHorizontalHeaderLabels(headers);
|
|
|
|
|
table->verticalHeader()->setVisible(false);
|
|
|
|
|
table->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
|
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
|
table->setAlternatingRowColors(true);
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 14:14:29 +08:00
|
|
|
int textColumnWidth(const QTableWidget *table, const QString &text)
|
|
|
|
|
{
|
|
|
|
|
return table ? QFontMetrics(table->font()).horizontalAdvance(text) + 28 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void configureTableColumns(QTableWidget *table, const QList<int> &baseWidths,
|
|
|
|
|
int cappedColumn, int maximumSharePercent)
|
|
|
|
|
{
|
|
|
|
|
if (!table || table->columnCount() <= 0
|
|
|
|
|
|| baseWidths.size() != table->columnCount()) return;
|
|
|
|
|
QHeaderView *header = table->horizontalHeader();
|
|
|
|
|
header->setStretchLastSection(false);
|
|
|
|
|
header->setMinimumSectionSize(1);
|
|
|
|
|
header->setSectionResizeMode(QHeaderView::Fixed);
|
|
|
|
|
table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
|
|
|
|
|
new ResponsiveTableColumns(table, baseWidths, cappedColumn, maximumSharePercent);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 09:40:43 +08:00
|
|
|
void addRow(QTableWidget *table, const QStringList &values)
|
|
|
|
|
{
|
|
|
|
|
const int row = table->rowCount();
|
|
|
|
|
table->insertRow(row);
|
|
|
|
|
for (int col = 0; col < values.size(); ++col) {
|
|
|
|
|
table->setItem(row, col, new QTableWidgetItem(values.at(col)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|