simplified some stuff I wrote
authorsheepluva
Fri, 21 Oct 2011 08:41:21 +0200
changeset 6167 728cabee2c9f
parent 6166 701c5b8fac56
child 6168 6f301dac12ff
simplified some stuff I wrote
QTfrontend/hwform.cpp
QTfrontend/main.cpp
QTfrontend/model/hats.cpp
QTfrontend/ui/page/pageeditteam.cpp
QTfrontend/ui/page/pagetraining.cpp
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/util/HWDataManager.cpp
QTfrontend/util/HWDataManager.h
QTfrontend/util/SDLInteraction.cpp
QTfrontend/util/SDLInteraction.h
QTfrontend/util/namegen.cpp
--- a/QTfrontend/hwform.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/hwform.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -99,13 +99,9 @@
   : QMainWindow(parent), pnetserver(0), pRegisterServer(0), editedTeam(0), hwnet(0)
 {
     // set music track
-    QFile * tmpFile =
-            HWDataManager::instance().findFileForRead("Music/main_theme.ogg");
-
-    SDLInteraction::instance().setMusicTrack(tmpFile->fileName());
-
-    // this QFile isn't needed any further
-    delete tmpFile;
+    SDLInteraction::instance().setMusicTrack(
+        HWDataManager::instance().findFileForRead("Music/main_theme.ogg")
+    );
 
 #ifdef USE_XFIRE
     xfire_init();
--- a/QTfrontend/main.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/main.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -406,25 +406,21 @@
         themes.sort();
         for(int i = themes.size() - 1; i >= 0; --i)
         {
-            QFile * tmpfile =
-            HWDataManager::instance().findFileForRead(
-                                QString("Themes/%1/icon.png").arg(themes.at(i))
-                            );
+            QString file = HWDataManager::instance().findFileForRead(
+                QString("Themes/%1/icon.png").arg(themes.at(i))
+            );
 
-            if(tmpfile->exists())
+            if(QFile::exists(file))
             { // load icon
                 QPair<QIcon, QIcon> ic;
-                ic.first = QIcon(tmpfile->fileName());
+                ic.first = QIcon(file);
 
-                QFile * previewIconFile =
+                // load preview icon
+                ic.second = QIcon(
                     HWDataManager::instance().findFileForRead(
-                            QString("Themes/%1/icon@2x.png").arg(themes.at(i))
-                        );
-
-                ic.second = QIcon(previewIconFile->fileName());
-
-                // this QFile is not needed any further
-                delete previewIconFile;
+                        QString("Themes/%1/icon@2x.png").arg(themes.at(i))
+                    )
+                );
 
                 icons.prepend(ic);
             }
@@ -432,9 +428,6 @@
             {
                 themes.removeAt(i);
             }
-
-            // this QFile is not needed any further
-            delete tmpfile;
         }
 
         themesModel = new ThemesModel(themes);
@@ -465,9 +458,13 @@
         QString cc = settings.value("misc/locale", QString()).toString();
         if(cc.isEmpty())
             cc = QLocale::system().name();
-        QFile * tmpfile = HWDataManager::instance().findFileForRead(
-                                            QString("Locale/hedgewars_" + cc));
-        Translator.load(tmpfile->fileName());
+
+        // load locale file into translator
+        Translator.load(
+            HWDataManager::instance().findFileForRead(
+                QString("Locale/hedgewars_" + cc)
+            )
+        );
         app.installTranslator(&Translator);
     }
 
