drop QApplication in favor of HWApplication and minor refactor of MessageDialog
authorkoda
Wed, 27 Mar 2013 12:56:07 +0100
changeset 8799 44e520374cfc
parent 8797 b7e0b7a18e3a
child 8801 c38ce9cf4c3a
drop QApplication in favor of HWApplication and minor refactor of MessageDialog
QTfrontend/HWApplication.cpp
QTfrontend/HWApplication.h
QTfrontend/gameuiconfig.cpp
QTfrontend/main.cpp
QTfrontend/net/tcpBase.cpp
QTfrontend/util/MessageDialog.cpp
QTfrontend/util/MessageDialog.h
--- a/QTfrontend/HWApplication.cpp	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/HWApplication.cpp	Wed Mar 27 12:56:07 2013 +0100
@@ -18,6 +18,7 @@
 
 #include "HWApplication.h"
 #include <QFileOpenEvent>
+#include <QEvent>
 
 #include "hwform.h"
 #include "MessageDialog.h"
--- a/QTfrontend/HWApplication.h	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/HWApplication.h	Wed Mar 27 12:56:07 2013 +0100
@@ -20,10 +20,9 @@
 #define HWAPP_H
 
 #include <QApplication>
-#include <QString>
-#include <QEvent>
 
 class HWForm;
+class QEvent;
 
 /**
  * @brief Main class of the Qt application.
--- a/QTfrontend/gameuiconfig.cpp	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/gameuiconfig.cpp	Wed Mar 27 12:56:07 2013 +0100
@@ -217,12 +217,12 @@
 void GameUIConfig::resizeToConfigValues()
 {
     // fill 2/3 of the screen desktop
-    const QRect deskSize = QApplication::desktop()->screenGeometry(-1);
+    const QRect deskSize = HWApplication::desktop()->screenGeometry(-1);
     Form->resize(value("frontend/width", qMin(qMax(deskSize.width()*2/3,800),deskSize.width())).toUInt(),
                  value("frontend/height", qMin(qMax(deskSize.height()*2/3,600),deskSize.height())).toUInt());
 
     // move the window to the center of the screen
-    QPoint center = QApplication::desktop()->availableGeometry(-1).center();
+    QPoint center = HWApplication::desktop()->availableGeometry(-1).center();
     center.setX(center.x() - (Form->width()/2));
     center.setY(center.y() - (Form->height()/2));
     Form->move(center);
--- a/QTfrontend/main.cpp	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/main.cpp	Wed Mar 27 12:56:07 2013 +0100
@@ -20,7 +20,6 @@
 
 #include <QTranslator>
 #include <QLocale>
-#include <QMessageBox>
 #include <QPlastiqueStyle>
 #include <QRegExp>
 #include <QMap>
@@ -36,6 +35,7 @@
 
 #include "DataManager.h"
 #include "FileEngine.h"
+#include "MessageDialog.h"
 
 #ifdef _WIN32
 #include <Shlobj.h>
@@ -99,12 +99,7 @@
     if (!tmpdir.exists())
         if (!tmpdir.mkpath(dir))
         {
-            QMessageBox directoryMsg(QApplication::activeWindow());
-            directoryMsg.setIcon(QMessageBox::Warning);
-            directoryMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
-            directoryMsg.setText(QMessageBox::tr("Cannot create directory %1").arg(dir));
-            directoryMsg.setWindowModality(Qt::WindowModal);
-            directoryMsg.exec();
+            MessageDialog::ShowErrorMessage(HWApplication::tr("Cannot create directory %1").arg(dir));
             return false;
         }
     return true;
@@ -144,7 +139,7 @@
     QPixmap pixmap(":res/splash.png");
     splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
     splash->setAttribute(Qt::WA_TranslucentBackground);
-    const QRect deskSize = QApplication::desktop()->screenGeometry(-1);
+    const QRect deskSize = HWApplication::desktop()->screenGeometry(-1);
     QPoint splashCenter = QPoint( (deskSize.width() - pixmap.width())/2,
                                   (deskSize.height() - pixmap.height())/2 );
     splash->move(splashCenter);
@@ -253,16 +248,9 @@
 
     datadir->cd(bindir->absolutePath());
     datadir->cd(*cDataDir);
-    if(!datadir->cd("Data"))
+    if (!datadir->cd("Data"))
     {
-        QMessageBox missingMsg(QApplication::activeWindow());
-        missingMsg.setIcon(QMessageBox::Critical);
-        missingMsg.setWindowTitle(QMessageBox::tr("Main - Error"));
-        missingMsg.setText(QMessageBox::tr("Failed to open data directory:\n%1\n\n"
-                                           "Please check your installation!").
-                                            arg(datadir->absolutePath()+"/Data"));
-        missingMsg.setWindowModality(Qt::WindowModal);
-        missingMsg.exec();
+        MessageDialog::ShowFatalMessage(HWApplication::tr("Failed to open data directory:\n%1\n\nPlease check your installation!").arg(datadir->absolutePath()+"/Data"));
         return 1;
     }
 
--- a/QTfrontend/net/tcpBase.cpp	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/net/tcpBase.cpp	Wed Mar 27 12:56:07 2013 +0100
@@ -19,13 +19,12 @@
 
 #include "tcpBase.h"
 
-#include <QMessageBox>
 #include <QList>
-#include <QApplication>
 #include <QImage>
 #include <QThread>
 
 #include "hwconsts.h"
+#include "MessageDialog.h"
 
 #ifdef HWLIBRARY
 extern "C" void Game(char**arguments);
@@ -89,13 +88,7 @@
         IPCServer->setMaxPendingConnections(1);
         if (!IPCServer->listen(QHostAddress::LocalHost))
         {
-            QMessageBox deniedMsg(QApplication::activeWindow());
-            deniedMsg.setIcon(QMessageBox::Critical);
-            deniedMsg.setWindowTitle(QMessageBox::tr("TCP - Error"));
-            deniedMsg.setText(QMessageBox::tr("Unable to start the server: %1.").arg(IPCServer->errorString()));
-            deniedMsg.setWindowModality(Qt::WindowModal);
-            deniedMsg.exec();
-
+            MessageDialog::ShowFatalMessage(tr("Unable to start server at %1.").arg(IPCServer->errorString()));
             exit(0); // FIXME - should be graceful exit here (lower Critical -> Warning above when implemented)
         }
     }
@@ -172,14 +165,7 @@
 
 void TCPBase::StartProcessError(QProcess::ProcessError error)
 {
-    QMessageBox deniedMsg(QApplication::activeWindow());
-    deniedMsg.setIcon(QMessageBox::Critical);
-    deniedMsg.setWindowTitle(QMessageBox::tr("TCP - Error"));
-    deniedMsg.setText(QMessageBox::tr("Unable to run engine at ") + bindir->absolutePath() + "/hwengine\n" +
-                      QMessageBox::tr("Error code: %1").arg(error));
-    deniedMsg.setWindowModality(Qt::WindowModal);
-    deniedMsg.exec();
-
+    MessageDialog::ShowFatalMessage(tr("Unable to run engine at %1\nError code: %2").arg(bindir->absolutePath() + "/hwengine").arg(error));
     ClientDisconnect();
 }
 
--- a/QTfrontend/util/MessageDialog.cpp	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/util/MessageDialog.cpp	Wed Mar 27 12:56:07 2013 +0100
@@ -17,6 +17,12 @@
  */
 
 #include "MessageDialog.h"
