author | unc0rr |
Thu, 30 Apr 2009 20:13:44 +0000 | |
changeset 2017 | 7845c77c8d31 |
parent 1974 | 77a9416ceead |
child 2023 | 41d3afaa20c7 |
permissions | -rw-r--r-- |
1881 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
1885 | 19 |
#include <QDebug> |
1881 | 20 |
#include <QModelIndex> |
1897 | 21 |
|
1881 | 22 |
#include "ammoSchemeModel.h" |
1897 | 23 |
#include "hwconsts.h" |
1881 | 24 |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
25 |
QList<QVariant> defaultScheme = QList<QVariant>() |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
26 |
<< QVariant("Default") // name 0 |
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
27 |
<< QVariant(false) // fortsmode 1 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
28 |
<< QVariant(false) // team divide 2 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
29 |
<< QVariant(false) // solid land 3 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
30 |
<< QVariant(false) // border 4 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
31 |
<< QVariant(false) // low gravity 5 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
32 |
<< QVariant(false) // laser sight 6 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
33 |
<< QVariant(false) // invulnerable 7 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
34 |
<< QVariant(true) // add mines 8 |
2017 | 35 |
<< QVariant(false) // vampiric 9 |
36 |
<< QVariant(false) // karma 10 |
|
37 |
<< QVariant(100) // damage modfier 11 |
|
38 |
<< QVariant(45) // turn time 12 |
|
39 |
<< QVariant(100) // init health 13 |
|
40 |
<< QVariant(15) // sudden death 14 |
|
41 |
<< QVariant(5) // case prob 15 |
|
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
42 |
; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
43 |
|
1897 | 44 |
AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & fileName) : |
45 |
QAbstractTableModel(parent), |
|
1974 | 46 |
numberOfDefaultSchemes(4), |
1940 | 47 |
fileConfig(fileName, QSettings::IniFormat) |
1881 | 48 |
{ |
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
49 |
QStringList predefSchemesNames; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
50 |
predefSchemesNames |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
51 |
<< "Default" |
1968 | 52 |
<< "Pro mode" |
1974 | 53 |
<< "Shoppa" |
54 |
<< "Basketball"; |
|
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
55 |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
56 |
spNames = QStringList() |
1897 | 57 |
<< "name" // 0 |
58 |
<< "fortsmode" // 1 |
|
59 |
<< "divteams" // 2 |
|
60 |
<< "solidland" // 3 |
|
61 |
<< "border" // 4 |
|
62 |
<< "lowgrav" // 5 |
|
63 |
<< "laser" // 6 |
|
64 |
<< "invulnerability" // 7 |
|
65 |
<< "mines" // 8 |
|
2017 | 66 |
<< "vampiric" // 9 |
67 |
<< "karma" // 10 |
|
68 |
<< "damagefactor" // 11 |
|
69 |
<< "turntime" // 12 |
|
70 |
<< "health" // 13 |
|
71 |
<< "suddendeath" // 14 |
|
72 |
<< "caseprobability" // 15 |
|
1897 | 73 |
; |
74 |
||
75 |
QList<QVariant> proMode; |
|
76 |
proMode |
|
1968 | 77 |
<< predefSchemesNames[1] // name 0 |
1897 | 78 |
<< QVariant(false) // fortsmode 1 |
79 |
<< QVariant(false) // team divide 2 |
|
80 |
<< QVariant(false) // solid land 3 |
|
81 |
<< QVariant(false) // border 4 |
|
82 |
<< QVariant(false) // low gravity 5 |
|
83 |
<< QVariant(false) // laser sight 6 |
|
84 |
<< QVariant(false) // invulnerable 7 |
|
85 |
<< QVariant(false) // add mines 8 |
|
2017 | 86 |
<< QVariant(false) // vampiric 9 |
87 |
<< QVariant(false) // karma 10 |
|
88 |
<< QVariant(100) // damage modfier 11 |
|
89 |
<< QVariant(15) // turn time 12 |
|
90 |
<< QVariant(100) // init health 13 |
|
91 |
<< QVariant(15) // sudden death 14 |
|
92 |
<< QVariant(0) // case prob 15 |
|
1897 | 93 |
; |
94 |
||
1968 | 95 |
QList<QVariant> shoppa; |
96 |
shoppa |
|
97 |
<< predefSchemesNames[2] // name 0 |
|
98 |
<< QVariant(false) // fortsmode 1 |
|
99 |
<< QVariant(false) // team divide 2 |
|
100 |
<< QVariant(true) // solid land 3 |
|
101 |
<< QVariant(true) // border 4 |
|
102 |
<< QVariant(false) // low gravity 5 |
|
103 |
<< QVariant(false) // laser sight 6 |
|
104 |
<< QVariant(false) // invulnerable 7 |
|
105 |
<< QVariant(false) // add mines 8 |
|
2017 | 106 |
<< QVariant(false) // vampiric 9 |
107 |
<< QVariant(false) // karma 10 |
|
108 |
<< QVariant(100) // damage modfier 11 |
|
109 |
<< QVariant(30) // turn time 12 |
|
110 |
<< QVariant(100) // init health 13 |
|
111 |
<< QVariant(50) // sudden death 14 |
|
112 |
<< QVariant(1) // case prob 15 |
|
1974 | 113 |
; |
114 |
||
115 |
QList<QVariant> basketball; |
|
116 |
basketball |
|
117 |
<< predefSchemesNames[3] // name 0 |
|
118 |
<< QVariant(false) // fortsmode 1 |
|
119 |
<< QVariant(false) // team divide 2 |
|
120 |
<< QVariant(true) // solid land 3 |
|
121 |
<< QVariant(true) // border 4 |
|
122 |
<< QVariant(true) // low gravity 5 |
|
123 |
<< QVariant(false) // laser sight 6 |
|
124 |
<< QVariant(true) // invulnerable 7 |
|
125 |
<< QVariant(false) // add mines 8 |
|
2017 | 126 |
<< QVariant(false) // vampiric 9 |
127 |
<< QVariant(false) // karma 10 |
|
128 |
<< QVariant(100) // damage modfier 11 |
|
129 |
<< QVariant(30) // turn time 12 |
|
130 |
<< QVariant(100) // init health 13 |
|
131 |
<< QVariant(15) // sudden death 14 |
|
132 |
<< QVariant(0) // case prob 15 |
|
1968 | 133 |
; |
134 |
||
1884 | 135 |
schemes.append(defaultScheme); |
1897 | 136 |
schemes.append(proMode); |
1968 | 137 |
schemes.append(shoppa); |
1974 | 138 |
schemes.append(basketball); |
1897 | 139 |
|
140 |
||
141 |
int size = fileConfig.beginReadArray("schemes"); |
|
142 |
for (int i = 0; i < size; ++i) { |
|
143 |
fileConfig.setArrayIndex(i); |
|
144 |
||
145 |
if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString())) |
|
146 |
{ |
|
147 |
QList<QVariant> scheme; |
|
148 |
||
149 |
for (int k = 0; k < spNames.size(); ++k) |
|
150 |
scheme << fileConfig.value(spNames[k], defaultScheme[k]); |
|
151 |
||
152 |
schemes.append(scheme); |
|
153 |
} |
|
154 |
} |
|
155 |
fileConfig.endArray(); |
|
1881 | 156 |
} |
157 |
||
158 |
QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
159 |
{ |
|
160 |
return QVariant(); |
|
161 |
} |
|
162 |
||
163 |
int AmmoSchemeModel::rowCount(const QModelIndex &parent) const |
|
164 |
{ |
|
165 |
if (parent.isValid()) |
|
166 |
return 0; |
|
167 |
else |
|
168 |
return schemes.size(); |
|
169 |
} |
|
170 |
||
171 |
int AmmoSchemeModel::columnCount(const QModelIndex & parent) const |
|
172 |
{ |
|
173 |
if (parent.isValid()) |
|
174 |
return 0; |
|
175 |
else |
|
1884 | 176 |
return defaultScheme.size(); |
1881 | 177 |
} |
178 |
||
179 |
Qt::ItemFlags AmmoSchemeModel::flags(const QModelIndex & index) const |
|
180 |
{ |
|
181 |
return |
|
182 |
Qt::ItemIsEnabled |
|
183 |
| Qt::ItemIsSelectable |
|
184 |
| Qt::ItemIsEditable; |
|
185 |
} |
|
186 |
||
187 |
bool AmmoSchemeModel::setData(const QModelIndex & index, const QVariant & value, int role) |
|
188 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
189 |
if (!index.isValid() || index.row() < numberOfDefaultSchemes |
1884 | 190 |
|| index.row() >= schemes.size() |
191 |
|| index.column() >= defaultScheme.size() |
|
1885 | 192 |
|| role != Qt::EditRole) |
1884 | 193 |
return false; |
194 |
||
1890 | 195 |
schemes[index.row()][index.column()] = value; |
1885 | 196 |
|
1881 | 197 |
emit dataChanged(index, index); |
1884 | 198 |
return true; |
1881 | 199 |
} |
1884 | 200 |
|
201 |
bool AmmoSchemeModel::insertRows(int row, int count, const QModelIndex & parent) |
|
202 |
{ |
|
203 |
beginInsertRows(parent, row, row); |
|
204 |
||
1890 | 205 |
QList<QVariant> newScheme = defaultScheme; |
206 |
newScheme[0] = QVariant(tr("new")); |
|
1889 | 207 |
|
208 |
schemes.insert(row, newScheme); |
|
1884 | 209 |
|
210 |
endInsertRows(); |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
211 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
212 |
return true; |
1884 | 213 |
} |
214 |
||
215 |
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent) |
|
216 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
217 |
if(count != 1 |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
218 |
|| row < numberOfDefaultSchemes |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
219 |
|| row >= schemes.size()) |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
220 |
return false; |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
221 |
|
1884 | 222 |
beginRemoveRows(parent, row, row); |
223 |
||
224 |
schemes.removeAt(row); |
|
225 |
||
226 |
endRemoveRows(); |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
227 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
228 |
return true; |
1884 | 229 |
} |
230 |
||
231 |
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const |
|
232 |
{ |
|
233 |
if (!index.isValid() || index.row() < 0 |
|
234 |
|| index.row() >= schemes.size() |
|
235 |
|| index.column() >= defaultScheme.size() |
|
1889 | 236 |
|| (role != Qt::EditRole && role != Qt::DisplayRole) |
237 |
) |
|
1884 | 238 |
return QVariant(); |
239 |
||
1890 | 240 |
return schemes[index.row()][index.column()]; |
1884 | 241 |
} |
1897 | 242 |
|
243 |
void AmmoSchemeModel::Save() |
|
244 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
245 |
fileConfig.beginWriteArray("schemes", schemes.size()); |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
246 |
|
1897 | 247 |
for (int i = 0; i < schemes.size(); ++i) { |
248 |
fileConfig.setArrayIndex(i); |
|
249 |
||
250 |
QList<QVariant> scheme = schemes[i]; |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
251 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
252 |
for (int k = 0; k < scheme.size(); ++k) |
1897 | 253 |
fileConfig.setValue(spNames[k], scheme[k]); |
254 |
} |
|
255 |
fileConfig.endArray(); |
|
256 |
} |
|
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
257 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
258 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
259 |
NetAmmoSchemeModel::NetAmmoSchemeModel(QObject * parent) : |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
260 |
QAbstractTableModel(parent) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
261 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
262 |
netScheme = defaultScheme; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
263 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
264 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
265 |
QVariant NetAmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
266 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
267 |
return QVariant(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
268 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
269 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
270 |
int NetAmmoSchemeModel::rowCount(const QModelIndex & parent) const |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
271 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
272 |
if (parent.isValid()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
273 |
return 0; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
274 |
else |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
275 |
return 1; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
276 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
277 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
278 |
int NetAmmoSchemeModel::columnCount(const QModelIndex & parent) const |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
279 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
280 |
if (parent.isValid()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
281 |
return 0; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
282 |
else |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
283 |
return defaultScheme.size(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
284 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
285 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
286 |
QVariant NetAmmoSchemeModel::data(const QModelIndex &index, int role) const |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
287 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
288 |
if (!index.isValid() || index.row() < 0 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
289 |
|| index.row() > 1 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
290 |
|| index.column() >= defaultScheme.size() |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
291 |
|| (role != Qt::EditRole && role != Qt::DisplayRole) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
292 |
) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
293 |
return QVariant(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
294 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
295 |
return netScheme[index.column()]; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
296 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
297 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
298 |
void NetAmmoSchemeModel::setNetSchemeConfig(QStringList & cfg) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
299 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
300 |
if(cfg.size() != netScheme.size()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
301 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
302 |
qWarning("Incorrect scheme cfg size"); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
303 |
return; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
304 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
305 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
306 |
for(int i = 0; i < cfg.size(); ++i) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
307 |
netScheme[i] = QVariant(cfg[i]); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
308 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
309 |
reset(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
310 |
} |