QTfrontend/model/MapModel.cpp
author unc0rr
Mon, 22 Oct 2012 21:24:38 +0400
changeset 7789 838d2e06c377
parent 6983 ede55af89e78
child 8049 133e22b5c410
permissions -rw-r--r--
Check cake and air attack again if walked far from initial position (not tested)
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
6952
7f70f37bbf08 license header year range adjustments
sheepluva
parents: 6948
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
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
     4
 *
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
 * 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
     6
 * 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
     7
 * 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
     8
 *
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
 * 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
    10
 * 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
    11
 * 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
    12
 * 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
    13
 *
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
 * 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
    15
 * 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
    16
 * 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
    17
 */
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
 * @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
    21
 * @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
    22
 */
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    23
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    24
#include "MapModel.h"
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    25
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
void MapModel::loadMaps()
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    28
{
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    29
    // 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
    30
    beginResetModel();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    31
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    32
    // 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
    33
    DataManager & datamgr = DataManager::instance();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    34
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    35
    // fetch list of available maps
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    36
    QStringList maps =
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    37
        datamgr.entryList("Maps", QDir::AllDirs | QDir::NoDotAndDotDot);
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    38
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    39
    // 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
    40
    QStandardItemModel::clear();
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    41
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
    42
    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
    43
    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
    44
    QList<QStandardItem *> staticMaps;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    45
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    46
    // add generated/handdrawn maps to list
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    47
    // TODO: icons for these
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    48
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
    49
    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
    50
        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
    51
    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
    52
        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
    53
    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
    54
        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
    55
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    56
    // only 2 map relate files are relevant:
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    57
    // - the cfg file that contains the settings/info of the map
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    58
    // - 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
    59
    QFile mapLuaFile;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    60
    QFile mapCfgFile;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    61
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    62
    // add mission/static maps to lists
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    63
    foreach (QString map, maps)
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    64
    {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    65
        mapCfgFile.setFileName(
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    66
            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
    67
        mapLuaFile.setFileName(
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    68
            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
    69
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
        if (mapCfgFile.open(QFile::ReadOnly))
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    72
        {
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
    73
            QString caption;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    74
            QString theme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    75
            quint32 limit = 0;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    76
            QString scheme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    77
            QString weapons;
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    78
            // 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
    79
            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
    80
            MapType type = isMission?MissionMap:StaticMap;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    81
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    82
            // load map info from file
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    83
            QTextStream input(&mapCfgFile);
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    84
            input >> theme;
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    85
            input >> limit;
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    86
            if (isMission) { // scheme and weapons are only relevant for missions
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    87
                input >> scheme;
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    88
                input >> weapons;
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    89
            }
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
    90
            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
    91
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    92
            // 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
    93
            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
    94
                limit = 18;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    95
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    96
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    97
            // the default scheme/weaponset for missions.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    98
            // if empty we assume the map sets these internally -> locked
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    99
            if (isMission)
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   100
            {
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   101
                if (scheme.isEmpty())
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   102
                    scheme = "locked";
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   103
                else
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   104
                    scheme.replace("_", " ");
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   105
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   106
                if (weapons.isEmpty())
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   107
                    weapons = "locked";
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   108
                else
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   109
                    weapons.replace("_", " ");
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   110
            }
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   111
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   112
            // 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
   113
            if (isMission)
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   114
            {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   115
                // 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
   116
                caption = QComboBox::tr("Mission") + ": " + map;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   117
            }
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
   118
            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
   119
                caption = map;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   120
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   121
            // 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
   122
            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
   123
                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
   124
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   125
            // 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
   126
            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
   127
                missionMaps.append(item);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   128
            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
   129
                staticMaps.append(item);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   130
        
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
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
   135
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   136
    // 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
   137
    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
   138
    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
   139
    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
   140
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   141
    // create list:
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   142
    // 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
   143
    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
   144
    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
   145
    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
   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(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
   148
    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
   149
    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
   150
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   151
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   152
    // create row-index lookup table
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   153
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   154
    m_mapIndexes.clear();
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   155
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   156
    int count = items.size();
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   157
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   158
    for (int i = 0; i < count; i++)
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   159
    {
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   160
        QStandardItem * si = items.at(i);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   161
        QVariant v = si->data(Qt::UserRole + 1);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   162
        if (v.canConvert<MapInfo>())
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   163
            m_mapIndexes.insert(v.value<MapInfo>().name, i);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   164
    }
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   165
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   166
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
   167
    // store start-index and count of relevant types
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   168
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   169
    m_typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   170
    m_typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   171
    m_typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
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
   172
    // 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
   173
    int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   174
    count = missionMaps.size();
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   175
    m_typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
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
   176
    // 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
   177
    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
   178
    count = staticMaps.size();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   179
    m_typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
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
   180
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   181
    // 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
   182
    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
   183
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   184
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   185
    endResetModel();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   186
}
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   187
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   188
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
   189
int MapModel::randomMap(MapType type) const
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   190
{
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
   191
    // return a random index for this type or -1 if none available
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   192
    QPair<int,int> loc = m_typeLoc.value(type, QPair<int,int>(-1,0));
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
   193
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
   194
    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
   195
    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
   196
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
   197
    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
   198
        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
   199
    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
   200
        return startIdx + (rand() % count);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   201
}
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
   202
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
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
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
   205
    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
   206
    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
   207
    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
   208
    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
   209
    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
   210
    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
   211
    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
   212
    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
   213
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
   214
{
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
    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
   216
    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
   217
    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
   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
    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
   220
    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
   221
    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
   222
    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
   223
    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
   224
    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
   225
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
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
   227
    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
   228
    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
   229
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
   230
    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
   231
}
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   232
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   233
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   234
int MapModel::indexOf(const QString & map) const
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   235
{
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   236
    return m_mapIndexes.value(map, -1);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   237
}
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
   238