+#include "HWApplication.h"
+
+int MessageDialog::ShowFatalMessage(const QString & msg, QWidget * parent)
+{
+    return ShowMessage(msg, QMessageBox::tr("Hedgewars - Fatal Error"), QMessageBox::Critical, parent);
+}
 
 int MessageDialog::ShowErrorMessage(const QString & msg, QWidget * parent)
 {
@@ -30,9 +36,9 @@
 
 int MessageDialog::ShowMessage(const QString & msg, const QString & title, QMessageBox::Icon icon, QWidget * parent)
 {
-    QMessageBox msgMsg(parent);
+    QMessageBox msgMsg(parent ? parent : HWApplication::activeWindow());
     msgMsg.setIcon(icon);
-    msgMsg.setWindowTitle(title.isEmpty() ? QMessageBox::tr("Hedgewars") : title);
+    msgMsg.setWindowTitle(title != NULL ? title : "Hedgewars");
     msgMsg.setText(msg);
     msgMsg.setWindowModality(Qt::WindowModal);
     return msgMsg.exec();
--- a/QTfrontend/util/MessageDialog.h	Wed Mar 27 12:20:22 2013 +0100
+++ b/QTfrontend/util/MessageDialog.h	Wed Mar 27 12:56:07 2013 +0100
@@ -19,7 +19,6 @@
 #ifndef MESSAGEDIALOG_H
 #define MESSAGEDIALOG_H
 
-#include <QString>
 #include <QMessageBox>
 
 class QWidget;
@@ -27,9 +26,10 @@
 class MessageDialog
 {
     public:
+        static int ShowFatalMessage(const QString & msg, QWidget * parent = 0);
         static int ShowErrorMessage(const QString & msg, QWidget * parent = 0);
         static int ShowInfoMessage(const QString & msg, QWidget * parent = 0);
-        static int ShowMessage(const QString & msg, const QString & title = QString(), QMessageBox::Icon icon = QMessageBox::NoIcon, QWidget * parent = 0);
+        static int ShowMessage(const QString & msg, const QString & title = 0, QMessageBox::Icon icon = QMessageBox::NoIcon, QWidget * parent = 0);
 };
 
 #endif