QTfrontend/model/MapModel.h
author unc0rr
Sun, 02 Dec 2012 01:25:11 +0400
changeset 8178 8bd087478b48
parent 6983 ede55af89e78
child 8377 869f80966a77
permissions -rw-r--r--
Fix QSettings problems: - Reopen file in ReadOnly mode if it was open in ReadWrite mode and is being read. This is needed for stupid QSettings which opens file in ReadWrite mode just to call readAll() on it. - Implement setSize(0)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     1
/*
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
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>
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     4
 *
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     8
 *
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    12
 * GNU General Public License for more details.
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    13
 *
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    17
 */
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    18
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    19
/**
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    20
 * @file
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    21
 * @brief MapModel class definition
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    22
 */
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
#ifndef HEDGEWARS_MAPMODEL_H
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    25
#define HEDGEWARS_MAPMODEL_H
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    26
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
    27
#include <QStandardItemModel>
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    28
#include <QStringList>
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    29
#include <QTextStream>
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    30
#include <QHash>
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    31
#include <QMap>
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    32
#include <QIcon>
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    33
#include <QComboBox>
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    34
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    35
#include "DataManager.h"
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    36
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    37
/**
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
    38
 * @brief A model that vertically lists available maps
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    39
 *
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    40
 * @author sheepluva
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    41
 * @since 0.9.18
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
class MapModel : public QStandardItemModel
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    44
{
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    45
        Q_OBJECT
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    46
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    47
    public:
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    48
        enum MapType {
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    49
            Invalid,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    50
            GeneratedMap,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    51
            GeneratedMaze,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    52
            HandDrawnMap,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    53
            MissionMap,
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    54
            StaticMap
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    55
        };
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
        /// a struct for holding the attributes of a map.
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    58
        struct MapInfo
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    59
        {
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    60
            MapType type; ///< The map-type
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    61
            QString name; ///< The internal name.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    62
            QString theme; ///< The theme to be used. (can be empty)
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    63
            quint32 limit; ///< The maximum allowed number of hedgehogs.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    64
            QString scheme; ///< Default scheme name or "locked", for mission-maps.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    65
            QString weapons; ///< Default weaponset name or "locked", for missions-maps.
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    66
        };
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    67
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    68
        /**
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    69
         * @brief Returns the row-index of the given map.
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    70
         * @param map map of which to get the row-index of.
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    71
         * @return row-index of map or -1 if not available.
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    72
         */
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    73
        int indexOf(const QString & map) const;
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
    74
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
    75
        /**
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
    76
         * @brief Returns the row-index of a random map with a specified 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
    77
         * @param type desired type of map.
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
    78
         * @return row-index of a map with the desired type, -1 if none found.
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
    79
         */
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
    80
        int randomMap(MapType type) const;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    81
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    82
    public slots:
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6947
diff changeset
    83
        /// Reloads the maps using the DataManager.
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    84
        void loadMaps();
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    85
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    86
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
    87
    private:
6948
7271ce89950f various comment tweaks/fixes
sheepluva
parents: 6947
diff changeset
    88
        /// start-index and map count for each map-type.
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    89
        QMap<MapType, QPair<int,int> > m_typeLoc;
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    90
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    91
        /// map index lookup table
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6952
diff changeset
    92
        QHash<QString, int> m_mapIndexes;
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
6943
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    94
        /**
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    95
         * @brief Creates a QStandardItem, that holds the map info and item appearance.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    96
         * The used role for the data is Qt::UserRole + 1.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    97
         * @param icon the icon to be displayed (can be an empty QIcon()).
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    98
         * @param caption the text to be displayed.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
    99
         * @param type the type of the map.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   100
         * @param name the internal name of the map.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   101
         * @param theme the theme of the map (or empty if none).
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   102
         * @param limit the hedgehog limit of the map.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   103
         * @param scheme mission map: default scheme name or "locked".
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   104
         * @param weapons mission map: default weaponset name or "locked".
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   105
         * @return pointer to item representing the map info: at Qt::UserRole + 1.
1fe601a2761b MapModel: comments/docs, small tweaks
sheepluva
parents: 6939
diff changeset
   106
         */
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
   107
        QStandardItem * 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
   108
            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
   109
            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
   110
            MapType 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
   111
            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
   112
            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
   113
            quint32 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
   114
            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
   115
            QString weapons = "") const;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   116
};
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
Q_DECLARE_METATYPE(MapModel::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
   119
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
diff changeset
   120
#endif // HEDGEWARS_MAPMODEL_H