QTfrontend/model/MapModel.cpp
changeset 8419 d99f46b676b5
parent 8393 85bd6c7b2641
child 8464 c708b4f5cffc
equal deleted inserted replaced
8418:4543cc2049af 8419:d99f46b676b5
    21  * @brief MapModel class implementation
    21  * @brief MapModel class implementation
    22  */
    22  */
    23 
    23 
    24 #include <QSettings>
    24 #include <QSettings>
    25 
    25 
       
    26 #include "physfs.h"
    26 #include "MapModel.h"
    27 #include "MapModel.h"
    27 #include "HWApplication.h"
    28 #include "HWApplication.h"
    28 
    29 
    29 MapModel::MapInfo MapModel::MapInfoRandom = {MapModel::GeneratedMap, "+rnd+", "", 0, "", "", ""};
    30 MapModel::MapInfo MapModel::MapInfoRandom = {MapModel::GeneratedMap, "+rnd+", "", 0, "", "", ""};
    30 MapModel::MapInfo MapModel::MapInfoMaze = {MapModel::GeneratedMaze, "+maze+", "", 0, "", "", ""};
    31 MapModel::MapInfo MapModel::MapInfoMaze = {MapModel::GeneratedMaze, "+maze+", "", 0, "", "", ""};
    31 MapModel::MapInfo MapModel::MapInfoDrawn = {MapModel::HandDrawnMap, "+drawn+", "", 0, "", "", ""};
    32 MapModel::MapInfo MapModel::MapInfoDrawn = {MapModel::HandDrawnMap, "+drawn+", "", 0, "", "", ""};
    32 
    33 
    33 void MapModel::loadMaps(MapType maptype)
    34 void MapModel::loadMaps(MapType maptype)
    34 {
    35 {
       
    36     const QString appDir = QString(PHYSFS_getBaseDir());
       
    37 
    35     // this method resets the contents of this model (important to know for views).
    38     // this method resets the contents of this model (important to know for views).
    36     beginResetModel();
    39     beginResetModel();
    37 
    40 
    38     // we'll need the DataManager a few times, so let's get a reference to it
    41     // we'll need the DataManager a few times, so let's get a reference to it
    39     DataManager & datamgr = DataManager::instance();
    42     DataManager & datamgr = DataManager::instance();
    64             QString theme;
    67             QString theme;
    65             quint32 limit = 0;
    68             quint32 limit = 0;
    66             QString scheme;
    69             QString scheme;
    67             QString weapons;
    70             QString weapons;
    68             QString desc;
    71             QString desc;
       
    72             bool dlc;
    69 
    73 
    70             // if there is a lua file for this map, then it's a mission
    74             // if there is a lua file for this map, then it's a mission
    71             bool isMission = mapLuaFile.exists();
    75             bool isMission = mapLuaFile.exists();
    72             MapType type = isMission ? MissionMap : StaticMap;
    76             MapType type = isMission ? MissionMap : StaticMap;
    73 
    77 
    74             // If we're supposed to ignore this type, continue
    78             // if we're supposed to ignore this type, continue
    75             if (type != maptype) continue;
    79             if (type != maptype) continue;
    76 
    80 
    77             // load map info from file
    81             // load map info from file
    78             QTextStream input(&mapCfgFile);
    82             QTextStream input(&mapCfgFile);
    79             theme = input.readLine();
    83             theme = input.readLine();
    82                 scheme = input.readLine();
    86                 scheme = input.readLine();
    83                 weapons = input.readLine();
    87                 weapons = input.readLine();
    84             }
    88             }
    85             mapCfgFile.close();
    89             mapCfgFile.close();
    86 
    90 
    87             // Load description (if applicable)
    91             // load description (if applicable)
    88             if (isMission)
    92             if (isMission)
    89             {
    93             {
    90                 QString locale = HWApplication::keyboardInputLocale().name();
    94                 QString locale = HWApplication::keyboardInputLocale().name();
    91 
    95 
    92                 QSettings descSettings(QString("physfs://Maps/%1/desc.txt").arg(map), QSettings::IniFormat);
    96                 QSettings descSettings(QString("physfs://Maps/%1/desc.txt").arg(map), QSettings::IniFormat);
    93                 desc = descSettings.value(locale, QString()).toString().replace("|", "\n").replace("\\,", ",");
    97                 desc = descSettings.value(locale, QString()).toString().replace("|", "\n").replace("\\,", ",");
    94             }
    98             }
       
    99 
       
   100             // detect if map is dlc
       
   101             QString mapDir = PHYSFS_getRealDir(QString("Maps/%1/map.cfg").arg(map).toLocal8Bit().data());
       
   102             dlc = !mapDir.startsWith(appDir);
    95 
   103 
    96             // let's use some semi-sane hedgehog limit, rather than none
   104             // let's use some semi-sane hedgehog limit, rather than none
    97             if (limit == 0)
   105             if (limit == 0)
    98                 limit = 18;
   106                 limit = 18;
    99 
   107 
   115             // caption
   123             // caption
   116             caption = map;
   124             caption = map;
   117 
   125 
   118             // we know everything there is about the map, let's get am item for it
   126             // we know everything there is about the map, let's get am item for it
   119             QStandardItem * item = MapModel::infoToItem(
   127             QStandardItem * item = MapModel::infoToItem(
   120                 QIcon(), caption, type, map, theme, limit, scheme, weapons, desc);
   128                 QIcon(), caption, type, map, theme, limit, scheme, weapons, desc, dlc);
   121 
   129 
   122             // append item to the list
   130             // append item to the list
   123             mapList.append(item);
   131             mapList.append(item);
   124         }
   132         }
   125 
   133 
   168     QString name,
   176     QString name,
   169     QString theme,
   177     QString theme,
   170     quint32 limit,
   178     quint32 limit,
   171     QString scheme,
   179     QString scheme,
   172     QString weapons,
   180     QString weapons,
   173     QString desc)
   181     QString desc,
   174 {
   182     bool dlc)
   175     QStandardItem * item = new QStandardItem(icon, caption);
   183 {
       
   184     QStandardItem * item = new QStandardItem(icon, (dlc ? "*" : "") + caption);
   176     MapInfo mapInfo;
   185     MapInfo mapInfo;
   177     QVariant qvar(QVariant::UserType);
   186     QVariant qvar(QVariant::UserType);
   178 
   187 
   179     mapInfo.type = type;
   188     mapInfo.type = type;
   180     mapInfo.name = name;
   189     mapInfo.name = name;
   181     mapInfo.theme = theme;
   190     mapInfo.theme = theme;
   182     mapInfo.limit = limit;
   191     mapInfo.limit = limit;
   183     mapInfo.scheme = scheme;
   192     mapInfo.scheme = scheme;
   184     mapInfo.weapons = weapons;
   193     mapInfo.weapons = weapons;
   185     mapInfo.desc = desc.isEmpty() ? tr("No description available.") : desc;
   194     mapInfo.desc = desc.isEmpty() ? tr("No description available.") : desc;
       
   195     mapInfo.dlc = dlc;
   186 
   196 
   187     qvar.setValue(mapInfo);
   197     qvar.setValue(mapInfo);
   188     item->setData(qvar, Qt::UserRole + 1);
   198     item->setData(qvar, Qt::UserRole + 1);
   189 
   199 
   190     return item;
   200     return item;