QTfrontend/model/MapModel.cpp
changeset 6939 970389573788
parent 6938 217ed62e872c
child 6943 1fe601a2761b
equal deleted inserted replaced
6938:217ed62e872c 6939:970389573788
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2012 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 /**
       
    21  * @file
       
    22  * @brief MapModel class implementation
       
    23  */
     1 
    24 
     2 #include "MapModel.h"
    25 #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 
    26 
    65 
    27 
    66 void MapModel::loadMaps()
    28 void MapModel::loadMaps()
    67 {
    29 {
    68     beginResetModel();
    30     beginResetModel();
    71     DataManager & datamgr = DataManager::instance();
    33     DataManager & datamgr = DataManager::instance();
    72 
    34 
    73     QStringList maps =
    35     QStringList maps =
    74         datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
    36         datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
    75 
    37 
    76     m_data.clear();
    38     QStandardItemModel::clear();
    77 
    39 
    78 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
    40     QList<QStandardItem *> genMaps;
    79     m_data.reserve(maps.size());
    41     QList<QStandardItem *> missionMaps;
    80 #endif
    42     QList<QStandardItem *> staticMaps;
    81 
       
    82     QMap<int, QVariant> tmp;
       
    83     QList<QVariant> mapInfo;
       
    84 
    43 
    85     // TODO: icons for these
    44     // 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 
    45 
    99     m_nGenerators = 3;
    46     genMaps.append(
       
    47         infoToItem(QIcon(), QComboBox::tr("generated map..."), GeneratedMap, "+rnd+"));
       
    48     genMaps.append(
       
    49         infoToItem(QIcon(), QComboBox::tr("generated maze..."), GeneratedMaze, "+maze+"));
       
    50     genMaps.append(
       
    51         infoToItem(QIcon(), QComboBox::tr("hand drawn map..."), HandDrawnMap, "+drawn+"));
   100 
    52 
   101 
       
   102     m_nMissions = 0;
       
   103 
    53 
   104     QFile mapLuaFile;
    54     QFile mapLuaFile;
   105     QFile mapCfgFile;
    55     QFile mapCfgFile;
   106 
    56 
   107     foreach (QString map, maps)
    57     foreach (QString map, maps)
   109         mapCfgFile.setFileName(
    59         mapCfgFile.setFileName(
   110             datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
    60             datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
   111         mapLuaFile.setFileName(
    61         mapLuaFile.setFileName(
   112             datamgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
    62             datamgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
   113 
    63 
   114         QMap<int, QVariant> dataset;
       
   115 
       
   116 
    64 
   117         if (mapCfgFile.open(QFile::ReadOnly))
    65         if (mapCfgFile.open(QFile::ReadOnly))
   118         {
    66         {
       
    67             QString caption;
   119             QString theme;
    68             QString theme;
   120             quint32 limit = 0;
    69             quint32 limit = 0;
   121             QString scheme;
    70             QString scheme;
   122             QString weapons;
    71             QString weapons;
   123             QList<QVariant> mapInfo;
       
   124             bool isMission = mapLuaFile.exists();
    72             bool isMission = mapLuaFile.exists();
   125             int type = isMission?MissionMap:StaticMap;
    73             MapType type = isMission?MissionMap:StaticMap;
   126 
    74 
   127             QTextStream input(&mapCfgFile);
    75             QTextStream input(&mapCfgFile);
   128             input >> theme;
    76             input >> theme;
   129             input >> limit;
    77             input >> limit;
   130             input >> scheme;
    78             input >> scheme;
   131             input >> weapons;
    79             input >> weapons;
   132             mapInfo.push_back(type);
    80             mapCfgFile.close();
   133             mapInfo.push_back(map);
    81 
   134             mapInfo.push_back(theme);
    82             if (limit == 0)
   135             if (limit)
    83                 limit = 18;
   136                 mapInfo.push_back(limit);
       
   137             else
       
   138                 mapInfo.push_back(18);
       
   139 
    84 
   140 
    85 
   141             if (scheme.isEmpty())
    86             if (scheme.isEmpty())
   142                 scheme = "locked";
    87                 scheme = "locked";
   143             scheme.replace("_", " ");
    88             else
       
    89                 scheme.replace("_", " ");
   144 
    90 
   145             if (weapons.isEmpty())
    91             if (weapons.isEmpty())
   146                 weapons = "locked";
    92                 weapons = "locked";
   147             weapons.replace("_", " ");
    93             else
       
    94                 weapons.replace("_", " ");
   148 
    95 
   149             mapInfo.push_back(scheme);
    96             if (isMission)
   150             mapInfo.push_back(weapons);
       
   151 
       
   152             if(isMission)
       
   153             {
    97             {
   154                 // TODO: icon
    98                 // TODO: icon
   155                 map = QComboBox::tr("Mission") + ": " + map;
    99                 caption = QComboBox::tr("Mission") + ": " + map;
   156                 m_nMissions++;
   100                 m_nMissions++;
   157             }
   101             }
       
   102             else
       
   103                 caption = map;
   158 
   104 
   159             mapCfgFile.close();
   105             QStandardItem * item = infoToItem(
       
   106                 QIcon(), caption, type, map, theme, limit, scheme, weapons);
   160 
   107 
   161             // set name
   108             if (isMission)
   162             dataset.insert(Qt::DisplayRole, map);
   109                 missionMaps.append(item);
   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
   110             else
   173                 m_data.append(dataset);
   111                 staticMaps.append(item);
   174         
   112         
   175         }
   113         }
   176 
   114 
   177     }
   115     }
   178 
   116 
       
   117     m_nMissions = missionMaps.size();
       
   118 
       
   119     QStandardItem separator("---");
       
   120     separator.setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
       
   121     separator.setFlags(separator.flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
       
   122 
       
   123     QList<QStandardItem * > items;
       
   124     items.append(genMaps);
       
   125     items.append(separator.clone());
       
   126     items.append(separator.clone());
       
   127     items.append(missionMaps);
       
   128     items.append(separator.clone());
       
   129     items.append(staticMaps);
       
   130 
       
   131     QStandardItemModel::appendColumn(items);
       
   132 
   179     endResetModel();
   133     endResetModel();
   180 }
   134 }
   181 
   135 
   182 int MapModel::generatorCount() const
       
   183 {
       
   184     return m_nGenerators;
       
   185 }
       
   186 
   136 
   187 int MapModel::missionCount() const
   137 int MapModel::missionCount() const
   188 {
   138 {
   189     return m_nMissions;
   139     return m_nMissions;
   190 }
   140 }
       
   141 
       
   142 
       
   143 QStandardItem * MapModel::infoToItem(
       
   144     const QIcon & icon,
       
   145     const QString caption,
       
   146     MapType type,
       
   147     QString name,
       
   148     QString theme,
       
   149     quint32 limit,
       
   150     QString scheme,
       
   151     QString weapons)
       
   152 const
       
   153 {
       
   154     QStandardItem * item = new QStandardItem(icon, caption);
       
   155     MapInfo mapInfo;
       
   156     QVariant qvar(QVariant::UserType);
       
   157 
       
   158     mapInfo.type = type;
       
   159     mapInfo.name = name;
       
   160     mapInfo.theme = theme;
       
   161     mapInfo.limit = limit;
       
   162     mapInfo.scheme = scheme;
       
   163     mapInfo.weapons = weapons;
       
   164 
       
   165 
       
   166     qvar.setValue(mapInfo);
       
   167     item->setData(qvar, Qt::UserRole + 1);
       
   168 
       
   169     if (mapInfo.type == Invalid)
       
   170             Q_ASSERT(false);
       
   171 
       
   172     return item;
       
   173 }