--- a/QTfrontend/model/hats.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/model/hats.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -28,12 +28,9 @@
 HatsModel::HatsModel(QObject* parent) :
   QAbstractListModel(parent)
 {
-    QFile * hhfile =
-        HWDataManager::instance().findFileForRead("Graphics/Hedgehog/Idle.png");
-    QPixmap hhpix = QPixmap(hhfile->fileName()).copy(0, 0, 32, 32);
-
-    // this QFile is not needed any further
-    delete hhfile;
+    QPixmap hhpix = QPixmap(
+        HWDataManager::instance().findFileForRead("Graphics/Hedgehog/Idle.png")
+    ).copy(0, 0, 32, 32);
 
     // my reserved hats
     QStringList hatsList = HWDataManager::instance().entryList(
@@ -61,15 +58,12 @@
 
         QString str = hatsList.at(i);
         str = str.remove(QRegExp("\\.png$"));
-        QFile * hatFile = HWDataManager::instance().findFileForRead(
-                                            "Graphics/Hats/" + 
-                                            QString(isReserved?"Reserved/":"") +
-                                            str + ".png"
-                                        );
-        QPixmap pix(hatFile->fileName());
-
-        // this QFile isn't needed any further
-        delete hatFile;
+        QPixmap pix(
+            HWDataManager::instance().findFileForRead(
+                "Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str + 
+                ".png"
+            )
+        );
 
         // rename properly
         if (isReserved)
--- a/QTfrontend/ui/page/pageeditteam.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/ui/page/pageeditteam.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -404,25 +404,20 @@
 
     QStringList list = HWDataManager::instance().entryList(
             voiceDir,
-            QDir::Files/*,
+            QDir::Files,
             QStringList() <<
                 "Illgetyou.ogg" <<
                 "Incoming.ogg" <<
                 "Stupid.ogg" <<
                 "Coward.ogg" <<
-                "Firstblood.ogg"*/
+                "Firstblood.ogg"
             );
 
-    if (list.size())
-    {
-        QFile * tmpFile = HWDataManager::instance().findFileForRead(
-                                voiceDir + "/" + list[rand() % list.size()]);
-
-        SDLInteraction::instance().playSoundFile(tmpFile->fileName());
-
-        // this QFile isn't needed any further
-        delete tmpFile;
-    }
+    if (!list.isEmpty())
+        SDLInteraction::instance().playSoundFile(
+            HWDataManager::instance().findFileForRead(voiceDir + "/" +
+                list[rand() % list.size()])
+        );
 }
 
 void PageEditTeam::createTeam(const QString & name, const QString & playerHash)
--- a/QTfrontend/ui/page/pagetraining.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/ui/page/pagetraining.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -123,32 +123,25 @@
     if (loc.isEmpty())
         loc = QLocale::system().name();
 
-    QFile * infoFile = HWDataManager::instance().findFileForRead(
-                                    QString("Locale/missions_" + loc + ".txt"));
+    QString infoFile = HWDataManager::instance().findFileForRead(
+                            QString("Locale/missions_" + loc + ".txt"));
 
     // if file is non-existant try with language only
-    if (!infoFile->exists())
-    {
-        delete infoFile;
+    if (!QFile::exists(infoFile))
         infoFile = HWDataManager::instance().findFileForRead(QString(
                 "Locale/missions_" + loc.remove(QRegExp("_.*$")) + ".txt"));
-    }
 
     // fallback if file for current locale is non-existant
-    if (!infoFile->exists())
+    if (!QFile::exists(infoFile))
     {
-        delete infoFile;
         infoFile = HWDataManager::instance().findFileForRead(
                                             QString("Locale/missions_en.txt"));
     }
 
 
     // preload mission info for current locale
-    m_info =
-        new QSettings(infoFile->fileName(), QSettings::IniFormat, this);
+    m_info = new QSettings(infoFile, QSettings::IniFormat, this);
 
-    // this QFile isn't needed any further
-    delete infoFile;
 
     QStringList missionList =
             HWDataManager::instance().entryList(
--- a/QTfrontend/ui/widget/chatwidget.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -122,15 +122,9 @@
 {
     this->gameSettings = gameSettings;
     this->notify = notify;
-    if(notify && gameSettings->value("frontend/sound", true).toBool()) {
-        QFile * tmpFile = HWDataManager::instance().findFileForRead(
-                                            "Sounds/voices/Classic/Hello.ogg");
-
-        helloSound = tmpFile->fileName();
-
-        // this QFile isn't needed any further
-        delete tmpFile;
-    }
+    if(notify && gameSettings->value("frontend/sound", true).toBool())
+        helloSound = HWDataManager::instance().findFileForRead(
+                        "Sounds/voices/Classic/Hello.ogg");
 
     mainLayout.setSpacing(1);
     mainLayout.setMargin(1);
--- a/QTfrontend/util/HWDataManager.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/util/HWDataManager.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -75,17 +75,15 @@
 }
 
 
-QFile * HWDataManager::findFileForRead(
+QString HWDataManager::findFileForRead(
                                 const QString & relativeDataFilePath) const
 {
-    QFile * file =
-            new QFile(userData->absolutePath()+"/"+relativeDataFilePath);
-    if (!file->exists())
-    {
-        delete file;
-        file = new QFile(defaultData->absolutePath()+"/"+relativeDataFilePath);
-    }
-    return file;
+    QString path = userData->absolutePath()+"/"+relativeDataFilePath;
+
+    if (!QFile::exists(path))
+        path = defaultData->absolutePath()+"/"+relativeDataFilePath;
+
+    return path;
 }
 
 
--- a/QTfrontend/util/HWDataManager.h	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/util/HWDataManager.h	Fri Oct 21 08:41:21 2011 +0200
@@ -63,15 +63,14 @@
                          ) const;
 
     /**
-     * @brief Creates a file object for the desired data path and
-     * returns a pointer to it.
+     * @brief Returns path for the desires data file.
      * 
      * Use this method if you want to read an existing data file.
      * 
-     * @param relativeDataFilePath path to the data file.
-     * @return respective <code>QFile</code> pointer, the file may not exist.
+     * @param relativeDataFilePath relative path of the data file.
+     * @return real path to the file.
      */
-    QFile * findFileForRead(const QString & relativeDataFilePath) const;
+    QString findFileForRead(const QString & relativeDataFilePath) const;
 
 
     /**
--- a/QTfrontend/util/SDLInteraction.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/util/SDLInteraction.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -50,7 +50,7 @@
         addGameControllerKeys();
     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 
-    soundMap = new QMap<QString,Mix_Chunk*>();
+    m_soundMap = new QMap<QString,Mix_Chunk*>();
 }
 
 
@@ -65,7 +65,7 @@
     }
     SDL_Quit();
 
-    delete soundMap;
+    delete m_soundMap;
 }
 
 
@@ -184,10 +184,10 @@
 void SDLInteraction::playSoundFile(const QString & soundFile)
 {
     SDLAudioInit();
-    if (!soundMap->contains(soundFile))
-        soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
+    if (!m_soundMap->contains(soundFile))
+        m_soundMap->insert(soundFile, Mix_LoadWAV(soundFile.toLocal8Bit().constData()));
 
-    Mix_PlayChannel(-1, soundMap->value(soundFile), 0);
+    Mix_PlayChannel(-1, m_soundMap->value(soundFile), 0);
 }
 
 
--- a/QTfrontend/util/SDLInteraction.h	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/util/SDLInteraction.h	Fri Oct 21 08:41:21 2011 +0200
@@ -52,8 +52,7 @@
     QString m_musicTrack; ///< path to the music track;
     bool m_isPlayingMusic; ///< true if music was started but not stopped again.
 
-
-    QMap<QString,Mix_Chunk*> * soundMap; ///< maps sound file paths to channels
+    QMap<QString,Mix_Chunk*> * m_soundMap; ///< maps sound file paths to channels
 
 public:
     /**
--- a/QTfrontend/util/namegen.cpp	Fri Oct 21 08:03:42 2011 +0200
+++ b/QTfrontend/util/namegen.cpp	Fri Oct 21 08:41:21 2011 +0200
@@ -125,8 +125,8 @@
     QStringList list;
 
     // find .txt to load the names from
-    QFile * file = HWDataManager::instance().findFileForRead(QString(
-                                                "Names/%1.txt").arg(filename));
+    QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
+                                                "Names/%1.txt").arg(filename)));
 
     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
     {
@@ -154,8 +154,8 @@
     QStringList list;
 
     // find .cfg to load the dicts from
-    QFile * file = HWDataManager::instance().findFileForRead(QString(
-                                                "Names/%1.cfg").arg(hatname));
+    QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
+                                                "Names/%1.cfg").arg(hatname)));
 
     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
     {
@@ -183,8 +183,8 @@
     typesAvailable = false;
 
     // find .ini to load the names from
-    QFile * file =
-          HWDataManager::instance().findFileForRead(QString("Names/types.ini"));
+    QFile * file = new QFile(
+        HWDataManager::instance().findFileForRead(QString("Names/types.ini")));
 
 
     if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))