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