QTfrontend/model/MapModel.cpp
author sheepluva
Sat, 28 Apr 2012 11:56:09 +0200
changeset 6947 1be3e48e1d53
parent 6943 1fe601a2761b
child 6948 7271ce89950f
permissions -rw-r--r--
MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     1
/*
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     2
 * Hedgewars, a free turn based strategy game
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     3
 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     4
 * Copyright (c) 2007-2012 Andrey Korotaev <unC0Rr@gmail.com>
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     5
 *
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
     9
 *
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    13
 * GNU General Public License for more details.
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    14
 *
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    15
 * You should have received a copy of the GNU General Public License
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    16
 * along with this program; if not, write to the Free Software
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    18
 */
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    19
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    20
/**
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    21
 * @file
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    22
 * @brief MapModel class implementation
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    23
 */
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    24
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    25
#include "MapModel.h"
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    26
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    27
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    28
void MapModel::loadMaps()
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    29
{
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    30
    // this method resets the contents of this model (important to know for views).
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    31
    beginResetModel();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    32
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    33
    // we'll need the DataManager a few times, so let's get a reference to it
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    34
    DataManager & datamgr = DataManager::instance();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    35
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    36
    // fetch list of available maps
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    37
    QStringList maps =
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    38
        datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    39
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    40
    // empty list, so that we can (re)fill it
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    41
    QStandardItemModel::clear();
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    42
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    43
    QList<QStandardItem *> genMaps;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    44
    QList<QStandardItem *> missionMaps;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    45
    QList<QStandardItem *> staticMaps;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    46
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    47
    // add generated/handdrawn maps to list
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    48
    // TODO: icons for these
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    49
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    50
    genMaps.append(
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    51
        infoToItem(QIcon(), QComboBox::tr("generated map..."), GeneratedMap, "+rnd+"));
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    52
    genMaps.append(
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    53
        infoToItem(QIcon(), QComboBox::tr("generated maze..."), GeneratedMaze, "+maze+"));
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    54
    genMaps.append(
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    55
        infoToItem(QIcon(), QComboBox::tr("hand drawn map..."), HandDrawnMap, "+drawn+"));
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    56
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    57
    // only 2 map relate files are relevant:
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    58
    // - the cfg file that contains the settings/info of the map
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    59
    // - the lua file - if it exists it's a mission, otherwise it isn't
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    60
    QFile mapLuaFile;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    61
    QFile mapCfgFile;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    62
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    63
    // add mission/static maps to lists
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    64
    foreach (QString map, maps)
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    65
    {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    66
        mapCfgFile.setFileName(
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    67
            datamgr.findFileForRead(QString("Maps/%1/map.cfg").arg(map)));
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    68
        mapLuaFile.setFileName(
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    69
            datamgr.findFileForRead(QString("Maps/%1/map.lua").arg(map)));
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    70
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    71
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    72
        if (mapCfgFile.open(QFile::ReadOnly))
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    73
        {
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    74
            QString caption;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    75
            QString theme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    76
            quint32 limit = 0;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    77
            QString scheme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    78
            QString weapons;
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    79
            // if there is a lua file for this map, then it's a mission
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    80
            bool isMission = mapLuaFile.exists();
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    81
            MapType type = isMission?MissionMap:StaticMap;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    82
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    83
            // load map info from file
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    84
            QTextStream input(&mapCfgFile);
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    85
            input >> theme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    86
            input >> limit;
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    87
            if (isMission) { // scheme and weapons are only relevant for missions
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    88
                input >> scheme;
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    89
                input >> weapons;
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    90
            }
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    91
            mapCfgFile.close();
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    92
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    93
            // let's use some semi-sane hedgehog limit, rather than none
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    94
            if (limit == 0)
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
    95
                limit = 18;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    96
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    97
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    98
            // the default scheme/weaponset for missions.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    99
            // if empty we assume the map sets these internally -> locked
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   100
            if (isMission)
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   101
            {
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   102
                if (scheme.isEmpty())
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   103
                    scheme = "locked";
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   104
                else
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   105
                    scheme.replace("_", " ");
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   106
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   107
                if (weapons.isEmpty())
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   108
                    weapons = "locked";
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   109
                else
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   110
                    weapons.replace("_", " ");
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   111
            }
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   112
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   113
            // add a mission caption prefix to missions
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   114
            if (isMission)
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   115
            {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   116
                // TODO: icon
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   117
                caption = QComboBox::tr("Mission") + ": " + map;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   118
            }
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   119
            else
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   120
                caption = map;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   121
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   122
            // we know everything there is about the map, let's get am item for it
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   123
            QStandardItem * item = infoToItem(
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   124
                QIcon(), caption, type, map, theme, limit, scheme, weapons);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   125
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   126
            // append item to the list
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   127
            if (isMission)
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   128
                missionMaps.append(item);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   129
            else
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   130
                staticMaps.append(item);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   131
        
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   132
        }
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   133
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   134
    }
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   135
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   136
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   137
    // define a separator item
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   138
    QStandardItem separator("---");
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   139
    separator.setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   140
    separator.setFlags(separator.flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   141
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   142
    // create list:
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   143
    // generated+handdrawn maps, 2 saperators, missions, 1 separator, static maps
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   144
    QList<QStandardItem * > items;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   145
    items.append(genMaps);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   146
    items.append(separator.clone());
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   147
    items.append(separator.clone());
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   148
    items.append(missionMaps);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   149
    items.append(separator.clone());
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   150
    items.append(staticMaps);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   151
6947
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   152
    // store start-index and count of relevant types
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   153
    typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   154
    typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   155
    typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   156
    // mission maps
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   157
    int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   158
    int count = missionMaps.size();
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   159
    typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   160
    // static maps
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   161
    startIdx += count + 1; // start after missions and 2 separators
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   162
    count = staticMaps.size();
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   163
    typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   164
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   165
    // store list contents in the item model
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   166
    QStandardItemModel::appendColumn(items);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   167
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   168
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   169
    endResetModel();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   170
}
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   171
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   172
6947
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   173
int MapModel::mapCount(MapType type) const
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   174
{
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   175
    // return the count for this type
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   176
    // fetch it from the second int in typeLoc, return 0 if no entry
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   177
    return typeLoc.value(type, QPair<int,int>(0,0)).second;
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   178
}
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   179
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   180
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   181
int MapModel::randomMap(MapType type) const
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   182
{
6947
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   183
    // return a random index for this type or -1 if none available
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   184
    QPair<int,int> loc = typeLoc.value(type, QPair<int,int>(-1,0));
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   185
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   186
    int startIdx = loc.first;
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   187
    int count = loc.second;
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   188
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   189
    if (count < 1)
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   190
        return -1;
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   191
    else
1be3e48e1d53 MapModel: add getters for random maps based on type, so that the mapContainer does not have to magically know how maps and separators are ordered within the model
sheepluva
parents: 6943
diff changeset
   192
        return startIdx + (rand() % count);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   193
}
6939
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   194
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   195
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   196
QStandardItem * MapModel::infoToItem(
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   197
    const QIcon & icon,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   198
    const QString caption,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   199
    MapType type,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   200
    QString name,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   201
    QString theme,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   202
    quint32 limit,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   203
    QString scheme,
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   204
    QString weapons)
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   205
const
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   206
{
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   207
    QStandardItem * item = new QStandardItem(icon, caption);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   208
    MapInfo mapInfo;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   209
    QVariant qvar(QVariant::UserType);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   210
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   211
    mapInfo.type = type;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   212
    mapInfo.name = name;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   213
    mapInfo.theme = theme;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   214
    mapInfo.limit = limit;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   215
    mapInfo.scheme = scheme;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   216
    mapInfo.weapons = weapons;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   217
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   218
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   219
    qvar.setValue(mapInfo);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   220
    item->setData(qvar, Qt::UserRole + 1);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   221
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   222
    if (mapInfo.type == Invalid)
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   223
            Q_ASSERT(false);
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   224
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   225
    return item;
970389573788 MapModel converted to QStandardItemList; separators fixed; changed used data format (human-readable struct instead of magic list) for map info in HwMapContainer (+ various small cleanups in that class)
sheepluva
parents: 6938
diff changeset
   226
}