2023-04-18 14:14:46 +08:00
|
|
|
#include "qgui.h"
|
2023-06-20 16:09:25 +08:00
|
|
|
#include <QResizeEvent>
|
2023-04-18 14:14:46 +08:00
|
|
|
|
|
|
|
const int AlignRight = Qt::AlignRight | Qt::AlignVCenter;
|
|
|
|
|
2023-04-28 18:26:41 +08:00
|
|
|
Table::Table(std::initializer_list<ColAttr> colAttrs, int rows, QWidget *parent) : QTableWidget{rows, (int)colAttrs.size(), parent} {
|
2023-04-18 14:14:46 +08:00
|
|
|
int i = 0;
|
|
|
|
for(typename std::initializer_list<ColAttr>::const_iterator it = colAttrs.begin(); it != colAttrs.end(); ++it) {
|
|
|
|
auto item = horizontalHeaderItem(i);
|
|
|
|
if(item==0) setHorizontalHeaderItem(i, item = new QTableWidgetItem());
|
|
|
|
item->setText(it->text);
|
|
|
|
if(it->width > 0) horizontalHeader()->resizeSection(i, it->width);
|
2023-06-20 16:09:25 +08:00
|
|
|
if(it->resizeMode != QHeaderView::Interactive) {
|
|
|
|
if(it->resizeMode==QHeaderView::Stretch && it->width>0) {
|
|
|
|
item->setData(0x99, it->width > 0 ? it->width : 100);
|
|
|
|
noStretch = false;
|
|
|
|
} else horizontalHeader()->setSectionResizeMode(i, (QHeaderView::ResizeMode)it->resizeMode);
|
|
|
|
}
|
2023-08-07 09:04:53 +08:00
|
|
|
mFieldMap.emplace(it->field, i++);
|
2023-04-18 14:14:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 16:09:25 +08:00
|
|
|
void Table::resizeEvent(QResizeEvent *event) {
|
|
|
|
QTableWidget::resizeEvent(event);
|
|
|
|
if(noStretch || event->size().width() == event->oldSize().width()) return;
|
|
|
|
resizeSec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Table::resizeSec() {
|
2023-04-18 14:14:46 +08:00
|
|
|
auto header = horizontalHeader();
|
2023-06-20 16:09:25 +08:00
|
|
|
int colCnt = columnCount(), remainWidth = header->width(), stretchWidth = 0, secWidth;
|
|
|
|
QTableWidgetItem *item;
|
|
|
|
for(int cc=0; cc<colCnt; cc++) if((item = horizontalHeaderItem(cc))) {
|
2023-08-07 09:04:53 +08:00
|
|
|
if((secWidth = item->data(0x99).toInt()) > 0) stretchWidth += secWidth;
|
|
|
|
else remainWidth -= header->sectionSize(cc);
|
|
|
|
}
|
2023-06-20 16:09:25 +08:00
|
|
|
if(remainWidth<=0 || stretchWidth==0) return;
|
|
|
|
for(int cc=0; cc<colCnt; cc++) if((item = horizontalHeaderItem(cc)) && (secWidth = item->data(0x99).toInt()) > 0) header->resizeSection(cc, secWidth * remainWidth / stretchWidth);
|
2023-04-18 14:14:46 +08:00
|
|
|
}
|
2023-08-07 09:04:53 +08:00
|
|
|
|
|
|
|
void Table::updateGeometries() {
|
|
|
|
QTableWidget::updateGeometries();
|
|
|
|
emit updGeos();
|
|
|
|
}
|