qt/LedOK/gutil/qgui.h

644 lines
20 KiB
C
Raw Permalink Normal View History

2023-04-18 14:14:46 +08:00
#ifndef QGUI_H
#define QGUI_H
#include <QComboBox>
2023-09-19 11:49:20 +08:00
#include <QDialog>
#include <QDialogButtonBox>
2023-04-18 14:14:46 +08:00
#include <QHeaderView>
2023-06-20 16:09:25 +08:00
#include <QLabel>
2023-08-07 09:04:53 +08:00
#include <QListWidget>
#include <QSplitter>
#include <QStackedLayout>
#include <QTableWidget>
2023-06-20 16:09:25 +08:00
#include <QTextEdit>
2023-09-19 11:49:20 +08:00
#include <QTreeWidget>
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-09-19 11:49:20 +08:00
extern const Qt::Alignment AlignRight;
2023-04-23 17:01:35 +08:00
2023-09-19 11:49:20 +08:00
inline int operator|(const QVariant &value, int def) {
return value.isValid() ? value.toInt() : def;
}
2023-04-18 14:14:46 +08:00
2023-08-01 11:42:41 +08:00
inline QWidget *parentWgt(QObject *obj) {
while(obj && ! obj->isWidgetType()) obj = obj->parent();
return (QWidget*) obj;
}
inline QWidget *parentWin(QObject *obj) {
while(obj) {
if(obj->isWidgetType()) return ((QWidget*) obj)->window();
obj = obj->parent();
}
return (QWidget*) obj;
}
2023-09-19 11:49:20 +08:00
inline int SetCurData(QComboBox *combo, const QVariant &data) {
2023-04-18 14:14:46 +08:00
auto idx = combo->findData(data);
if(idx>-1) combo->setCurrentIndex(idx);
return idx;
}
2023-09-19 11:49:20 +08:00
inline int SetCurText(QComboBox *combo, const QString& text) {
auto idx = combo->findText(text);
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);
}
2023-09-19 11:49:20 +08:00
inline void gFont(QWidget *wgt, const QString& family, int size = 0, bool bold = false, bool italic = false) {
2023-06-20 16:09:25 +08:00
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);
}
2023-09-19 11:49:20 +08:00
inline QFont qfont(const QString& family, int pixelSize, bool bold = false, bool italic = false) {
2023-06-20 16:09:25 +08:00
QFont ft(family);
ft.setPixelSize(pixelSize);
if(bold) ft.setBold(true);
if(italic) ft.setItalic(true);
return ft;
}
2023-09-19 11:49:20 +08:00
inline void gAppendText(QTextEdit *wgt, const QString& text, const QColor &color) {
2023-06-20 16:09:25 +08:00
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:
2023-09-19 11:49:20 +08:00
VBox(QWidget *parent=nullptr) : QBoxLayout(TopToBottom, parent) {}
VBox(QBoxLayout *parent) : QBoxLayout(TopToBottom) {
2023-04-18 14:14:46 +08:00
parent->addLayout(this);
};
2023-09-19 11:49:20 +08:00
VBox(QStackedLayout *stack) : QBoxLayout(TopToBottom, new QWidget) {
2023-07-21 17:40:49 +08:00
stack->addWidget(parentWidget());
2023-04-18 14:14:46 +08:00
setContentsMargins(0,0,0,0);
};
2023-09-19 11:49:20 +08:00
VBox(QSplitter *splitter) : QBoxLayout(TopToBottom, new QWidget) {
2023-07-21 17:40:49 +08:00
splitter->addWidget(parentWidget());
2023-06-20 16:09:25 +08:00
setContentsMargins(0,0,0,0);
};
2023-09-19 11:49:20 +08:00
QLabel *addLabel() {
auto lb = new QLabel;
addWidget(lb);
return lb;
}
QLabel *addLabel(const QString& text) {
2023-06-20 16:09:25 +08:00
auto lb = new QLabel(text);
addWidget(lb);
return lb;
}
2023-09-19 11:49:20 +08:00
QDialogButtonBox *addBtnBox(QDialog *dlg = 0) {
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
if(dlg) connect(btnBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
addWidget(btnBox);
return btnBox;
}
2023-04-18 14:14:46 +08:00
};
class HBox : public QBoxLayout {
public:
2023-09-19 11:49:20 +08:00
HBox(QWidget *parent=nullptr) : QBoxLayout(LeftToRight, parent) {}
HBox(QBoxLayout *parent) : QBoxLayout(LeftToRight) {
2023-04-18 14:14:46 +08:00
parent->addLayout(this);
};
2023-09-19 11:49:20 +08:00
HBox(QStackedLayout *stack) : QBoxLayout(LeftToRight, new QWidget) {
2023-07-21 17:40:49 +08:00
stack->addWidget(parentWidget());
2023-04-18 14:14:46 +08:00
setContentsMargins(0,0,0,0);
};
2023-09-19 11:49:20 +08:00
HBox(QSplitter *splitter) : QBoxLayout(LeftToRight, new QWidget) {
2023-07-21 17:40:49 +08:00
splitter->addWidget(parentWidget());
2023-06-20 16:09:25 +08:00
setContentsMargins(0,0,0,0);
};
2023-09-19 11:49:20 +08:00
QLabel *addLabel() {
auto lb = new QLabel;
addWidget(lb);
return lb;
}
QLabel *addLabel(const QString& text) {
2023-06-20 16:09:25 +08:00
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-09-19 11:49:20 +08:00
Grid(QBoxLayout *parent) {
2023-04-18 14:14:46 +08:00
parent->addLayout(this);
};
2023-09-19 11:49:20 +08:00
Grid(QStackedLayout *stack) : QGridLayout(new QWidget) {
2023-07-21 17:40:49 +08:00
stack->addWidget(parentWidget());
2023-04-18 14:14:46 +08:00
};
2023-09-19 11:49:20 +08:00
Grid(QSplitter *splitter) : QGridLayout(new QWidget) {
2023-07-21 17:40:49 +08:00
splitter->addWidget(parentWidget());
};
2024-01-28 20:28:02 +08:00
QLabel *addLabel(const QString& text, int row, int column, Qt::Alignment align = Qt::Alignment()) {
2023-07-21 17:40:49 +08:00
auto lb = new QLabel(text);
2024-01-28 20:28:02 +08:00
addWidget(lb, row, column, align);
2023-07-21 17:40:49 +08:00
return lb;
}
2023-04-18 14:14:46 +08:00
};
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;
2023-09-19 11:49:20 +08:00
auto addItem(const QString& text, const QVariant &value) {
2023-04-28 18:26:41 +08:00
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-09-19 11:49:20 +08:00
ColAttr(const QString& field, const QString& text, int width=0, QHeaderView::ResizeMode resizeMode = QHeaderView::Interactive) : field(field), text(text), width(width), resizeMode(resizeMode) {}
ColAttr(const QString& field, const QString& text, QHeaderView::ResizeMode resizeMode) : field(field), text(text), resizeMode(resizeMode) {}
2023-04-18 14:14:46 +08:00
QString field;
QString text;
2023-09-19 11:49:20 +08:00
int width = 0;
2023-06-20 16:09:25 +08:00
QHeaderView::ResizeMode resizeMode;
2023-04-18 14:14:46 +08:00
};
2023-09-19 11:49:20 +08:00
enum ItemDataRole {
MarginRole = 0xfc,
AlignRole,
FieldRole,
WidthRole
};
struct ColItem {
QTreeWidgetItem *item;
int i;
auto &align(int align) {
item->setData(i, AlignRole, align);
return *this;
}
auto &alignC() {
return align(Qt::AlignCenter);
}
auto &alignR() {
return align(Qt::AlignVCenter | Qt::AlignRight);
}
ColItem &margin(int margin);
};
class TreeWidgetItem;
class TreeWidget : public QTreeWidget {
friend ColItem;
Q_OBJECT
public:
using QTreeWidget::QTreeWidget;
ColItem addCol(const QString& field, const QString& text, int width = 0, QHeaderView::ResizeMode resizeMode = QHeaderView::Interactive);
ColItem addCol(const QString& field, const QString& text, QHeaderView::ResizeMode resizeMode) {
return addCol(field, text, 0, resizeMode);
}
auto setDefs() {
if(! hasRowNum) setIndentation(0);
setAlternatingRowColors(true);
header()->setStretchLastSection(false);
return this;
}
auto setHeaderAlignC() {
auto item = headerItem();
for(int cc=0; cc<columnCount(); ++cc) item->setTextAlignment(cc, Qt::AlignCenter);
}
auto setColFit() {
header()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
auto setColWidth(int value, QHeaderView::ResizeMode mode = QHeaderView::Interactive) {
header()->setDefaultSectionSize(value);
if(mode!=QHeaderView::Interactive) header()->setSectionResizeMode(mode);
return this;
}
auto setHeaderText(const QString& column, const QString& text) {
headerItem()->setText(fdmap.at(column), text);
return this;
}
2024-08-07 18:18:37 +08:00
using QTreeWidget::setColumnHidden;
void setColumnHidden(const QString& column, bool hide) {
setColumnHidden(fdmap.at(column), hide);
}
2023-09-19 11:49:20 +08:00
using QTreeWidget::showColumn;
void showColumn(const QString& column) {
showColumn(fdmap.at(column));
}
using QTreeWidget::hideColumn;
void hideColumn(const QString& column) {
hideColumn(fdmap.at(column));
}
auto item(int idx) const {
return (TreeWidgetItem*) topLevelItem(idx);
}
auto selectedItem() const {
auto is = selectedItems();
return is.isEmpty() ? 0 : (TreeWidgetItem*) is.at(0);
}
auto curItem() const {
return (TreeWidgetItem*) currentItem();
}
QString field(int column) const {
return headerItem()->data(column, FieldRole).toString();
}
QString sortField() const {
return field(sortColumn());
}
using QTreeWidget::sortItems;
void sortItems(const QString& column, Qt::SortOrder order = Qt::AscendingOrder) {
sortItems(fdmap.at(column), order);
}
std::unordered_map<QString, int> fdmap;
int minRowHeight = 0;
char hasGrid = 1;
bool hasRowNum = false;
int sizeHintForColumn(int column) const override {
return QTreeWidget::sizeHintForColumn(column);
}
2024-01-28 20:28:02 +08:00
bool adjSections(int index, int size);
2023-09-19 11:49:20 +08:00
signals:
void updGeos();
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
void updateGeometries() override {
QTreeWidget::updateGeometries();
emit updGeos();
}
void rowsInserted(const QModelIndex &parent, int start, int end) override;
void drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const override;
void onSectionResized(int logicalIndex, int oldSize, int newSize);
bool noStretch = true;
bool noMargin = true;
bool isSectionResized = false;
bool blocked = false;
};
inline int operator*(const QString& key, QTreeView &table) {
2023-10-23 11:44:22 +08:00
if((size_t)&table==0) return -1;
2023-09-19 11:49:20 +08:00
return ((TreeWidget&)table).fdmap.at(key);
}
inline int operator*(const char *key, QTreeView &table) {
2023-10-23 11:44:22 +08:00
if((size_t)&table==0) return -1;
2023-09-19 11:49:20 +08:00
return ((TreeWidget&)table).fdmap.at(key);
}
class TreeWidgetItem : public QTreeWidgetItem {
public:
using QTreeWidgetItem::QTreeWidgetItem;
using QTreeWidgetItem::checkState;
auto checkState(const QString& column) const {
return checkState(column**treeWidget());
}
using QTreeWidgetItem::setCheckState;
auto setCheckState(const QString& column, Qt::CheckState state) {
setCheckState(column**treeWidget(), state);
return this;
}
using QTreeWidgetItem::text;
auto text(const QString& column) const {
return text(column**treeWidget());
}
using QTreeWidgetItem::setText;
auto setText(const QString& column, const QString& text) {
setText(column**treeWidget(), text);
return this;
}
auto setText(const QString& column, const QString& text, const QVariant &value) {
auto idx = column**treeWidget();
setText(idx, text);
setData(idx, Qt::UserRole, value);
return this;
}
auto setText(const QString& column, const QString& text, int alignment) {
auto idx = column**treeWidget();
setText(idx, text);
setTextAlignment(idx, (Qt::Alignment)alignment);
return this;
}
using QTreeWidgetItem::background;
auto background(const QString& column) const {
return background(column**treeWidget());
}
using QTreeWidgetItem::setBackground;
auto setBackground(const QString& column, const QBrush &brush) {
setBackground(column**treeWidget(), brush);
return this;
}
using QTreeWidgetItem::foreground;
auto foreground(const QString& column) const {
return foreground(column**treeWidget());
}
using QTreeWidgetItem::setForeground;
auto setForeground(const QString& column, const QBrush &brush) {
setForeground(column**treeWidget(), brush);
return this;
}
using QTreeWidgetItem::data;
auto data(int col) const {
return data(col, Qt::UserRole);
}
auto data(const QString& column, int role = Qt::UserRole) const {
return data(column**treeWidget(), role);
}
using QTreeWidgetItem::setData;
auto setData(int col, const QVariant &value) {
setData(col, Qt::UserRole, value);
return this;
}
auto setData(const QString& column, const QVariant &value) {
setData(column**treeWidget(), Qt::UserRole, value);
return this;
}
auto setData(const QString& column, int role, const QVariant &value) {
setData(column**treeWidget(), role, value);
return this;
}
auto cellWidget(int column) {
return treeWidget()->itemWidget(this, column);
}
auto cellWidget(const QString& column) {
return treeWidget()->itemWidget(this, column**treeWidget());
}
void setCellWidget(int column, QWidget *widget) {
treeWidget()->setItemWidget(this, column, widget);
}
void setCellWidget(const QString& column, QWidget *widget) {
treeWidget()->setItemWidget(this, column**treeWidget(), widget);
}
};
2024-05-23 17:13:51 +08:00
inline QWidget *WrapVCenter(QWidget *wgt) {
auto vvv = new VBox(new QWidget);
vvv->setContentsMargins(0,0,0,0);
vvv->setSpacing(0);
vvv->addStretch();
vvv->addWidget(wgt);
vvv->addStretch();
return vvv->parentWidget();
}
2023-09-19 11:49:20 +08:00
class TableWidget : 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-09-19 11:49:20 +08:00
TableWidget() {}
TableWidget(std::initializer_list<ColAttr> colAttrs, int rows = 0, QWidget *parent = 0);
2023-04-18 14:14:46 +08:00
2023-09-19 11:49:20 +08:00
auto setDefs() {
2023-04-18 14:14:46 +08:00
setSelectionBehavior(QTableWidget::SelectRows);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setAlternatingRowColors(true);
return this;
}
2023-09-19 11:49:20 +08:00
auto setColStretch() {
2023-04-18 14:14:46 +08:00
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return this;
}
2023-09-19 11:49:20 +08:00
auto setRowStretch() {
2023-04-18 14:14:46 +08:00
verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return this;
}
2023-09-19 11:49:20 +08:00
auto setColFit() {
2023-04-18 14:14:46 +08:00
horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
2023-09-19 11:49:20 +08:00
auto setRowFit() {
2023-04-18 14:14:46 +08:00
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this;
}
2023-09-19 11:49:20 +08:00
auto setColWidth(int value) {
if(horizontalHeader()->minimumSectionSize() > value) horizontalHeader()->setMinimumSectionSize(value);
horizontalHeader()->setDefaultSectionSize(value);
return this;
}
auto setColResize(QHeaderView::ResizeMode mode) {
horizontalHeader()->setSectionResizeMode(mode);
2023-04-18 14:14:46 +08:00
return this;
}
2023-09-19 11:49:20 +08:00
auto setRowHeight(int value) {
if(verticalHeader()->minimumSectionSize() > value) verticalHeader()->setMinimumSectionSize(value);
verticalHeader()->setDefaultSectionSize(value);
return this;
}
auto setRowResize(QHeaderView::ResizeMode mode) {
verticalHeader()->setSectionResizeMode(mode);
2023-04-18 14:14:46 +08:00
return this;
}
2023-09-19 11:49:20 +08:00
auto setHeaderText(int col, const 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-09-19 11:49:20 +08:00
auto setHeaderText(const QString& column, const QString& text) {
return setHeaderText(fdmap.at(column), text);
2023-04-28 18:26:41 +08:00
}
2023-04-18 14:14:46 +08:00
2023-09-19 11:49:20 +08:00
auto setVHeaderText(int row, const QString& text) {
2023-06-20 16:09:25 +08:00
auto item = verticalHeaderItem(row);
if(item==0) setVerticalHeaderItem(row, item = new QTableWidgetItem());
item->setText(text);
return item;
}
2023-09-19 11:49:20 +08:00
auto appendRow() {
2023-04-18 14:14:46 +08:00
auto value = rowCount();
setRowCount(value + 1);
return value;
}
2023-04-28 18:26:41 +08:00
using QTableWidget::item;
2023-09-19 11:49:20 +08:00
auto item(int row, const QString& column) {
return item(row, fdmap.at(column));
2023-04-18 14:14:46 +08:00
}
2023-09-19 11:49:20 +08:00
auto itemValid(int row, int col) {
auto itm = item(row, col);
if(itm==0) setItem(row, col, itm = new QTableWidgetItem);
return itm;
2023-04-28 18:26:41 +08:00
}
2023-09-19 11:49:20 +08:00
auto itemValid(int row, const QString& column) {
return itemValid(row, fdmap.at(column));
2023-04-28 18:26:41 +08:00
}
using QTableWidget::setItem;
2023-09-19 11:49:20 +08:00
void setItem(int row, const QString& column, QTableWidgetItem *item) {
setItem(row, fdmap.at(column), item);
2023-04-28 18:26:41 +08:00
}
2023-09-19 11:49:20 +08:00
auto text(int row, int col) {
2023-04-28 18:26:41 +08:00
auto itm = item(row, col);
if(itm==0) return QString();
return itm->text();
2023-04-18 14:14:46 +08:00
}
2023-09-19 11:49:20 +08:00
auto text(int row, const QString& column) {
return text(row, fdmap.at(column));
2023-04-28 18:26:41 +08:00
}
2023-09-19 11:49:20 +08:00
auto setText(int row, int col, const QString& text) {
2023-04-28 18:26:41 +08:00
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
}
2023-09-19 11:49:20 +08:00
auto setText(int row, const QString& column, const QString& text) {
return setText(row, fdmap.at(column), text);
}
auto setText(int row, int col, const QString& text, const QVariant &value) {
auto itm = item(row, col);
if(itm) itm->setText(text);
else setItem(row, col, itm = new QTableWidgetItem(text));
itm->setData(Qt::UserRole, value);
return itm;
}
auto setText(int row, const QString& column, const QString& text, const QVariant &value) {
return setText(row, fdmap.at(column), text, value);
2023-04-18 14:14:46 +08:00
}
2023-09-19 11:49:20 +08:00
auto data(int row, int col) {
2023-04-28 18:26:41 +08:00
auto itm = item(row, col);
if(itm==0) return QVariant();
return itm->data(Qt::UserRole);
}
2023-09-19 11:49:20 +08:00
auto data(int row, const QString& column) {
return data(row, fdmap.at(column));
2023-04-28 18:26:41 +08:00
}
2023-09-19 11:49:20 +08:00
auto setData(int row, int col, const QVariant &value) {
2023-04-28 18:26:41 +08:00
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
}
2023-09-19 11:49:20 +08:00
auto setData(int row, const QString& column, const QVariant &value) {
return setData(row, fdmap.at(column), value);
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
using QTableWidget::cellWidget;
2023-09-19 11:49:20 +08:00
auto cellWidget(int row, const QString& column) {
return cellWidget(row, fdmap.at(column));
2023-04-18 14:14:46 +08:00
}
2023-04-28 18:26:41 +08:00
using QTableWidget::setCellWidget;
2023-09-19 11:49:20 +08:00
void setCellWidget(int row, const QString& column, QWidget *widget) {
setCellWidget(row, fdmap.at(column), widget);
}
using QTableWidget::sortItems;
void sortItems(const QString& column, Qt::SortOrder order) {
sortItems(fdmap.at(column), order);
2023-04-18 14:14:46 +08:00
}
2023-09-19 11:49:20 +08:00
std::unordered_map<QString, int> fdmap;
2023-04-18 14:14:46 +08:00
public Q_SLOTS:
2023-09-19 11:49:20 +08:00
void clearRows() {setRowCount(0);}
2023-08-07 09:04:53 +08:00
signals:
void updGeos();
2023-04-18 14:14:46 +08:00
protected:
2023-09-19 11:49:20 +08:00
bool eventFilter(QObject *watched, QEvent *event) override;
void updateGeometries() override {
QTableWidget::updateGeometries();
emit updGeos();
};
void onSectionResized(int logicalIndex, int oldSize, int newSize);
bool adjSections(int index, int size);
bool noStretch = true;
bool isSectionResized = false;
bool blocked = false;
2023-04-18 14:14:46 +08:00
};
template<class T>
class Wrp {
public:
T *obj;
Wrp(T *obj = nullptr){
this->obj = obj;
};
2023-09-19 11:49:20 +08:00
Wrp& operator()(T *obj){
2023-04-18 14:14:46 +08:00
this->obj = obj;
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& operator()(T *obj, QLayout *layout){
2023-04-18 14:14:46 +08:00
this->obj = obj;
layout->addWidget(obj);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& addTo(QLayout *layout){
2023-04-18 14:14:46 +08:00
layout->addWidget(obj);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& margin(int a){
2023-04-18 14:14:46 +08:00
obj->setMargin(a);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& font(const QFont &font){
2023-04-18 14:14:46 +08:00
obj->setFont(font);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& font(int size){
2023-04-23 17:01:35 +08:00
auto font = obj->font();
font.setPixelSize(size);
obj->setFont(font);
return *this;
}
2023-04-18 14:14:46 +08:00
2023-09-19 11:49:20 +08:00
Wrp& width(int w){
2023-04-18 14:14:46 +08:00
obj->setFixedWidth(w);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& height(int h){
2023-04-18 14:14:46 +08:00
obj->setFixedHeight(h);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& padding(int wAdd, int hAdd, int minW = 32, int minH = 16){
2023-04-18 14:14:46 +08:00
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;
}
2023-09-19 11:49:20 +08:00
Wrp& alignC(){
2023-04-18 14:14:46 +08:00
obj->setAlignment(Qt::AlignCenter);
return *this;
}
2023-09-19 11:49:20 +08:00
Wrp& alignR(){
2023-04-18 14:14:46 +08:00
obj->setAlignment(Qt::AlignRight);
return *this;
}
};
#endif