QTfrontend/model/MapModel.cpp
changeset 6938 217ed62e872c
child 6939 970389573788
equal deleted inserted replaced
6937:7f77fa908a4e 6938:217ed62e872c
       
     1 
       
     2 #include "MapModel.h"
       
     3 
       
     4 MapModel::MapInfo MapModel::mapInfoFromData(const QVariant data)
       
     5 {
       
     6     MapInfo mapInfo;
       
     7 
       
     8     mapInfo.type = Invalid;
       
     9     mapInfo.name = "";
       
    10     mapInfo.theme = "";
       
    11     mapInfo.limit = 0;
       
    12     mapInfo.scheme = "";
       
    13     mapInfo.weapons = "";
       
    14 
       
    15     if (data.isValid())
       
    16     {
       
    17         QList<QVariant> list = data.toList();
       
    18         if (list.size() < 1) {
       
    19             mapInfo.type = Invalid;
       
    20             return mapInfo;
       
    21         }
       
    22         mapInfo.type = (MapType)list[0].toInt();
       
    23         switch (mapInfo.type)
       
    24         {
       
    25             case GeneratedMap:
       
    26             case GeneratedMaze:
       
    27             case HandDrawnMap:
       
    28                 return mapInfo;
       
    29 
       
    30             default:
       
    31                 mapInfo.name = list[1].toString();
       
    32                 mapInfo.theme = list[2].toString();
       
    33                 mapInfo.limit = list[3].toInt();
       
    34                 mapInfo.scheme = list[4].toString();
       
    35                 mapInfo.weapons = list[5].toString();
       
    36         }
       
    37     }
       
    38 
       
    39     return mapInfo;
       
    40 }
       
    41 
       
    42 MapModel::MapModel(QObject *parent) :
       
    43     QAbstractListModel(parent)
       
    44 {
       
    45     m_data = QList<QMap<int, QVariant> >();
       
    46 }
       
    47 
       
    48 int MapModel::rowCount(const QModelIndex &parent) const
       
    49 {
       
    50     if(parent.isValid())
       
    51         return 0;
       
    52     else
       
    53         return m_data.size();
       
    54 }
       
    55 
       
    56 
       
    57 QVariant MapModel::data(const QModelIndex &index, int role) const
       
    58 {
       
    59     if(index.column() > 0 || index.row() >= m_data.size())
       
    60         return QVariant();
       
    61     else
       
    62         return m_data.at(index.row()).value(role, QVariant());
       
    63 }
       
    64 
       
    65 
       
    66 void MapModel::loadMaps()
       
    67 {
       
    68     beginResetModel();
       
    69 
       
    70 
       
    71     DataManager & datamgr = DataManager::instance();
       
    72 
       
    73     QStringList maps =
       
    74         datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
       
    75 
       
    76     m_data.clear();
       
    77 
       
    78 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
       
    79     m_data.reserve(maps.size());
       
    80 #endif
       
    81 
       
    82     QMap<int, QVariant> tmp;
       
    83     QList<QVariant> mapInfo;
       
    84 
       
    85     // TODO: icons for these
       
    86     tmp.insert(Qt::DisplayRole, QComboBox::tr("generated map..."));
       
    87     mapInfo.append(GeneratedMap);
       
    88     tmp.insert(Qt::UserRole, mapInfo);
       
    89     m_data.append(tmp);
       
    90     tmp.insert(Qt::DisplayRole, QComboBox::tr("generated maze..."));
       
    91     mapInfo.replace(0, GeneratedMaze);
       
    92     tmp.insert(Qt::UserRole, mapInfo);
       
    93     m_data.append(tmp);
       
    94     tmp.insert(Qt::DisplayRole, QComboBox::tr("hand drawn map..."));
       
    95     mapInfo.replace(0, HandDrawnMap);
       
    96     tmp.insert(Qt::UserRole, mapInfo);
       
    97     m_data.append(tmp);
       
    98 
       
    99     m_nGenerators = 3;
       
   100 
       
   101 
       
   102     m_nMissions = 0;
       
   103 
       
   104     QFile mapLuaFile;
       
   105     QFile mapCfgFile;
       
   106 
       
   107     foreach (QString map, maps)
       
   108     {
       
   109         mapCfgFile.setFileName(
       
   110             datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
       
   111         mapLuaFile.setFileName(
       
   112             datamgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
       
   113 
       
   114         QMap<int, QVariant> dataset;
       
   115 
       
   116 
       
   117         if (mapCfgFile.open(QFile::ReadOnly))
       
   118         {
       
   119             QString theme;
       
   120             quint32 limit = 0;
       
   121             QString scheme;
       
   122             QString weapons;
       
   123             QList<QVariant> mapInfo;
       
   124             bool isMission = mapLuaFile.exists();
       
   125             int type = isMission?MissionMap:StaticMap;
       
   126 
       
   127             QTextStream input(&mapCfgFile);
       
   128             input >> theme;
       
   129             input >> limit;
       
   130             input >> scheme;
       
   131             input >> weapons;
       
   132             mapInfo.push_back(type);
       
   133             mapInfo.push_back(map);
       
   134             mapInfo.push_back(theme);
       
   135             if (limit)
       
   136                 mapInfo.push_back(limit);
       
   137             else
       
   138                 mapInfo.push_back(18);
       
   139 
       
   140 
       
   141             if (scheme.isEmpty())
       
   142                 scheme = "locked";
       
   143             scheme.replace("_", " ");
       
   144 
       
   145             if (weapons.isEmpty())
       
   146                 weapons = "locked";
       
   147             weapons.replace("_", " ");
       
   148 
       
   149             mapInfo.push_back(scheme);
       
   150             mapInfo.push_back(weapons);
       
   151 
       
   152             if(isMission)
       
   153             {
       
   154                 // TODO: icon
       
   155                 map = QComboBox::tr("Mission") + ": " + map;
       
   156                 m_nMissions++;
       
   157             }
       
   158 
       
   159             mapCfgFile.close();
       
   160 
       
   161             // set name
       
   162             dataset.insert(Qt::DisplayRole, map);
       
   163 
       
   164             // TODO
       
   165             // dataset.insert(Qt::DecorationRole, icon);
       
   166 
       
   167             // set mapinfo
       
   168             dataset.insert(Qt::UserRole, mapInfo);
       
   169 
       
   170             if (isMission) // insert missions before regular maps
       
   171                 m_data.insert(m_nGenerators + m_nMissions, dataset);
       
   172             else
       
   173                 m_data.append(dataset);
       
   174         
       
   175         }
       
   176 
       
   177     }
       
   178 
       
   179     endResetModel();
       
   180 }
       
   181 
       
   182 int MapModel::generatorCount() const
       
   183 {
       
   184     return m_nGenerators;
       
   185 }
       
   186 
       
   187 int MapModel::missionCount() const
       
   188 {
       
   189     return m_nMissions;
       
   190 }