#include "sendprogramdialog.h" #include "cfg.h" #include "sendprogthread.h" #include "deviceitem.h" #include "devicepanel.h" #include "gutil/qnetwork.h" #include "base/waitingdlg.h" #include #include #include #include #include #include #include SendProgramDialog::SendProgramDialog(QString progName, QWidget *parent) : QDialog(parent), mProgName(progName) { #ifdef Q_OS_WIN setWindowFlag(Qt::WindowMaximizeButtonHint); #endif setWindowTitle(tr("Publish")+" "+mProgName); resize(1024, 700); auto vBox = new VBox(this); vBox->setContentsMargins(6,0,6,9); auto hBox = new HBox(vBox); label = new QLabel(tr("success info")); hBox->addWidget(label); hBox->addStretch(); auto btnRefresh = new QPushButton(tr("Refresh")); btnRefresh->setProperty("ssType", "progManageTool"); connect(btnRefresh, &QPushButton::clicked, this, &SendProgramDialog::refresh); 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->rowCount(); for(int rr=0; rrsetRowHidden(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))); }); hBox->addWidget(txtSearch); table = new Table{ {"id", "ID", 130}, {"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); fdCheckAll = new QCheckBox(table); connect(fdCheckAll, &QCheckBox::stateChanged, this, [=](int state) { if(state==Qt::PartiallyChecked) return; int cnt = table->rowCount(); for(int rr=0; rrisRowHidden(rr)) table->itemValid(rr, 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 rr=0; rrisRowHidden(rr) && table->itemValid(rr, 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, [=](int row, int column) { if(column!=colId) return; if((QApplication::mouseButtons() & Qt::LeftButton) == 0) return; cellClicked(row, column); }); auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); btnBox->button(QDialogButtonBox::Ok)->setText(tr("Publish")); connect(btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(btnBox, &QDialogButtonBox::accepted, this, [=] { if(mWaitCnt > 0) return; int cnt = table->rowCount(); int sentCnt{0}; for(int rr=0; rrisRowHidden(rr)) { auto fdId = table->itemValid(rr, colId); if(fdId->checkState()!=Qt::Checked) continue; if(fdId->data(isSending).toBool()) continue; auto btnUnlock = (QPushButton*) table->cellWidget(rr, "encrypt"); auto fdRemarks = table->itemValid(rr, "remarks"); if(btnUnlock && btnUnlock->property("isLocked").toBool()) { fdRemarks->setText(tr("This screen is encrypted")); fdRemarks->setForeground(Qt::red); continue; } auto fdProgress = (QProgressBar*) table->cellWidget(rr, "progress"); fdProgress->setValue(0); if(sentCnt>=5) { fdRemarks->setText(" "+tr("Waiting")+" ..."); fdRemarks->setForeground(Qt::black); mWaitCnt++; continue; } fdId->setData(isSending, true); fdRemarks->setText(""); auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", table->text(rr, "ip"), 3333); connect(sendProg, &SendProgThread::emErr, fdProgress, [=](QString strTip) { fdId->setData(isSending, false); if(strTip=="OK") { fdId->setCheckState(Qt::Unchecked); fdRemarks->setText("OK"); fdRemarks->setForeground(Qt::darkGreen); } else { fdRemarks->setText(strTip); fdRemarks->setForeground(Qt::red); } sendNext(); }); connect(sendProg, &SendProgThread::emProgress, fdProgress, &QProgressBar::setValue); connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop); sendProg->start(); sentCnt++; } }); vBox->addWidget(btnBox); refresh(); auto timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &SendProgramDialog::refresh); timer->start(60000); } void SendProgramDialog::addRow(LedCard card) { int cnt = table->rowCount(); for(int rr=0; rrtext(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(item->flags() & ~Qt::ItemIsUserCheckable); 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")); //已验证显示绿色 没验证蓝色 } } void SendProgramDialog::closeEvent(QCloseEvent *) { emit stopAllThd(); } void SendProgramDialog::sendNext() { if(mWaitCnt <= 0) return; int cnt = table->rowCount(); for(int rr=0; rrisRowHidden(rr)) { auto fdId = table->itemValid(rr, "id"); if(fdId->checkState()!=Qt::Checked) continue; if(fdId->data(isSending).toBool()) continue; auto btnUnlock = (QPushButton*) table->cellWidget(rr, "encrypt"); if(btnUnlock && btnUnlock->property("isLocked").toBool()) continue; auto fdRemarks = table->itemValid(rr, "remarks"); if(! fdRemarks->text().startsWith(" ")) continue; fdRemarks->setText(""); auto fdProgress = (QProgressBar*) table->cellWidget(rr, "progress"); fdProgress->setValue(0); fdId->setData(isSending, true); mWaitCnt--; auto sendProg = new SendProgThread(programsDir()+"/"+mProgName+"_tmp", table->text(rr, "ip"), 3333); connect(sendProg, &SendProgThread::emErr, fdProgress, [=](QString strTip) { fdId->setData(isSending, false); if(strTip=="OK") { fdId->setCheckState(Qt::Unchecked); fdRemarks->setText("OK"); fdRemarks->setForeground(Qt::darkGreen); } else { fdRemarks->setText(strTip); fdRemarks->setForeground(Qt::red); } sendNext(); }); connect(sendProg, &SendProgThread::emProgress, fdProgress, &QProgressBar::setValue); connect(this, &SendProgramDialog::stopAllThd, sendProg, &SendProgThread::stop); sendProg->start(); return; } } void SendProgramDialog::refresh() { int cnt = gDevicePanel->mDeviceTable->topLevelItemCount(); for(int rr=0; rrmDeviceTable->topLevelItem(rr))->mCard); label->setText(tr("All")+": "+QString::number(cnt)); }