From 7686eab57f640dd41f9c3ee8b7e080767c5930e0 Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Thu, 27 Apr 2023 10:20:54 +0300 Subject: [PATCH] Use async WarningDialog (#1182) IB-7665 Signed-off-by: Raul Metsma --- client/Application.cpp | 11 ++---- client/Application.h | 1 - client/CryptoDoc.cpp | 2 +- client/DigiDoc.cpp | 50 ++++++++++++------------- client/DocumentModel.cpp | 7 +--- client/MainWindow.cpp | 62 ++++++++++++++----------------- client/QSigner.cpp | 11 +++--- client/dialogs/AddRecipients.cpp | 20 +++++----- client/dialogs/SettingsDialog.cpp | 20 +++++----- client/dialogs/WarningDialog.cpp | 43 ++++++++++++--------- client/dialogs/WarningDialog.h | 1 + client/dialogs/WarningDialog.ui | 26 ++++++------- client/widgets/ContainerPage.cpp | 6 +-- client/widgets/FileList.cpp | 28 +++++++------- client/widgets/SignatureItem.cpp | 10 +++-- 15 files changed, 148 insertions(+), 150 deletions(-) diff --git a/client/Application.cpp b/client/Application.cpp index 6a8c966c9..43665da75 100644 --- a/client/Application.cpp +++ b/client/Application.cpp @@ -776,11 +776,11 @@ bool Application::notify(QObject *object, QEvent *event) } catch(const std::bad_alloc &e) { - showWarning(tr("Added file(s) exceeds the maximum size limit of the container(120MB)."), QString::fromLocal8Bit(e.what())); + WarningDialog::show(tr("Added file(s) exceeds the maximum size limit of the container(120MB)."), QString::fromLocal8Bit(e.what())); } catch(...) { - showWarning( tr("Caught exception!") ); + WarningDialog::show(tr("Caught exception!")); } return false; @@ -939,12 +939,7 @@ void Application::showWarning( const QString &msg, const digidoc::Exception &e ) { digidoc::Exception::ExceptionCode code = digidoc::Exception::General; QStringList causes = DigiDoc::parseException(e, code); - WarningDialog::show(mainWindow(), msg, causes.join('\n')); -} - -void Application::showWarning( const QString &msg, const QString &details ) -{ - WarningDialog(msg, details, mainWindow()).exec(); + WarningDialog::show(msg, causes.join('\n')); } QSigner* Application::signer() const { return d->signer; } diff --git a/client/Application.h b/client/Application.h index 488815683..e66e3dbc6 100644 --- a/client/Application.h +++ b/client/Application.h @@ -78,7 +78,6 @@ class Application final: public Common public Q_SLOTS: void showClient(const QStringList ¶ms = {}, bool crypto = false, bool sign = false, bool newWindow = false); - static void showWarning(const QString &msg, const QString &details = {}); private Q_SLOTS: void parseArgs(const QString &msg = {}); diff --git a/client/CryptoDoc.cpp b/client/CryptoDoc.cpp index 40e048f9b..04a535cfc 100644 --- a/client/CryptoDoc.cpp +++ b/client/CryptoDoc.cpp @@ -95,7 +95,7 @@ class CryptoDoc::Private final: public QThread void run() final; static void showError(const QString &err, const QString &details = {}) { - WarningDialog::show(Application::mainWindow(), err, details); + WarningDialog::show(err, details); } static QString size(const QString &size) { diff --git a/client/DigiDoc.cpp b/client/DigiDoc.cpp index db738fa07..34b57190d 100644 --- a/client/DigiDoc.cpp +++ b/client/DigiDoc.cpp @@ -281,20 +281,20 @@ bool SDocumentModel::addFile(const QString &file, const QString &mime) QFileInfo info(file); if(info.size() == 0) { - WarningDialog::show(qApp->mainWindow(), DocumentModel::tr("Cannot add empty file to the container.")); + WarningDialog::show(DocumentModel::tr("Cannot add empty file to the container.")); return false; } QString fileName(info.fileName()); if(fileName == QStringLiteral("mimetype")) { - WarningDialog::show(qApp->mainWindow(), DocumentModel::tr("Cannot add file with name 'mimetype' to the envelope.")); + WarningDialog::show(DocumentModel::tr("Cannot add file with name 'mimetype' to the envelope.")); return false; } for(int row = 0; row < rowCount(); row++) { if(fileName == from(doc->b->dataFiles().at(size_t(row))->fileName())) { - WarningDialog::show(qApp->mainWindow(), DocumentModel::tr("Cannot add the file to the envelope. File '%1' is already in container.") + WarningDialog::show(DocumentModel::tr("Cannot add the file to the envelope. File '%1' is already in container.") .arg(FileDialog::normalized(fileName))); return false; } @@ -455,9 +455,9 @@ QString DigiDoc::fileName() const { return m_fileName; } bool DigiDoc::isError(bool failure, const QString &msg) const { if(!b) - qApp->showWarning(tr("Container is not open")); + WarningDialog::show(tr("Container is not open")); else if(failure) - qApp->showWarning(msg); + WarningDialog::show(msg); return !b || failure; } @@ -496,16 +496,16 @@ bool DigiDoc::open( const QString &file ) { QWidget *parent = qobject_cast(QObject::parent()); if(parent == nullptr) - parent = qApp->activeWindow(); + parent = Application::activeWindow(); qApp->waitForTSL( file ); clear(); auto serviceConfirmation = [parent] { - WarningDialog dlg(tr("Signed document in PDF and DDOC format will be transmitted to the Digital Signature Validation Service SiVa to verify the validity of the digital signature. " + auto *dlg = new WarningDialog(tr("Signed document in PDF and DDOC format will be transmitted to the Digital Signature Validation Service SiVa to verify the validity of the digital signature. " "Read more information about transmitted data to Digital Signature Validation service from here.
" "Do you want to continue?"), parent); - dlg.setCancelText(tr("CANCEL")); - dlg.addButton(tr("YES"), ContainerSave); - return dlg.exec() == ContainerSave; + dlg->setCancelText(tr("CANCEL")); + dlg->addButton(tr("YES"), ContainerSave); + return dlg->exec() == ContainerSave; }; if((file.endsWith(QStringLiteral(".pdf"), Qt::CaseInsensitive) || file.endsWith(QStringLiteral(".ddoc"), Qt::CaseInsensitive)) && !serviceConfirmation()) @@ -543,7 +543,7 @@ bool DigiDoc::open( const QString &file ) for(const Signature *signature: parentContainer->signatures()) m_timestamps.append(DigiDocSignature(signature, this)); } - qApp->addRecent(file); + Application::addRecent(file); m_fileName = file; containerState = signatures().isEmpty() ? ContainerState::UnsignedSavedContainer : ContainerState::SignedContainer; return true; @@ -614,7 +614,7 @@ bool DigiDoc::save( const QString &filename ) m_fileName = filename; if(!saveAs(m_fileName)) return false; - qApp->addRecent(m_fileName); + Application::addRecent(m_fileName); modified = false; containerState = signatures().isEmpty() ? ContainerState::UnsignedSavedContainer : ContainerState::SignedContainer; return true; @@ -644,33 +644,33 @@ void DigiDoc::setLastError( const QString &msg, const Exception &e ) switch( code ) { case Exception::CertificateRevoked: - qApp->showWarning(tr("Certificate status revoked"), causes.join('\n')); break; + WarningDialog::show(tr("Certificate status revoked"), causes.join('\n')); break; case Exception::CertificateUnknown: - qApp->showWarning(tr("Certificate status unknown"), causes.join('\n')); break; + WarningDialog::show(tr("Certificate status unknown"), causes.join('\n')); break; case Exception::OCSPTimeSlot: - qApp->showWarning(tr("Please check your computer time. Additional information"), causes.join('\n')); break; + WarningDialog::show(tr("Please check your computer time. Additional information"), causes.join('\n')); break; case Exception::OCSPRequestUnauthorized: - qApp->showWarning(tr("You have not granted IP-based access. " + WarningDialog::show(tr("You have not granted IP-based access. " "Check your validity confirmation service access settings."), causes.join('\n')); break; case Exception::TSForbidden: - qApp->showWarning(tr("Failed to sign container. " + WarningDialog::show(tr("Failed to sign container. " "Check your Time-Stamping service access settings."), causes.join('\n')); break; case Exception::TSTooManyRequests: - qApp->showWarning(tr("The limit for digital signatures per month has been reached for this IP address. " + WarningDialog::show(tr("The limit for digital signatures per month has been reached for this IP address. " "Additional information"), causes.join('\n')); break; case Exception::PINCanceled: break; case Exception::PINFailed: - qApp->showWarning(tr("PIN Login failed"), causes.join('\n')); break; + WarningDialog::show(tr("PIN Login failed"), causes.join('\n')); break; case Exception::PINIncorrect: - qApp->showWarning(tr("PIN Incorrect"), causes.join('\n')); break; + WarningDialog::show(tr("PIN Incorrect"), causes.join('\n')); break; case Exception::PINLocked: - qApp->showWarning(tr("PIN Locked. Unblock to reuse PIN."), causes.join('\n')); break; + WarningDialog::show(tr("PIN Locked. Unblock to reuse PIN."), causes.join('\n')); break; case Exception::NetworkError: // use passed message for these thre exceptions case Exception::HostNotFound: case Exception::InvalidUrl: default: - qApp->showWarning(msg, causes.join('\n')); break; + WarningDialog::show(msg, causes.join('\n')); break; } } @@ -702,13 +702,13 @@ bool DigiDoc::sign(const QString &city, const QString &state, const QString &zip switch(code) { case Exception::PINIncorrect: - qApp->showWarning(tr("PIN Incorrect")); + (new WarningDialog(tr("PIN Incorrect"), Application::mainWindow()))->exec(); return sign(city, state, zip, country, role, signer); case Exception::NetworkError: case Exception::HostNotFound: - qApp->showWarning(tr("Failed to sign container. Please check the access to signing services and network settings."), causes.join('\n')); break; + WarningDialog::show(tr("Failed to sign container. Please check the access to signing services and network settings."), causes.join('\n')); break; case Exception::InvalidUrl: - qApp->showWarning(tr("Failed to sign container. Signing service URL is incorrect."), causes.join('\n')); break; + WarningDialog::show(tr("Failed to sign container. Signing service URL is incorrect."), causes.join('\n')); break; default: setLastError(tr("Failed to sign container."), e); break; } diff --git a/client/DocumentModel.cpp b/client/DocumentModel.cpp index bb5115d6d..e3ea89972 100644 --- a/client/DocumentModel.cpp +++ b/client/DocumentModel.cpp @@ -58,12 +58,9 @@ bool DocumentModel::verifyFile(const QString &f) QStringLiteral("pps"), QStringLiteral("ppt"), QStringLiteral("pptx"), QStringLiteral("png"), QStringLiteral("jpg"), QStringLiteral("jpeg"), QStringLiteral("bmp"), QStringLiteral("ai"), QStringLiteral("gif"), QStringLiteral("ico"), QStringLiteral("ps"), QStringLiteral("psd"), QStringLiteral("tif"), QStringLiteral("tiff"), QStringLiteral("csv")}; - QJsonArray allowedExts = qApp->confValue(QLatin1String("ALLOWED-EXTENSIONS")).toArray(defaultArray); + QJsonArray allowedExts = Application::confValue(QLatin1String("ALLOWED-EXTENSIONS")).toArray(defaultArray); if(!allowedExts.contains(QJsonValue(QFileInfo(f).suffix().toLower()))){ - auto *dlg = new WarningDialog(tr("A file with this extension cannot be opened in the DigiDoc4 Client. Download the file to view it."), qApp->activeWindow()); - dlg->setAttribute(Qt::WA_DeleteOnClose); - dlg->setCancelText(tr("OK")); - dlg->open(); + WarningDialog::show(tr("A file with this extension cannot be opened in the DigiDoc4 Client. Download the file to view it."))->setCancelText(tr("OK")); return false; } diff --git a/client/MainWindow.cpp b/client/MainWindow.cpp index ea2e763b2..1586b2f7e 100644 --- a/client/MainWindow.cpp +++ b/client/MainWindow.cpp @@ -324,10 +324,10 @@ bool MainWindow::encrypt() return false; if(!FileDialog::fileIsWritable(cryptoDoc->fileName())) { - WarningDialog dlg(tr("Cannot alter container %1. Save different location?") + auto *dlg = new WarningDialog(tr("Cannot alter container %1. Save different location?") .arg(FileDialog::normalized(cryptoDoc->fileName())), this); - dlg.addButton(tr("YES").toUpper(), QMessageBox::Yes); - if(dlg.exec() == QMessageBox::Yes) { + dlg->addButton(tr("YES").toUpper(), QMessageBox::Yes); + if(dlg->exec() == QMessageBox::Yes) { moveCryptoContainer(); return encrypt(); } @@ -572,9 +572,9 @@ void MainWindow::onCryptoAction(int action, const QString &/*id*/, const QString break; if( !FileDialog::fileIsWritable(target)) { - WarningDialog dlg(tr("Cannot alter container %1. Save different location?").arg(target), this); - dlg.addButton(tr("YES").toUpper(), QMessageBox::Yes); - if(dlg.exec() == QMessageBox::Yes) { + auto *dlg = new WarningDialog(tr("Cannot alter container %1. Save different location?").arg(target), this); + dlg->addButton(tr("YES").toUpper(), QMessageBox::Yes); + if(dlg->exec() == QMessageBox::Yes) { QString file = selectFile(tr("Save file"), target, true); if(!file.isEmpty()) cryptoDoc->saveCopy(file); @@ -741,10 +741,10 @@ void MainWindow::resetDigiDoc(DigiDoc *doc, bool warnOnChange) saveTxt = tr("SAVE"); } - WarningDialog dlg(warning, this); - dlg.setCancelText(cancelTxt); - dlg.addButton(saveTxt, ContainerSave); - if(dlg.exec() == ContainerSave) + auto *dlg = new WarningDialog(warning, this); + dlg->setCancelText(cancelTxt); + dlg->addButton(saveTxt, ContainerSave); + if(dlg->exec() == ContainerSave) save(); } @@ -775,9 +775,9 @@ bool MainWindow::save(bool saveAs) if(!FileDialog::fileIsWritable(target)) { - WarningDialog dlg(tr("Cannot alter container %1. Save different location?").arg(target), this); - dlg.addButton(tr("YES").toUpper(), QMessageBox::Yes); - if(dlg.exec() == QMessageBox::Yes) { + auto *dlg = new WarningDialog(tr("Cannot alter container %1. Save different location?").arg(target), this); + dlg->addButton(tr("YES").toUpper(), QMessageBox::Yes); + if(dlg->exec() == QMessageBox::Yes) { QString file = selectFile(tr("Save file"), target, true); if(!file.isEmpty()) return saveAs ? digiDoc->saveAs(file) : digiDoc->save(file); @@ -960,11 +960,11 @@ bool MainWindow::removeFile(DocumentModel *model, int index) } else { - WarningDialog dlg(tr("You are about to delete the last file in the container, it is removed along with the container."), this); - dlg.setCancelText(tr("CANCEL")); - dlg.resetCancelStyle(); - dlg.addButton(tr("REMOVE"), ContainerSave, true); - if (dlg.exec() == ContainerSave) { + auto *dlg = new WarningDialog(tr("You are about to delete the last file in the container, it is removed along with the container."), this); + dlg->setCancelText(tr("CANCEL")); + dlg->resetCancelStyle(); + dlg->addButton(tr("REMOVE"), ContainerSave, true); + if (dlg->exec() == ContainerSave) { window()->setWindowFilePath({}); window()->setWindowTitle(tr("DigiDoc4 Client")); return true; @@ -1025,18 +1025,12 @@ bool MainWindow::validateFiles(const QString &container, const QStringList &file { // Check that container is not dropped into itself QFileInfo containerInfo(container); - for(const auto &file: files) - { - if(containerInfo == QFileInfo(file)) - { - WarningDialog dlg(tr("Cannot add container to same container\n%1").arg(FileDialog::normalized(container)), this); - dlg.setCancelText(tr("CANCEL")); - dlg.exec(); - return false; - } - } - - return true; + if(std::none_of(files.cbegin(), files.cend(), + [containerInfo] (const QString &file) { return containerInfo == QFileInfo(file); })) + return true; + WarningDialog::show(this, tr("Cannot add container to same container\n%1") + .arg(FileDialog::normalized(container)))->setCancelText(tr("CANCEL")); + return false; } void MainWindow::warningClicked(const QString &link) @@ -1078,10 +1072,10 @@ bool MainWindow::wrapContainer(bool signing) QString msg = signing ? tr("Files can not be added to the signed container. The system will create a new container which shall contain the signed document and the files you wish to add.") : tr("Files can not be added to the cryptocontainer. The system will create a new container which shall contain the cypto-document and the files you wish to add."); - WarningDialog dlg(msg, this); - dlg.setCancelText(tr("CANCEL")); - dlg.addButton(tr("CONTINUE"), ContainerSave); - return dlg.exec() == ContainerSave; + auto *dlg = new WarningDialog(msg, this); + dlg->setCancelText(tr("CANCEL")); + dlg->addButton(tr("CONTINUE"), ContainerSave); + return dlg->exec() == ContainerSave; } void MainWindow::updateSelector() diff --git a/client/QSigner.cpp b/client/QSigner.cpp index a6d6e8c00..463f31222 100644 --- a/client/QSigner.cpp +++ b/client/QSigner.cpp @@ -29,6 +29,7 @@ #include "QPKCS11.h" #include "SslCertificate.h" #include "Utils.h" +#include "dialogs/WarningDialog.h" #include #include @@ -90,8 +91,8 @@ QSigner::QSigner(QObject *parent) EC_KEY_METHOD_set_sign(d->ecmethod, sign, sign_setup, Private::ecdsa_do_sign); d->smartcard = new QSmartCard(parent); - connect(this, &QSigner::error, qApp, [](const QString &msg) { - qApp->showWarning(msg); + connect(this, &QSigner::error, this, [](const QString &msg) { + WarningDialog::show(msg); }); connect(this, &QSigner::signDataChanged, this, [this](const TokenData &token) { std::string method = (CONF(signatureDigestUri)); @@ -199,7 +200,7 @@ QByteArray QSigner::decrypt(std::function &&func) QCardLock::instance().exclusiveUnlock(); return {}; case QCryptoBackend::PinIncorrect: - qApp->showWarning(QCryptoBackend::errorString(status)); + (new WarningDialog(QCryptoBackend::errorString(status), Application::mainWindow()))->exec(); continue; case QCryptoBackend::PinLocked: QCardLock::instance().exclusiveUnlock(); @@ -240,7 +241,7 @@ QSslKey QSigner::key() const { case QCryptoBackend::PinOK: break; case QCryptoBackend::PinIncorrect: - qApp->showWarning(QCryptoBackend::errorString(status)); + (new WarningDialog(QCryptoBackend::errorString(status), Application::mainWindow()))->exec(); continue; case QCryptoBackend::PinLocked: default: @@ -408,7 +409,7 @@ std::vector QSigner::sign(const std::string &method, const std::v d->smartcard->reload(); // QSmartCard should also know that PIN2 info is updated throwException((tr("Failed to login token") + " " + QCryptoBackend::errorString(status)), Exception::PINCanceled) case QCryptoBackend::PinIncorrect: - qApp->showWarning(QCryptoBackend::errorString(status)); + (new WarningDialog(QCryptoBackend::errorString(status), Application::mainWindow()))->exec(); continue; case QCryptoBackend::PinLocked: QCardLock::instance().exclusiveUnlock(); diff --git a/client/dialogs/AddRecipients.cpp b/client/dialogs/AddRecipients.cpp index 3e56ad92c..a9bf937e1 100644 --- a/client/dialogs/AddRecipients.cpp +++ b/client/dialogs/AddRecipients.cpp @@ -224,22 +224,22 @@ bool AddRecipients::addRecipientToRightPane(const CKey &key, bool update) auto expiryDate = key.cert.expiryDate(); if(expiryDate <= QDateTime::currentDateTime()) { - WarningDialog dlg(tr("Are you sure that you want use certificate for encrypting, which expired on %1?
" - "When decrypter has updated certificates then decrypting is impossible.") - .arg(expiryDate.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss"))), this); - dlg.setCancelText(tr("NO")); - dlg.addButton(tr("YES"), QMessageBox::Yes); - if(dlg.exec() != QMessageBox::Yes) + auto *dlg = new WarningDialog(tr("Are you sure that you want use certificate for encrypting, which expired on %1?
" + "When decrypter has updated certificates then decrypting is impossible.") + .arg(expiryDate.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss"))), this); + dlg->setCancelText(tr("NO")); + dlg->addButton(tr("YES"), QMessageBox::Yes); + if(dlg->exec() != QMessageBox::Yes) return false; } QList errors = QSslCertificate::verify({ key.cert }); errors.removeAll(QSslError(QSslError::CertificateExpired, key.cert)); if(!errors.isEmpty()) { - WarningDialog dlg(tr("Recipient’s certification chain contains certificates that are not trusted. Continue with encryption?"), this); - dlg.setCancelText(tr("NO")); - dlg.addButton(tr("YES"), QMessageBox::Yes); - if(dlg.exec() != QMessageBox::Yes) + auto *dlg = new WarningDialog(tr("Recipient’s certification chain contains certificates that are not trusted. Continue with encryption?"), this); + dlg->setCancelText(tr("NO")); + dlg->addButton(tr("YES"), QMessageBox::Yes); + if(dlg->exec() != QMessageBox::Yes) return false; } } diff --git a/client/dialogs/SettingsDialog.cpp b/client/dialogs/SettingsDialog.cpp index bc1fb7599..01c0daf78 100644 --- a/client/dialogs/SettingsDialog.cpp +++ b/client/dialogs/SettingsDialog.cpp @@ -186,7 +186,7 @@ SettingsDialog::SettingsDialog(int page, QWidget *parent) WarningDialog::show(this, tr("Checking updates has failed.") + "
" + tr("Please try again."), error); return; } - WarningDialog(tr("DigiDoc4 Client configuration update was successful."), this).exec(); + WarningDialog::show(this, tr("DigiDoc4 Client configuration update was successful.")); #ifdef Q_OS_WIN QString path = qApp->applicationDirPath() + QStringLiteral("/id-updater.exe"); if (QFile::exists(path)) @@ -484,14 +484,16 @@ void SettingsDialog::initFunctionality() WarningDialog::show(this, tr("Restart DigiDoc4 Client to activate logging. Read more " "here.")); #else - WarningDialog dlg(tr("Restart DigiDoc4 Client to activate logging. Read more " - "here. Restart now?"), this); - dlg.setCancelText(tr("NO")); - dlg.addButton(tr("YES"), 1) ; - if(dlg.exec() == 1) { - qApp->setProperty("restart", true); - qApp->quit(); - } + auto *dlg = WarningDialog::show(this, tr("Restart DigiDoc4 Client to activate logging. Read more " + "here. Restart now?")); + dlg->setCancelText(tr("NO")); + dlg->addButton(tr("YES"), 1); + connect(dlg, &WarningDialog::finished, qApp, [](int result) { + if(result == 1) { + qApp->setProperty("restart", true); + qApp->quit(); + } + }); #endif }); } diff --git a/client/dialogs/WarningDialog.cpp b/client/dialogs/WarningDialog.cpp index df4a8ce89..ca27bee20 100644 --- a/client/dialogs/WarningDialog.cpp +++ b/client/dialogs/WarningDialog.cpp @@ -20,14 +20,21 @@ #include "WarningDialog.h" #include "ui_WarningDialog.h" +#include "Application.h" #include "Styles.h" +#include + WarningDialog::WarningDialog(const QString &text, const QString &details, QWidget *parent) : QDialog(parent) , ui(new Ui::WarningDialog) { ui->setupUi(this); setWindowFlags(Qt::Dialog|Qt::CustomizeWindowHint); + setAttribute(Qt::WA_DeleteOnClose); +#ifdef Q_OS_DARWIN + setParent(parent, Qt::Sheet); +#endif connect( ui->cancel, &QPushButton::clicked, this, &WarningDialog::reject ); connect( this, &WarningDialog::finished, this, &WarningDialog::close ); @@ -58,17 +65,6 @@ WarningDialog::~WarningDialog() delete ui; } -void WarningDialog::setCancelText(const QString& label) -{ - ui->cancel->setText(label); - ui->cancel->setAccessibleName(label.toLower()); -} - -void WarningDialog::resetCancelStyle() -{ - ui->cancel->setStyleSheet({}); -} - void WarningDialog::addButton(const QString& label, int ret, bool red) { auto *button = new QPushButton(label, this); @@ -80,11 +76,7 @@ void WarningDialog::addButton(const QString& label, int ret, bool red) ui->cancel->minimumHeight()); if(red) { - button->setStyleSheet(QStringLiteral( - "QPushButton { border-radius: 2px; border: none; color: #ffffff; background-color: #981E32;}\n" - "QPushButton:pressed { background-color: #F24A66; }\n" - "QPushButton:hover:!pressed { background-color: #CD2541; }\n" - "QPushButton:disabled {background-color: #BEDBED;}")); + button->setProperty("warning", true); #ifdef Q_OS_WIN // For Windows this button should be on the left side of the dialog window setLayoutDirection(Qt::RightToLeft); #endif @@ -98,6 +90,13 @@ void WarningDialog::addButton(const QString& label, int ret, bool red) ui->buttonBarLayout->insertWidget(ui->buttonBarLayout->findChildren().size() + 1, button); } +void WarningDialog::resetCancelStyle() +{ + style()->unpolish(ui->cancel); + ui->cancel->setProperty("warning", false); + style()->polish(ui->cancel); +} + void WarningDialog::setButtonSize(int width, int margin) { ui->buttonBarLayout->setSpacing(margin); @@ -105,16 +104,26 @@ void WarningDialog::setButtonSize(int width, int margin) ui->cancel->setMaximumSize(width, ui->cancel->minimumHeight()); } +void WarningDialog::setCancelText(const QString& label) +{ + ui->cancel->setText(label); + ui->cancel->setAccessibleName(label.toLower()); +} + void WarningDialog::setText(const QString& text) { ui->text->setText(text); adjustSize(); } +WarningDialog* WarningDialog::show(const QString &text, const QString &details) +{ + return show(Application::mainWindow(), text, details); +} + WarningDialog* WarningDialog::show(QWidget *parent, const QString &text, const QString &details) { auto *dlg = new WarningDialog(text, details, parent); - dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->open(); return dlg; } diff --git a/client/dialogs/WarningDialog.h b/client/dialogs/WarningDialog.h index 5f476210c..bc0526621 100644 --- a/client/dialogs/WarningDialog.h +++ b/client/dialogs/WarningDialog.h @@ -41,6 +41,7 @@ class WarningDialog final: public QDialog void setCancelText(const QString& label); void resetCancelStyle(); void setText(const QString& text); + static WarningDialog *show(const QString &text, const QString &details = {}); static WarningDialog *show(QWidget *parent, const QString &text, const QString &details = {}); private: diff --git a/client/dialogs/WarningDialog.ui b/client/dialogs/WarningDialog.ui index 7be9b5ecc..b1f7ddf3a 100644 --- a/client/dialogs/WarningDialog.ui +++ b/client/dialogs/WarningDialog.ui @@ -32,6 +32,15 @@ background-color: #008DCF; } QPushButton:disabled { background-color: #BEDBED; +} +QPushButton[warning="true"] { +background-color: #981E32; +} +QPushButton[warning="true"]:pressed { +background-color: #F24A66; +} +QPushButton:hover[warning="true"]:!pressed { +background-color: #CD2541; } @@ -172,26 +181,15 @@ border: none; Close - - QPushButton { - border-radius: 2px; - border: none; - color: #ffffff; - background-color: #981E32; -} -QPushButton:pressed { - background-color: #F24A66; -} -QPushButton:hover:!pressed { - background-color: #CD2541; -} - CLOSE false + + true + diff --git a/client/widgets/ContainerPage.cpp b/client/widgets/ContainerPage.cpp index 20e006c2b..fb233f16c 100644 --- a/client/widgets/ContainerPage.cpp +++ b/client/widgets/ContainerPage.cpp @@ -130,9 +130,9 @@ bool ContainerPage::checkAction(int code, const QString& selectedCard, const QSt } )) { - WarningDialog dlg(tr("The document has already been signed by you."), this); - dlg.addButton(tr("CONTINUE SIGNING"), SignatureAdd); - return dlg.exec() == SignatureAdd; + auto *dlg = new WarningDialog(tr("The document has already been signed by you."), this); + dlg->addButton(tr("CONTINUE SIGNING"), SignatureAdd); + return dlg->exec() == SignatureAdd; } break; default: break; diff --git a/client/widgets/FileList.cpp b/client/widgets/FileList.cpp index e72851de6..468853345 100644 --- a/client/widgets/FileList.cpp +++ b/client/widgets/FileList.cpp @@ -50,7 +50,7 @@ FileList::FileList(QWidget *parent) void FileList::addFile( const QString& file ) { - FileItem *item = new FileItem(file, state); + auto *item = new FileItem(file, state); item->installEventFilter(this); addWidget(item); @@ -74,20 +74,20 @@ bool FileList::eventFilter(QObject *obj, QEvent *event) { case QEvent::MouseButtonPress: { - QMouseEvent *mouse = static_cast(event); + auto *mouse = static_cast(event); if (mouse->button() == Qt::LeftButton) obj->setProperty("dragStartPosition", mouse->pos()); break; } case QEvent::MouseMove: { - QMouseEvent *mouse = static_cast(event); + auto *mouse = static_cast(event); if(!(mouse->buttons() & Qt::LeftButton)) break; if((mouse->pos() - obj->property("dragStartPosition").toPoint()).manhattanLength() < QApplication::startDragDistance()) break; - FileItem *fileItem = qobject_cast(obj); + auto *fileItem = qobject_cast(obj); if(!documentModel || !fileItem) break; int i = index(fileItem); @@ -96,10 +96,10 @@ bool FileList::eventFilter(QObject *obj, QEvent *event) QString path = FileDialog::tempPath(fileItem->getFile()); documentModel->save(i, path); documentModel->addTempReference(path); - QMimeData *mimeData = new QMimeData; + auto *mimeData = new QMimeData; mimeData->setText(QFileInfo(path).fileName()); mimeData->setUrls({ QUrl::fromLocalFile(path) }); - QDrag *drag = new QDrag(this); + auto *drag = new QDrag(this); drag->setMimeData(mimeData); drag->exec(Qt::CopyAction); return true; @@ -166,14 +166,14 @@ void FileList::saveAll() documentModel->save( i, dest ); continue; } - WarningDialog dlg(tr("%1 already exists.
Do you want replace it?").arg( dest ), this); - dlg.setButtonSize(60, 5); - dlg.setCancelText(tr("NO")); - dlg.addButton(tr("YES"), QMessageBox::Yes); - dlg.addButton(tr("SAVE WITH OTHER NAME"), QMessageBox::No); - dlg.addButton(tr("REPLACE ALL"), QMessageBox::YesToAll); - dlg.addButton(tr("CANCEL"), QMessageBox::NoToAll); - b = dlg.exec(); + auto *dlg = new WarningDialog(tr("%1 already exists.
Do you want replace it?").arg( dest ), this); + dlg->setButtonSize(60, 5); + dlg->setCancelText(tr("NO")); + dlg->addButton(tr("YES"), QMessageBox::Yes); + dlg->addButton(tr("SAVE WITH OTHER NAME"), QMessageBox::No); + dlg->addButton(tr("REPLACE ALL"), QMessageBox::YesToAll); + dlg->addButton(tr("CANCEL"), QMessageBox::NoToAll); + b = dlg->exec(); if(b == QDialog::Rejected) continue; diff --git a/client/widgets/SignatureItem.cpp b/client/widgets/SignatureItem.cpp index 753b77359..fa72b29cd 100644 --- a/client/widgets/SignatureItem.cpp +++ b/client/widgets/SignatureItem.cpp @@ -162,7 +162,7 @@ bool SignatureItem::eventFilter(QObject *o, QEvent *e) details(); return true; case QEvent::KeyRelease: - if(QKeyEvent *ke = static_cast(e)) + if(auto *ke = static_cast(e)) { if(isEnabled() && (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Space)) details(); @@ -200,12 +200,14 @@ void SignatureItem::removeSignature() QString msg = tr("Remove signature %1?") .arg(c.toString(c.showCN() ? QStringLiteral("CN serialNumber") : QStringLiteral("GN SN serialNumber"))); - WarningDialog *dlg = new WarningDialog(msg, this); + auto *dlg = WarningDialog::show(this, msg); dlg->setCancelText(tr("CANCEL")); dlg->resetCancelStyle(); dlg->addButton(QStringLiteral("OK"), SignatureRemove, true); - if(dlg->exec() == SignatureRemove) - emit remove(this); + connect(dlg, &WarningDialog::finished, this, [this](int result) { + if(result == SignatureRemove) + emit remove(this); + }); } void SignatureItem::updateNameField()