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