author | unc0rr |
Fri, 17 Apr 2009 17:02:24 +0000 | |
changeset 2000 | f9f47e681aad |
parent 1974 | 77a9416ceead |
child 2017 | 7845c77c8d31 |
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 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
35 |
<< QVariant(100) // damage modfier 9 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
36 |
<< QVariant(45) // turn time 10 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
37 |
<< QVariant(100) // init health 11 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
38 |
<< QVariant(15) // sudden death 12 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
39 |
<< QVariant(5) // case prob 13 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
40 |
; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
41 |
|
1897 | 42 |
AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & fileName) : |
43 |
QAbstractTableModel(parent), |
|
1974 | 44 |
numberOfDefaultSchemes(4), |
1940 | 45 |
fileConfig(fileName, QSettings::IniFormat) |
1881 | 46 |
{ |
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
47 |
QStringList predefSchemesNames; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
48 |
predefSchemesNames |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
49 |
<< "Default" |
1968 | 50 |
<< "Pro mode" |
1974 | 51 |
<< "Shoppa" |
52 |
<< "Basketball"; |
|
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
53 |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
54 |
spNames = QStringList() |
1897 | 55 |
<< "name" // 0 |
56 |
<< "fortsmode" // 1 |
|
57 |
<< "divteams" // 2 |
|
58 |
<< "solidland" // 3 |
|
59 |
<< "border" // 4 |
|
60 |
<< "lowgrav" // 5 |
|
61 |
<< "laser" // 6 |
|
62 |
<< "invulnerability" // 7 |
|
63 |
<< "mines" // 8 |
|
64 |
<< "damagefactor" // 9 |
|
65 |
<< "turntime" // 10 |
|
66 |
<< "health" // 11 |
|
67 |
<< "suddendeath" // 12 |
|
68 |
<< "caseprobability" // 13 |
|
69 |
; |
|
70 |
||
71 |
QList<QVariant> proMode; |
|
72 |
proMode |
|
1968 | 73 |
<< predefSchemesNames[1] // name 0 |
1897 | 74 |
<< QVariant(false) // fortsmode 1 |
75 |
<< QVariant(false) // team divide 2 |
|
76 |
<< QVariant(false) // solid land 3 |
|
77 |
<< QVariant(false) // border 4 |
|
78 |
<< QVariant(false) // low gravity 5 |
|
79 |
<< QVariant(false) // laser sight 6 |
|
80 |
<< QVariant(false) // invulnerable 7 |
|
81 |
<< QVariant(false) // add mines 8 |
|
82 |
<< QVariant(100) // damage modfier 9 |
|
83 |
<< QVariant(15) // turn time 10 |
|
84 |
<< QVariant(100) // init health 11 |
|
85 |
<< QVariant(15) // sudden death 12 |
|
86 |
<< QVariant(0) // case prob 13 |
|
87 |
; |
|
88 |
||
1968 | 89 |
QList<QVariant> shoppa; |
90 |
shoppa |
|
91 |
<< predefSchemesNames[2] // name 0 |
|
92 |
<< QVariant(false) // fortsmode 1 |
|
93 |
<< QVariant(false) // team divide 2 |
|
94 |
<< QVariant(true) // solid land 3 |
|
95 |
<< QVariant(true) // border 4 |
|
96 |
<< QVariant(false) // low gravity 5 |
|
97 |
<< QVariant(false) // laser sight 6 |
|
98 |
<< QVariant(false) // invulnerable 7 |
|
99 |
<< QVariant(false) // add mines 8 |
|
100 |
<< QVariant(100) // damage modfier 9 |
|
101 |
<< QVariant(30) // turn time 10 |
|
102 |
<< QVariant(100) // init health 11 |
|
103 |
<< QVariant(50) // sudden death 12 |
|
1974 | 104 |
<< QVariant(1) // case prob 13 |
105 |
; |
|
106 |
||
107 |
QList<QVariant> basketball; |
|
108 |
basketball |
|
109 |
<< predefSchemesNames[3] // name 0 |
|
110 |
<< QVariant(false) // fortsmode 1 |
|
111 |
<< QVariant(false) // team divide 2 |
|
112 |
<< QVariant(true) // solid land 3 |
|
113 |
<< QVariant(true) // border 4 |
|
114 |
<< QVariant(true) // low gravity 5 |
|
115 |
<< QVariant(false) // laser sight 6 |
|
116 |
<< QVariant(true) // invulnerable 7 |
|
117 |
<< QVariant(false) // add mines 8 |
|
118 |
<< QVariant(100) // damage modfier 9 |
|
119 |
<< QVariant(30) // turn time 10 |
|
120 |
<< QVariant(100) // init health 11 |
|
121 |
<< QVariant(15) // sudden death 12 |
|
1968 | 122 |
<< QVariant(0) // case prob 13 |
123 |
; |
|
124 |
||
1884 | 125 |
schemes.append(defaultScheme); |
1897 | 126 |
schemes.append(proMode); |
1968 | 127 |
schemes.append(shoppa); |
1974 | 128 |
schemes.append(basketball); |
1897 | 129 |
|
130 |
||
131 |
int size = fileConfig.beginReadArray("schemes"); |
|
132 |
for (int i = 0; i < size; ++i) { |
|
133 |
fileConfig.setArrayIndex(i); |
|
134 |
||
135 |
if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString())) |
|
136 |
{ |
|
137 |
QList<QVariant> scheme; |
|
138 |
||
139 |
for (int k = 0; k < spNames.size(); ++k) |
|
140 |
scheme << fileConfig.value(spNames[k], defaultScheme[k]); |
|
141 |
||
142 |
schemes.append(scheme); |
|
143 |
} |
|
144 |
} |
|
145 |
fileConfig.endArray(); |
|
1881 | 146 |
} |
147 |
||
148 |
QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
149 |
{ |
|
150 |
return QVariant(); |
|
151 |
} |
|
152 |
||
153 |
int AmmoSchemeModel::rowCount(const QModelIndex &parent) const |
|
154 |
{ |
|
155 |
if (parent.isValid()) |
|
156 |
return 0; |
|
157 |
else |
|
158 |
return schemes.size(); |
|
159 |
} |
|
160 |
||
161 |
int AmmoSchemeModel::columnCount(const QModelIndex & parent) const |
|
162 |
{ |
|
163 |
if (parent.isValid()) |
|
164 |
return 0; |
|
165 |
else |
|
1884 | 166 |
return defaultScheme.size(); |
1881 | 167 |
} |
168 |
||
169 |
Qt::ItemFlags AmmoSchemeModel::flags(const QModelIndex & index) const |
|
170 |
{ |
|
171 |
return |
|
172 |
Qt::ItemIsEnabled |
|
173 |
| Qt::ItemIsSelectable |
|
174 |
| Qt::ItemIsEditable; |
|
175 |
} |
|
176 |
||
177 |
bool AmmoSchemeModel::setData(const QModelIndex & index, const QVariant & value, int role) |
|
178 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
179 |
if (!index.isValid() || index.row() < numberOfDefaultSchemes |
1884 | 180 |
|| index.row() >= schemes.size() |
181 |
|| index.column() >= defaultScheme.size() |
|
1885 | 182 |
|| role != Qt::EditRole) |
1884 | 183 |
return false; |
184 |
||
1890 | 185 |
schemes[index.row()][index.column()] = value; |
1885 | 186 |
|
1881 | 187 |
emit dataChanged(index, index); |
1884 | 188 |
return true; |
1881 | 189 |
} |
1884 | 190 |
|
191 |
bool AmmoSchemeModel::insertRows(int row, int count, const QModelIndex & parent) |
|
192 |
{ |
|
193 |
beginInsertRows(parent, row, row); |
|
194 |
||
1890 | 195 |
QList<QVariant> newScheme = defaultScheme; |
196 |
newScheme[0] = QVariant(tr("new")); |
|
1889 | 197 |
|
198 |
schemes.insert(row, newScheme); |
|
1884 | 199 |
|
200 |
endInsertRows(); |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
201 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
202 |
return true; |
1884 | 203 |
} |
204 |
||
205 |
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent) |
|
206 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
207 |
if(count != 1 |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
208 |
|| row < numberOfDefaultSchemes |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
209 |
|| row >= schemes.size()) |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
210 |
return false; |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
211 |
|
1884 | 212 |
beginRemoveRows(parent, row, row); |
213 |
||
214 |
schemes.removeAt(row); |
|
215 |
||
216 |
endRemoveRows(); |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
217 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
218 |
return true; |
1884 | 219 |
} |
220 |
||
221 |
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const |
|
222 |
{ |
|
223 |
if (!index.isValid() || index.row() < 0 |
|
224 |
|| index.row() >= schemes.size() |
|
225 |
|| index.column() >= defaultScheme.size() |
|
1889 | 226 |
|| (role != Qt::EditRole && role != Qt::DisplayRole) |
227 |
) |
|
1884 | 228 |
return QVariant(); |
229 |
||
1890 | 230 |
return schemes[index.row()][index.column()]; |
1884 | 231 |
} |
1897 | 232 |
|
233 |
void AmmoSchemeModel::Save() |
|
234 |
{ |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
235 |
fileConfig.beginWriteArray("schemes", schemes.size()); |
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
236 |
|
1897 | 237 |
for (int i = 0; i < schemes.size(); ++i) { |
238 |
fileConfig.setArrayIndex(i); |
|
239 |
||
240 |
QList<QVariant> scheme = schemes[i]; |
|
1902
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
241 |
|
aeadb10c2d77
Add delete button and scheme selection combobox to scheme edit page
unc0rr
parents:
1899
diff
changeset
|
242 |
for (int k = 0; k < scheme.size(); ++k) |
1897 | 243 |
fileConfig.setValue(spNames[k], scheme[k]); |
244 |
} |
|
245 |
fileConfig.endArray(); |
|
246 |
} |
|
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
247 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
248 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
249 |
NetAmmoSchemeModel::NetAmmoSchemeModel(QObject * parent) : |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
250 |
QAbstractTableModel(parent) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
251 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
252 |
netScheme = defaultScheme; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
253 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
254 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
255 |
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
|
256 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
257 |
return QVariant(); |
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 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
260 |
int NetAmmoSchemeModel::rowCount(const QModelIndex & parent) const |
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 |
if (parent.isValid()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
263 |
return 0; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
264 |
else |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
265 |
return 1; |
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 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
268 |
int NetAmmoSchemeModel::columnCount(const QModelIndex & parent) const |
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 |
if (parent.isValid()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
271 |
return 0; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
272 |
else |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
273 |
return defaultScheme.size(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
274 |
} |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
275 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
276 |
QVariant NetAmmoSchemeModel::data(const QModelIndex &index, int role) const |
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 |
if (!index.isValid() || index.row() < 0 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
279 |
|| index.row() > 1 |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
280 |
|| index.column() >= defaultScheme.size() |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
281 |
|| (role != Qt::EditRole && role != Qt::DisplayRole) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
282 |
) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
283 |
return QVariant(); |
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 |
return netScheme[index.column()]; |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
286 |
} |
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 |
void NetAmmoSchemeModel::setNetSchemeConfig(QStringList & cfg) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
289 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
290 |
if(cfg.size() != netScheme.size()) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
291 |
{ |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
292 |
qWarning("Incorrect scheme cfg size"); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
293 |
return; |
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 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
296 |
for(int i = 0; i < cfg.size(); ++i) |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
297 |
netScheme[i] = QVariant(cfg[i]); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
298 |
|
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
299 |
reset(); |
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset
|
300 |
} |