qt/LedOK/program/progitem.cpp
2023-05-04 14:38:17 +08:00

211 lines
8.1 KiB
C++

#include "progitem.h"
#include "progpanel.h"
#include "QtWidgets/qdialogbuttonbox.h"
#include "QtWidgets/qlineedit.h"
#include "gutil/qcore.h"
#include "gutil/qgui.h"
#include "base/waitingdlg.h"
#include "tools.h"
#include "progeditorwin.h"
#include "base/waitingdlg.h"
#include "gentmpthread.h"
#include <globaldefine.h>
#include "sendprogramdialog.h"
#include <QJsonArray>
#include <QMessageBox>
#include <QStorageInfo>
ProgItem::ProgItem(const QString &progsDir, const QString &name, int w, int h, const QString &remarks, QList<int> &splitWidths, int maxWidth, LoQTreeWidget *tree, ProgPanel *progPanel) : QTreeWidgetItem(UserType),
mName(name), mWidth(w), mHeight(h), mRemark(remarks), mSplitWidths(splitWidths), mMaxWidth(maxWidth), mProgsDir(progsDir), mProgPanel(progPanel), mTree(tree) {
m_last = QDateTime::currentDateTime();
mProgDir = progsDir + "/" + mName;
m_orgName = mName;
init();
}
ProgItem::ProgItem(const QString &progsDir, const QJsonObject &json, LoQTreeWidget *tree, ProgPanel *progPanel) : QTreeWidgetItem(UserType), mProgsDir(progsDir), mProgPanel(progPanel), mTree(tree) {
mName = json["name"].toString();
mWidth = json["resolution"]["w"].toInt();
mHeight = json["resolution"]["h"].toInt();
mRemark = json["remarks"].toString();
auto splitWidths = json["splitWidths"].toArray();
foreach(auto splitWidth, splitWidths) {
int width = splitWidth.toInt();
if(mMaxWidth < width) mMaxWidth = width;
mSplitWidths.append(width);
}
m_fsize = json["file_size"].toDouble();
m_last = QDateTime::fromString(json["last_edit"].toString(), "yyyy-MM-dd hh:mm:ss");
mProgDir = progsDir + "/" + mName;
m_orgName = mName;
mProgPanel = progPanel;
init();
}
void ProgItem::init() {
setForeground(ENUM_PROGRAMLISTHEADERITEM_NAME, Qt::transparent);
setText(ENUM_PROGRAMLISTHEADERITEM_NAME, mName);
setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, 0, QString("%1 x %2").arg(mWidth).arg(mHeight));
if(m_fsize<=0) setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, byteSizeStr((qint64)100));
else setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, byteSizeStr(m_fsize));
setData(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, 0, m_last.toString("yyyy-MM-dd hh:mm:ss"));
setTextAlignment(ENUM_PROGRAMLISTHEADERITEM_NAME, Qt::AlignLeft | Qt::AlignVCenter);
for(int i=1; i<ENUM_PROGRAMLISTHEADERITEM_END; i++) setTextAlignment(i, Qt::AlignCenter);
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState(0, Qt::Unchecked);
mTree->insertTopLevelItem(0,this);
m_bnName = new QPushButton(mName);
m_bnName->setStyleSheet(R"delimiter(
QPushButton{border-radius: 4px;}
QPushButton:hover {
background-color: #cccccc;
text-decoration: underline;
}
)delimiter");
m_bnName->setCursor(QCursor(Qt::PointingHandCursor));
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_NAME, m_bnName);
connect(m_bnName, &QPushButton::clicked, this, [this] {
auto editor = new ProgEditorWin(this, gMainWin);
editor->show();
});
m_bnExport = new QPushButton();
m_bnExport->setCursor(QCursor(Qt::PointingHandCursor));
m_bnExport->setToolTip(tr("ExportButtonTip"));
m_bnExport->setStyleSheet(R"rrr(
QPushButton {
border-radius: 4px;
image: url(:/res/program/bnExport_u.png);
}
QPushButton:pressed{image: url(:/res/program/bnExport_s.png);}
QPushButton:hover{background-color: #cccccc;}
)rrr");
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, m_bnExport);
connect(m_bnExport, SIGNAL(clicked()), this, SLOT(onUsbExportProgram()));
m_bnSend = new QPushButton();
m_bnSend->setCursor(QCursor(Qt::PointingHandCursor));
m_bnSend->setToolTip(tr("SendButtonTip"));
m_bnSend->setStyleSheet(R"rrr(
QPushButton{
border-radius: 4px;
image: url(:/res/program/bnSend_u.png);
}
QPushButton:pressed{image: url(:/res/program/bnSend_s.png);}
QPushButton:hover{background-color: #cccccc;}
)rrr");
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_SEND, m_bnSend);
connect(m_bnSend, SIGNAL(clicked()), this, SLOT(onSendProgram()));
}
void ProgItem::refreshLable() {
m_bnExport->setToolTip(tr("ExportButtonTip"));
m_bnSend->setToolTip(tr("SendButtonTip"));
}
void ProgItem::save() {
QDir dRoot(mProgDir);
QDir dParent(mProgsDir);
if(!dRoot.exists()) {
dParent.mkdir(mName);
} else if(mName != m_orgName) {
dParent.rename(m_orgName, mName);
m_orgName = mName;
mProgDir = mProgsDir + "/" + mName;
}
dRoot = QDir(mProgDir);
if(dRoot.exists()) {
QJsonObject obj;
QJsonObject oSize;
obj["name"] = mName;
obj["resolution"] = QJsonObject{{"w", mWidth}, {"h", mHeight}};
obj["remarks"] = mRemark;
QJsonArray splitWidths;
foreach(auto splitWidth, mSplitWidths) splitWidths.append(splitWidth);
obj["splitWidths"] = splitWidths;
obj["file_size"] = m_fsize;
obj["last_edit"] = m_last.toString("yyyy-MM-dd hh:mm:ss");
QJsonDocument json(obj);
QFile f(mProgDir + "/pro.json");
f.open(QIODevice::WriteOnly);
f.write(json.toJson());
f.close();
}
}
void ProgItem::del() {
QDir dRoot(mProgDir);
if(dRoot.exists()) dRoot.removeRecursively();
}
//设置列表项的值
void ProgItem::onSetProgram() {
m_bnName->setText(mName);
setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, 0, QString("%1 x %2").arg(mWidth).arg(mHeight));
setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, byteSizeStr(m_fsize<=0 ? 100 : m_fsize));
setData(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, 0, m_last.toString("yyyy-MM-dd hh:mm:ss"));
save();
}
void ProgItem::onUsbExportProgram() {
QDialog dlg(gMainWin);
dlg.setWindowFlag(Qt::WindowContextHelpButtonHint, 0);
dlg.setWindowTitle(tr("Usb upgrade program"));
dlg.resize(300, 260);
auto vBox = new VBox(&dlg);
auto hBox = new HBox(vBox);
hBox->addWidget(new QLabel(tr("Password")));
auto fdPassword = new QLineEdit;
fdPassword->setEchoMode(QLineEdit::Password);
fdPassword->setPlaceholderText(tr("Input password"));
hBox->addWidget(fdPassword);
auto fdDrives = new ListWgt;
fdDrives->setSelectionRectVisible(true);
vBox->addWidget(fdDrives);
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(btnBox, &QDialogButtonBox::accepted, &dlg, [=, &dlg] {
auto selects = fdDrives->selectedItems();
if(selects.count() > 0) {
foreach(auto select, selects) {
auto strPath = select->data(Qt::UserRole).toString();
auto waitingDlg = new WaitingDlg(mProgPanel, tr("Convertering")+" ...");
auto converter = new GenTmpThread(this, mName, strPath + (strPath.endsWith('/') ? "program.zip" : "/program.zip"), fdPassword->text(), this);
connect(converter, &QThread::finished, waitingDlg, &WaitingDlg::success);
connect(converter, &GenTmpThread::sProgress, waitingDlg->mIndicator, &CustomProgressIndicator::onProgress);
converter->start();
waitingDlg->exec();
}
dlg.accept();
return;
}
if(fdDrives->count() <= 0) QMessageBox::warning(&dlg, tr("Tip"),tr("No checked USB device"));
else QMessageBox::warning(&dlg, tr("Tip"),tr("please select usb device in list"));
});
connect(btnBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
vBox->addWidget(btnBox);
fdDrives->clear();
auto volumes = QStorageInfo::mountedVolumes();
foreach(auto volume, volumes) fdDrives->addItem(volume.displayName(), volume.rootPath());
dlg.exec();
}
void ProgItem::onSendProgram() {
auto waitingDlg = new WaitingDlg(mProgPanel, tr("Convertering")+" ...");
auto converter = new GenTmpThread(this, mName, "", "", this);
connect(converter, &QThread::finished, waitingDlg, &WaitingDlg::close);
connect(converter, &GenTmpThread::sProgress, waitingDlg->mIndicator, &CustomProgressIndicator::onProgress);
converter->start();
waitingDlg->exec();
SendProgramDialog dlg(mName, mProgPanel);
dlg.exec();
}