From 7202b1807999a260b1e12041a6e9bfd6c8f0a70c Mon Sep 17 00:00:00 2001 From: TheLastProject Date: Fri, 20 Feb 2015 16:36:35 +0100 Subject: [PATCH] Make askQuestion more flexible, defaulting to Yes/No --- src/widget/form/settings/privacyform.cpp | 4 ++-- src/widget/gui.cpp | 30 +++++++++++------------- src/widget/gui.h | 6 +++-- src/widget/widget.cpp | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/widget/form/settings/privacyform.cpp b/src/widget/form/settings/privacyform.cpp index f4735f98c..29db32533 100644 --- a/src/widget/form/settings/privacyform.cpp +++ b/src/widget/form/settings/privacyform.cpp @@ -114,8 +114,8 @@ bool PrivacyForm::setChatLogsPassword() } else { - if (GUI::askQuestion(tr("Old encrypted chat history", "popup title"), tr("There is currently an unused encrypted chat history, but the password you just entered doesn't match.\n\nIf you don't care about the old history, you may click Ok to delete it and use the password you just entered.\nOtherwise, hit cancel to try again.", "This happens when enabling encryption after previously \"Disabling History\""), false, true)) - if (GUI::askQuestion(tr("Old encrypted chat history", "popup title"), tr("Are you absolutely sure you want to lose the unused encrypted chat history?", "secondary popup"), false, true)) + if (GUI::askQuestion(tr("Old encrypted chat history", "popup title"), tr("There is currently an unused encrypted chat history, but the password you just entered doesn't match.\n\nIf you don't care about the old history, you may click Ok to delete it and use the password you just entered.\nOtherwise, hit cancel to try again.", "This happens when enabling encryption after previously \"Disabling History\""), false, true, false)) + if (GUI::askQuestion(tr("Old encrypted chat history", "popup title"), tr("Are you absolutely sure you want to lose the unused encrypted chat history?", "secondary popup"))) haveEncHist = false; // logically this is really just a `break`, but conceptually this is more accurate } } while (haveEncHist); diff --git a/src/widget/gui.cpp b/src/widget/gui.cpp index 12515fc3c..9f75ffb49 100644 --- a/src/widget/gui.cpp +++ b/src/widget/gui.cpp @@ -113,11 +113,12 @@ void GUI::showError(const QString& title, const QString& msg) } bool GUI::askQuestion(const QString& title, const QString& msg, - bool defaultAns, bool warning) + bool defaultAns, bool warning, + bool yesno) { if (QThread::currentThread() == qApp->thread()) { - return getInstance()._askQuestion(title, msg, defaultAns, warning); + return getInstance()._askQuestion(title, msg, defaultAns, warning, yesno); } else { @@ -125,7 +126,8 @@ bool GUI::askQuestion(const QString& title, const QString& msg, QMetaObject::invokeMethod(&getInstance(), "_askQuestion", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, ret), Q_ARG(const QString&, title), Q_ARG(const QString&, msg), - Q_ARG(bool, defaultAns), Q_ARG(bool, warning)); + Q_ARG(bool, defaultAns), Q_ARG(bool, warning), + Q_ARG(bool, yesno)); return ret; } } @@ -211,22 +213,18 @@ void GUI::_showError(const QString& title, const QString& msg) } bool GUI::_askQuestion(const QString& title, const QString& msg, - bool defaultAns, bool warning) + bool defaultAns, bool warning, + bool yesno) { + QMessageBox::StandardButton positiveButton = yesno ? QMessageBox::Yes : QMessageBox::Ok; + QMessageBox::StandardButton negativeButton = yesno ? QMessageBox::No : QMessageBox::Cancel; + + QMessageBox::StandardButton defButton = defaultAns ? positiveButton : negativeButton; + if (warning) - { - QMessageBox::StandardButton def = QMessageBox::Cancel; - if (defaultAns) - def = QMessageBox::Ok; - return QMessageBox::warning(getMainWidget(), title, msg, QMessageBox::Ok | QMessageBox::Cancel, def) == QMessageBox::Ok; - } + return QMessageBox::warning(getMainWidget(), title, msg, positiveButton | negativeButton, defButton) == positiveButton; else - { - QMessageBox::StandardButton def = QMessageBox::No; - if (defaultAns) - def = QMessageBox::Yes; - return QMessageBox::question(getMainWidget(), title, msg, QMessageBox::Yes | QMessageBox::No, def) == QMessageBox::Yes; - } + return QMessageBox::question(getMainWidget(), title, msg, positiveButton | negativeButton, defButton) == positiveButton; } QString GUI::_itemInputDialog(QWidget * parent, const QString & title, diff --git a/src/widget/gui.h b/src/widget/gui.h index 926685467..c3a673403 100644 --- a/src/widget/gui.h +++ b/src/widget/gui.h @@ -33,7 +33,8 @@ public: /// If warning is true, we will use a special warning style. /// Returns the answer. static bool askQuestion(const QString& title, const QString& msg, - bool defaultAns = false, bool warning = true); + bool defaultAns = false, bool warning = true, + bool yesno = true); /// Asks the user to input text and returns the answer. /// The interface is equivalent to QInputDialog::getItem() static QString itemInputDialog(QWidget * parent, const QString & title, @@ -63,7 +64,8 @@ private slots: void _showWarning(const QString& title, const QString& msg); void _showError(const QString& title, const QString& msg); bool _askQuestion(const QString& title, const QString& msg, - bool defaultAns = false, bool warning = true); + bool defaultAns = false, bool warning = true, + bool yesno = true); QString _itemInputDialog(QWidget * parent, const QString & title, const QString & label, const QStringList & items, int current = 0, bool editable = true, bool * ok = 0, diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 85522b25b..a9af76975 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -502,7 +502,7 @@ bool Widget::confirmExecutableOpen(const QFileInfo file) { if (file.isExecutable()) { - if(!GUI::askQuestion(tr("Executable file", "popup title"), tr("You have asked qTox to open an executable file. Executable files can potentially damage your computer. Are you sure want to open this file?", "popup text"), false, false)) + if(!GUI::askQuestion(tr("Executable file", "popup title"), tr("You have asked qTox to open an executable file. Executable files can potentially damage your computer. Are you sure want to open this file?", "popup text"))) { return false; }