QTfrontend/model/MapModel.cpp
changeset 6943 1fe601a2761b
parent 6939 970389573788
child 6947 1be3e48e1d53
equal deleted inserted replaced
6942:11f52445e8cd 6943:1fe601a2761b
    25 #include "MapModel.h"
    25 #include "MapModel.h"
    26 
    26 
    27 
    27 
    28 void MapModel::loadMaps()
    28 void MapModel::loadMaps()
    29 {
    29 {
       
    30     // this method resets the contents of this model (important to know for views).
    30     beginResetModel();
    31     beginResetModel();
    31 
    32 
    32 
    33     // we'll need the DataManager a few times, so let's get a reference to it
    33     DataManager & datamgr = DataManager::instance();
    34     DataManager & datamgr = DataManager::instance();
    34 
    35 
       
    36     // fetch list of available maps
    35     QStringList maps =
    37     QStringList maps =
    36         datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
    38         datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
    37 
    39 
       
    40     // empty list, so that we can (re)fill it
    38     QStandardItemModel::clear();
    41     QStandardItemModel::clear();
    39 
    42 
    40     QList<QStandardItem *> genMaps;
    43     QList<QStandardItem *> genMaps;
    41     QList<QStandardItem *> missionMaps;
    44     QList<QStandardItem *> missionMaps;
    42     QList<QStandardItem *> staticMaps;
    45     QList<QStandardItem *> staticMaps;
    43 
    46 
       
    47     // add generated/handdrawn maps to list
    44     // TODO: icons for these
    48     // TODO: icons for these
    45 
    49 
    46     genMaps.append(
    50     genMaps.append(
    47         infoToItem(QIcon(), QComboBox::tr("generated map..."), GeneratedMap, "+rnd+"));
    51         infoToItem(QIcon(), QComboBox::tr("generated map..."), GeneratedMap, "+rnd+"));
    48     genMaps.append(
    52     genMaps.append(
    49         infoToItem(QIcon(), QComboBox::tr("generated maze..."), GeneratedMaze, "+maze+"));
    53         infoToItem(QIcon(), QComboBox::tr("generated maze..."), GeneratedMaze, "+maze+"));
    50     genMaps.append(
    54     genMaps.append(
    51         infoToItem(QIcon(), QComboBox::tr("hand drawn map..."), HandDrawnMap, "+drawn+"));
    55         infoToItem(QIcon(), QComboBox::tr("hand drawn map..."), HandDrawnMap, "+drawn+"));
    52 
    56 
    53 
    57     // only 2 map relate files are relevant:
       
    58     // - the cfg file that contains the settings/info of the map
       
    59     // - the lua file - if it exists it's a mission, otherwise it isn't
    54     QFile mapLuaFile;
    60     QFile mapLuaFile;
    55     QFile mapCfgFile;
    61     QFile mapCfgFile;
    56 
    62 
       
    63     // add mission/static maps to lists
    57     foreach (QString map, maps)
    64     foreach (QString map, maps)
    58     {
    65     {
    59         mapCfgFile.setFileName(
    66         mapCfgFile.setFileName(
    60             datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
    67             datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
    61         mapLuaFile.setFileName(
    68         mapLuaFile.setFileName(
    67             QString caption;
    74             QString caption;
    68             QString theme;
    75             QString theme;
    69             quint32 limit = 0;
    76             quint32 limit = 0;
    70             QString scheme;
    77             QString scheme;
    71             QString weapons;
    78             QString weapons;
       
    79             // if there is a lua file for this map, then it's a mission
    72             bool isMission = mapLuaFile.exists();
    80             bool isMission = mapLuaFile.exists();
    73             MapType type = isMission?MissionMap:StaticMap;
    81             MapType type = isMission?MissionMap:StaticMap;
    74 
    82 
       
    83             // load map info from file
    75             QTextStream input(&mapCfgFile);
    84             QTextStream input(&mapCfgFile);
    76             input >> theme;
    85             input >> theme;
    77             input >> limit;
    86             input >> limit;
    78             input >> scheme;
    87             if (isMission) { // scheme and weapons are only relevant for missions
    79             input >> weapons;
    88                 input >> scheme;
       
    89                 input >> weapons;
       
    90             }
    80             mapCfgFile.close();
    91             mapCfgFile.close();
    81 
    92 
       
    93             // let's use some semi-sane hedgehog limit, rather than none
    82             if (limit == 0)
    94             if (limit == 0)
    83                 limit = 18;
    95                 limit = 18;
    84 
    96 
    85 
    97 
    86             if (scheme.isEmpty())
    98             // the default scheme/weaponset for missions.
    87                 scheme = "locked";
    99             // if empty we assume the map sets these internally -> locked
    88             else
   100             if (isMission)
    89                 scheme.replace("_", " ");
   101             {
       
   102                 if (scheme.isEmpty())
       
   103                     scheme = "locked";
       
   104                 else
       
   105                     scheme.replace("_", " ");
    90 
   106 
    91             if (weapons.isEmpty())
   107                 if (weapons.isEmpty())
    92                 weapons = "locked";
   108                     weapons = "locked";
    93             else
   109                 else
    94                 weapons.replace("_", " ");
   110                     weapons.replace("_", " ");
       
   111             }
    95 
   112 
       
   113             // add a mission caption prefix to missions
    96             if (isMission)
   114             if (isMission)
    97             {
   115             {
    98                 // TODO: icon
   116                 // TODO: icon
    99                 caption = QComboBox::tr("Mission") + ": " + map;
   117                 caption = QComboBox::tr("Mission") + ": " + map;
   100                 m_nMissions++;
   118                 m_nMissions++;
   101             }
   119             }
   102             else
   120             else
   103                 caption = map;
   121                 caption = map;
   104 
   122 
       
   123             // we know everything there is about the map, let's get am item for it
   105             QStandardItem * item = infoToItem(
   124             QStandardItem * item = infoToItem(
   106                 QIcon(), caption, type, map, theme, limit, scheme, weapons);
   125                 QIcon(), caption, type, map, theme, limit, scheme, weapons);
   107 
   126 
       
   127             // append item to the list
   108             if (isMission)
   128             if (isMission)
   109                 missionMaps.append(item);
   129                 missionMaps.append(item);
   110             else
   130             else
   111                 staticMaps.append(item);
   131                 staticMaps.append(item);
   112         
   132         
   113         }
   133         }
   114 
   134 
   115     }
   135     }
   116 
   136 
       
   137     // update mission count member
   117     m_nMissions = missionMaps.size();
   138     m_nMissions = missionMaps.size();
   118 
   139 
       
   140     // define a separator item
   119     QStandardItem separator("---");
   141     QStandardItem separator("---");
   120     separator.setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
   142     separator.setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
   121     separator.setFlags(separator.flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
   143     separator.setFlags(separator.flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
   122 
   144 
       
   145     // create list:
       
   146     // generated+handdrawn maps, 2 saperators, missions, 1 separator, static maps
   123     QList<QStandardItem * > items;
   147     QList<QStandardItem * > items;
   124     items.append(genMaps);
   148     items.append(genMaps);
   125     items.append(separator.clone());
   149     items.append(separator.clone());
   126     items.append(separator.clone());
   150     items.append(separator.clone());
   127     items.append(missionMaps);
   151     items.append(missionMaps);
   128     items.append(separator.clone());
   152     items.append(separator.clone());
   129     items.append(staticMaps);
   153     items.append(staticMaps);
   130 
   154 
       
   155     // store list contents in the item model
   131     QStandardItemModel::appendColumn(items);
   156     QStandardItemModel::appendColumn(items);
       
   157 
   132 
   158 
   133     endResetModel();
   159     endResetModel();
   134 }
   160 }
   135 
   161 
   136 
   162