QTfrontend/util/DataManager.cpp
author unc0rr
Sat, 15 Sep 2018 20:42:02 +0200
branch0.9.24
changeset 13813 c384117f90eb
parent 13284 b504131f7405
child 14553 e1ae6d8e84b0
permissions -rw-r--r--
Fix SetAmmoTexts not working in stats-only mode
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     1
/*
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10248
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     4
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     8
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    12
 * GNU General Public License for more details.
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    13
 *
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    17
 */
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    18
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    19
/**
6170
2b1748161278 fix multiplayer page layout, doc/comment fixes
sheepluva
parents: 6168
diff changeset
    20
 * @file
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    21
 * @brief DataManager class implementation
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    22
 */
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6167
diff changeset
    23
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    24
#include <QMap>
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    25
#include <QStringList>
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    26
#include <QStandardItemModel>
6199
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
    27
#include <QFileInfo>
8918
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
    28
#include <QSettings>
8920
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
    29
#include <QColor>
6199
582e85254110 on file save: create parent directories if needed
sheepluva
parents: 6196
diff changeset
    30
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    31
#include "hwconsts.h"
7260
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
    32
#include "HWApplication.h"
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
    33
#include "sdlkeys.h"
12295
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    34
#include "physfs.h"
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    35
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    36
#include "DataManager.h"
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    37
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    38
#include "GameStyleModel.h"
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    39
#include "HatModel.h"
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    40
#include "MapModel.h"
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    41
#include "ThemeModel.h"
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    42
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    43
DataManager::DataManager()
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    44
{
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    45
    m_hatModel = NULL;
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
    46
    m_staticMapModel = NULL;
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
    47
    m_missionMapModel = NULL;
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
    48
    m_themeModel = NULL;
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
    49
    m_colorsModel = NULL;
7260
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
    50
    m_bindsModel = NULL;
10248
7b9b44a051f8 Fix some of issues found by coverity
unc0rr
parents: 10108
diff changeset
    51
    m_gameStyleModel = NULL;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    52
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    53
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    54
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    55
DataManager & DataManager::instance()
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    56
{
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    57
    static DataManager instance;
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    58
    return instance;
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    59
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    60
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    61
6930
d187ea93fc4f renaming HWDataManager -> DataManager
sheepluva
parents: 6700
diff changeset
    62
QStringList DataManager::entryList(
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    63
    const QString & subDirectory,
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    64
    QDir::Filters filters,
12295
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    65
    const QStringList & nameFilters,
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    66
    bool withDLC
6616
f77bb02b669f astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents: 6199
diff changeset
    67
) const
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    68
{
7774
27f9b9362f47 Simplify DataManager as physfs partially implements its functions
unc0rr
parents: 7681
diff changeset
    69
    QDir tmpDir(QString("physfs://%1").arg(subDirectory));
27f9b9362f47 Simplify DataManager as physfs partially implements its functions
unc0rr
parents: 7681
diff changeset
    70
    QStringList result = tmpDir.entryList(nameFilters, filters);
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    71
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    72
    // sort case-insensitive
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    73
    QMap<QString, QString> sortedFileNames;
12295
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    74
    QString absolutePath = datadir->absolutePath().toLocal8Bit().data();
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    75
    foreach ( QString fn, result)
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    76
    {
12295
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    77
        // Filter out DLC entries if desired
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    78
        QString realDir = PHYSFS_getRealDir(QString(subDirectory + "/" + fn).toLocal8Bit().data());
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    79
        if(withDLC || realDir == absolutePath)
ac57d564efce Add DLC filtering in DataManager.cpp and namegen.cpp
Wuzzy <almikes@aol.com>
parents: 11046
diff changeset
    80
            sortedFileNames.insert(fn.toLower(), fn);
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    81
    }
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    82
    result = sortedFileNames.values();
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6159
diff changeset
    83
6159
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    84
    return result;
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    85
}
c780b8cf4d75 introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff changeset
    86
6958
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    87
GameStyleModel * DataManager::gameStyleModel()
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    88
{
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    89
    if (m_gameStyleModel == NULL) {
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    90
        m_gameStyleModel = new GameStyleModel();
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    91
        m_gameStyleModel->loadGameStyles();
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    92
    }
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    93
    return m_gameStyleModel;
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    94
}
8230a516ba93 hello GameStyleModel
sheepluva
parents: 6953
diff changeset
    95
6953
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    96
HatModel * DataManager::hatModel()
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    97
{
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    98
    if (m_hatModel == NULL) {
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
    99
        m_hatModel = new HatModel();
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   100
        m_hatModel->loadHats();
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   101
    }
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   102
    return m_hatModel;
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   103
}
4c2dd25630a7 * make HatModel update automatically (also renamed class and files)
sheepluva
parents: 6952
diff changeset
   104
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   105
MapModel * DataManager::staticMapModel()
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   106
{
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   107
    if (m_staticMapModel == NULL) {
9744
1ee4842a9c86 Be lazy in loading maps list
unc0rr
parents: 9389
diff changeset
   108
        m_staticMapModel = new MapModel(MapModel::StaticMap, this);
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   109
    }
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   110
    return m_staticMapModel;
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   111
}
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   112
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   113
MapModel * DataManager::missionMapModel()
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   114
{
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   115
    if (m_missionMapModel == NULL) {
9744
1ee4842a9c86 Be lazy in loading maps list
unc0rr
parents: 9389
diff changeset
   116
        m_missionMapModel = new MapModel(MapModel::MissionMap, this);
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   117
    }
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 8346
diff changeset
   118
    return m_missionMapModel;
6938
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   119
}
217ed62e872c MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents: 6937
diff changeset
   120
6937
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   121
ThemeModel * DataManager::themeModel()
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   122
{
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   123
    if (m_themeModel == NULL) {
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   124
        m_themeModel = new ThemeModel();
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   125
    }
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   126
    return m_themeModel;
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   127
}
7f77fa908a4e messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents: 6931
diff changeset
   128
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   129
QStandardItemModel * DataManager::colorsModel()
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   130
{
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   131
    if(m_colorsModel == NULL)
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   132
    {
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   133
        m_colorsModel = new QStandardItemModel();
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   134
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   135
        int i = 0;
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   136
        while(colors[i])
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   137
        {
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   138
            QStandardItem * item = new QStandardItem();
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   139
            item->setData(QColor(colors[i]));
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   140
            m_colorsModel->appendRow(item);
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   141
            ++i;
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   142
        }
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   143
    }
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   144
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   145
    return m_colorsModel;
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   146
}
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6959
diff changeset
   147
7260
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   148
QStandardItemModel * DataManager::bindsModel()
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   149
{
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   150
    if(m_bindsModel == NULL)
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   151
    {
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   152
        m_bindsModel = new QStandardItemModel();
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   153
8346
3443e0de2c9d GCI2012: Advanced Keyboard Configuration
dag10
parents: 8049
diff changeset
   154
        QStandardItem * firstItem = new QStandardItem();
3443e0de2c9d GCI2012: Advanced Keyboard Configuration
dag10
parents: 8049
diff changeset
   155
        firstItem->setData(tr("Use Default"), Qt::DisplayRole);
3443e0de2c9d GCI2012: Advanced Keyboard Configuration
dag10
parents: 8049
diff changeset
   156
        firstItem->setData("default", Qt::UserRole + 1);
3443e0de2c9d GCI2012: Advanced Keyboard Configuration
dag10
parents: 8049
diff changeset
   157
        m_bindsModel->appendRow(firstItem);
3443e0de2c9d GCI2012: Advanced Keyboard Configuration
dag10
parents: 8049
diff changeset
   158
7260
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   159
        for(int j = 0; sdlkeys[j][1][0] != '\0'; j++)
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   160
        {
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   161
            QStandardItem * item = new QStandardItem();
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   162
            item->setData(HWApplication::translate("binds (keys)", sdlkeys[j][1]).contains(": ") ? HWApplication::translate("binds (keys)", sdlkeys[j][1]) : HWApplication::translate("binds (keys)", "Keyboard") + QString(": ") + HWApplication::translate("binds (keys)", sdlkeys[j][1]), Qt::DisplayRole);
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   163
            item->setData(sdlkeys[j][0], Qt::UserRole + 1);
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   164
            m_bindsModel->appendRow(item);
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   165
        }
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   166
    }
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   167
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   168
    return m_bindsModel;
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   169
}
c3584a3ab730 BindsModel
unc0rr
parents: 7258
diff changeset
   170
8918
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   171
QString DataManager::settingsFileName()
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   172
{
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   173
    if(m_settingsFileName.isEmpty())
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   174
    {
13284
b504131f7405 Fix team files and settings not saving properly, and remove FileEngine stuff from DLC
Wuzzy <Wuzzy2@mail.ru>
parents: 12295
diff changeset
   175
        QFile settingsFile(cfgdir->absoluteFilePath("settings.ini"));
8918
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   176
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   177
        if(!settingsFile.exists())
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   178
        {
13284
b504131f7405 Fix team files and settings not saving properly, and remove FileEngine stuff from DLC
Wuzzy <Wuzzy2@mail.ru>
parents: 12295
diff changeset
   179
            QFile oldSettingsFile(cfgdir->absoluteFilePath("hedgewars.ini"));
8918
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   180
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   181
            settingsFile.open(QFile::WriteOnly);
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   182
            settingsFile.close();
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   183
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   184
            if(oldSettingsFile.exists())
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   185
            {
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   186
                QSettings sOld(oldSettingsFile.fileName(), QSettings::IniFormat);
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   187
                QSettings sNew(settingsFile.fileName(), QSettings::IniFormat);
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   188
                sNew.setIniCodec("UTF-8");
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   189
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   190
                foreach(const QString & key, sOld.allKeys())
8920
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   191
                {
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   192
                    if(key.startsWith("colors/color"))
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   193
                        sNew.setValue(key, sOld.value(key).value<QColor>().name());
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   194
                    else
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   195
                        sNew.setValue(key, sOld.value(key));
caa614af152d Store colors in human-readable form
unc0rr
parents: 8918
diff changeset
   196
                }
8918
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   197
            }
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   198
        }
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   199
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   200
        m_settingsFileName = settingsFile.fileName();
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   201
    }
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   202
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   203
    return m_settingsFileName;
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   204
}
512753ea4b1b Switch to 'settings.ini'. Copies old ini file if necessary on startup.
unc0rr
parents: 8508
diff changeset
   205
9389
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   206
QString DataManager::safeFileName(QString fileName)
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   207
{
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   208
    fileName.replace('\\', '_');
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   209
    fileName.replace('/', '_');
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   210
    fileName.replace(':', '_');
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   211
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   212
    return fileName;
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   213
}
3aae70f72126 Take care of issue #146
unc0rr
parents: 9080
diff changeset
   214
6931
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   215
void DataManager::reload()
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   216
{
7824
3df73ec7fcf2 disable F5 key and data refresh
sheepluva
parents: 7681
diff changeset
   217
    // removed for now (also code was a bit unclean, could lead to segfault if
3df73ec7fcf2 disable F5 key and data refresh
sheepluva
parents: 7681
diff changeset
   218
    // reload() is called before all members are initialized - because currently
3df73ec7fcf2 disable F5 key and data refresh
sheepluva
parents: 7681
diff changeset
   219
    // they are initialized in the getter methods rather than the constructor)
6931
86c951cd0f3f make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents: 6930
diff changeset
   220
}
7681
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   221
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   222
void DataManager::resetColors()
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   223
{
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   224
    for(int i = colorsModel()->rowCount() - 1; i >= 0; --i)
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   225
    {
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   226
        m_colorsModel->item(i)->setData(QColor(colors[i]));
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   227
    }
26978d581070 Reset to default colors button
unc0rr
parents: 7260
diff changeset
   228
}
8508
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   229
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   230
bool DataManager::ensureFileExists(const QString &fileName)
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   231
{
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   232
    QFile tmpfile(fileName);
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   233
    if (!tmpfile.exists())
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   234
        return tmpfile.open(QFile::WriteOnly);
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   235
    else
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   236
        return true;
f849b7b3af1d - Ensure that team file exists before saving into it (same fix as for hedgewars.ini, due to bug in Qt)
unc0rr
parents: 8377
diff changeset
   237
}