Moved MainWindow::requestConfirmation() to the QtCommon class

This commit is contained in:
Olivier Teulière 2012-01-20 18:08:12 +01:00
parent fff127333a
commit 5535717ff4
4 changed files with 32 additions and 22 deletions

View file

@ -432,7 +432,8 @@ void MainWindow::closeEvent(QCloseEvent *event)
if (m_game) if (m_game)
{ {
QString msg = _q("A game has been started."); QString msg = _q("A game has been started.");
if (!requestConfirmation(msg, _q("Do you really want to quit?"))) QString question = _q("Do you really want to quit?");
if (!QtCommon::requestConfirmation(msg, question, this))
{ {
event->ignore(); event->ignore();
return; return;
@ -491,7 +492,7 @@ void MainWindow::changeDictionary(QString iFileName)
if (m_game) if (m_game)
{ {
QString msg = _q("Loading a dictionary will stop the current game."); QString msg = _q("Loading a dictionary will stop the current game.");
if (!requestConfirmation(msg)) if (!QtCommon::requestConfirmation(msg, "", this))
return; return;
} }
@ -519,21 +520,6 @@ void MainWindow::changeDictionary(QString iFileName)
} }
bool MainWindow::requestConfirmation(QString msg, QString question)
{
QMessageBox confirmationBox(QMessageBox::Question, _q("Eliot"), msg,
QMessageBox::Yes | QMessageBox::No, this);
if (question != "")
confirmationBox.setInformativeText(question);
else
confirmationBox.setInformativeText(_q("Do you want to continue?"));
confirmationBox.setDefaultButton(QMessageBox::Yes);
confirmationBox.setEscapeButton(QMessageBox::No);
int res = confirmationBox.exec();
return res == QMessageBox::Yes;
}
QAction * MainWindow::addMenuAction(QMenu *menu, QString iText, QAction * MainWindow::addMenuAction(QMenu *menu, QString iText,
const QKeySequence &iShortcut, const QKeySequence &iShortcut,
QString iStatusTip, const char *iMember, QString iStatusTip, const char *iMember,
@ -662,7 +648,7 @@ void MainWindow::onGameNew()
if (m_game) if (m_game)
{ {
QString msg = _q("Starting a new game will stop the current one."); QString msg = _q("Starting a new game will stop the current one.");
if (!requestConfirmation(msg)) if (!QtCommon::requestConfirmation(msg, "", this))
return; return;
} }
@ -1105,7 +1091,7 @@ void MainWindow::onHistoryReplayTurn()
QString msg = _q("Replaying this turn will modify the game history " QString msg = _q("Replaying this turn will modify the game history "
"by deleting the turns after the displayed one (i.e. " "by deleting the turns after the displayed one (i.e. "
"turns \"in the future\")."); "turns \"in the future\").");
if (!requestConfirmation(msg)) if (!QtCommon::requestConfirmation(msg, "", this))
return; return;
} }

View file

@ -191,9 +191,6 @@ private:
*/ */
void linkTrainingAnd7P1(); void linkTrainingAnd7P1();
/// Ask for a confirmation
bool requestConfirmation(QString msg, QString question = "");
}; };
#endif #endif

View file

@ -19,6 +19,7 @@
*****************************************************************************/ *****************************************************************************/
#include <QtGui/QWidget> #include <QtGui/QWidget>
#include <QtGui/QMessageBox>
#include "qtcommon.h" #include "qtcommon.h"
#include <iostream> #include <iostream>
@ -117,3 +118,18 @@ void QtCommon::DestroyObject(QWidget *ioObjectToDestroy,
ioObjectToDestroy->deleteLater(); ioObjectToDestroy->deleteLater();
} }
bool QtCommon::requestConfirmation(QString iMsg, QString iQuestion, QWidget *iParent)
{
QMessageBox confirmationBox(QMessageBox::Question, _q("Eliot"), iMsg,
QMessageBox::Yes | QMessageBox::No, iParent);
if (iQuestion != "")
confirmationBox.setInformativeText(iQuestion);
else
confirmationBox.setInformativeText(_q("Do you want to continue?"));
confirmationBox.setDefaultButton(QMessageBox::Yes);
confirmationBox.setEscapeButton(QMessageBox::No);
int res = confirmationBox.exec();
return res == QMessageBox::Yes;
}

View file

@ -75,6 +75,17 @@ public:
*/ */
static void DestroyObject(QWidget *ioWidgetToDestroy, static void DestroyObject(QWidget *ioWidgetToDestroy,
QObject *iSource = NULL); QObject *iSource = NULL);
/**
* Ask for a confirmation.
* Return true if the user confirms, false otherwise.
* By default, the question is "Do you want to continue?".
* If a parent is provided, the window is centered on it.
*/
static bool requestConfirmation(QString iMsg,
QString iQuestion = "",
QWidget *iParent = 0);
}; };
#endif #endif