1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: tspmodel.cpp 42 2009-07-31 18:23:07Z laleppa $ |
---|
6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/tspmodel.cpp $ |
---|
7 | * |
---|
8 | * This file is part of TSPSG. |
---|
9 | * |
---|
10 | * TSPSG is free software: you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation, either version 3 of the License, or |
---|
13 | * (at your option) any later version. |
---|
14 | * |
---|
15 | * TSPSG is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | * GNU General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with TSPSG. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "tspmodel.h" |
---|
25 | |
---|
26 | CTSPModel::CTSPModel(QObject *parent) |
---|
27 | : QAbstractTableModel(parent), nCities(0) |
---|
28 | { |
---|
29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
30 | } |
---|
31 | |
---|
32 | inline int CTSPModel::rand(int min, int max) const |
---|
33 | { |
---|
34 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
35 | } |
---|
36 | |
---|
37 | int CTSPModel::rowCount(const QModelIndex &) const |
---|
38 | { |
---|
39 | return nCities; |
---|
40 | } |
---|
41 | |
---|
42 | int CTSPModel::columnCount(const QModelIndex &) const |
---|
43 | { |
---|
44 | return nCities; |
---|
45 | } |
---|
46 | |
---|
47 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
48 | { |
---|
49 | if (role == Qt::DisplayRole) { |
---|
50 | if (orientation == Qt::Vertical) |
---|
51 | return trUtf8("City %1").arg(section + 1); |
---|
52 | else |
---|
53 | return trUtf8("%1").arg(section + 1); |
---|
54 | } |
---|
55 | return QVariant(); |
---|
56 | } |
---|
57 | |
---|
58 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
59 | { |
---|
60 | if (!index.isValid()) |
---|
61 | return QVariant(); |
---|
62 | if (role == Qt::TextAlignmentRole) |
---|
63 | return int(Qt::AlignCenter); |
---|
64 | else if (role == Qt::FontRole) { |
---|
65 | QFont font; |
---|
66 | font.setBold(true); |
---|
67 | return font; |
---|
68 | } else if (role == Qt::DisplayRole || role == Qt::EditRole) { |
---|
69 | if (index.row() < nCities && index.column() < nCities) |
---|
70 | if (table[index.row()][index.column()] == INFINITY) |
---|
71 | return trUtf8(INFSTR); |
---|
72 | else |
---|
73 | // HACK: Converting to string to prevent spinbox in edit mode |
---|
74 | return QVariant(table[index.row()][index.column()]).toString(); |
---|
75 | else |
---|
76 | return QVariant(); |
---|
77 | } else if (role == Qt::UserRole) |
---|
78 | return table[index.row()][index.column()]; |
---|
79 | return QVariant(); |
---|
80 | } |
---|
81 | |
---|
82 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
83 | { |
---|
84 | if (!index.isValid()) |
---|
85 | return false; |
---|
86 | if (role == Qt::EditRole && index.row() != index.column()) { |
---|
87 | if (value.toString().compare(INFSTR) == 0) |
---|
88 | table[index.row()][index.column()] = INFINITY; |
---|
89 | else { |
---|
90 | bool ok; |
---|
91 | double tmp = value.toDouble(&ok); |
---|
92 | if (!ok || tmp < 0) |
---|
93 | return false; |
---|
94 | else |
---|
95 | table[index.row()][index.column()] = tmp; |
---|
96 | } |
---|
97 | emit dataChanged(index,index); |
---|
98 | return true; |
---|
99 | } |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
104 | { |
---|
105 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
106 | if (index.row() != index.column()) |
---|
107 | flags |= Qt::ItemIsEditable; |
---|
108 | return flags; |
---|
109 | } |
---|
110 | |
---|
111 | quint16 CTSPModel::numCities() const |
---|
112 | { |
---|
113 | return nCities; |
---|
114 | } |
---|
115 | |
---|
116 | void CTSPModel::setNumCities(int n) |
---|
117 | { |
---|
118 | if (n == nCities) |
---|
119 | return; |
---|
120 | emit layoutAboutToBeChanged(); |
---|
121 | table.resize(n); |
---|
122 | for (int k = 0; k < n; k++) { |
---|
123 | table[k].resize(n); |
---|
124 | } |
---|
125 | if (n > nCities) |
---|
126 | for (int k = nCities; k < n; k++) |
---|
127 | table[k][k] = INFINITY; |
---|
128 | nCities = n; |
---|
129 | emit layoutChanged(); |
---|
130 | } |
---|
131 | |
---|
132 | void CTSPModel::clear() |
---|
133 | { |
---|
134 | for (int r = 0; r < nCities; r++) |
---|
135 | for (int c = 0; c < nCities; c++) |
---|
136 | if (r != c) |
---|
137 | table[r][c] = 0; |
---|
138 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
139 | } |
---|
140 | |
---|
141 | inline bool CTSPModel::loadError(QDataStream::Status status) |
---|
142 | { |
---|
143 | QString err; |
---|
144 | if (status == QDataStream::Ok) |
---|
145 | return false; |
---|
146 | else if (status == QDataStream::ReadPastEnd) |
---|
147 | err = trUtf8("Unexpected end of file."); |
---|
148 | else if (status == QDataStream::ReadCorruptData) |
---|
149 | err = trUtf8("Corrupt data read. File possibly corrupted."); |
---|
150 | else |
---|
151 | err = trUtf8("Unknown error."); |
---|
152 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec(); |
---|
153 | return true; |
---|
154 | } |
---|
155 | |
---|
156 | void CTSPModel::loadTask(QString fname) |
---|
157 | { |
---|
158 | QFile f(fname); |
---|
159 | if (!f.open(QIODevice::ReadOnly)) { |
---|
160 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
161 | return; |
---|
162 | } |
---|
163 | QDataStream ds(&f); |
---|
164 | ds.setVersion(QDataStream::Qt_4_4); |
---|
165 | quint32 sig; |
---|
166 | ds >> sig; |
---|
167 | if (loadError(ds.status())) |
---|
168 | return; |
---|
169 | ds.device()->reset(); |
---|
170 | if (sig == TSPT) |
---|
171 | loadTSPT(&ds); |
---|
172 | else if ((sig >> 16) == ZKT) |
---|
173 | loadZKT(&ds); |
---|
174 | else |
---|
175 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); |
---|
176 | f.close(); |
---|
177 | } |
---|
178 | |
---|
179 | void CTSPModel::loadTSPT(QDataStream *ds) |
---|
180 | { |
---|
181 | // Skipping signature |
---|
182 | ds->skipRawData(sizeof(TSPT)); |
---|
183 | if (loadError(ds->status())) |
---|
184 | return; |
---|
185 | // File version |
---|
186 | quint8 version; |
---|
187 | *ds >> version; |
---|
188 | if (loadError(ds->status())) |
---|
189 | return; |
---|
190 | if (version > TSPT_VERSION) { |
---|
191 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
192 | return; |
---|
193 | } |
---|
194 | // Skipping metadata |
---|
195 | ds->skipRawData(TSPT_META_SIZE); |
---|
196 | if (loadError(ds->status())) |
---|
197 | return; |
---|
198 | // Cities number |
---|
199 | quint16 size; |
---|
200 | *ds >> size; |
---|
201 | if (loadError(ds->status())) |
---|
202 | return; |
---|
203 | if (size < 3) { |
---|
204 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
205 | return; |
---|
206 | } |
---|
207 | if (nCities != size) |
---|
208 | emit numCitiesChanged(size); |
---|
209 | // Costs |
---|
210 | for (int r = 0; r < size; r++) |
---|
211 | for (int c = 0; c < size; c++) |
---|
212 | if (r != c) { |
---|
213 | *ds >> table[r][c]; |
---|
214 | if (loadError(ds->status())) { |
---|
215 | clear(); |
---|
216 | return; |
---|
217 | } |
---|
218 | } |
---|
219 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
220 | } |
---|
221 | |
---|
222 | void CTSPModel::loadZKT(QDataStream *ds) |
---|
223 | { |
---|
224 | // Skipping signature |
---|
225 | ds->skipRawData(sizeof(ZKT)); |
---|
226 | if (loadError(ds->status())) |
---|
227 | return; |
---|
228 | // File version |
---|
229 | quint16 version; |
---|
230 | ds->readRawData(reinterpret_cast<char *>(&version),2); |
---|
231 | if (loadError(ds->status())) |
---|
232 | return; |
---|
233 | if (version > ZKT_VERSION) { |
---|
234 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
235 | return; |
---|
236 | } |
---|
237 | // Cities number |
---|
238 | quint8 size; |
---|
239 | ds->readRawData(reinterpret_cast<char *>(&size),1); |
---|
240 | if (loadError(ds->status())) |
---|
241 | return; |
---|
242 | if ((size < 3) || (size > 5)) { |
---|
243 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
244 | return; |
---|
245 | } |
---|
246 | if (nCities != size) |
---|
247 | emit numCitiesChanged(size); |
---|
248 | // Costs |
---|
249 | double val; |
---|
250 | for (int r = 0; r < 5; r++) |
---|
251 | for (int c = 0; c < 5; c++) |
---|
252 | if ((r != c) && (r < size)) { |
---|
253 | ds->readRawData(reinterpret_cast<char *>(&val),8); |
---|
254 | if (loadError(ds->status())) { |
---|
255 | clear(); |
---|
256 | return; |
---|
257 | } |
---|
258 | table[r][c] = val; |
---|
259 | } else { |
---|
260 | ds->skipRawData(8); |
---|
261 | if (loadError(ds->status())) { |
---|
262 | clear(); |
---|
263 | return; |
---|
264 | } |
---|
265 | } |
---|
266 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
267 | } |
---|
268 | |
---|
269 | bool CTSPModel::saveTask(QString fname) |
---|
270 | { |
---|
271 | QFile f(fname); |
---|
272 | if (!f.open(QIODevice::WriteOnly)) { |
---|
273 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),QString(trUtf8("Unable to create task file.\nError: %1\nMaybe, file is read-only?")).arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
274 | return false; |
---|
275 | } |
---|
276 | QDataStream ds(&f); |
---|
277 | ds.setVersion(QDataStream::Qt_4_4); |
---|
278 | if (f.error() != QFile::NoError) { |
---|
279 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
280 | f.close(); |
---|
281 | return false; |
---|
282 | } |
---|
283 | // File signature |
---|
284 | ds << TSPT; |
---|
285 | if (f.error() != QFile::NoError) { |
---|
286 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
287 | f.close(); |
---|
288 | return false; |
---|
289 | } |
---|
290 | // File version |
---|
291 | ds << TSPT_VERSION; |
---|
292 | if (f.error() != QFile::NoError) { |
---|
293 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
294 | f.close(); |
---|
295 | return false; |
---|
296 | } |
---|
297 | // File metadata version |
---|
298 | ds << TSPT_META_VERSION; |
---|
299 | if (f.error() != QFile::NoError) { |
---|
300 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
301 | f.close(); |
---|
302 | return false; |
---|
303 | } |
---|
304 | // Metadata |
---|
305 | ds << OSID; |
---|
306 | if (f.error() != QFile::NoError) { |
---|
307 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
308 | f.close(); |
---|
309 | return false; |
---|
310 | } |
---|
311 | // Number of cities |
---|
312 | ds << nCities; |
---|
313 | if (f.error() != QFile::NoError) { |
---|
314 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
315 | f.close(); |
---|
316 | return false; |
---|
317 | } |
---|
318 | // Costs |
---|
319 | for (int r = 0; r < nCities; r++) |
---|
320 | for (int c = 0; c < nCities; c++) |
---|
321 | if (r != c) { |
---|
322 | ds << table[r][c]; |
---|
323 | if (f.error() != QFile::NoError) { |
---|
324 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
325 | f.close(); |
---|
326 | return false; |
---|
327 | } |
---|
328 | } |
---|
329 | f.close(); |
---|
330 | return true; |
---|
331 | } |
---|
332 | |
---|
333 | void CTSPModel::randomize() |
---|
334 | { |
---|
335 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
336 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
337 | for (int r = 0; r < nCities; r++) |
---|
338 | for (int c = 0; c < nCities; c++) |
---|
339 | if (r != c) |
---|
340 | table[r][c] = rand(randMin,randMax); |
---|
341 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
342 | } |
---|