From 0cb51c6e2a69b1d36aedc9ea4067b6733b463cd3 Mon Sep 17 00:00:00 2001 From: gangphon Date: Wed, 7 Sep 2022 23:10:35 +0800 Subject: [PATCH] =?UTF-8?q?ledset=E5=A2=9E=E5=8A=A0=E8=99=9A=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ledset/expertsmartpointsetwin.cpp | 90 ++++++++++++++++++++++++------- ledset/expertsmartpointsetwin.h | 10 +++- ledset/table.cpp | 19 ++++++- ledset/table.h | 21 +------- 4 files changed, 97 insertions(+), 43 deletions(-) diff --git a/ledset/expertsmartpointsetwin.cpp b/ledset/expertsmartpointsetwin.cpp index 7c7e4b8..1d623c0 100644 --- a/ledset/expertsmartpointsetwin.cpp +++ b/ledset/expertsmartpointsetwin.cpp @@ -2,12 +2,11 @@ #include #include #include - #include #include #include #include - +#include ExpertSmartPointSetWin::ExpertSmartPointSetWin(QWidget *parent) : BaseWin{parent} { setWindowModality(Qt::WindowModal); setAttribute(Qt::WA_DeleteOnClose); @@ -215,10 +214,12 @@ ExpertSmartPointSetWin::ExpertSmartPointSetWin(QWidget *parent) : BaseWin{parent auto hhhh = new QHBoxLayout; hhhh->addStretch(); auto fdSta1 = new QRadioButton("状态1"); + fdSta1->setChecked(true); hhhh->addWidget(fdSta1); hhhh->addStretch(); auto fdR1 = new QRadioButton("红"); + fdR1->setChecked(true); hhhh->addWidget(fdR1); hhhh->addSpacing(20); @@ -248,6 +249,7 @@ ExpertSmartPointSetWin::ExpertSmartPointSetWin(QWidget *parent) : BaseWin{parent hhhh->addSpacing(20); auto fdG2 = new QRadioButton("绿"); + fdG2->setChecked(true); hhhh->addWidget(fdG2); hhhh->addSpacing(20); @@ -277,6 +279,7 @@ ExpertSmartPointSetWin::ExpertSmartPointSetWin(QWidget *parent) : BaseWin{parent hhhh->addSpacing(20); auto fdB3 = new QRadioButton("蓝"); + fdB3->setChecked(true); hhhh->addWidget(fdB3); hhhh->addSpacing(20); @@ -312,46 +315,93 @@ ExpertSmartPointSetWin::ExpertSmartPointSetWin(QWidget *parent) : BaseWin{parent auto hhh = new QHBoxLayout; + auto btnUndo = new QPushButton("回撤"); + btnUndo->setMinimumSize(90, 30); + connect(btnUndo, &QPushButton::clicked, this, [this] { + auto lastIdx = tableRow->columnCount()-1; + if(lastIdx==-1) return; + auto last = tableRow->item(1, lastIdx); + if(last->text()=="×") { + virtualCnt--; + } else { + auto point = last->data(Qt::UserRole).toPoint(); + table->setItem(point.y(), point.x(), 0); + if(lastIdx==virtualCnt) table->selectionModel()->clearCurrentIndex(); + else { + for(int i=lastIdx-1; i>-1; i--) { + auto item = tableRow->item(1, i); + if(item->text()=="×") continue; + auto point = item->data(Qt::UserRole).toPoint(); + table->setCurrentCell(point.y(), point.x()); + break; + } + } + } + tableRow->setColumnCount(lastIdx); + }); + hhh->addWidget(btnUndo); + + auto btnAddVoid = new QPushButton("插入虚点"); + btnAddVoid->setMinimumSize(90, 30); + connect(btnAddVoid, &QPushButton::clicked, this, [this] { + virtualCnt++; + auto cnt = tableRow->columnCount(); + tableRow->setColumnCount(cnt+1); + tableRow->setItem(0, cnt, new QTableWidgetItem(QString::number(cnt+1))); + auto item = new QTableWidgetItem("×"); + tableRow->setItem(1, cnt, item); + }); + hhh->addWidget(btnAddVoid); + auto btnReset = new QPushButton("重新走点"); btnReset->setMinimumSize(90, 30); connect(btnReset, &QPushButton::clicked, this, [this] { table->clearContents(); - points.clear(); + tableRow->setColumnCount(0); + virtualCnt = 0; }); hhh->addWidget(btnReset); - auto btnUndo = new QPushButton("回撤"); - btnUndo->setMinimumSize(90, 30); - connect(btnUndo, &QPushButton::clicked, this, [this] { - if(points.isEmpty()) return; - auto last = points.takeLast(); - table->setItem(last.y(), last.x(), 0); - if(points.isEmpty()) table->selectionModel()->clearCurrentIndex(); - else { - last = points.last(); - table->setCurrentCell(last.y(), last.x()); - } - }); - hhh->addWidget(btnUndo); - hhh->addStretch(); vBox->addLayout(hhh); table = new PointTable; table->setEditTriggers(QAbstractItemView::NoEditTriggers); + table->setSelectionMode(QTableWidget::SingleSelection); table->horizontalHeader()->setDefaultSectionSize(40); table->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter); table->verticalHeader()->setMinimumWidth(40); table->verticalHeader()->setDefaultAlignment(Qt::AlignCenter); - table->setSelectionMode(QTableWidget::SingleSelection); connect(table, &QTableWidget::currentCellChanged, this, [this](int row, int col) { if(row < 0) return; auto item = table->item(row, col); if(item) return; - points.append({col, row}); - table->setItem(row, col, new QTableWidgetItem(QString::number(points.size()))); + auto cnt = tableRow->columnCount() + 1; + tableRow->setColumnCount(cnt); + tableRow->setItem(0, cnt-1, new QTableWidgetItem(QString::number(cnt))); + item = new QTableWidgetItem("√"); + item->setData(Qt::UserRole, QPoint{col, row}); + tableRow->setItem(1, cnt-1, item); + item = new QTableWidgetItem(QString::number(cnt - virtualCnt)); + table->setItem(row, col, item); }); vBox->addWidget(table); + + tableRow = new PointTable(2, 0); + tableRow->setEditTriggers(QAbstractItemView::NoEditTriggers); + tableRow->setSelectionMode(QTableWidget::NoSelection); + tableRow->horizontalHeader()->setDefaultSectionSize(40); + tableRow->horizontalHeader()->setVisible(false); + tableRow->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed); + tableRow->verticalHeader()->setMinimumWidth(120); + tableRow->setMaximumHeight(80); + auto item = tableRow->verticalHeaderItem(0); + if(item==0) tableRow->setVerticalHeaderItem(0, item = new QTableWidgetItem()); + item->setText("第一扫灯数:"); + item = tableRow->verticalHeaderItem(1); + if(item==0) tableRow->setVerticalHeaderItem(1, item = new QTableWidgetItem()); + item->setText("实: 虚:"); + vBox->addWidget(tableRow); } auto hBox = new QHBoxLayout; diff --git a/ledset/expertsmartpointsetwin.h b/ledset/expertsmartpointsetwin.h index 083b678..bc96da2 100644 --- a/ledset/expertsmartpointsetwin.h +++ b/ledset/expertsmartpointsetwin.h @@ -8,11 +8,17 @@ class PointTable : public QTableWidget { public: explicit PointTable(QWidget *parent = nullptr) : QTableWidget{parent} {} + PointTable(int rows, int columns, QWidget *parent = nullptr) : QTableWidget{rows, columns, parent} {} void setItem(int row, int column, QTableWidgetItem *item) { if(item) item->setTextAlignment(Qt::AlignCenter); QTableWidget::setItem(row, column, item); } }; +struct Point { + int x; + int y; + int c{0}; +}; class ExpertSmartPointSetWin : public BaseWin { Q_OBJECT @@ -20,8 +26,8 @@ public: explicit ExpertSmartPointSetWin(QWidget *parent = nullptr); QSpinBox *fdModuleWidth, *fdModuleHeight; - PointTable *table; - QList points; + PointTable *table, *tableRow; + int virtualCnt = 0; int moduleWidth{0}, moduleHeight{0}; }; diff --git a/ledset/table.cpp b/ledset/table.cpp index d2392db..799cb1e 100644 --- a/ledset/table.cpp +++ b/ledset/table.cpp @@ -1,10 +1,25 @@ #include "table.h" +Table::Table(std::initializer_list colAttrs, QWidget *parent) : QTableWidget{0, (int)colAttrs.size(), parent} { + int i = 0; + for(typename std::initializer_list::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); + item->setData(0x99, it->width); + 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++); + } +} int Table::sizeHintForColumn(int column) const { auto item = horizontalHeaderItem(column); - if(!item) return QTableWidget::sizeHintForColumn(column); + if(item==0) return QTableWidget::sizeHintForColumn(column); int width = item->data(0x99).toInt(); - if(!width) return QTableWidget::sizeHintForColumn(column); + if(width==0) return QTableWidget::sizeHintForColumn(column); auto header = horizontalHeader(); if(header->sectionResizeMode(column)!=QHeaderView::ResizeToContents) return QTableWidget::sizeHintForColumn(column); int colCnt = columnCount(); diff --git a/ledset/table.h b/ledset/table.h index 3b8d886..7fadef6 100644 --- a/ledset/table.h +++ b/ledset/table.h @@ -17,26 +17,9 @@ public: ResizeToContents }; explicit Table(QWidget *parent = nullptr) : QTableWidget{parent} {} - Table(std::initializer_list colAttrs, QWidget *parent = nullptr) : QTableWidget{0, (int)colAttrs.size(), parent} { - int i = 0; - for(typename std::initializer_list::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(std::initializer_list colAttrs, QWidget *parent = nullptr); - Table *setDefs() { + inline Table *setDefs() { setSelectionBehavior(QTableWidget::SelectRows); setEditTriggers(QAbstractItemView::NoEditTriggers); setAlternatingRowColors(true);