This commit is contained in:
Gangphon 2023-06-20 16:09:25 +08:00
parent a69c444f2c
commit 0cb71807e6
4 changed files with 126 additions and 48 deletions

View File

@ -1,4 +1,5 @@
#include "qgui.h" #include "qgui.h"
#include <QResizeEvent>
const int AlignRight = Qt::AlignRight | Qt::AlignVCenter; const int AlignRight = Qt::AlignRight | Qt::AlignVCenter;
@ -8,26 +9,31 @@ Table::Table(std::initializer_list<ColAttr> colAttrs, int rows, QWidget *parent)
auto item = horizontalHeaderItem(i); auto item = horizontalHeaderItem(i);
if(item==0) setHorizontalHeaderItem(i, item = new QTableWidgetItem()); if(item==0) setHorizontalHeaderItem(i, item = new QTableWidgetItem());
item->setText(it->text); item->setText(it->text);
item->setData(0x99, it->width);
if(it->width > 0) horizontalHeader()->resizeSection(i, it->width); if(it->width > 0) horizontalHeader()->resizeSection(i, it->width);
if(it->resizeMode > -1) horizontalHeader()->setSectionResizeMode(i, (QHeaderView::ResizeMode)it->resizeMode); 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);
}
mFieldMap.insert(it->field, i++); mFieldMap.insert(it->field, i++);
} }
} }
int Table::sizeHintForColumn(int column) const { void Table::resizeEvent(QResizeEvent *event) {
auto item = horizontalHeaderItem(column); QTableWidget::resizeEvent(event);
if(item==0) return QTableWidget::sizeHintForColumn(column); if(noStretch || event->size().width() == event->oldSize().width()) return;
int width = item->data(0x99).toInt(); resizeSec();
if(width==0) return QTableWidget::sizeHintForColumn(column); }
auto header = horizontalHeader();
if(header->sectionResizeMode(column) != QHeaderView::ResizeToContents) return QTableWidget::sizeHintForColumn(column); void Table::resizeSec() {
int colCnt = columnCount(); auto header = horizontalHeader();
int remainWidth = header->width(), stretchWidth = width; int colCnt = columnCount(), remainWidth = header->width(), stretchWidth = 0, secWidth;
for(int cc=0; cc<colCnt; cc++) if(cc!=column && (item = horizontalHeaderItem(cc))) { QTableWidgetItem *item;
if(header->sectionResizeMode(cc) == QHeaderView::ResizeToContents) stretchWidth += item->data(0x99).toInt(); for(int cc=0; cc<colCnt; cc++) if((item = horizontalHeaderItem(cc))) {
else remainWidth -= item->data(0x99).toInt(); if((secWidth = item->data(0x99).toInt()) > 0) stretchWidth += secWidth;
} else remainWidth -= header->sectionSize(cc);
if(remainWidth<=0) return QTableWidget::sizeHintForColumn(column); }
return width * remainWidth / stretchWidth; 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);
} }

View File

