# HG changeset patch
# User sheepluva
# Date 1335606969 -7200
# Node ID 1be3e48e1d53616d67b6f02712b01d7ff000ea35
# Parent  576c453822bf54e1943235c261e65e63ee60b5c9
MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model

diff -r 576c453822bf -r 1be3e48e1d53 QTfrontend/model/MapModel.cpp
--- a/QTfrontend/model/MapModel.cpp	Sat Apr 28 09:25:30 2012 +0200
+++ b/QTfrontend/model/MapModel.cpp	Sat Apr 28 11:56:09 2012 +0200
@@ -115,7 +115,6 @@
             {
                 // TODO: icon
                 caption = QComboBox::tr("Mission") + ": " + map;
-                m_nMissions++;
             }
             else
                 caption = map;
@@ -134,8 +133,6 @@
 
     }
 
-    // update mission count member
-    m_nMissions = missionMaps.size();
 
     // define a separator item
     QStandardItem separator("---");
@@ -152,6 +149,19 @@
     items.append(separator.clone());
     items.append(staticMaps);
 
+    // store start-index and count of relevant types
+    typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
+    typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
+    typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
+    // mission maps
+    int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
+    int count = missionMaps.size();
+    typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
+    // static maps
+    startIdx += count + 1; // start after missions and 2 separators
+    count = staticMaps.size();
+    typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
+
     // store list contents in the item model
     QStandardItemModel::appendColumn(items);
 
@@ -160,9 +170,26 @@
 }
 
 
-int MapModel::missionCount() const
+int MapModel::mapCount(MapType type) const
+{
+    // return the count for this type
+    // fetch it from the second int in typeLoc, return 0 if no entry
+    return typeLoc.value(type, QPair<int,int>(0,0)).second;
+}
+
+
+int MapModel::randomMap(MapType type) const
 {
-    return m_nMissions;
+    // return a random index for this type or -1 if none available
+    QPair<int,int> loc = typeLoc.value(type, QPair<int,int>(-1,0));
+
+    int startIdx = loc.first;
+    int count = loc.second;
+
+    if (count < 1)
+        return -1;
+    else
+        return startIdx + (rand() % count);
 }
 
 
diff -r 576c453822bf -r 1be3e48e1d53 QTfrontend/model/MapModel.h
--- a/QTfrontend/model/MapModel.h	Sat Apr 28 09:25:30 2012 +0200
+++ b/QTfrontend/model/MapModel.h	Sat Apr 28 11:56:09 2012 +0200
@@ -35,7 +35,7 @@
 #include "DataManager.h"
 
 /**
- * @brief A model listing available themes
+ * @brief A model that vertically lists available maps
  *
  * @author sheepluva
  * @since 0.9.18
@@ -66,10 +66,18 @@
         };
 
         /**
-         * @brief Returns the number of available mission maps.
-         * @return mission map count.
+         * @brief Returns the number of available maps of a specified type.
+         * @param type map type to get the count of.
+         * @return count of maps that have the specified type.
          */
-        int missionCount() const;
+        int mapCount(MapType type) const;
+
+        /**
+         * @brief Returns the row-index of a random map with a specified type.
+         * @param type desired type of map.
+         * @return row-index of a map with the desired type, -1 if none found.
+         */
+        int randomMap(MapType type) const;
 
 
     public slots:
@@ -78,7 +86,8 @@
 
 
     private:
-        int m_nMissions; ///< used to keep track of the mission amount
+        ///< start-index and map count for each map-type
+        QMap<MapType, QPair<int,int> > typeLoc;
 
         /**
          * @brief Creates a QStandardItem, that holds the map info and item appearance.
diff -r 576c453822bf -r 1be3e48e1d53 QTfrontend/ui/widget/mapContainer.cpp
--- a/QTfrontend/ui/widget/mapContainer.cpp	Sat Apr 28 09:25:30 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Sat Apr 28 11:56:09 2012 +0200
@@ -378,11 +378,12 @@
     int id = 0;
     for(int i = 0; i < chooseMap->count(); i++)
     {
-        // skip separators
-        if (chooseMap->itemData(i, Qt::AccessibleDescriptionRole) == QLatin1String("separator"))
+        QVariant data = chooseMap->itemData(i, Qt::UserRole + 1);
+        // skip separators etc
+        if (!data.isValid())
             continue;
-        Q_ASSERT(chooseMap->itemData(i, Qt::UserRole + 1).canConvert<MapModel::MapInfo>());
-        MapModel::MapInfo mapInfo = chooseMap->itemData(i, Qt::UserRole + 1).value<MapModel::MapInfo>();
+        Q_ASSERT(data.canConvert<MapModel::MapInfo>());
+        MapModel::MapInfo mapInfo = data.value<MapModel::MapInfo>();
 
         if (mapInfo.name == map)
         {
@@ -419,6 +420,8 @@
 
 void HWMapContainer::setRandomMap()
 {
+    int idx;
+
     setRandomSeed();
     switch(m_mapInfo.type)
     {
@@ -430,29 +433,17 @@
             emit drawMapRequested();
             break;
         case MapModel::MissionMap:
-            setRandomMission();
-            break;
         case MapModel::StaticMap:
-            setRandomStatic();
+            // get random map of same type
+            idx = m_mapModel->randomMap(m_mapInfo.type);
+            chooseMap->setCurrentIndex(idx);
+            mapChanged(idx);
             break;
         case MapModel::Invalid:
             Q_ASSERT(false);
     }
 }
 
-void HWMapContainer::setRandomStatic()
-{
-    int i = MAPGEN_MAP + 3 + numMissions + rand() % (chooseMap->count() - MAPGEN_MAP - 3 - numMissions);
-    chooseMap->setCurrentIndex(i);
-    mapChanged(i);
-}
-
-void HWMapContainer::setRandomMission()
-{
-    int i = MAPGEN_MAP + 2 + rand() % numMissions;
-    chooseMap->setCurrentIndex(i);
-    mapChanged(i);
-}
 
 void HWMapContainer::setRandomSeed()
 {
@@ -604,8 +595,8 @@
 
 void HWMapContainer::updateModelViews()
 {
-    numMissions = m_mapModel->missionCount();
-
+    // TODO: reselect theme
+    // FIXME: issues with generated maps?
     if (!m_mapInfo.name.isEmpty())
         intSetMap(m_mapInfo.name);
 }
diff -r 576c453822bf -r 1be3e48e1d53 QTfrontend/ui/widget/mapContainer.h
--- a/QTfrontend/ui/widget/mapContainer.h	Sat Apr 28 09:25:30 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.h	Sat Apr 28 11:56:09 2012 +0200
@@ -91,8 +91,6 @@
         void setRandomSeed();
         void setRandomTheme();
         void setRandomMap();
-        void setRandomStatic();
-        void setRandomMission();
         void themeSelected(const QModelIndex & current, const QModelIndex &);
         void addInfoToPreview(QPixmap image);
         void seedEdited();
@@ -120,7 +118,6 @@
         QLabel *maze_size_label;
         QComboBox *cbMazeSize;
         MapGenerator mapgen;
-        int numMissions;
         DrawMapScene drawMapScene;
 
         void intSetSeed(const QString & seed);