author | Drew Gottlieb |
Sun, 02 Dec 2012 17:23:13 +0100 | |
changeset 8187 | fa725fe25708 |
parent 8049 | 133e22b5c410 |
child 8346 | 3443e0de2c9d |
permissions | -rw-r--r-- |
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 |
6952 | 3 |
* Copyright (c) 2004-2012 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 |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
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 | 19 |
/** |
6170 | 20 |
* @file |
6930 | 21 |
* @brief DataManager class implementation |
6168 | 22 |
*/ |
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 | 26 |
#include <QStandardItemModel> |
6199
582e85254110
on file save: create parent directories if needed
sheepluva
parents:
6196
diff
changeset
|
27 |
#include <QFileInfo> |
582e85254110
on file save: create parent directories if needed
sheepluva
parents:
6196
diff
changeset
|
28 |
|
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
29 |
#include "hwconsts.h" |
7260 | 30 |
#include "HWApplication.h" |
31 |
#include "sdlkeys.h" |
|
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
32 |
|
6930 | 33 |
#include "DataManager.h" |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
34 |
|
7258 | 35 |
#include "GameStyleModel.h" |
36 |
#include "HatModel.h" |
|
37 |
#include "MapModel.h" |
|
38 |
#include "ThemeModel.h" |
|
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
39 |
|
6930 | 40 |
DataManager::DataManager() |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
41 |
{ |
6953
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
42 |
m_hatModel = NULL; |
6938
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
43 |
m_mapModel = NULL; |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
44 |
m_themeModel = NULL; |
7258 | 45 |
m_colorsModel = NULL; |
7260 | 46 |
m_bindsModel = NULL; |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
47 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
48 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
49 |
|
6930 | 50 |
DataManager & DataManager::instance() |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
51 |
{ |
6930 | 52 |
static DataManager instance; |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
53 |
return instance; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
54 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
55 |
|
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
56 |
|
6930 | 57 |
QStringList DataManager::entryList( |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6199
diff
changeset
|
58 |
const QString & subDirectory, |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6199
diff
changeset
|
59 |
QDir::Filters filters, |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6199
diff
changeset
|
60 |
const QStringList & nameFilters |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6199
diff
changeset
|
61 |
) const |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
62 |
{ |
7774
27f9b9362f47
Simplify DataManager as physfs partially implements its functions
unc0rr
parents:
7681
diff
changeset
|
63 |
QDir tmpDir(QString("physfs://%1").arg(subDirectory)); |
27f9b9362f47
Simplify DataManager as physfs partially implements its functions
unc0rr
parents:
7681
diff
changeset
|
64 |
QStringList result = tmpDir.entryList(nameFilters, filters); |
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
65 |
|
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
|
66 |
// 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
|
67 |
QMap<QString, QString> sortedFileNames; |
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
|
68 |
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
|
69 |
{ |
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
|
70 |
sortedFileNames.insert(fn.toLower(), fn); |
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
|
71 |
} |
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 |
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
|
73 |
|
6159
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
74 |
return result; |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
75 |
} |
c780b8cf4d75
introduce HWDataManager util for transparent access to [user-]data files
sheepluva
parents:
diff
changeset
|
76 |
|
6958 | 77 |
GameStyleModel * DataManager::gameStyleModel() |
78 |
{ |
|
79 |
if (m_gameStyleModel == NULL) { |
|
80 |
m_gameStyleModel = new GameStyleModel(); |
|
81 |
m_gameStyleModel->loadGameStyles(); |
|
82 |
} |
|
83 |
return m_gameStyleModel; |
|
84 |
} |
|
85 |
||
6953
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
86 |
HatModel * DataManager::hatModel() |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
87 |
{ |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
88 |
if (m_hatModel == NULL) { |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
89 |
m_hatModel = new HatModel(); |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
90 |
m_hatModel->loadHats(); |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
91 |
} |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
92 |
return m_hatModel; |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
93 |
} |
4c2dd25630a7
* make HatModel update automatically (also renamed class and files)
sheepluva
parents:
6952
diff
changeset
|
94 |
|
6938
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
95 |
MapModel * DataManager::mapModel() |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
96 |
{ |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
97 |
if (m_mapModel == NULL) { |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
98 |
m_mapModel = new MapModel(); |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
99 |
m_mapModel->loadMaps(); |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
100 |
} |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
101 |
return m_mapModel; |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
102 |
} |
217ed62e872c
MapModel, various cleanups. TODO/FIXME: QComboBox separator not working with custom models
sheepluva
parents:
6937
diff
changeset
|
103 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
104 |
ThemeModel * DataManager::themeModel() |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
105 |
{ |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
m_themeModel->loadThemes(); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
109 |
} |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
110 |
return m_themeModel; |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
111 |
} |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6931
diff
changeset
|
112 |
|
7258 | 113 |
QStandardItemModel * DataManager::colorsModel() |
114 |
{ |
|
115 |
if(m_colorsModel == NULL) |
|
116 |
{ |
|
117 |
m_colorsModel = new QStandardItemModel(); |
|
118 |
||
119 |
int i = 0; |
|
120 |
while(colors[i]) |
|
121 |
{ |
|
122 |
QStandardItem * item = new QStandardItem(); |
|
123 |
item->setData(QColor(colors[i])); |
|
124 |
m_colorsModel->appendRow(item); |
|
125 |
++i; |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
return m_colorsModel; |
|
130 |
} |
|
131 |
||
7260 | 132 |
QStandardItemModel * DataManager::bindsModel() |
133 |
{ |
|
134 |
if(m_bindsModel == NULL) |
|
135 |
{ |
|
136 |
m_bindsModel = new QStandardItemModel(); |
|
137 |
||
138 |
for(int j = 0; sdlkeys[j][1][0] != '\0'; j++) |
|
139 |
{ |
|
140 |
QStandardItem * item = new QStandardItem(); |
|
141 |
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); |
|
142 |
item->setData(sdlkeys[j][0], Qt::UserRole + 1); |
|
143 |
m_bindsModel->appendRow(item); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
return m_bindsModel; |
|
148 |
} |
|
149 |
||
6931
86c951cd0f3f
make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents:
6930
diff
changeset
|
150 |
void DataManager::reload() |
86c951cd0f3f
make DataManager a QObject with reload() slot that emits signal updated()
sheepluva
parents:
6930
diff
changeset
|
151 |
{ |
7824 | 152 |
// removed for now (also code was a bit unclean, could lead to segfault if |
153 |
// reload() is called before all members are initialized - because currently |
|
154 |
// 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
|
155 |
} |
7681 | 156 |
|
157 |
void DataManager::resetColors() |
|
158 |
{ |
|
159 |
for(int i = colorsModel()->rowCount() - 1; i >= 0; --i) |
|
160 |
{ |
|
161 |
m_colorsModel->item(i)->setData(QColor(colors[i])); |
|
162 |
} |
|
163 |
} |