QTfrontend/model/MapModel.cpp
changeset 6947 1be3e48e1d53
parent 6943 1fe601a2761b
child 6948 7271ce89950f
--- 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);
 }