qt/LedOK/program/progitem.cpp

208 lines
7.7 KiB
C++
Raw Normal View History

2022-09-30 18:08:39 +08:00
#include "progitem.h"
2023-05-04 14:38:17 +08:00
#include "QtWidgets/qdialogbuttonbox.h"
#include "QtWidgets/qlineedit.h"
2023-04-18 14:14:46 +08:00
#include "gutil/qcore.h"
2023-05-04 14:38:17 +08:00
#include "gutil/qgui.h"
2023-09-19 11:49:20 +08:00
#include "gutil/qwaitingdlg.h"
#include "gutil/qwaitingdlg.h"
2022-10-27 15:07:45 +08:00
#include "gentmpthread.h"
2022-01-04 18:11:48 +08:00
#include <globaldefine.h>
#include "sendprogramdialog.h"
2023-09-19 11:49:20 +08:00
#include "program/progeditorwin.h"
2023-04-18 14:14:46 +08:00
#include <QJsonArray>
2023-05-04 14:38:17 +08:00
#include <QMessageBox>
#include <QStorageInfo>
2022-01-04 18:11:48 +08:00
2023-09-19 11:49:20 +08:00
ProgItem::ProgItem(const QString &progsDir, const QString &name, int w, int h, const QString &remarks, QList<int> &splitWidths, int maxWidth, LoQTreeWidget *tree) : TreeWidgetItem(tree),
mName(name), mWidth(w), mHeight(h), mRemark(remarks), mSplitWidths(splitWidths), mMaxWidth(maxWidth), mProgsDir(progsDir) {
2022-10-27 15:07:45 +08:00
m_last = QDateTime::currentDateTime();
mProgDir = progsDir + "/" + mName;
m_orgName = mName;
2022-01-04 18:11:48 +08:00
init();
}
2023-09-19 11:49:20 +08:00
ProgItem::ProgItem(const QString &progsDir, const QJsonObject &json, LoQTreeWidget *tree) : TreeWidgetItem(tree), mProgsDir(progsDir) {
2022-10-27 15:07:45 +08:00
mName = json["name"].toString();
mWidth = json["resolution"]["w"].toInt();
mHeight = json["resolution"]["h"].toInt();
mRemark = json["remarks"].toString();
2023-04-18 14:14:46 +08:00
auto splitWidths = json["splitWidths"].toArray();
foreach(auto splitWidth, splitWidths) {
int width = splitWidth.toInt();
if(mMaxWidth < width) mMaxWidth = width;
mSplitWidths.append(width);
}
2022-10-27 15:07:45 +08:00
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;
2022-01-04 18:11:48 +08:00
init();
}
2022-09-30 18:08:39 +08:00
2023-04-18 14:14:46 +08:00
void ProgItem::init() {
2023-09-19 11:49:20 +08:00
setFlags(flags() & ~Qt::ItemIsUserCheckable);
setCheckState("check", Qt::Unchecked);
2023-08-01 11:42:41 +08:00
auto ft = font(0);
ft.setPixelSize(14);
2023-09-19 11:49:20 +08:00
for(int i=1; i<treeWidget()->columnCount()-2; i++) {
setFont(i, ft);
setTextAlignment(i, Qt::AlignCenter);
}
setForeground("name"**treeWidget(), Qt::transparent);
setText("name", mName);
setText("resolution", QString("%1 x %2").arg(mWidth).arg(mHeight));
setText("size", m_fsize<=0 ? "100B" : byteSizeStr(m_fsize));
setText("lasttime", m_last.toString("yyyy-MM-dd hh:mm:ss"));
2022-01-04 18:11:48 +08:00
2022-10-27 15:07:45 +08:00
m_bnName = new QPushButton(mName);
2023-08-01 11:42:41 +08:00
m_bnName->setFont(ft);
2023-09-19 11:49:20 +08:00
m_bnName->setCursor(QCursor(Qt::PointingHandCursor));
2022-08-25 18:37:24 +08:00
m_bnName->setStyleSheet(R"delimiter(
QPushButton{border-radius: 4px;}
QPushButton:hover {
2023-09-19 11:49:20 +08:00
background-color: #ccc;
2022-08-25 18:37:24 +08:00
text-decoration: underline;
}
)delimiter");
2023-09-19 11:49:20 +08:00
QObject::connect(m_bnName, &QPushButton::clicked, treeWidget(), [=] {
auto editor = new ProgEditorWin(this, treeWidget()->parentWidget());
2023-04-28 16:02:14 +08:00
editor->show();
2022-10-27 15:07:45 +08:00
});
2023-09-19 11:49:20 +08:00
setCellWidget("name", m_bnName);
auto btnExport = new QPushButton;
btnExport->setCursor(QCursor(Qt::PointingHandCursor));
btnExport->setStyleSheet(R"rrr(
2022-08-25 18:37:24 +08:00
QPushButton {
border-radius: 4px;
2023-04-18 14:14:46 +08:00
image: url(:/res/program/bnExport_u.png);
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
QPushButton:pressed{image: url(:/res/program/bnExport_s.png);}
2022-08-25 18:37:24 +08:00
QPushButton:hover{background-color: #cccccc;}
)rrr");
2023-09-19 11:49:20 +08:00
QObject::connect(btnExport, &QPushButton::clicked, treeWidget(), [=] {
QDialog dlg(treeWidget());
2023-08-07 09:04:53 +08:00
dlg.setWindowTitle(QObject::tr("USB Update Program"));
2023-05-11 11:47:00 +08:00
#ifdef Q_OS_WIN
dlg.setWindowFlag(Qt::WindowContextHelpButtonHint, 0);
#endif
dlg.resize(300, 260);
auto vBox = new VBox(&dlg);
auto hBox = new HBox(vBox);
hBox->addWidget(new QLabel(QObject::tr("Password")));
auto fdPassword = new QLineEdit;
fdPassword->setEchoMode(QLineEdit::Password);
fdPassword->setPlaceholderText(QObject::tr("Input password"));
hBox->addWidget(fdPassword);
auto fdDrives = new ListWgt;
fdDrives->setSelectionRectVisible(true);
vBox->addWidget(fdDrives);
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QObject::connect(btnBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
QObject::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(&dlg, QObject::tr("Convertering")+" ...");
auto gen = new GenTmpThread(this, mName, strPath + (strPath.endsWith('/') ? "program.zip" : "/program.zip"), fdPassword->text());
QObject::connect(gen, &GenTmpThread::onErr, &dlg, [=, &dlg](QString err) {
QMessageBox::warning(&dlg, "GenTmpThread Error", err);
});
QObject::connect(gen, &QThread::finished, waitingDlg, &WaitingDlg::success);
gen->start();
waitingDlg->exec();
}
dlg.accept();
return;
}
if(fdDrives->count() <= 0) QMessageBox::warning(&dlg, QObject::tr("Tip"), QObject::tr("No checked USB device"));
else QMessageBox::warning(&dlg, QObject::tr("Tip"), QObject::tr("please select usb device in list"));
});
vBox->addWidget(btnBox);
fdDrives->clear();
auto volumes = QStorageInfo::mountedVolumes();
2023-09-19 11:49:20 +08:00
foreach(auto volume, volumes) {
auto text = volume.displayName();
auto rootPath = volume.rootPath();
if(text!=rootPath) text += " ("+rootPath+")";
fdDrives->addItem(text, rootPath);
}
2023-05-11 11:47:00 +08:00
dlg.exec();
});
2023-09-19 11:49:20 +08:00
setCellWidget("usb", btnExport);
2022-01-04 18:11:48 +08:00
2023-09-19 11:49:20 +08:00
auto btnSend = new QPushButton;
btnSend->setCursor(QCursor(Qt::PointingHandCursor));
btnSend->setStyleSheet(R"rrr(
2022-08-25 18:37:24 +08:00
QPushButton{
border-radius: 4px;
2023-04-18 14:14:46 +08:00
image: url(:/res/program/bnSend_u.png);
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
QPushButton:pressed{image: url(:/res/program/bnSend_s.png);}
2022-08-25 18:37:24 +08:00
QPushButton:hover{background-color: #cccccc;}
)rrr");
2023-09-19 11:49:20 +08:00
QObject::connect(btnSend, &QPushButton::clicked, treeWidget(), [this] {
auto waitingDlg = new WaitingDlg(treeWidget(), QObject::tr("Convertering")+" ...");
2023-05-11 11:47:00 +08:00
auto gen = new GenTmpThread(this, mName, "", "");
2023-09-19 11:49:20 +08:00
QObject::connect(gen, &GenTmpThread::onErr, treeWidget(), [=](QString err) {
QMessageBox::warning(treeWidget(), "GenTmpThread Error", err);
2023-05-11 11:47:00 +08:00
});
QObject::connect(gen, &QThread::finished, waitingDlg, &WaitingDlg::close);
gen->start();
waitingDlg->exec();
2023-09-19 11:49:20 +08:00
SendProgramDialog dlg(mName, treeWidget()->window());
2023-05-11 11:47:00 +08:00
dlg.exec();
});
2023-09-19 11:49:20 +08:00
setCellWidget("send", btnSend);
2022-01-04 18:11:48 +08:00
}
2022-09-30 18:08:39 +08:00
void ProgItem::save() {
2022-08-25 18:37:24 +08:00
QDir dRoot(mProgDir);
2022-10-27 15:07:45 +08:00
QDir dParent(mProgsDir);
2022-01-04 18:11:48 +08:00
if(!dRoot.exists()) {
2022-10-27 15:07:45 +08:00
dParent.mkdir(mName);
} else if(mName != m_orgName) {
dParent.rename(m_orgName, mName);
m_orgName = mName;
2023-04-18 14:14:46 +08:00
mProgDir = mProgsDir + "/" + mName;
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
dRoot = QDir(mProgDir);
2022-01-04 18:11:48 +08:00
if(dRoot.exists()) {
2022-10-27 15:07:45 +08:00
QJsonObject obj;
obj["name"] = mName;
obj["resolution"] = QJsonObject{{"w", mWidth}, {"h", mHeight}};
obj["remarks"] = mRemark;
2023-04-18 14:14:46 +08:00
QJsonArray splitWidths;
foreach(auto splitWidth, mSplitWidths) splitWidths.append(splitWidth);
obj["splitWidths"] = splitWidths;
2022-10-27 15:07:45 +08:00
obj["file_size"] = m_fsize;
obj["last_edit"] = m_last.toString("yyyy-MM-dd hh:mm:ss");
QFile f(mProgDir + "/pro.json");
2022-01-04 18:11:48 +08:00
f.open(QIODevice::WriteOnly);
2023-09-19 11:49:20 +08:00
f.write(QJsonDocument(obj).toJson());
2022-01-04 18:11:48 +08:00
f.close();
}
}
2022-10-27 15:07:45 +08:00
void ProgItem::del() {
2022-08-25 18:37:24 +08:00
QDir dRoot(mProgDir);
2022-10-27 15:07:45 +08:00
if(dRoot.exists()) dRoot.removeRecursively();
2022-01-04 18:11:48 +08:00
}
//设置列表项的值
2022-10-27 15:07:45 +08:00
void ProgItem::onSetProgram() {
m_bnName->setText(mName);
2023-09-19 11:49:20 +08:00
setText("resolution", QString("%1 x %2").arg(mWidth).arg(mHeight));
setText("size", byteSizeStr(m_fsize<=0 ? 100 : m_fsize));
setText("lasttime", m_last.toString("yyyy-MM-dd hh:mm:ss"));
2022-01-04 18:11:48 +08:00
save();
}