mapContainer: use DataManager, reload maps after data update (e.g. by DLC)
authorsheepluva
Thu, 26 Apr 2012 09:48:52 +0200
changeset 6933 78d194a30520
parent 6932 005393616925
child 6934 14a230552c2e
mapContainer: use DataManager, reload maps after data update (e.g. by DLC)
QTfrontend/hwconsts.cpp.in
QTfrontend/hwconsts.h
QTfrontend/main.cpp
QTfrontend/ui/widget/mapContainer.cpp
QTfrontend/ui/widget/mapContainer.h
--- a/QTfrontend/hwconsts.cpp.in	Thu Apr 26 09:47:35 2012 +0200
+++ b/QTfrontend/hwconsts.cpp.in	Thu Apr 26 09:48:52 2012 +0200
@@ -28,7 +28,6 @@
 QDir * datadir = new QDir();
 
 ThemesModel * themesModel;
-QStringList * mapList;
 QStringList * scriptList;
 
 bool custom_config = false;
--- a/QTfrontend/hwconsts.h	Thu Apr 26 09:47:35 2012 +0200
+++ b/QTfrontend/hwconsts.h	Thu Apr 26 09:48:52 2012 +0200
@@ -42,7 +42,6 @@
 class QStringListModel;
 
 extern ThemesModel * themesModel;
-extern QStringList * mapList;
 extern QStringList * scriptList;
 
 extern QString * cDefaultAmmoStore;
--- a/QTfrontend/main.cpp	Thu Apr 26 09:47:35 2012 +0200
+++ b/QTfrontend/main.cpp	Thu Apr 26 09:48:52 2012 +0200
@@ -258,12 +258,6 @@
         }
     }
 
-    mapList = new QStringList(dataMgr.entryList(
-                                  QString("Maps"),
-                                  QDir::Dirs | QDir::NoDotAndDotDot
-                              )
-                             );
-
     scriptList = new QStringList(dataMgr.entryList(
                                      QString("Scripts/Multiplayer"),
                                      QDir::Files,
--- a/QTfrontend/ui/widget/mapContainer.cpp	Thu Apr 26 09:47:35 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Thu Apr 26 09:48:52 2012 +0200
@@ -68,102 +68,9 @@
 
     chooseMap = new QComboBox(mapWidget);
     chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
-    chooseMap->addItem(
-// FIXME - need real icons. Disabling until then
-//QIcon(":/res/mapRandom.png"),
-        QComboBox::tr("generated map..."));
-    chooseMap->addItem(
-// FIXME - need real icons. Disabling until then
-//QIcon(":/res/mapMaze.png"),
-        QComboBox::tr("generated maze..."));
 
-    chooseMap->addItem(QComboBox::tr("hand drawn map..."));
-    chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
-
-    chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
-
-    int missionindex = chooseMap->count();
-    numMissions = 0;
-    QFile mapLuaFile;
-    QFile mapCfgFile;
-    for (int i = 0; i < mapList->size(); ++i)
-    {
-        QString map = (*mapList)[i];
-        mapCfgFile.setFileName(
-            QString("%1/Data/Maps/%2/map.cfg")
-            .arg(cfgdir->absolutePath())
-            .arg(map));
-        if (mapCfgFile.exists())
-        {
-            mapLuaFile.setFileName(
-                QString("%1/Data/Maps/%2/map.lua")
-                .arg(cfgdir->absolutePath())
-                .arg(map));
-        }
-        else
-        {
-            mapCfgFile.setFileName(
-                QString("%1/Maps/%2/map.cfg")
-                .arg(datadir->absolutePath())
-                .arg(map));
-            mapLuaFile.setFileName(
-                QString("%1/Maps/%2/map.lua")
-                .arg(datadir->absolutePath())
-                .arg(map));
-        }
-
-        if (mapCfgFile.open(QFile::ReadOnly))
-        {
-            QString theme;
-            quint32 limit = 0;
-            QString scheme;
-            QString weapons;
-            QList<QVariant> mapInfo;
-            bool isMission = mapLuaFile.exists();
-
-            QTextStream input(&mapCfgFile);
-            input >> theme;
-            input >> limit;
-            input >> scheme;
-            input >> weapons;
-            mapInfo.push_back(map);
-            mapInfo.push_back(theme);
-            if (limit)
-                mapInfo.push_back(limit);
-            else
-                mapInfo.push_back(18);
-
-
-            mapInfo.push_back(isMission);
-
-            if (scheme.isEmpty())
-                scheme = "locked";
-            scheme.replace("_", " ");
-
-            if (weapons.isEmpty())
-                weapons = "locked";
-            weapons.replace("_", " ");
-
-            mapInfo.push_back(scheme);
-            mapInfo.push_back(weapons);
-
-            if(isMission)
-            {
-                chooseMap->insertItem(missionindex++,
-// FIXME - need real icons. Disabling until then
-//QIcon(":/res/mapMission.png"),
-                                      QComboBox::tr("Mission") + ": " + map, mapInfo);
-                numMissions++;
-            }
-            else
-                chooseMap->addItem(
-// FIXME - need real icons. Disabling until then
-//QIcon(":/res/mapCustom.png"),
-                    map, mapInfo);
-            mapCfgFile.close();
-        }
-    }
-    chooseMap->insertSeparator(missionindex); // separator between missions and maps
+    loadMapList();
+    connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(loadMapList()));
 
     connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
     mapLayout->addWidget(chooseMap, 1, 1);
@@ -683,3 +590,106 @@
 
     updatePreview();
 }