@ -3,9 +3,12 @@
#include <QComboBox> #include <QComboBox>
#include <QStackedLayout> #include <QStackedLayout>
#include <QSplitter>
#include <QListWidget> #include <QListWidget>
#include <QTableWidget> #include <QTableWidget>
#include <QHeaderView> #include <QHeaderView>
#include <QLabel>
#include <QTextEdit>
#define MainMust \ #define MainMust \
#if(QT_VERSION_MAJOR > 5) \ #if(QT_VERSION_MAJOR > 5) \
@ -24,6 +27,34 @@ inline int setCurrentData(QComboBox *combo, const QVariant &data) {
if(idx>-1) combo->setCurrentIndex(idx); if(idx>-1) combo->setCurrentIndex(idx);
return idx; return idx;
} }
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);
}
class VBox : public QBoxLayout { class VBox : public QBoxLayout {
public: public:
@ -37,6 +68,17 @@ public:
wgt->setLayout(this); wgt->setLayout(this);
parent->addWidget(wgt); parent->addWidget(wgt);
}; };
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;
}
}; };
class HBox : public QBoxLayout { class HBox : public QBoxLayout {
public: public:
@ -50,6 +92,17 @@ public:
wgt->setLayout(this); wgt->setLayout(this);
parent->addWidget(wgt); parent->addWidget(wgt);
}; };
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;
}
}; };
class Grid : public QGridLayout { class Grid : public QGridLayout {
public: public:
@ -64,14 +117,6 @@ public:
}; };
}; };
inline QFont qfont(const QString &family, int pixelSize, bool bold = false, bool italic = false) {
QFont font(family);
font.setPixelSize(pixelSize);
if(bold) font.setBold(true);
if(italic) font.setItalic(true);
return font;
}
class ListWgt : public QListWidget { class ListWgt : public QListWidget {
public: public:
using QListWidget::QListWidget; using QListWidget::QListWidget;
@ -86,15 +131,18 @@ public:
}; };
struct ColAttr { struct ColAttr {
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) {}
QString field; QString field;
QString text; QString text;
int width{0}; int width{0};
int resizeMode{-1}; QHeaderView::ResizeMode resizeMode;
}; };
class Table : public QTableWidget { class Table : public QTableWidget {
Q_OBJECT Q_OBJECT
public: public:
using QTableWidget::QTableWidget; using QTableWidget::QTableWidget;
Table() {}
Table(std::initializer_list<ColAttr> colAttrs, int rows = 0, QWidget *parent = 0); Table(std::initializer_list<ColAttr> colAttrs, int rows = 0, QWidget *parent = 0);
inline auto setNoEdit() { inline auto setNoEdit() {
@ -123,12 +171,16 @@ public:
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
return this; return this;
} }
inline auto setColWidth(int value) { inline auto setColWidth(int value, QHeaderView::ResizeMode mode = QHeaderView::Interactive) {
horizontalHeader()->setDefaultSectionSize(value); auto header = horizontalHeader();
header->setDefaultSectionSize(value);
if(mode!=QHeaderView::Interactive) header->setSectionResizeMode(mode);
return this; return this;
} }
inline auto setRowHeight(int value) { inline auto setRowHeight(int value, QHeaderView::ResizeMode mode = QHeaderView::Interactive) {
verticalHeader()->setDefaultSectionSize(value); auto header = verticalHeader();
header->setDefaultSectionSize(value);
if(mode!=QHeaderView::Interactive) header->setSectionResizeMode(mode);
return this; return this;
} }
@ -143,6 +195,13 @@ public:
return setHeaderText(col, text); return setHeaderText(col, text);
} }
inline auto setVHeaderText(int row, QString text) {
auto item = verticalHeaderItem(row);
if(item==0) setVerticalHeaderItem(row, item = new QTableWidgetItem());
item->setText(text);
return item;
}
inline auto appendRow() { inline auto appendRow() {
auto value = rowCount(); auto value = rowCount();
setRowCount(value + 1); setRowCount(value + 1);
@ -226,9 +285,10 @@ public Q_SLOTS:
inline void clearRows() {setRowCount(0);} inline void clearRows() {setRowCount(0);}
protected: protected:
int sizeHintForColumn(int column) const override; void resizeEvent(QResizeEvent *event) override;
void resizeSec();
QMap<QString,int> mFieldMap; QMap<QString,int> mFieldMap;
bool noStretch{true};
}; };
class ResizeEmitedWgt : public QWidget { class ResizeEmitedWgt : public QWidget {

View File

@ -16,7 +16,6 @@
#include "program/eweb.h" #include "program/eweb.h"
#include "program/wplanlist.h" #include "program/wplanlist.h"
#include "tools.h" #include "tools.h"
#include <QBoxLayout>
#include <QDateEdit> #include <QDateEdit>
#include <QDir> #include <QDir>
#include <QGraphicsView> #include <QGraphicsView>
@ -35,7 +34,10 @@ public:
}; };
PageListItem::PageListItem(const QJsonObject &attr, const QString &pageDir) : mAttr(attr), mPageDir(pageDir) { PageListItem::PageListItem(const QJsonObject &attr, const QString &pageDir) : mAttr(attr), mPageDir(pageDir) {
setSizeHint(QSize(0,80)); scale = qMin(128.0 / gProgItem->mWidth, 96.0 / gProgItem->mHeight);
viewW = scale * gProgItem->mWidth;
viewH = scale * gProgItem->mHeight;
setSizeHint(QSize(0, viewH+20));
mScene = new PageScene(0, 0, gProgItem->mWidth, gProgItem->mHeight, this); mScene = new PageScene(0, 0, gProgItem->mWidth, gProgItem->mHeight, this);
auto elements = mAttr["elements"].toArray(); auto elements = mAttr["elements"].toArray();
foreach(auto ele, elements) { foreach(auto ele, elements) {
@ -124,31 +126,39 @@ bool PageListItem::saveFiles() {
QWidget *PageListItem::itemWgt() { QWidget *PageListItem::itemWgt() {
auto wgtPage = new QWidget; auto wgtPage = new QWidget;
auto hBox = new HBox(wgtPage); auto grid = new Grid(wgtPage);
grid->setContentsMargins(0,0,0,0);
grid->setSpacing(0);
mGraView = new QGraphicsView(); grid->addWidget(fdIdx = new QLabel, 0, 0);
fdIdx->setMinimumWidth(32);
fdIdx->setAlignment(Qt::AlignCenter);
fdIdx->setNum(listWidget()->count());
mGraView = new QGraphicsView;
mGraView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); mGraView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mGraView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); mGraView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mGraView->setEnabled(false); mGraView->setEnabled(false);
mGraView->setFrameShape(QFrame::NoFrame); mGraView->setFrameShape(QFrame::NoFrame);
mGraView->setFixedSize(120, 60); mGraView->setFixedSize(viewW, viewH);
mGraView->setStyleSheet("QGraphicsView{background-color:transparent;}"); mGraView->setStyleSheet("QGraphicsView{background-color:transparent;}");
mGraView->setScene(mScene); mGraView->setScene(mScene);
qreal scale = qMin(mGraView->width() / mScene->width(), mGraView->height() / mScene->height());
mGraView->scale(scale, scale); mGraView->scale(scale, scale);
hBox->addWidget(mGraView); grid->addWidget(mGraView, 0, 1);
auto vBox = new VBox(hBox); auto hBox = new HBox;
vBox->addWidget(fdIdx = new QLabel, 0, Qt::AlignCenter); hBox->setSpacing(4);
if(listWidget()->count()>4) fdIdx->setNum(listWidget()->count()); grid->addLayout(hBox, 1, 1);
grid->setColumnStretch(2, 1);
grid->setRowStretch(2, 1);
auto hh = new HBox(vBox); hBox->addWidget(fdPlayTimes = new QLabel(QString::number(mAttr["repeat"].toInt())));
hh->addStretch(); gFont(fdPlayTimes, 12);
fdPlayTimes = new QLabel(QString::number(mAttr["repeat"].toInt()));
hh->addWidget(fdPlayTimes);
hh->addWidget(new QLabel(tr("times")));
vBox->addStretch(); auto fdTimes = new QLabel(tr("times"));
gFont(fdTimes, 12);
hBox->addWidget(fdTimes);
hBox->addStretch();
return wgtPage; return wgtPage;
} }

View File

@ -26,6 +26,8 @@ public:
QGraphicsScene *mScene{0}; QGraphicsScene *mScene{0};
QGraphicsView *mGraView{0}; QGraphicsView *mGraView{0};
QLabel *fdIdx{0}, *fdPlayTimes{0}; QLabel *fdIdx{0}, *fdPlayTimes{0};
qreal scale{1};
int viewW{120}, viewH{80};
}; };
struct AudioInfo { struct AudioInfo {