QTfrontend/model/MapModel.cpp
changeset 6983 ede55af89e78
parent 6952 7f70f37bbf08
child 8049 133e22b5c410
equal deleted inserted replaced
6982:8d41d22a291d 6983:ede55af89e78
   146     items.append(separator.clone());
   146     items.append(separator.clone());
   147     items.append(missionMaps);
   147     items.append(missionMaps);
   148     items.append(separator.clone());
   148     items.append(separator.clone());
   149     items.append(staticMaps);
   149     items.append(staticMaps);
   150 
   150 
       
   151 
       
   152     // create row-index lookup table
       
   153 
       
   154     m_mapIndexes.clear();
       
   155 
       
   156     int count = items.size();
       
   157 
       
   158     for (int i = 0; i < count; i++)
       
   159     {
       
   160         QStandardItem * si = items.at(i);
       
   161         QVariant v = si->data(Qt::UserRole + 1);
       
   162         if (v.canConvert<MapInfo>())
       
   163             m_mapIndexes.insert(v.value<MapInfo>().name, i);
       
   164     }
       
   165 
       
   166 
   151     // store start-index and count of relevant types
   167     // store start-index and count of relevant types
   152     typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
   168 
   153     typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
   169     m_typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
   154     typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
   170     m_typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
       
   171     m_typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
   155     // mission maps
   172     // mission maps
   156     int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
   173     int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
   157     int count = missionMaps.size();
   174     count = missionMaps.size();
   158     typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
   175     m_typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
   159     // static maps
   176     // static maps
   160     startIdx += count + 1; // start after missions and 2 separators
   177     startIdx += count + 1; // start after missions and 2 separators
   161     count = staticMaps.size();
   178     count = staticMaps.size();
   162     typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
   179     m_typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
   163 
   180 
   164     // store list contents in the item model
   181     // store list contents in the item model
   165     QStandardItemModel::appendColumn(items);
   182     QStandardItemModel::appendColumn(items);
   166 
   183 
   167 
   184 
   168     endResetModel();
   185     endResetModel();
   169 }
   186 }
   170 
   187 
   171 
   188 
   172 int MapModel::mapCount(MapType type) const
       
   173 {
       
   174     // return the count for this type
       
   175     // fetch it from the second int in typeLoc, return 0 if no entry
       
   176     return typeLoc.value(type, QPair<int,int>(0,0)).second;
       
   177 }
       
   178 
       
   179 
       
   180 int MapModel::randomMap(MapType type) const
   189 int MapModel::randomMap(MapType type) const
   181 {
   190 {
   182     // return a random index for this type or -1 if none available
   191     // return a random index for this type or -1 if none available
   183     QPair<int,int> loc = typeLoc.value(type, QPair<int,int>(-1,0));
   192     QPair<int,int> loc = m_typeLoc.value(type, QPair<int,int>(-1,0));
   184 
   193 
   185     int startIdx = loc.first;
   194     int startIdx = loc.first;
   186     int count = loc.second;
   195     int count = loc.second;
   187 
   196 
   188     if (count < 1)
   197     if (count < 1)
   218     qvar.setValue(mapInfo);
   227     qvar.setValue(mapInfo);
   219     item->setData(qvar, Qt::UserRole + 1);
   228     item->setData(qvar, Qt::UserRole + 1);
   220 
   229 
   221     return item;
   230     return item;
   222 }
   231 }
       
   232 
       
   233 
       
   234 int MapModel::indexOf(const QString & map) const
       
   235 {
       
   236     return m_mapIndexes.value(map, -1);
       
   237 }
       
   238