+
+
+void HWMapContainer::loadMapList()
+{
+    // TODO: convert to model
+
+    // remember previous selection
+    QString selMap = getCurrentMap();
+
+    chooseMap->clear();
+
+    chooseMap->addItem(
+// FIXME - need real icons. Disabling until then
+//QIcon(":/res/mapRandom.png"),
+        QComboBox::tr("generated map..."));
+    chooseMap->addItem(
+// FIXME - need real icons. Disabling until then
+//QIcon(":/res/mapMaze.png"),
+        QComboBox::tr("generated maze..."));
+
+    chooseMap->addItem(QComboBox::tr("hand drawn map..."));
+
+    chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
+    chooseMap->insertSeparator(chooseMap->count()); // separator between generators and missions
+
+    int missionindex = chooseMap->count();
+    numMissions = 0;
+    QFile mapLuaFile;
+    QFile mapCfgFile;
+
+    DataManager & dataMgr = DataManager::instance();
+
+    QStringList mapList = dataMgr.entryList(
+                              QString("Maps"),
+                              QDir::Dirs | QDir::NoDotAndDotDot
+                          );
+
+    foreach (QString map, mapList)
+    {
+        mapCfgFile.setFileName(
+            dataMgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
+        mapLuaFile.setFileName(
+            dataMgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
+
+        if (mapCfgFile.open(QFile::ReadOnly))
+        {
+            QString theme;
+            quint32 limit = 0;
+            QString scheme;
+            QString weapons;
+            QList<QVariant> mapInfo;
+            bool isMission = mapLuaFile.exists();
+
+            QTextStream input(&mapCfgFile);
+            input >> theme;
+            input >> limit;
+            input >> scheme;
+            input >> weapons;
+            mapInfo.push_back(map);
+            mapInfo.push_back(theme);
+            if (limit)
+                mapInfo.push_back(limit);
+            else
+                mapInfo.push_back(18);
+
+
+            mapInfo.push_back(isMission);
+
+            if (scheme.isEmpty())
+                scheme = "locked";
+            scheme.replace("_", " ");
+
+            if (weapons.isEmpty())
+                weapons = "locked";
+            weapons.replace("_", " ");
+
+            mapInfo.push_back(scheme);
+            mapInfo.push_back(weapons);
+
+            if(isMission)
+            {
+                chooseMap->insertItem(missionindex++,
+// FIXME - need real icons. Disabling until then
+//QIcon(":/res/mapMission.png"),
+                                      QComboBox::tr("Mission") + ": " + map, mapInfo);
+                numMissions++;
+            }
+            else
+                chooseMap->addItem(
+// FIXME - need real icons. Disabling until then
+//QIcon(":/res/mapCustom.png"),
+                    map, mapInfo);
+            mapCfgFile.close();
+        }
+    }
+
+    chooseMap->insertSeparator(missionindex); // separator between missions and maps
+
+    // if a map was selected already let's reselect it after reloading the map list
+    if (!selMap.isEmpty()) {
+        setMap(selMap);
+    }
+}
--- a/QTfrontend/ui/widget/mapContainer.h	Thu Apr 26 09:47:35 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.h	Thu Apr 26 09:48:52 2012 +0200
@@ -27,6 +27,8 @@
 #include <QByteArray>
 #include <QLineEdit>
 
+#include "DataManager.h"
+
 #include "hwmap.h"
 #include "drawmapscene.h"
 
@@ -92,6 +94,7 @@
         void themeSelected(const QModelIndex & current, const QModelIndex &);
         void addInfoToPreview(QPixmap image);
         void seedEdited();
+        void loadMapList();
 
     protected:
         virtual void resizeEvent ( QResizeEvent * event );