qt/LedOK/program/sendprogramdialog.cpp
2023-10-23 11:44:22 +08:00

227 lines
10 KiB
C++

#include "sendprogramdialog.h"
#include "cfg.h"
#include "sendprogthread.h"
#include "deviceitem.h"
#include "devicepanel.h"
#include "gutil/qnetwork.h"
#include "gutil/qwaitingdlg.h"
#include <QAction>
#include <QLineEdit>
#include <QHeaderView>
#include <QDialogButtonBox>
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressBar>
SendProgramDialog::SendProgramDialog(QString progName, QWidget *parent) : QDialog(parent), mProgName(progName) {
#ifdef Q_OS_WIN
setWindowFlag(Qt::WindowMaximizeButtonHint);
#endif
setWindowTitle(tr("Publish")+" "+mProgName);
resize(1024, 720);
auto vBox = new VBox(this);
vBox->setContentsMargins(6,0,6,0);
auto hBox = new HBox(vBox);
label = new QLabel(tr("success info"));
hBox->addWidget(label);
hBox->addSpacing(20);
auto btnPublish = new QPushButton(tr("Publish"));
connect(btnPublish, &QPushButton::clicked, this, [=] {
if(mWaitCnt > 0) return;
int cnt = table->topLevelItemCount();
int sentCnt = 0;
SendProgramItem *item;
auto remarks = "remarks"**table;
for(int rr=0; rr<cnt; rr++) if(! (item = (SendProgramItem*) table->topLevelItem(rr))->isHidden() && item->checkState("id") == Qt::Checked && ! item->isSending) {
if(item->btnUnlock && item->isLocked) {
item->setResult(tr("This screen is encrypted"), Qt::red);
continue;
}
item->fdProgress->setValue(0);
if(sentCnt>=gSendBatch) {
item->setText(remarks, " "+tr("Waiting")+" ...");
item->setForeground(remarks, Qt::black);
mWaitCnt++;
continue;
}
item->isSending = true;
item->setText(remarks, "");
auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", item->text("ip"), 3333);
connect(sendProg, &SendProgThread::emErr, item->fdProgress, [=](QString strTip) {
item->isSending = false;
if(strTip=="OK") {
item->setCheckState("id", Qt::Unchecked);
item->setText(remarks, "OK");
item->setForeground(remarks, Qt::darkGreen);
} else {
item->setText(remarks, strTip);
item->setForeground(remarks, Qt::red);
}
sendNext();
});
connect(sendProg, &SendProgThread::emProgress, item->fdProgress, &QProgressBar::setValue);
connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop);
sendProg->start();
sentCnt++;
}
});
hBox->addWidget(btnPublish);
hBox->addStretch();
auto btnRefresh = new QPushButton(tr("Refresh"));
connect(btnRefresh, &QPushButton::clicked, this, [=] {
int cnt = gDevicePanel->mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) {
auto card = ((DeviceItem*)gDevicePanel->mDeviceTable->topLevelItem(i))->mCard;
int cnt = table->topLevelItemCount();
SendProgramItem *item;
for(int rr=0; rr<cnt; rr++) if((item = (SendProgramItem*) table->topLevelItem(rr))->text("id") == card.id) goto end;
item = new SendProgramItem(table);
item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
item->setCheckState("id", Qt::Unchecked);
item->setText("id", card.id);
{
item->fdOnline = new QLabel;
item->fdOnline->setScaledContents(true);
item->fdOnline->setFixedSize(24, 24);
auto wgt = new QWidget;
auto hhh = new HBox(wgt);
hhh->setContentsMargins(0,0,0,0);
hhh->addWidget(item->fdOnline);
item->setCellWidget("online", wgt);
}
item->fdProgress = new QProgressBar;
item->fdProgress->setAlignment(Qt::AlignCenter);
item->fdProgress->setStyleSheet("QProgressBar {margin-top: 6px; margin-bottom: 6px;}");
item->setCellWidget("progress", item->fdProgress);
end:
item->setText("ip", card.ip);
item->setText("alias", card.alias);
item->setText("size", QString("%1 x %2").arg(card.mWidth).arg(card.mHeight));
item->fdOnline->setPixmap({card.isOnline ? ":/res/online.png" : ":/res/offline.png"});
if(! card.hasPassword) {
item->isLocked = false;
item->setCellWidget("encrypt", item->btnUnlock = 0);
} else {
item->isLocked = card.isLocked;
if(item->btnUnlock==0) {
item->btnUnlock = new QPushButton;
item->btnUnlock->setMaximumHeight(36);
item->setCellWidget("encrypt", item->btnUnlock);
connect(item->btnUnlock, &QPushButton::clicked, item->btnUnlock, [=] {
if(! item->isLocked) return;
bool ok;
auto pwd = QInputDialog::getText(this, tr("Input password"), tr("Input password"), QLineEdit::Password, QString(), &ok);
if(! ok) return;
QJsonObject json;
json.insert("_id", "VerifyPassword");
json.insert("_type", "VerifyPassword");
json.insert("pwd", pwd);
auto waitingDlg = new WaitingDlg(item->btnUnlock, tr("VerifyPassword")+" ...");
waitingDlg->show();
auto reply = NetReq("http://"+card.ip+":2016/settings").timeout(30000).post(json);
ConnReply(reply, waitingDlg) [=] {
QJsonDocument json;
auto err = checkReplyForJson(reply, &json);
if(! err.isEmpty()) {
waitingDlg->close();
QMessageBox::critical(this, QObject::tr("Error"), err);
return;
}
if(! json["result"].toBool()) {
waitingDlg->close();
QMessageBox::warning(this, tr("Tip Info"), tr("password is wrong"));
return;
}
waitingDlg->success();
item->isLocked = false;
item->btnUnlock->setIcon(QIcon(":/res/UnLock.png"));
auto item = findItem(card.id);
if(item) {
item->mCard.isLocked = false;
item->btnUnlock->setIcon(QIcon(":/res/UnLock.png"));
}
});
});
}
item->btnUnlock->setIcon(QIcon(card.isLocked ? ":/res/Lock.png" : ":/res/UnLock.png")); //已验证显示绿色 没验证蓝色
}
}
label->setText(tr("All")+": "+QString::number(cnt));
});
hBox->addWidget(btnRefresh);
auto txtSearch = new QLineEdit;
txtSearch->setFixedWidth(150);
txtSearch->setClearButtonEnabled(true);
txtSearch->setStyleSheet("QLineEdit{border: 2px solid #aaa; padding: 2px;}");
txtSearch->addAction(new QAction(QIcon(":/res/program/bnSearch.png"), QString()), QLineEdit::LeadingPosition);
connect(txtSearch, &QLineEdit::textEdited, this, [=](const QString &text) {
auto cnt = table->topLevelItemCount();
for(int rr=0; rr<cnt; rr++) {
auto item = table->item(rr);
item->setHidden(! (text.isEmpty() || item->text("id").contains(text) || item->text("ip").contains(text) || item->text("alias").contains(text) || item->text("size").contains(text)));
}
});
hBox->addWidget(txtSearch);
table = new LoQTreeWidget;
table->addCol("#", "", 20);
table->addCol("id", "ID", 130).margin(4);
table->addCol("online", tr("Online"), 40);
table->addCol("ip", "IP", 100);
table->addCol("size", tr("Screen Size"), 80).alignC(),
table->addCol("alias", tr("Alias"), 120);
table->addCol("encrypt", tr("Security"), 40);
table->addCol("progress", tr("Progress"), 120);
table->addCol("remarks", tr("Remarks"), QHeaderView::Stretch);
table->setDefs()->setHeaderAlignC();
table->addFd();
table->setSelectionMode(QAbstractItemView::NoSelection);
table->setSortingEnabled(true);
vBox->addWidget(table);
auto sortField = gDevicePanel->mDeviceTable->sortField();
if(sortField=="id" || sortField=="ip" || sortField=="alias") table->sortItems(sortField, gDevicePanel->mDeviceTable->header()->sortIndicatorOrder());
else table->sortItems("id");
emit btnRefresh->clicked();
}
void SendProgramDialog::closeEvent(QCloseEvent *) {
emit stopAllThd();
}
void SendProgramDialog::sendNext() {
if(mWaitCnt <= 0) return;
int cnt = table->topLevelItemCount();
SendProgramItem *item;
auto remarks = "remarks"**table;
for(int rr=0; rr<cnt; rr++) if(! (item = (SendProgramItem*) table->item(rr))->isHidden() && item->checkState("id") == Qt::Checked && ! item->isSending && ! item->isLocked && item->text(remarks).startsWith(" ")) {
item->setText(remarks, "");
item->fdProgress->setValue(0);
item->isSending = true;
mWaitCnt--;
auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", item->text("ip"), 3333);
connect(sendProg, &SendProgThread::emErr, item->fdProgress, [=](QString strTip) {
item->isSending = false;
if(strTip=="OK") {
item->setCheckState("id", Qt::Unchecked);
item->setText(remarks, "OK");
item->setForeground(remarks, Qt::darkGreen);
} else {
item->setText(remarks, strTip);
item->setForeground(remarks, Qt::red);
}
sendNext();
});
connect(sendProg, &SendProgThread::emProgress, item->fdProgress, &QProgressBar::setValue);
connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop);
sendProg->start();
return;
}
}