qt/LedOK/program/sendprogramdialog.cpp

270 lines
12 KiB
C++
Raw Normal View History

2023-04-18 14:14:46 +08:00
#include "sendprogramdialog.h"
#include "cfg.h"
2023-05-11 11:47:00 +08:00
#include "sendprogthread.h"
2023-08-07 09:04:53 +08:00
#include "deviceitem.h"
#include "devicepanel.h"
#include "gutil/qnetwork.h"
#include "base/waitingdlg.h"
2023-04-18 14:14:46 +08:00
#include <QAction>
#include <QLineEdit>
#include <QHeaderView>
2023-05-05 17:22:54 +08:00
#include <QDialogButtonBox>
2023-08-07 09:04:53 +08:00
#include <QInputDialog>
#include <QMessageBox>
#include <QProgressBar>
2023-04-18 14:14:46 +08:00
SendProgramDialog::SendProgramDialog(QString progName, QWidget *parent) : QDialog(parent), mProgName(progName) {
2023-05-15 16:06:10 +08:00
#ifdef Q_OS_WIN
2023-04-18 14:14:46 +08:00
setWindowFlag(Qt::WindowMaximizeButtonHint);
2023-05-05 17:22:54 +08:00
#endif
2023-04-18 14:14:46 +08:00
setWindowTitle(tr("Publish")+" "+mProgName);
resize(1024, 700);
2023-05-05 17:22:54 +08:00
auto vBox = new VBox(this);
2023-08-07 09:04:53 +08:00
vBox->setContentsMargins(6,0,6,9);
2023-05-05 17:22:54 +08:00
auto hBox = new HBox(vBox);
2023-04-18 14:14:46 +08:00
label = new QLabel(tr("success info"));
hBox->addWidget(label);
hBox->addStretch();
2023-08-07 09:04:53 +08:00
auto btnRefresh = new QPushButton(tr("Refresh"));
btnRefresh->setProperty("ssType", "progManageTool");
connect(btnRefresh, &QPushButton::clicked, this, &SendProgramDialog::refresh);
hBox->addWidget(btnRefresh);
2023-04-18 14:14:46 +08:00
2023-08-07 09:04:53 +08:00
auto txtSearch = new QLineEdit;
2023-04-18 14:14:46 +08:00
txtSearch->setFixedWidth(150);
txtSearch->setClearButtonEnabled(true);
2023-08-07 09:04:53 +08:00
txtSearch->setStyleSheet("QLineEdit{border: 2px solid #aaa; padding: 2px;}");
2023-04-18 14:14:46 +08:00
txtSearch->addAction(new QAction(QIcon(":/res/program/bnSearch.png"), QString()), QLineEdit::LeadingPosition);
2023-08-07 09:04:53 +08:00
connect(txtSearch, &QLineEdit::textEdited, this, [=](const QString &text) {
auto cnt = table->rowCount();
for(int rr=0; rr<cnt; rr++) table->setRowHidden(rr, !(text.isEmpty() || table->text(rr, "id").contains(text) || table->text(rr, "ip").contains(text) || table->text(rr, "alias").contains(text) || table->text(rr, "size").contains(text)));
});
2023-04-18 14:14:46 +08:00
hBox->addWidget(txtSearch);
2023-08-07 09:04:53 +08:00
table = new Table{
{"id", "ID", 140},
{"online", tr("Online"), 40},
{"ip", "IP", 100},
{"size", tr("Screen Size"), 80},
{"alias", tr("Alias"), 120},
{"encrypt", tr("Security"), 40},
{"progress", tr("Progress"), 120},
{"remarks", tr("Remarks"), QHeaderView::Stretch}
};
table->setDefs();
auto colId = table->mFieldMap["id"];
table->sortItems(colId);
table->setSelectionMode(Table::NoSelection);
table->setStyle(new ViewItemStyle);
vBox->addWidget(table);
2023-04-18 14:14:46 +08:00
2023-08-07 09:04:53 +08:00
fdCheckAll = new QCheckBox(table);
connect(fdCheckAll, &QCheckBox::stateChanged, this, [=](int state) {
if(state==Qt::PartiallyChecked) return;
int cnt = table->rowCount();
for(int i=0; i<cnt; i++) table->itemValid(i, colId)->setCheckState((Qt::CheckState) state);
});
connect(table, &Table::updGeos, this, [=] {
fdCheckAll->move(table->verticalHeader()->size().width() + 8, 2);
});
auto cellClicked = [=](int row, int column) {
if(column!=colId) return;
auto item = table->itemValid(row, colId);
auto state = item->checkState()==Qt::Checked ? Qt::Unchecked : Qt::Checked;
item->setCheckState(state);
int cnt = table->rowCount();
fdCheckAll->blockSignals(true);
for(int i=0; i<cnt; i++) if(table->itemValid(i, colId)->checkState()!=state) {
fdCheckAll->setCheckState(Qt::PartiallyChecked);
goto end;
}
fdCheckAll->setCheckState(state);
end:fdCheckAll->blockSignals(false);
};
connect(table, &Table::cellClicked, table, cellClicked);
connect(table, &Table::cellEntered, table, cellClicked);
2023-04-18 14:14:46 +08:00
2023-05-05 17:22:54 +08:00
auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
btnBox->button(QDialogButtonBox::Ok)->setText(tr("Publish"));
connect(btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
2023-08-07 09:04:53 +08:00
connect(btnBox, &QDialogButtonBox::accepted, this, [=] {
2023-04-18 14:14:46 +08:00
if(mWaitCnt > 0) return;
2023-08-07 09:04:53 +08:00
int cnt = table->rowCount();
2023-04-18 14:14:46 +08:00
int sentCnt{0};
for(int i=0; i<cnt; i++) {
2023-08-07 09:04:53 +08:00
auto fdId = table->itemValid(i, colId);
if(fdId->checkState()!=Qt::Checked) continue;
if(fdId->data(isSending).toBool()) continue;
auto btnUnlock = (QPushButton*) table->cellWidget(i, "encrypt");
auto fdRemarks = table->itemValid(i, "remarks");
if(btnUnlock && btnUnlock->property("isLocked").toBool()) {
fdRemarks->setText(tr("This screen is encrypted"));
fdRemarks->setForeground(Qt::red);
2023-04-18 14:14:46 +08:00
continue;
}
2023-08-07 09:04:53 +08:00
auto fdProgress = (QProgressBar*) table->cellWidget(i, "progress");
fdProgress->setValue(0);
2023-04-18 14:14:46 +08:00
if(sentCnt>=5) {
2023-08-07 09:04:53 +08:00
fdRemarks->setText(" "+tr("Waiting")+" ...");
fdRemarks->setForeground(Qt::black);
2023-04-18 14:14:46 +08:00
mWaitCnt++;
continue;
}
2023-08-07 09:04:53 +08:00
fdId->setData(isSending, true);
fdRemarks->setText("");
auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", table->text(i, "ip"), 3333);
connect(sendProg, &SendProgThread::emErr, fdProgress, [=](QString strTip) {
fdId->setData(isSending, false);
2023-04-18 14:14:46 +08:00
if(strTip=="OK") {
2023-08-07 09:04:53 +08:00
fdId->setCheckState(Qt::Unchecked);
fdRemarks->setText("OK");
fdRemarks->setForeground(Qt::darkGreen);
2023-04-18 14:14:46 +08:00
} else {
2023-08-07 09:04:53 +08:00
fdRemarks->setText(strTip);
fdRemarks->setForeground(Qt::red);
2023-04-18 14:14:46 +08:00
}
sendNext();
});
2023-08-07 09:04:53 +08:00
connect(sendProg, &SendProgThread::emProgress, fdProgress, &QProgressBar::setValue);
2023-04-18 14:14:46 +08:00
connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop);
sendProg->start();
sentCnt++;
}
});
2023-05-05 17:22:54 +08:00
vBox->addWidget(btnBox);
2023-04-18 14:14:46 +08:00
2023-08-07 09:04:53 +08:00
refresh();
2023-04-18 14:14:46 +08:00
auto timer = new QTimer(this);
2023-08-07 09:04:53 +08:00
connect(timer, &QTimer::timeout, this, &SendProgramDialog::refresh);
2023-04-18 14:14:46 +08:00
timer->start(60000);
}
2023-08-07 09:04:53 +08:00
void SendProgramDialog::addRow(LedCard card) {
int cnt = table->rowCount();
for(int rr=0; rr<cnt; rr++) if(table->text(rr, "id") == card.id) {
updRow(rr, card);
return;
}
table->setSortingEnabled(false);
table->setRowCount(cnt+1);
auto item = table->setText(cnt, "id", card.id);
item->setCheckState(Qt::Unchecked);
item->setFlags(Qt::ItemIsEnabled);
auto fdOnline = new QLabel;
fdOnline->setAlignment(Qt::AlignCenter);
table->setCellWidget(cnt, "online", fdOnline);
auto fdProgress = new QProgressBar;
fdProgress->setAlignment(Qt::AlignCenter);
fdProgress->setStyleSheet("QProgressBar {margin-top: 6px; margin-bottom: 6px;}");
table->setCellWidget(cnt, "progress", fdProgress);
updRow(cnt, card);
table->setSortingEnabled(true);
}
void SendProgramDialog::updRow(int row, LedCard card) {
table->setText(row, "alias", card.alias);
table->setText(row, "ip", card.ip);
table->setText(row, "size", QString("%1 x %2").arg(card.mWidth).arg(card.mHeight))->setTextAlignment(Qt::AlignCenter);
((QLabel*) table->cellWidget(row, "online"))->setPixmap(QPixmap(card.isOnline ? ":/res/O_Online.png" : ":/res/O_Offline.png"));
if(! card.hasPassword) table->setCellWidget(row, "encrypt", 0);
else {
auto btnUnlock = (QPushButton*) table->cellWidget(row, "encrypt");
if(btnUnlock==0) {
btnUnlock = new QPushButton;
btnUnlock->setMaximumHeight(40);
btnUnlock->setProperty("isLocked", card.isLocked);
table->setCellWidget(row, "encrypt", btnUnlock);
connect(btnUnlock, &QPushButton::clicked, btnUnlock, [=] {
if(! btnUnlock->property("isLocked").toBool()) 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(this, tr("VerifyPassword")+" ...");
waitingDlg->show();
auto reply = NetReq("http://"+card.ip+":2016/settings").timeout(60000).post(json);
waitingDlg->connAbort(reply);
connect(reply, &QNetworkReply::finished, btnUnlock, [=] {
QJsonDocument json;
QString 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();
btnUnlock->setProperty("isLocked", false);
btnUnlock->setIcon(QIcon(":/res/UnLock.png"));
auto item = findItem(card.id);
if(item) {
item->mCard.isLocked = false;
item->btnUnlock->setIcon(QIcon(":/res/UnLock.png"));
}
});
});
}
btnUnlock->setIcon(QIcon(card.isLocked ? ":/res/Lock.png" : ":/res/UnLock.png")); //已验证显示绿色 没验证蓝色
}
}
2023-04-18 14:14:46 +08:00
void SendProgramDialog::closeEvent(QCloseEvent *) {
emit stopAllThd();
}
void SendProgramDialog::sendNext() {
if(mWaitCnt <= 0) return;
2023-08-07 09:04:53 +08:00
int cnt = table->rowCount();
2023-04-18 14:14:46 +08:00
for(int i=0; i<cnt; i++) {
2023-08-07 09:04:53 +08:00
auto fdId = table->itemValid(i, "id");
if(fdId->checkState()!=Qt::Checked) continue;
if(fdId->data(isSending).toBool()) continue;
auto btnUnlock = (QPushButton*) table->cellWidget(i, "encrypt");
if(btnUnlock && btnUnlock->property("isLocked").toBool()) continue;
auto fdRemarks = table->itemValid(i, "remarks");
if(! fdRemarks->text().startsWith(" ")) continue;
fdRemarks->setText("");
auto fdProgress = (QProgressBar*) table->cellWidget(i, "progress");
fdProgress->setValue(0);
fdId->setData(isSending, true);
2023-04-18 14:14:46 +08:00
mWaitCnt--;
2023-08-07 09:04:53 +08:00
auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", table->text(i, "ip"), 3333);
connect(sendProg, &SendProgThread::emErr, fdProgress, [=](QString strTip) {
fdId->setData(isSending, false);
2023-04-18 14:14:46 +08:00
if(strTip=="OK") {
2023-08-07 09:04:53 +08:00
fdId->setCheckState(Qt::Unchecked);
fdRemarks->setText("OK");
fdRemarks->setForeground(Qt::darkGreen);
2023-04-18 14:14:46 +08:00
} else {
2023-08-07 09:04:53 +08:00
fdRemarks->setText(strTip);
fdRemarks->setForeground(Qt::red);
2023-04-18 14:14:46 +08:00
}
sendNext();
});
2023-08-07 09:04:53 +08:00
connect(sendProg, &SendProgThread::emProgress, fdProgress, &QProgressBar::setValue);
2023-04-18 14:14:46 +08:00
connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop);
sendProg->start();
return;
}
}
2023-08-07 09:04:53 +08:00
void SendProgramDialog::refresh() {
2023-05-15 16:06:10 +08:00
int cnt = gDevicePanel->mDeviceTable->topLevelItemCount();
2023-08-07 09:04:53 +08:00
for(int i=0; i<cnt; i++) addRow(static_cast<DeviceItem*>(gDevicePanel->mDeviceTable->topLevelItem(i))->mCard);
label->setText(tr("All")+": "+QString::number(cnt));
2023-04-18 14:14:46 +08:00
}