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 |
|
1897
|
25 |
AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & fileName) :
|
|
26 |
QAbstractTableModel(parent),
|
|
27 |
fileConfig(fileName, QSettings::IniFormat)
|
1881
|
28 |
{
|
1897
|
29 |
spNames
|
|
30 |
<< "name" // 0
|
|
31 |
<< "fortsmode" // 1
|
|
32 |
<< "divteams" // 2
|
|
33 |
<< "solidland" // 3
|
|
34 |
<< "border" // 4
|
|
35 |
<< "lowgrav" // 5
|
|
36 |
<< "laser" // 6
|
|
37 |
<< "invulnerability" // 7
|
|
38 |
<< "mines" // 8
|
|
39 |
<< "damagefactor" // 9
|
|
40 |
<< "turntime" // 10
|
|
41 |
<< "health" // 11
|
|
42 |
<< "suddendeath" // 12
|
|
43 |
<< "caseprobability" // 13
|
|
44 |
;
|
|
45 |
|
|
46 |
|
|
47 |
QStringList predefSchemesNames;
|
|
48 |
predefSchemesNames
|
|
49 |
<< tr("Default")
|
|
50 |
<< tr("Pro mode");
|
|
51 |
|
1884
|
52 |
defaultScheme
|
1895
|
53 |
<< QVariant(tr("Default")) // name 0
|
|
54 |
<< QVariant(false) // fortsmode 1
|
|
55 |
<< QVariant(false) // team divide 2
|
|
56 |
<< QVariant(false) // solid land 3
|
|
57 |
<< QVariant(false) // border 4
|
|
58 |
<< QVariant(false) // low gravity 5
|
|
59 |
<< QVariant(false) // laser sight 6
|
|
60 |
<< QVariant(false) // invulnerable 7
|
|
61 |
<< QVariant(true) // add mines 8
|
|
62 |
<< QVariant(100) // damage modfier 9
|
|
63 |
<< QVariant(45) // turn time 10
|
|
64 |
<< QVariant(100) // init health 11
|
|
65 |
<< QVariant(15) // sudden death 12
|
|
66 |
<< QVariant(5) // case prob 13
|
1884
|
67 |
;
|
1881
|
68 |
|
1897
|
69 |
QList<QVariant> proMode;
|
|
70 |
proMode
|
|
71 |
<< QVariant(tr("Pro mode"))// name 0
|
|
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 |
{
|
1884
|
139 |
if (!index.isValid() || index.row() < 0
|
|
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();
|
|
161 |
}
|
|
162 |
|
|
163 |
bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent)
|
|
164 |
{
|
|
165 |
beginRemoveRows(parent, row, row);
|
|
166 |
|
1897
|
167 |
fileConfig.remove(schemes[row][0].toString());
|
1884
|
168 |
schemes.removeAt(row);
|
|
169 |
|
|
170 |
endRemoveRows();
|
|
171 |
}
|
|
172 |
|
|
173 |
QVariant AmmoSchemeModel::data(const QModelIndex &index, int role) const
|
|
174 |
{
|
|
175 |
if (!index.isValid() || index.row() < 0
|
|
176 |
|| index.row() >= schemes.size()
|
|
177 |
|| index.column() >= defaultScheme.size()
|
1889
|
178 |
|| (role != Qt::EditRole && role != Qt::DisplayRole)
|
|
179 |
)
|
1884
|
180 |
return QVariant();
|
|
181 |
|
1890
|
182 |
return schemes[index.row()][index.column()];
|
1884
|
183 |
}
|
1897
|
184 |
|
|
185 |
void AmmoSchemeModel::Save()
|
|
186 |
{
|
|
187 |
fileConfig.beginWriteArray("schemes");
|
|
188 |
for (int i = 0; i < schemes.size(); ++i) {
|
|
189 |
fileConfig.setArrayIndex(i);
|
|
190 |
|
|
191 |
QList<QVariant> scheme = schemes[i];
|
|
192 |
|
|
193 |
for (int k = 0; k < spNames.size(); ++k)
|
|
194 |
fileConfig.setValue(spNames[k], scheme[k]);
|
|
195 |
}
|
|
196 |
fileConfig.endArray();
|
|
197 |
}
|