qt/LedOK/gutil/qgui.h

373 lines
11 KiB
C
Raw Normal View History

2023-04-18 14:14:46 +08:00
#ifndef QGUI_H
#define QGUI_H
#include <QComboBox>
#include <QStackedLayout>
2023-06-20 16:09:25 +08:00
#include <QSplitter>
2023-04-28 18:26:41 +08:00
#include <QListWidget>
2023-04-18 14:14:46 +08:00
#include <QTableWidget>
#include <QHeaderView>
2023-06-20 16:09:25 +08:00
#include <QLabel>
#include <QTextEdit>
2023-04-18 14:14:46 +08:00
2023-04-23 17:01:35 +08:00
#define MainMust \
#if(QT_VERSION_MAJOR > 5) \
QImageReader::setAllocationLimit(0);\
#else\
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);\
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);\
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);\
#endif
2023-04-18 14:14:46 +08:00
extern const int AlignRight;
inline int setCurrentData(QComboBox *combo, const QVariant &data) {
auto idx = combo->findData(data);
if(idx>-1) combo->setCurrentIndex(idx);
return idx;
}
2023-06-20 16:09:25 +08:00
inline void gFont(QWidget *wgt, int size, bool bold = false, bool italic = false) {
auto ft = wgt->font();
ft.setPixelSize(size);
if(bold) ft.setBold(true);
if(italic) ft.setItalic(true);
wgt->setFont(ft);
}
inline void gFont(QWidget *wgt, const QString &family, int size = 0, bool bold = false, bool italic = false) {
auto ft = wgt->font();
ft.setFamily(family);
if(size) ft.setPixelSize(size);
if(bold) ft.setBold(true);
if(italic) ft.setItalic(true);
wgt->setFont(ft);
}
inline QFont qfont(const QString &family, int pixelSize, bool bold = false, bool italic = false) {
QFont ft(family);
ft.setPixelSize(pixelSize);
if(bold) ft.setBold(true);
if(italic) ft.setItalic(true);
return ft;
}
inline void gAppendText(QTextEdit *wgt, const QString &text, const QColor &color) {
auto c0 = wgt->textColor();
wgt->setTextColor(color);
wgt->append(text);
wgt->setTextColor(c0);
}
2023-04-18 14:14:46 +08:00
class VBox : public QBoxLayout {
public:
inline VBox(QWidget *parent=nullptr) : QBoxLayout(TopToBottom, parent) {}
inline VBox(QBoxLayout *parent) : QBoxLayout(TopToBottom) {
parent->addLayout(this);
};
inline VBox(QStackedLayout *parent) : QBoxLayout(TopToBottom) {
setContentsMargins(0,0,0,0);
auto wgt = new QWidget;
wgt->setLayout(this);
parent->addWidget(wgt);
};
2023-06-20 16:09:25 +08:00
inline VBox(QSplitter *parent) : QBoxLayout(TopToBottom) {
setContentsMargins(0,0,0,0);
auto wgt = new QWidget;
wgt->setLayout(this);
parent->addWidget(wgt);
};
inline QLabel *addLabel(const QString &text) {
auto lb = new QLabel(text);
addWidget(lb);
return lb;
}
2023-04-18 14:14:46 +08:00
};
class HBox : public QBoxLayout {
public:
inline HBox(QWidget *parent=nullptr) : QBoxLayout(LeftToRight, parent) {}
inline HBox(QBoxLayout *parent) : QBoxLayout(LeftToRight) {
parent->addLayout(this);
};
inline HBox(QStackedLayout *parent) : QBoxLayout(LeftToRight) {
setContentsMargins(0,0,0,0);
auto wgt = new QWidget;
wgt->setLayout(this);
parent->addWidget(wgt);
};
2023-06-20 16:09:25 +08:00
inline HBox(QSplitter *parent) : QBoxLayout(LeftToRight) {
setContentsMargins(0,0,0,0);
auto wgt = new QWidget;
wgt->setLayout(this);
parent->addWidget(wgt);
};
inline QLabel *addLabel(const QString &text) {
auto lb = new QLabel(text);
addWidget(lb);
return lb;
}
2023-04-18 14:14:46 +08:00
};
class Grid : public QGridLayout {
public:
2023-04-28 18:26:41 +08:00
using QGridLayout::QGridLayout;
2023-04-18 14:14:46 +08:00
inline Grid(QBoxLayout *parent) {
parent->addLayout(this);
};
inline Grid(QStackedLayout *parent) {
auto wgt = new QWidget;
wgt->setLayout(this);
parent->addWidget(wgt);
};
};
2023-04-28 18:26:41 +08:00
class ListWgt : public QListWidget {
2023-04-18 14:14:46 +08:00
public:
2023-04-28 18:26:41 +08:00
using QListWidget::QListWidget;
2023-04-18 14:14:46 +08:00
2023-04-28 18:26:41 +08:00
using QListWidget::addItem;
inline auto addItem(const QString &text, const QVariant &value) {
auto item = new QListWidgetItem(text);
2023-04-18 14:14:46 +08:00
item->setData(Qt::UserRole, value);
2023-04-28 18:26:41 +08:00
insertItem(count(), item);
return this;
2023-04-18 14:14:46 +08:00
}
};
struct ColAttr {
2023-06-20 16:09:25 +08:00
ColAttr(QString field, QString text, int width=0, QHeaderView::ResizeMode resizeMode = QHeaderView::Interactive) : field(field), text(text), width(width), resizeMode(resizeMode) {}
ColAttr(QString field, QString text, QHeaderView::ResizeMode resizeMode) : field(field), text(text), resizeMode(resizeMode) {}
2023-04-18 14:14:46 +08:00
QString field;
QString text;
int width{0};
2023-06-20 16:09:25 +08:00
QHeaderView::ResizeMode resizeMode;
2023-04-18 14:14:46 +08:00
};
2023-04-28 18:26:41 +08:00
class Table : public QTableWidget {
2023-04-18 14:14:46 +08:00
Q_OBJECT
public:
2023-04-28 18:26:41 +08:00
using QTableWidget::QTableWidget;
2023-06-20 16:09:25 +08:00
Table() {}
2023-04-28 18:26:41 +08:00
Table(std::initializer_list<ColAttr> colAttrs, int rows = 0, QWidget *parent = 0);
2023-04-18 14:14:46 +08:00
2023-04-28 18:26:41 +08:00
inline auto setNoEdit() {
setEditTriggers(QAbstractItemView::NoEditTriggers);
return this;
}
2023-04-18 14:14:46 +08:00
inline auto setDefs() {
setSelectionBehavior(QTableWidget::SelectRows);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setAlternatingRowColors(true);
return this;
}
inline auto setColStretch() {
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return this;
}
inline auto setRowStretch() {
verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return this;
}
inline auto setColFit() {
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
inline auto setRowFit() {
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
2023-06-20 16:09:25 +08:00
inline auto setColWidth(int value, QHeaderView::ResizeMode mode = QHeaderView::Interactive) {
auto header = horizontalHeader();
header->setDefaultSectionSize(value);
if(mode!=QHeaderView::Interactive) header->setSectionResizeMode(mode);
2023-04-18 14:14:46 +08:00
return this;
}
2023-06-20 16:09:25 +08:00
inline auto setRowHeight(int value, QHeaderView::ResizeMode mode = QHeaderView::Interactive) {
auto header = verticalHeader();
header->setDefaultSectionSize(value);
if(mode!=QHeaderView::Interactive) header->setSectionResizeMode(mode);
2023-04-18 14:14:46 +08:00
return this;
}
2023-04-28 18:26:41 +08:00
inline auto setHeaderText(int col, QString text) {
2023-04-18 14:14:46 +08:00
auto item = horizontalHeaderItem(col);
if(item==0) setHorizontalHeaderItem(col, item = new QTableWidgetItem());
item->setText(text);
return item;
}
2023-04-28 18:26:41 +08:00
inline auto setHeaderText(QString column, QString text) {
auto col = mFieldMap[column];
return setHeaderText(col, text);
}
2023-04-18 14:14:46 +08:00
2023-06-20 16:09:25 +08:00
inline auto setVHeaderText(int row, QString text) {
auto item = verticalHeaderItem(row);
if(item==0) setVerticalHeaderItem(row, item = new QTableWidgetItem());
item->setText(text);
return item;
}
2023-04-18 14:14:46 +08:00
inline auto appendRow() {
auto value = rowCount();
setRowCount(value + 1);
return value;
}
2023-04-28 18:26:41 +08:00
using QTableWidget::item;
2023-04-18 14:14:46 +08:00
inline auto item(int row, QString column) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return item(row, col);
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
inline auto itemValid(int row, int col) {
auto anitem = item(row, col);
if(anitem==0) setItem(row, col, anitem = new QTableWidgetItem);
return anitem;
}
inline auto itemValid(int row, QString column) {
auto col = mFieldMap[column];
return itemValid(row, col);
}
using QTableWidget::setItem;
2023-04-18 14:14:46 +08:00
inline void setItem(int row, QString column, QTableWidgetItem *item) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
setItem(row, col, item);
}
inline auto text(int row, int col) {
auto itm = item(row, col);
if(itm==0) return QString();
return itm->text();
2023-04-18 14:14:46 +08:00
}
inline auto text(int row, QString column) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return text(row, col);
}
inline auto setText(int row, int col, const QString &text) {
auto itm = item(row, col);
if(itm) itm->setText(text);
else setItem(row, col, itm = new QTableWidgetItem(text));
return itm;
2023-04-18 14:14:46 +08:00
}
inline auto setText(int row, QString column, const QString &text) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return setText(row, col, text);
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
inline auto data(int row, int col) {
auto itm = item(row, col);
if(itm==0) return QVariant();
return itm->data(Qt::UserRole);
}
2023-04-18 14:14:46 +08:00
inline auto data(int row, QString column) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return data(row, col);
}
inline auto setData(int row, int col, const QVariant &value) {
auto itm = item(row, col);
if(itm==0) setItem(row, col, itm = new QTableWidgetItem);
itm->setData(Qt::UserRole, value);
return itm;
2023-04-18 14:14:46 +08:00
}
inline auto setData(int row, QString column, const QVariant &value) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return setData(row, col, value);
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
using QTableWidget::cellWidget;
2023-04-18 14:14:46 +08:00
inline auto cellWidget(int row, QString column) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
return cellWidget(row, col);
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
using QTableWidget::setCellWidget;
2023-04-18 14:14:46 +08:00
inline void setCellWidget(int row, QString column, QWidget *widget) {
auto col = mFieldMap[column];
2023-04-28 18:26:41 +08:00
setCellWidget(row, col, widget);
2023-04-18 14:14:46 +08:00
}
public Q_SLOTS:
inline void clearRows() {setRowCount(0);}
protected:
2023-06-20 16:09:25 +08:00
void resizeEvent(QResizeEvent *event) override;
void resizeSec();
2023-04-28 18:26:41 +08:00
QMap<QString,int> mFieldMap;
2023-06-20 16:09:25 +08:00
bool noStretch{true};
2023-04-18 14:14:46 +08:00
};
class ResizeEmitedWgt : public QWidget {
Q_OBJECT
public:
2023-04-28 18:26:41 +08:00
using QWidget::QWidget;
2023-04-18 14:14:46 +08:00
protected:
void resizeEvent(QResizeEvent *) override {emit resized();}
signals:
void resized();
};
template<class T>
class Wrp {
public:
T *obj;
Wrp(T *obj = nullptr){
this->obj = obj;
};
inline Wrp& operator()(T *obj){
this->obj = obj;
return *this;
}
inline Wrp& operator()(T *obj, QLayout *layout){
this->obj = obj;
layout->addWidget(obj);
return *this;
}
inline Wrp& addTo(QLayout *layout){
layout->addWidget(obj);
return *this;
}
inline Wrp& margin(int a){
obj->setMargin(a);
return *this;
}
inline Wrp& font(const QFont &font){
obj->setFont(font);
return *this;
}
2023-04-23 17:01:35 +08:00
inline Wrp& font(int size){
auto font = obj->font();
font.setPixelSize(size);
obj->setFont(font);
return *this;
}
2023-04-18 14:14:46 +08:00
inline Wrp& width(int w){
obj->setFixedWidth(w);
return *this;
}
inline Wrp& height(int h){
obj->setFixedHeight(h);
return *this;
}
inline Wrp& padding(int wAdd, int hAdd, int minW = 32, int minH = 16){
wAdd+=8;
hAdd+=8;
QSize size = obj->fontMetrics().size(Qt::TextShowMnemonic, obj->text());
int &rwidth = size.rwidth();
rwidth += wAdd;
if(rwidth < minW) rwidth = minW;
int &rheight = size.rheight();
rheight += hAdd;
if(rheight < minH) rheight = minH;
obj->setFixedSize(size);
return *this;
}
inline Wrp& alignC(){
obj->setAlignment(Qt::AlignCenter);
return *this;
}
inline Wrp& alignR(){
obj->setAlignment(Qt::AlignRight);
return *this;
}
};
#endif