From 2ae750ce906a74b65def76fa5b1249c43ac425cf Mon Sep 17 00:00:00 2001 From: gangphon Date: Fri, 28 Apr 2023 18:26:41 +0800 Subject: [PATCH] ledok --- LedOK/gutil/qgui.cpp | 2 +- LedOK/gutil/qgui.h | 147 ++++++++++++++---------------- LedOK/program/progitem.cpp | 6 +- LedOK/program/usbdetectdialog.cpp | 50 +++++----- LedOK/program/usbdetectdialog.h | 4 +- 5 files changed, 100 insertions(+), 109 deletions(-) diff --git a/LedOK/gutil/qgui.cpp b/LedOK/gutil/qgui.cpp index 745f9da..e65709a 100644 --- a/LedOK/gutil/qgui.cpp +++ b/LedOK/gutil/qgui.cpp @@ -2,7 +2,7 @@ const int AlignRight = Qt::AlignRight | Qt::AlignVCenter; -Table::Table(std::initializer_list colAttrs, int rows, QWidget *parent) : NumTable{rows, (int)colAttrs.size(), parent} { +Table::Table(std::initializer_list colAttrs, int rows, QWidget *parent) : QTableWidget{rows, (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); diff --git a/LedOK/gutil/qgui.h b/LedOK/gutil/qgui.h index b1391e9..64d094f 100644 --- a/LedOK/gutil/qgui.h +++ b/LedOK/gutil/qgui.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -52,7 +53,7 @@ public: }; class Grid : public QGridLayout { public: - inline Grid(QWidget *parent=0) : QGridLayout(parent) {} + using QGridLayout::QGridLayout; inline Grid(QBoxLayout *parent) { parent->addLayout(this); }; @@ -71,59 +72,16 @@ inline QFont qfont(const QString &family, int pixelSize, bool bold = false, bool return font; } -class NumTable : public QTableWidget { - Q_OBJECT +class ListWgt : public QListWidget { public: - explicit NumTable(QWidget *parent = 0) : QTableWidget{parent} {} - NumTable(int rows, int columns, QWidget *parent = 0) : QTableWidget{rows, columns, parent} {} + using QListWidget::QListWidget; - inline auto setNoEdit() { - setEditTriggers(QAbstractItemView::NoEditTriggers); - return this; - } - inline auto setColWidth(int value) { - horizontalHeader()->setDefaultSectionSize(value); - return this; - } - inline auto setRowHeight(int value) { - verticalHeader()->setDefaultSectionSize(value); - return this; - } - - inline auto setHeaderText(int column, QString text) { - auto item = horizontalHeaderItem(column); - if(item==0) setHorizontalHeaderItem(column, item = new QTableWidgetItem()); - item->setText(text); - return item; - } - - inline auto itemValid(int row, int column) { - auto item = QTableWidget::item(row, column); - if(item==0) setItem(row, column, item = new QTableWidgetItem); - return item; - } - inline auto text(int row, int column) { - auto item = QTableWidget::item(row, column); - if(item==0) return QString(); - return item->text(); - } - inline auto setText(int row, int column, const QString &text) { - auto item = QTableWidget::item(row, column); - if(item) item->setText(text); - else setItem(row, column, item = new QTableWidgetItem(text)); - return item; - } - - inline auto data(int row, int col) { - auto item = QTableWidget::item(row, col); - if(item==0) return QVariant(); - return item->data(Qt::UserRole); - } - inline auto setData(int row, int col, const QVariant &value) { - auto item = QTableWidget::item(row, col); - if(item==0) setItem(row, col, item = new QTableWidgetItem); + using QListWidget::addItem; + inline auto addItem(const QString &text, const QVariant &value) { + auto item = new QListWidgetItem(text); item->setData(Qt::UserRole, value); - return item; + insertItem(count(), item); + return this; } }; @@ -133,12 +91,16 @@ struct ColAttr { int width{0}; int resizeMode{-1}; }; -class Table : public NumTable { +class Table : public QTableWidget { Q_OBJECT public: - explicit Table(QWidget *parent = nullptr) : NumTable{parent} {} - Table(std::initializer_list colAttrs, int rows = 0, QWidget *parent = nullptr); + using QTableWidget::QTableWidget; + Table(std::initializer_list colAttrs, int rows = 0, QWidget *parent = 0); + inline auto setNoEdit() { + setEditTriggers(QAbstractItemView::NoEditTriggers); + return this; + } inline auto setDefs() { setSelectionBehavior(QTableWidget::SelectRows); setEditTriggers(QAbstractItemView::NoEditTriggers); @@ -170,13 +132,16 @@ public: return this; } - inline auto setHeaderText(QString column, QString text) { - auto col = mFieldMap[column]; + inline auto setHeaderText(int col, QString text) { auto item = horizontalHeaderItem(col); if(item==0) setHorizontalHeaderItem(col, item = new QTableWidgetItem()); item->setText(text); return item; } + inline auto setHeaderText(QString column, QString text) { + auto col = mFieldMap[column]; + return setHeaderText(col, text); + } inline auto appendRow() { auto value = rowCount(); @@ -184,64 +149,92 @@ public: return value; } + using QTableWidget::item; inline auto item(int row, QString column) { auto col = mFieldMap[column]; - return QTableWidget::item(row, col); + return item(row, col); } + 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; inline void setItem(int row, QString column, QTableWidgetItem *item) { auto col = mFieldMap[column]; - QTableWidget::setItem(row, col, item); + setItem(row, col, item); + } + + inline auto text(int row, int col) { + auto itm = item(row, col); + if(itm==0) return QString(); + return itm->text(); } inline auto text(int row, QString column) { auto col = mFieldMap[column]; - auto item = QTableWidget::item(row, col); - if(item==0) return QString(); - return item->text(); + 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; } inline auto setText(int row, QString column, const QString &text) { auto col = mFieldMap[column]; - auto item = QTableWidget::item(row, col); - if(item) item->setText(text); - else QTableWidget::setItem(row, col, item = new QTableWidgetItem(text)); - return item; + return setText(row, col, text); } + inline auto data(int row, int col) { + auto itm = item(row, col); + if(itm==0) return QVariant(); + return itm->data(Qt::UserRole); + } inline auto data(int row, QString column) { auto col = mFieldMap[column]; - auto item = QTableWidget::item(row, col); - if(item==0) return QVariant(); - return item->data(Qt::UserRole); + 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; } inline auto setData(int row, QString column, const QVariant &value) { auto col = mFieldMap[column]; - auto item = QTableWidget::item(row, col); - if(item==0) QTableWidget::setItem(row, col, item = new QTableWidgetItem); - item->setData(Qt::UserRole, value); - return item; + return setData(row, col, value); } + using QTableWidget::cellWidget; inline auto cellWidget(int row, QString column) { auto col = mFieldMap[column]; - return QTableWidget::cellWidget(row, col); + return cellWidget(row, col); } + using QTableWidget::setCellWidget; inline void setCellWidget(int row, QString column, QWidget *widget) { auto col = mFieldMap[column]; - QTableWidget::setCellWidget(row, col, widget); + setCellWidget(row, col, widget); } - QMap mFieldMap; - public Q_SLOTS: inline void clearRows() {setRowCount(0);} protected: int sizeHintForColumn(int column) const override; + + QMap mFieldMap; }; class ResizeEmitedWgt : public QWidget { Q_OBJECT public: - explicit ResizeEmitedWgt(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) : QWidget{parent, f} {} + using QWidget::QWidget; protected: void resizeEvent(QResizeEvent *) override {emit resized();} signals: diff --git a/LedOK/program/progitem.cpp b/LedOK/program/progitem.cpp index da97cc4..df28b02 100644 --- a/LedOK/program/progitem.cpp +++ b/LedOK/program/progitem.cpp @@ -146,9 +146,9 @@ void ProgItem::onSetProgram() { } void ProgItem::onUsbExportProgram(){ - auto pDlg = new UsbDetectDialog(gMainWin); - connect(pDlg, &UsbDetectDialog::acceptData, this, &ProgItem::onUsbExportProgramPro); - pDlg->exec(); + UsbDetectDialog dlg(gMainWin); + connect(&dlg, &UsbDetectDialog::acceptData, this, &ProgItem::onUsbExportProgramPro); + dlg.exec(); } void ProgItem::onUsbExportProgramPro(QString strPath, QString strPassword) { auto waitingDlg = new WaitingDlg(mProgPanel, tr("Convertering")+" ..."); diff --git a/LedOK/program/usbdetectdialog.cpp b/LedOK/program/usbdetectdialog.cpp index da90ee9..10b39c3 100644 --- a/LedOK/program/usbdetectdialog.cpp +++ b/LedOK/program/usbdetectdialog.cpp @@ -1,17 +1,18 @@ #include "usbdetectdialog.h" #include "gutil/qgui.h" #include +#include #include #include #include -#include +#include #include #include UsbDetectDialog::UsbDetectDialog(QWidget *parent) : QDialog(parent) { - setAttribute(Qt::WA_DeleteOnClose); + setWindowFlag(Qt::WindowContextHelpButtonHint, 0); setWindowTitle(tr("Usb upgrade program")); - resize(240, 220); + resize(300, 260); auto vBox = new VBox(this); auto hBox = new HBox(vBox); @@ -23,43 +24,40 @@ UsbDetectDialog::UsbDetectDialog(QWidget *parent) : QDialog(parent) { fdPassword->setPlaceholderText(tr("Input password")); hBox->addWidget(fdPassword); - fdDrives = new QListWidget(); + fdDrives = new ListWgt; fdDrives->setSelectionRectVisible(true); - fdDrives->setProperty("ssType", "usbList"); - fdDrives->setProperty("ssName", "usbListName"); vBox->addWidget(fdDrives); - hBox = new HBox(vBox); - hBox->addStretch(); - auto bnOk = new QPushButton(tr("OK")); - bnOk->setProperty("ssType","progManageTool"); - connect(bnOk, &QPushButton::clicked, this, [this] { + auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + connect(btnBox, &QDialogButtonBox::accepted, this, [this] { auto selects = fdDrives->selectedItems(); if(selects.count() > 0) { - foreach(auto select, selects) emit acceptData(select->text(), fdPassword->text()); + foreach(auto select, selects) emit acceptData(select->data(Qt::UserRole).toString(), fdPassword->text()); accept(); return; } if(fdDrives->count() <= 0) QMessageBox::warning(this, tr("Tip"),tr("No checked USB device")); else QMessageBox::warning(this, tr("Tip"),tr("please select usb device in list")); }); - hBox->addWidget(bnOk); - hBox->addStretch(); - auto bnCancel = new QPushButton(tr("Cancel")); - bnCancel->setProperty("ssType","progManageTool"); - connect(bnCancel, &QPushButton::clicked, this, &QDialog::reject); - hBox->addWidget(bnCancel); - hBox->addStretch(); + connect(btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + vBox->addWidget(btnBox); detectDriver(); } - +#include void UsbDetectDialog::detectDriver() { -#ifdef Q_OS_WINDOWS - auto drives = QDir::drives(); //获取当前系统的盘符 -#else - auto drives = QDir("/Volumes").entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); -#endif fdDrives->clear(); - foreach(auto drive, drives) fdDrives->addItem(drive.fileName()); +//#ifdef Q_OS_WINDOWS + auto volumes = QStorageInfo::mountedVolumes(); + foreach(auto volume, volumes) fdDrives->addItem(volume.displayName(), volume.rootPath()); + foreach(auto volume, volumes) { + qDebug() << "rootPath" << volume.rootPath(); + qDebug() << "device" << volume.device(); + qDebug() << "name" << volume.name(); + qDebug() << "displayName" << volume.displayName(); + } +//#else +// auto drives = QDir("/Volumes").entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); +//foreach(auto drive, drives) fdDrives->addItem(drive.fileName(), drive.filePath()); +//#endif } diff --git a/LedOK/program/usbdetectdialog.h b/LedOK/program/usbdetectdialog.h index 7265fc8..f421088 100644 --- a/LedOK/program/usbdetectdialog.h +++ b/LedOK/program/usbdetectdialog.h @@ -1,8 +1,8 @@ #ifndef USBDETECTDIALOG_H #define USBDETECTDIALOG_H +#include "gutil/qgui.h" #include -#include class UsbDetectDialog : public QDialog { Q_OBJECT @@ -12,7 +12,7 @@ signals: void acceptData(QString string1, QString string2); private: void detectDriver(); - QListWidget *fdDrives; + ListWgt *fdDrives; QLineEdit *fdPassword; };