qt/LedOK/wProgramManager/progitem.cpp

182 lines
7.3 KiB
C++
Raw Normal View History

2022-09-30 18:08:39 +08:00
#include "progitem.h"
2022-08-25 18:37:24 +08:00
#include "tools.h"
#include "progeditorwin.h"
2022-10-27 15:07:45 +08:00
#include "gentmpthread.h"
#include "base/loemptydialog.h"
2022-01-04 18:11:48 +08:00
#include "usbdetectdialog.h"
#include <globaldefine.h>
#include "sendprogramdialog.h"
#include "mainwindow.h"
2022-10-27 15:07:45 +08:00
ProgItem::ProgItem(const QString &progsDir, const QString &name, int w, int h, const QString &remarks, int splitWidth, LoQTreeWidget *tree, ProgPanel *progPanel) : QTreeWidgetItem(UserType),
mName(name), mWidth(w), mHeight(h), mRemark(remarks), mSplitWidth(splitWidth), mProgsDir(progsDir), mProgPanel(progPanel), mTree(tree) {
m_last = QDateTime::currentDateTime();
mProgDir = progsDir + "/" + mName;
m_orgName = mName;
2022-01-04 18:11:48 +08:00
init();
}
2022-10-27 15:07:45 +08:00
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();
mSplitWidth = json["splitWidth"].toInt();
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;
2022-01-04 18:11:48 +08:00
init();
}
2022-09-30 18:08:39 +08:00
void ProgItem::init()
2022-01-04 18:11:48 +08:00
{
2022-08-25 18:37:24 +08:00
auto cfg = Tools::getInstance();
2022-10-27 15:07:45 +08:00
setData(ENUM_PROGRAMLISTHEADERITEM_NAME, 0,mName);
setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, 0, QString("%1 x %2").arg(mWidth).arg(mHeight));
2022-01-04 18:11:48 +08:00
if(m_fsize<=0)
2022-08-25 18:37:24 +08:00
setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, cfg->convertFileSize((qint64)100));
2022-01-04 18:11:48 +08:00
else
setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, cfg->convertFileSize(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);
2022-10-27 15:07:45 +08:00
mTree->insertTopLevelItem(0,this);
2022-01-04 18:11:48 +08:00
2022-10-27 15:07:45 +08:00
m_bnName = new QPushButton(mName);
2022-08-25 18:37:24 +08:00
m_bnName->setStyleSheet(R"delimiter(
QPushButton{border-radius: 4px;}
QPushButton:hover {
background-color: #cccccc;
text-decoration: underline;
}
)delimiter");
2022-01-04 18:11:48 +08:00
m_bnName->setCursor(QCursor(Qt::PointingHandCursor));
2022-10-27 15:07:45 +08:00
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_NAME, m_bnName);
connect(m_bnName, &QPushButton::clicked, this, [this] {
auto editor = new ProgEditorWin(this, gMainWin);
editor->show();
});
2022-01-04 18:11:48 +08:00
m_bnExport = new QPushButton();
m_bnExport->setCursor(QCursor(Qt::PointingHandCursor));
m_bnExport->setToolTip(tr("ExportButtonTip"));
2022-08-25 18:37:24 +08:00
m_bnExport->setStyleSheet(R"rrr(
QPushButton {
border-radius: 4px;
image: url(:/res/ProgramManager/bnExport_u.png);
}
QPushButton:pressed{image: url(:/res/ProgramManager/bnExport_s.png);}
QPushButton:hover{background-color: #cccccc;}
)rrr");
2022-10-27 15:07:45 +08:00
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_USB_EXPORT, m_bnExport);
2022-01-04 18:11:48 +08:00
connect(m_bnExport, SIGNAL(clicked()), this, SLOT(onUsbExportProgram()));
m_bnSend = new QPushButton();
m_bnSend->setCursor(QCursor(Qt::PointingHandCursor));
m_bnSend->setToolTip(tr("SendButtonTip"));
2022-08-25 18:37:24 +08:00
m_bnSend->setStyleSheet(R"rrr(
QPushButton{
border-radius: 4px;
image: url(:/res/ProgramManager/bnSend_u.png);
}
QPushButton:pressed{image: url(:/res/ProgramManager/bnSend_s.png);}
QPushButton:hover{background-color: #cccccc;}
)rrr");
2022-10-27 15:07:45 +08:00
mTree->setItemWidget(this, ENUM_PROGRAMLISTHEADERITEM_SEND, m_bnSend);
2022-01-04 18:11:48 +08:00
connect(m_bnSend, SIGNAL(clicked()), this, SLOT(onSendProgram()));
}
2022-10-27 15:07:45 +08:00
void ProgItem::refreshLable() {
2022-01-04 18:11:48 +08:00
m_bnExport->setToolTip(tr("ExportButtonTip"));
m_bnSend->setToolTip(tr("SendButtonTip"));
}
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;
mProgDir = mProgsDir + MACRO_FENGEFU + 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;
QJsonObject oSize;
obj["name"] = mName;
obj["resolution"] = QJsonObject{{"w", mWidth}, {"h", mHeight}};
obj["remarks"] = mRemark;
obj["splitWidth"] = mSplitWidth;
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");
2022-01-04 18:11:48 +08:00
f.open(QIODevice::WriteOnly);
f.write(json.toJson());
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() {
2022-08-25 18:37:24 +08:00
auto cfg = Tools::getInstance();
2022-10-27 15:07:45 +08:00
m_bnName->setText(mName);
setData(ENUM_PROGRAMLISTHEADERITEM_RESOLUTION, 0, QString("%1 x %2").arg(mWidth).arg(mHeight));
setData(ENUM_PROGRAMLISTHEADERITEM_SIZE, 0, cfg->convertFileSize(m_fsize<=0 ? 100 : m_fsize));
2022-01-04 18:11:48 +08:00
setData(ENUM_PROGRAMLISTHEADERITEM_LASTTIME, 0, m_last.toString("yyyy-MM-dd hh:mm:ss"));
save();
}
2022-09-30 18:08:39 +08:00
void ProgItem::onUsbExportProgram(){
2022-08-25 18:37:24 +08:00
auto pDlg = new UsbDetectDialog(gMainWin);
2022-09-30 18:08:39 +08:00
connect(pDlg, &UsbDetectDialog::acceptData, this, &ProgItem::onUsbExportProgramPro);
2022-08-25 18:37:24 +08:00
pDlg->exec();
2022-01-04 18:11:48 +08:00
}
2022-09-30 18:08:39 +08:00
void ProgItem::onUsbExportProgramPro(QString strPath, QString strPassword){
2022-10-27 15:07:45 +08:00
LoEmptyDialog *msgbox = new LoEmptyDialog(mProgPanel);
QString strUsbProgramName = mName;
auto converter = new GenTmpThread(this, strUsbProgramName, strPath + (strPath.endsWith('/') ? "program.zip" : "/program.zip"), strPassword, this);
2022-08-25 18:37:24 +08:00
connect(converter, &QThread::finished, converter, &QThread::deleteLater);
2022-01-04 18:11:48 +08:00
//转换结束发送信号触发converter释放内存
2022-08-25 18:37:24 +08:00
connect(converter, SIGNAL(finished()), msgbox, SLOT(unlock()));
connect(converter, SIGNAL(finished()), this, SLOT(UsbExportFinished()));
2022-01-04 18:11:48 +08:00
//链接转换进度信号到进度条槽函数
2022-08-25 18:37:24 +08:00
connect(converter, SIGNAL(sProgress(QString,int,bool)), msgbox->mIndicator, SLOT(onProgress(QString,int,bool)));
2022-01-04 18:11:48 +08:00
//链接进度对话框启动信号到转换开始槽函数
connect(msgbox, SIGNAL(startUp()), converter, SLOT(start()));
msgbox->lock(tr("Convertering")+"...",tr("USB Export Successed"),tr("USB Export TimeOut"));
msgbox->exec();
}
2022-09-30 18:08:39 +08:00
void ProgItem::onSendProgram(){
2022-10-27 15:07:45 +08:00
LoEmptyDialog *msgbox = new LoEmptyDialog(mProgPanel);
2022-01-04 18:11:48 +08:00
//转换节目
2022-10-27 15:07:45 +08:00
auto converter = new GenTmpThread(this, mName, "", "", this);
2022-08-25 18:37:24 +08:00
connect(converter, &QThread::finished, converter, &QThread::deleteLater);
2022-01-04 18:11:48 +08:00
//转换结束发送信号触发converter释放内存
connect(converter, SIGNAL(finished()), msgbox, SLOT(unlock()));
//链接转换进度信号到进度条槽函数
2022-08-25 18:37:24 +08:00
connect(converter, SIGNAL(sProgress(QString,int,bool)), msgbox->mIndicator, SLOT(onProgress(QString,int,bool)));
2022-01-04 18:11:48 +08:00
//链接进度对话框启动信号到转换开始槽函数
connect(msgbox, SIGNAL(startUp()), converter, SLOT(start()));
msgbox->lock(tr("Convertering")+"...",tr("Converter Successed"),tr("Converter TimeOut"));
msgbox->exec();
2022-10-27 15:07:45 +08:00
auto dlg = new SendProgramDialog(mProgPanel, mName, programsDir());
2022-08-25 18:37:24 +08:00
dlg->show();
2022-01-04 18:11:48 +08:00
}