QTfrontend/model/hats.cpp
changeset 6160 863d3edf5690
parent 6061 15b4b485a1c5
child 6167 728cabee2c9f
--- a/QTfrontend/model/hats.cpp	Thu Oct 20 13:10:24 2011 +0200
+++ b/QTfrontend/model/hats.cpp	Thu Oct 20 23:25:16 2011 +0200
@@ -23,27 +23,57 @@
 #include "hwform.h"
 #include "hats.h"
 
+#include "HWDataManager.h"
+
 HatsModel::HatsModel(QObject* parent) :
   QAbstractListModel(parent)
 {
-    QFile hhfile;
-    hhfile.setFileName(cfgdir->absolutePath() + "/Data/Graphics/Hedgehog/Idle.png");
-    if (!hhfile.exists()) hhfile.setFileName(datadir->absolutePath() + "/Graphics/Hedgehog/Idle.png");
-    QPixmap hhpix = QPixmap(QFileInfo(hhfile).absoluteFilePath()).copy(0, 0, 32, 32);
+    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;
+
+    // my reserved hats
+    QStringList hatsList = HWDataManager::instance().entryList(
+                                   "Graphics/Hats/Reserved",
+                                   QDir::Files,
+                                   QStringList(playerHash+"*.png")
+                               );
+
+    int nReserved = hatsList.size();
 
-    QDir tmpdir;
-    tmpdir.cd(cfgdir->absolutePath());
-    tmpdir.cd("Data");
-    tmpdir.cd("Graphics");
-    tmpdir.cd("Hats");
+    // regular hats
+    hatsList.append(HWDataManager::instance().entryList(
+                                   "Graphics/Hats",
+                                   QDir::Files,
+                                   QStringList("*.png")
+                               )
+                   );
+
+
+    int nHats = hatsList.size();
+
+    for (int i = 0; i < nHats; i++)
+    {
+        bool isReserved = (i < nReserved);
 
-    tmpdir.setFilter(QDir::Files);
+        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());
 
-    QStringList userhatsList = tmpdir.entryList(QStringList("*.png"));
-    for (QStringList::Iterator it = userhatsList.begin(); it != userhatsList.end(); ++it )
-    {
-        QString str = QString(*it).replace(QRegExp("^(.*)\\.png"), "\\1");
-        QPixmap pix(cfgdir->absolutePath() + "/Data/Graphics/Hats/" + str + ".png");
+        // this QFile isn't needed any further
+        delete hatFile;
+
+        // rename properly
+        if (isReserved)
+            str = "Reserved "+str.remove(0,32);
 
         QPixmap tmppix(32, 37);
         tmppix.fill(QColor(Qt::transparent));
@@ -55,49 +85,10 @@
             painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
         painter.end();
 
-        hats.append(qMakePair(str, QIcon(tmppix)));
-    }
-
-    tmpdir.cd(datadir->absolutePath());
-    tmpdir.cd("Graphics");
-    tmpdir.cd("Hats");
-
-    QStringList hatsList = tmpdir.entryList(QStringList("*.png"));
-    for (QStringList::Iterator it = hatsList.begin(); it != hatsList.end(); ++it )
-    {
-        if (userhatsList.contains(*it,Qt::CaseInsensitive)) continue;
-        QString str = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
-        QPixmap pix(datadir->absolutePath() + "/Graphics/Hats/" + str + ".png");
-
-        QPixmap tmppix(32, 37);
-        tmppix.fill(QColor(Qt::transparent));
-
-        QPainter painter(&tmppix);
-        painter.drawPixmap(QPoint(0, 5), hhpix);
-        painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
-        if(pix.width() > 32)
-            painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32));
-        painter.end();
-
-        hats.append(qMakePair(str, QIcon(tmppix)));
-    }
-    // Reserved hats
-    tmpdir.cd("Reserved");
-    hatsList = tmpdir.entryList(QStringList(playerHash+"*.png"));
-    for (QStringList::Iterator it = hatsList.begin(); it != hatsList.end(); ++it )
-    {
-        QString str = (*it).replace(QRegExp("^(.*)\\.png"), "\\1");
-        QPixmap pix(datadir->absolutePath() + "/Graphics/Hats/Reserved/" + str + ".png");
-
-        QPixmap tmppix(32, 37);
-        tmppix.fill(QColor(Qt::transparent));
-
-        QPainter painter(&tmppix);
-        painter.drawPixmap(QPoint(0, 5), hhpix);
-        painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32));
-        painter.end();
-
-        hats.append(qMakePair("Reserved "+str.remove(0,32), QIcon(tmppix)));
+        if (str == "NoHat")
+            hats.prepend(qMakePair(str, QIcon(tmppix)));
+        else
+            hats.append(qMakePair(str, QIcon(tmppix)));
     }
 }