feat(production): 界面表格优化
This commit is contained in:
+120
-1
@@ -1,9 +1,12 @@
|
||||
#include "uihelpers.h"
|
||||
|
||||
#include <QAbstractItemView>
|
||||
#include <QEvent>
|
||||
#include <QFrame>
|
||||
#include <QFontMetrics>
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QObject>
|
||||
#include <QSize>
|
||||
#include <QSizePolicy>
|
||||
#include <QTableWidget>
|
||||
@@ -26,6 +29,105 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
QFrame *makeCard(const QString &title)
|
||||
{
|
||||
QFrame *card = new QFrame;
|
||||
@@ -74,7 +176,6 @@ QTableWidget *makeTable(const QStringList &headers)
|
||||
{
|
||||
QTableWidget *table = new QTableWidget(0, headers.size());
|
||||
table->setHorizontalHeaderLabels(headers);
|
||||
table->horizontalHeader()->setStretchLastSection(true);
|
||||
table->verticalHeader()->setVisible(false);
|
||||
table->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
@@ -82,6 +183,24 @@ QTableWidget *makeTable(const QStringList &headers)
|
||||
return table;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void addRow(QTableWidget *table, const QStringList &values)
|
||||
{
|
||||
const int row = table->rowCount();
|
||||
|
||||
Reference in New Issue
Block a user