author  unc0rr 
Wed, 18 Mar 2009 15:48:43 +0000  
changeset 1899  5763f46d7486 
parent 1897  e9dcb47013c7 
child 1902  aeadb10c2d77 
permissions  rwrr 
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 021111307, 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 

1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

25 
QList<QVariant> defaultScheme = QList<QVariant>() 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

26 
<< QVariant("Default") // name 0 
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), 

44 
fileConfig(fileName, QSettings::IniFormat) 

1881  45 
{ 
1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

46 
QStringList predefSchemesNames; 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

47 
predefSchemesNames 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

48 
<< "Default" 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

49 
<< "Pro mode"; 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

50 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

51 
QStringList spNames = QStringList() 
1897  52 
<< "name" // 0 
53 
<< "fortsmode" // 1 

54 
<< "divteams" // 2 

55 
<< "solidland" // 3 

56 
<< "border" // 4 

57 
<< "lowgrav" // 5 

58 
<< "laser" // 6 

59 
<< "invulnerability" // 7 

60 
<< "mines" // 8 

61 
<< "damagefactor" // 9 

62 
<< "turntime" // 10 

63 
<< "health" // 11 

64 
<< "suddendeath" // 12 

65 
<< "caseprobability" // 13 

66 
; 

67 

68 
QList<QVariant> proMode; 

69 
proMode 

1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

70 
<< QVariant("Pro mode") // name 0 
1897  71 
<< QVariant(false) // fortsmode 1 
72 
<< QVariant(false) // team divide 2 

73 
<< QVariant(false) // solid land 3 

74 
<< QVariant(false) // border 4 

75 
<< QVariant(false) // low gravity 5 

76 
<< QVariant(false) // laser sight 6 

77 
<< QVariant(false) // invulnerable 7 

78 
<< QVariant(false) // add mines 8 

79 
<< QVariant(100) // damage modfier 9 

80 
<< QVariant(15) // turn time 10 

81 
<< QVariant(100) // init health 11 

82 
<< QVariant(15) // sudden death 12 

83 
<< QVariant(0) // case prob 13 

84 
; 

85 

1884  86 
schemes.append(defaultScheme); 
1897  87 
schemes.append(proMode); 
88 

89 

90 
int size = fileConfig.beginReadArray("schemes"); 

91 
for (int i = 0; i < size; ++i) { 

92 
fileConfig.setArrayIndex(i); 

93 

94 
if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString())) 

95 
{ 

96 
QList<QVariant> scheme; 

97 

98 
for (int k = 0; k < spNames.size(); ++k) 

99 
scheme << fileConfig.value(spNames[k], defaultScheme[k]); 

100 

101 
schemes.append(scheme); 

102 
} 

103 
} 

104 
fileConfig.endArray(); 

1881  105 
} 
106 

107 
QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const 

108 
{ 

109 
return QVariant(); 

110 
} 

111 

112 
int AmmoSchemeModel::rowCount(const QModelIndex &parent) const 

113 
{ 

114 
if (parent.isValid()) 

115 
return 0; 

116 
else 

117 
return schemes.size(); 

118 
} 

119 

120 
int AmmoSchemeModel::columnCount(const QModelIndex & parent) const 

121 
{ 

122 
if (parent.isValid()) 

123 
return 0; 

124 
else 

1884  125 
return defaultScheme.size(); 
1881  126 
} 
127 

128 
Qt::ItemFlags AmmoSchemeModel::flags(const QModelIndex & index) const 

129 
{ 

130 
return 

131 
Qt::ItemIsEnabled 

132 
 Qt::ItemIsSelectable 

133 
 Qt::ItemIsEditable; 

134 
} 

135 

136 
bool AmmoSchemeModel::setData(const QModelIndex & index, const QVariant & value, int role) 

137 
{ 

1884  138 
if (!index.isValid()  index.row() < 0 
139 
 index.row() >= schemes.size() 

140 
 index.column() >= defaultScheme.size() 

1885  141 
 role != Qt::EditRole) 
1884  142 
return false; 
143 

1890  144 
schemes[index.row()][index.column()] = value; 
1885  145 

1881  146 
emit dataChanged(index, index); 
1884  147 
return true; 
1881  148 
} 
1884  149 

150 
bool AmmoSchemeModel::insertRows(int row, int count, const QModelIndex & parent) 

151 
{ 

152 
beginInsertRows(parent, row, row); 

153 

1890  154 
QList<QVariant> newScheme = defaultScheme; 
155 
newScheme[0] = QVariant(tr("new")); 

1889  156 

157 
schemes.insert(row, newScheme); 

1884  158 

159 
endInsertRows(); 

160 
} 

161 

162 
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent) 

163 
{ 

164 
beginRemoveRows(parent, row, row); 

165 

1897  166 
fileConfig.remove(schemes[row][0].toString()); 
1884  167 
schemes.removeAt(row); 
168 

169 
endRemoveRows(); 

170 
} 

171 

172 
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const 

173 
{ 

174 
if (!index.isValid()  index.row() < 0 

175 
 index.row() >= schemes.size() 

176 
 index.column() >= defaultScheme.size() 

1889  177 
 (role != Qt::EditRole && role != Qt::DisplayRole) 
178 
) 

1884  179 
return QVariant(); 
180 

1890  181 
return schemes[index.row()][index.column()]; 
1884  182 
} 
1897  183 

184 
void AmmoSchemeModel::Save() 

185 
{ 

186 
fileConfig.beginWriteArray("schemes"); 

187 
for (int i = 0; i < schemes.size(); ++i) { 

188 
fileConfig.setArrayIndex(i); 

189 

190 
QList<QVariant> scheme = schemes[i]; 

191 

192 
for (int k = 0; k < spNames.size(); ++k) 

193 
fileConfig.setValue(spNames[k], scheme[k]); 

194 
} 

195 
fileConfig.endArray(); 

196 
} 

1899
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

197 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

198 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

199 
NetAmmoSchemeModel::NetAmmoSchemeModel(QObject * parent) : 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

200 
QAbstractTableModel(parent) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

201 
{ 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

202 
netScheme = defaultScheme; 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

203 
} 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

204 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

205 
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

206 
{ 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

207 
return QVariant(); 
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 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

210 
int NetAmmoSchemeModel::rowCount(const QModelIndex & parent) const 
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 
if (parent.isValid()) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

213 
return 0; 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

214 
else 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

215 
return 1; 
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 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

218 
int NetAmmoSchemeModel::columnCount(const QModelIndex & parent) const 
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 
if (parent.isValid()) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

221 
return 0; 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

222 
else 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

223 
return defaultScheme.size(); 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

224 
} 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

225 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

226 
QVariant NetAmmoSchemeModel::data(const QModelIndex &index, int role) const 
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 
if (!index.isValid()  index.row() < 0 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

229 
 index.row() > 1 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

230 
 index.column() >= defaultScheme.size() 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

231 
 (role != Qt::EditRole && role != Qt::DisplayRole) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

232 
) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

233 
return QVariant(); 
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 
return netScheme[index.column()]; 
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 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

238 
void NetAmmoSchemeModel::setNetSchemeConfig(QStringList & cfg) 
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 
if(cfg.size() != netScheme.size()) 
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 
qWarning("Incorrect scheme cfg size"); 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

243 
return; 
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 

5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

246 
for(int i = 0; i < cfg.size(); ++i) 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

247 
netScheme[i] = QVariant(cfg[i]); 
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 
reset(); 
5763f46d7486
Sync schemes config over net should work now (untested)
unc0rr
parents:
1897
diff
changeset

250 
} 