84 lines
2.7 KiB
C
84 lines
2.7 KiB
C
|
#ifndef TABLE_H
|
||
|
#define TABLE_H
|
||
|
|
||
|
#include <QTableWidget>
|
||
|
#include <QHeaderView>
|
||
|
|
||
|
struct ColAttr {
|
||
|
QString field;
|
||
|
QString text;
|
||
|
int width{0};
|
||
|
};
|
||
|
class Table : public QTableWidget {
|
||
|
Q_OBJECT
|
||
|
public:
|
||
|
enum ResizeMode {
|
||
|
Stretch = -0x1000000,
|
||
|
ResizeToContents
|
||
|
};
|
||
|
explicit Table(QWidget *parent = nullptr) : QTableWidget{parent} {}
|
||
|
Table(std::initializer_list<ColAttr> colAttrs, QWidget *parent = nullptr) : QTableWidget{0, (int)colAttrs.size(), parent} {
|
||
|
int i = 0;
|
||
|
for(typename std::initializer_list<ColAttr>::const_iterator it = colAttrs.begin(); it != colAttrs.end(); ++it) {
|
||
|
auto item = horizontalHeaderItem(i);
|
||
|
if(!item) {
|
||
|
item = new QTableWidgetItem();
|
||
|
item->setData(0x99, it->width);
|
||
|
setHorizontalHeaderItem(i, item);
|
||
|
}
|
||
|
item->setText(it->text);
|
||
|
if(it->width > 0) horizontalHeader()->resizeSection(i, it->width);
|
||
|
else if(it->width < 0) {
|
||
|
if(it->width == Stretch) horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
|
||
|
if(it->width == ResizeToContents) horizontalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents);
|
||
|
}
|
||
|
mFieldMap.insert(it->field, i++);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Table *setDefs() {
|
||
|
setSelectionBehavior(QTableWidget::SelectRows);
|
||
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||
|
setAlternatingRowColors(true);
|
||
|
horizontalHeader()->setBackgroundRole(QPalette::Window);
|
||
|
return this;
|
||
|
}
|
||
|
inline Table *setStretch() {
|
||
|
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||
|
return this;
|
||
|
}
|
||
|
inline Table *setResizeToContents() {
|
||
|
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
||
|
return this;
|
||
|
}
|
||
|
|
||
|
auto item(int row, QString column) {
|
||
|
auto col = mFieldMap[column];
|
||
|
return QTableWidget::item(row, col);
|
||
|
}
|
||
|
void setItem(int row, QString column, QTableWidgetItem *item) {
|
||
|
auto col = mFieldMap[column];
|
||
|
QTableWidget::setItem(row, col, item);
|
||
|
}
|
||
|
void setValue(int row, QString column, const QString &text) {
|
||
|
auto col = mFieldMap[column];
|
||
|
QTableWidget::setItem(row, col, new QTableWidgetItem(text));
|
||
|
}
|
||
|
|
||
|
auto cellWidget(int row, QString column) {
|
||
|
auto col = mFieldMap[column];
|
||
|
return QTableWidget::cellWidget(row, col);
|
||
|
}
|
||
|
void setCellWidget(int row, QString column, QWidget *widget) {
|
||
|
auto col = mFieldMap[column];
|
||
|
QTableWidget::setCellWidget(row, col, widget);
|
||
|
}
|
||
|
|
||
|
QMap<QString,int> mFieldMap;
|
||
|
|
||
|
protected:
|
||
|
int sizeHintForColumn(int column) const override;
|
||
|
};
|
||
|
|
||
|
#endif // TABLE_H
|