From 0f088697c6d5a4b0f95ff7bfaff755758c440755 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 6 Feb 2024 15:13:06 +0400 Subject: [PATCH] Introduced showConfirmBox() --- src/contentmanager.cpp | 16 ++-------------- src/kiwixconfirmbox.h | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index c0d366c..a0e58b6 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -538,14 +538,8 @@ void ContentManager::eraseBook(const QString& id) text += formatText(gt("perma-delete-files-text")); } text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle())); - KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("delete-book"), text, false, mp_view); - dialog->show(); - connect(dialog, &KiwixConfirmBox::yesClicked, [=]() { + showConfirmBox(gt("delete-book"), text, mp_view, [=]() { reallyEraseBook(id, moveToTrash); - dialog->deleteLater(); - }); - connect(dialog, &KiwixConfirmBox::noClicked, [=]() { - dialog->deleteLater(); }); } @@ -587,15 +581,9 @@ void ContentManager::cancelBook(const QString& id, QModelIndex index) { auto text = gt("cancel-download-text"); text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle())); - KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("cancel-download"), text, false, mp_view); - dialog->show(); - connect(dialog, &KiwixConfirmBox::yesClicked, [=]() { + showConfirmBox(gt("cancel-download"), text, mp_view, [=]() { cancelBook(id); emit managerModel->cancelDownload(index); - dialog->deleteLater(); - }); - connect(dialog, &KiwixConfirmBox::noClicked, [=]() { - dialog->deleteLater(); }); } diff --git a/src/kiwixconfirmbox.h b/src/kiwixconfirmbox.h index 65e5a38..9ec6304 100644 --- a/src/kiwixconfirmbox.h +++ b/src/kiwixconfirmbox.h @@ -29,4 +29,19 @@ private: void showInfoBox(QString title, QString text, QWidget *parent = nullptr); +template +void showConfirmBox(QString title, QString text, QWidget *parent, + YesAction yesAction) +{ + KiwixConfirmBox *dialog = new KiwixConfirmBox(title, text, false, parent); + dialog->show(); + QObject::connect(dialog, &KiwixConfirmBox::yesClicked, [=]() { + yesAction(); + dialog->deleteLater(); + }); + QObject::connect(dialog, &KiwixConfirmBox::noClicked, [=]() { + dialog->deleteLater(); + }); +} + #endif // KIWIXCONFIRMBOX_H