[2f915f19f2] | 1 | /* |
---|
[430bd7f7e9] | 2 | * TSPSG: TSP Solver and Generator |
---|
[5354a01311] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[2f915f19f2] | 4 | * |
---|
| 5 | * $Id$ |
---|
| 6 | * $URL$ |
---|
| 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 | |
---|
[bcfd415fe2] | 24 | #include "tspmodel.h" |
---|
[2f915f19f2] | 25 | |
---|
| 26 | CTSPModel::CTSPModel(QObject *parent) |
---|
[aecdf994f9] | 27 | : QAbstractTableModel(parent), nCities(0) |
---|
[2f915f19f2] | 28 | { |
---|
[665d32434f] | 29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[2f915f19f2] | 30 | } |
---|
| 31 | |
---|
[9b7064d770] | 32 | inline int CTSPModel::rand(int min, int max) const |
---|
[2f915f19f2] | 33 | { |
---|
| 34 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
| 35 | } |
---|
| 36 | |
---|
[4c96f94558] | 37 | int CTSPModel::rowCount(const QModelIndex &) const |
---|
[2f915f19f2] | 38 | { |
---|
| 39 | return nCities; |
---|
| 40 | } |
---|
| 41 | |
---|
[4c96f94558] | 42 | int CTSPModel::columnCount(const QModelIndex &) const |
---|
[2f915f19f2] | 43 | { |
---|
| 44 | return nCities; |
---|
| 45 | } |
---|
| 46 | |
---|
[5354a01311] | 47 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
[2f915f19f2] | 48 | { |
---|
[c02b4903bd] | 49 | if (role == Qt::DisplayRole) { |
---|
[5354a01311] | 50 | if (orientation == Qt::Vertical) |
---|
[5587b87fac] | 51 | return trUtf8("City %1").arg(section + 1); |
---|
[5354a01311] | 52 | else |
---|
| 53 | return trUtf8("%1").arg(section + 1); |
---|
[c02b4903bd] | 54 | } |
---|
[2f915f19f2] | 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); |
---|
[2bc8e278b7] | 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) { |
---|
[2f915f19f2] | 69 | if (index.row() < nCities && index.column() < nCities) |
---|
[2bc8e278b7] | 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(); |
---|
[2f915f19f2] | 75 | else |
---|
| 76 | return QVariant(); |
---|
[2bc8e278b7] | 77 | } else if (role == Qt::UserRole) |
---|
| 78 | return table[index.row()][index.column()]; |
---|
[2f915f19f2] | 79 | return QVariant(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 83 | { |
---|
[2bc8e278b7] | 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; |
---|
[2f915f19f2] | 101 | } |
---|
| 102 | |
---|
| 103 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 104 | { |
---|
| 105 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
[2bc8e278b7] | 106 | if (index.row() != index.column()) |
---|
[2f915f19f2] | 107 | flags |= Qt::ItemIsEditable; |
---|
| 108 | return flags; |
---|
| 109 | } |
---|
| 110 | |
---|
[993d5af6f6] | 111 | quint16 CTSPModel::numCities() const |
---|
[2f915f19f2] | 112 | { |
---|
| 113 | return nCities; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void CTSPModel::setNumCities(int n) |
---|
| 117 | { |
---|
[2bc8e278b7] | 118 | if (n == nCities) |
---|
| 119 | return; |
---|
| 120 | emit layoutAboutToBeChanged(); |
---|
[430bd7f7e9] | 121 | table.resize(n); |
---|
| 122 | for (int k = 0; k < n; k++) { |
---|
| 123 | table[k].resize(n); |
---|
[2f915f19f2] | 124 | } |
---|
[430bd7f7e9] | 125 | if (n > nCities) |
---|
| 126 | for (int k = nCities; k < n; k++) |
---|
| 127 | table[k][k] = INFINITY; |
---|
[2f915f19f2] | 128 | nCities = n; |
---|
[2bc8e278b7] | 129 | emit layoutChanged(); |
---|
| 130 | } |
---|
| 131 | |
---|
[899d1b8e15] | 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 | |
---|
[430bd7f7e9] | 141 | inline bool CTSPModel::loadError(QDataStream::Status status) |
---|
[9b7064d770] | 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 | |
---|
[b424a7e320] | 156 | bool CTSPModel::loadTask(QString fname) |
---|
[993d5af6f6] | 157 | { |
---|
| 158 | QFile f(fname); |
---|
[9b7064d770] | 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(); |
---|
[b424a7e320] | 161 | return false; |
---|
[9b7064d770] | 162 | } |
---|
[993d5af6f6] | 163 | QDataStream ds(&f); |
---|
| 164 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 165 | quint32 sig; |
---|
| 166 | ds >> sig; |
---|
[9b7064d770] | 167 | if (loadError(ds.status())) |
---|
[b424a7e320] | 168 | return false; |
---|
[993d5af6f6] | 169 | ds.device()->reset(); |
---|
| 170 | if (sig == TSPT) |
---|
[b424a7e320] | 171 | if (!loadTSPT(&ds)) { |
---|
| 172 | f.close(); |
---|
| 173 | return false; |
---|
| 174 | } |
---|
[993d5af6f6] | 175 | else if ((sig >> 16) == ZKT) |
---|
[b424a7e320] | 176 | if (!loadZKT(&ds)) { |
---|
| 177 | f.close(); |
---|
| 178 | return false; |
---|
| 179 | } |
---|
| 180 | else { |
---|
[9b7064d770] | 181 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); |
---|
[b424a7e320] | 182 | f.close(); |
---|
| 183 | return false; |
---|
| 184 | } |
---|
[993d5af6f6] | 185 | f.close(); |
---|
[b424a7e320] | 186 | return true; |
---|
[993d5af6f6] | 187 | } |
---|
| 188 | |
---|
[b424a7e320] | 189 | bool CTSPModel::loadTSPT(QDataStream *ds) |
---|
[993d5af6f6] | 190 | { |
---|
| 191 | // Skipping signature |
---|
| 192 | ds->skipRawData(sizeof(TSPT)); |
---|
[9b7064d770] | 193 | if (loadError(ds->status())) |
---|
[b424a7e320] | 194 | return false; |
---|
[993d5af6f6] | 195 | // File version |
---|
| 196 | quint8 version; |
---|
| 197 | *ds >> version; |
---|
[9b7064d770] | 198 | if (loadError(ds->status())) |
---|
[b424a7e320] | 199 | return false; |
---|
[993d5af6f6] | 200 | if (version > TSPT_VERSION) { |
---|
[9b7064d770] | 201 | 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(); |
---|
[b424a7e320] | 202 | return false; |
---|
[993d5af6f6] | 203 | } |
---|
| 204 | // Skipping metadata |
---|
| 205 | ds->skipRawData(TSPT_META_SIZE); |
---|
[9b7064d770] | 206 | if (loadError(ds->status())) |
---|
[b424a7e320] | 207 | return false; |
---|
[993d5af6f6] | 208 | // Cities number |
---|
| 209 | quint16 size; |
---|
| 210 | *ds >> size; |
---|
[9b7064d770] | 211 | if (loadError(ds->status())) |
---|
[b424a7e320] | 212 | return false; |
---|
[9b7064d770] | 213 | if (size < 3) { |
---|
| 214 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
[b424a7e320] | 215 | return false; |
---|
[9b7064d770] | 216 | } |
---|
[993d5af6f6] | 217 | if (nCities != size) |
---|
| 218 | emit numCitiesChanged(size); |
---|
| 219 | // Costs |
---|
| 220 | for (int r = 0; r < size; r++) |
---|
| 221 | for (int c = 0; c < size; c++) |
---|
[9b7064d770] | 222 | if (r != c) { |
---|
[993d5af6f6] | 223 | *ds >> table[r][c]; |
---|
[9b7064d770] | 224 | if (loadError(ds->status())) { |
---|
| 225 | clear(); |
---|
[b424a7e320] | 226 | return false; |
---|
[9b7064d770] | 227 | } |
---|
| 228 | } |
---|
[993d5af6f6] | 229 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[b424a7e320] | 230 | return true; |
---|
[993d5af6f6] | 231 | } |
---|
| 232 | |
---|
[b424a7e320] | 233 | bool CTSPModel::loadZKT(QDataStream *ds) |
---|
[993d5af6f6] | 234 | { |
---|
| 235 | // Skipping signature |
---|
| 236 | ds->skipRawData(sizeof(ZKT)); |
---|
[9b7064d770] | 237 | if (loadError(ds->status())) |
---|
[b424a7e320] | 238 | return false; |
---|
[993d5af6f6] | 239 | // File version |
---|
| 240 | quint16 version; |
---|
| 241 | ds->readRawData(reinterpret_cast<char *>(&version),2); |
---|
[9b7064d770] | 242 | if (loadError(ds->status())) |
---|
[b424a7e320] | 243 | return false; |
---|
[993d5af6f6] | 244 | if (version > ZKT_VERSION) { |
---|
[9b7064d770] | 245 | 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(); |
---|
[b424a7e320] | 246 | return false; |
---|
[993d5af6f6] | 247 | } |
---|
| 248 | // Cities number |
---|
| 249 | quint8 size; |
---|
| 250 | ds->readRawData(reinterpret_cast<char *>(&size),1); |
---|
[9b7064d770] | 251 | if (loadError(ds->status())) |
---|
[b424a7e320] | 252 | return false; |
---|
[9b7064d770] | 253 | if ((size < 3) || (size > 5)) { |
---|
| 254 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
[b424a7e320] | 255 | return false; |
---|
[9b7064d770] | 256 | } |
---|
[993d5af6f6] | 257 | if (nCities != size) |
---|
| 258 | emit numCitiesChanged(size); |
---|
| 259 | // Costs |
---|
| 260 | double val; |
---|
[430bd7f7e9] | 261 | for (int r = 0; r < 5; r++) |
---|
| 262 | for (int c = 0; c < 5; c++) |
---|
| 263 | if ((r != c) && (r < size)) { |
---|
[993d5af6f6] | 264 | ds->readRawData(reinterpret_cast<char *>(&val),8); |
---|
[9b7064d770] | 265 | if (loadError(ds->status())) { |
---|
| 266 | clear(); |
---|
[b424a7e320] | 267 | return false; |
---|
[9b7064d770] | 268 | } |
---|
[993d5af6f6] | 269 | table[r][c] = val; |
---|
[9b7064d770] | 270 | } else { |
---|
[993d5af6f6] | 271 | ds->skipRawData(8); |
---|
[9b7064d770] | 272 | if (loadError(ds->status())) { |
---|
| 273 | clear(); |
---|
[b424a7e320] | 274 | return false; |
---|
[9b7064d770] | 275 | } |
---|
| 276 | } |
---|
[993d5af6f6] | 277 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[b424a7e320] | 278 | return true; |
---|
[993d5af6f6] | 279 | } |
---|
| 280 | |
---|
[690f6939a7] | 281 | bool CTSPModel::saveTask(QString fname) |
---|
[993d5af6f6] | 282 | { |
---|
| 283 | QFile f(fname); |
---|
[9b7064d770] | 284 | if (!f.open(QIODevice::WriteOnly)) { |
---|
| 285 | 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(); |
---|
[690f6939a7] | 286 | return false; |
---|
[9b7064d770] | 287 | } |
---|
[993d5af6f6] | 288 | QDataStream ds(&f); |
---|
| 289 | ds.setVersion(QDataStream::Qt_4_4); |
---|
[9b7064d770] | 290 | if (f.error() != QFile::NoError) { |
---|
| 291 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 292 | f.close(); |
---|
[690f6939a7] | 293 | return false; |
---|
[9b7064d770] | 294 | } |
---|
[993d5af6f6] | 295 | // File signature |
---|
| 296 | ds << TSPT; |
---|
[9b7064d770] | 297 | if (f.error() != QFile::NoError) { |
---|
| 298 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 299 | f.close(); |
---|
[690f6939a7] | 300 | return false; |
---|
[9b7064d770] | 301 | } |
---|
[993d5af6f6] | 302 | // File version |
---|
| 303 | ds << TSPT_VERSION; |
---|
[9b7064d770] | 304 | if (f.error() != QFile::NoError) { |
---|
| 305 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 306 | f.close(); |
---|
[690f6939a7] | 307 | return false; |
---|
[9b7064d770] | 308 | } |
---|
[993d5af6f6] | 309 | // File metadata version |
---|
| 310 | ds << TSPT_META_VERSION; |
---|
[9b7064d770] | 311 | if (f.error() != QFile::NoError) { |
---|
| 312 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 313 | f.close(); |
---|
[690f6939a7] | 314 | return false; |
---|
[9b7064d770] | 315 | } |
---|
[993d5af6f6] | 316 | // Metadata |
---|
| 317 | ds << OSID; |
---|
[9b7064d770] | 318 | if (f.error() != QFile::NoError) { |
---|
| 319 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 320 | f.close(); |
---|
[690f6939a7] | 321 | return false; |
---|
[9b7064d770] | 322 | } |
---|
[993d5af6f6] | 323 | // Number of cities |
---|
| 324 | ds << nCities; |
---|
[9b7064d770] | 325 | if (f.error() != QFile::NoError) { |
---|
| 326 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 327 | f.close(); |
---|
[690f6939a7] | 328 | return false; |
---|
[9b7064d770] | 329 | } |
---|
[993d5af6f6] | 330 | // Costs |
---|
| 331 | for (int r = 0; r < nCities; r++) |
---|
| 332 | for (int c = 0; c < nCities; c++) |
---|
[9b7064d770] | 333 | if (r != c) { |
---|
[993d5af6f6] | 334 | ds << table[r][c]; |
---|
[9b7064d770] | 335 | if (f.error() != QFile::NoError) { |
---|
| 336 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 337 | f.close(); |
---|
[690f6939a7] | 338 | return false; |
---|
[9b7064d770] | 339 | } |
---|
| 340 | } |
---|
[993d5af6f6] | 341 | f.close(); |
---|
[690f6939a7] | 342 | return true; |
---|
[993d5af6f6] | 343 | } |
---|
| 344 | |
---|
[2bc8e278b7] | 345 | void CTSPModel::randomize() |
---|
| 346 | { |
---|
[aecdf994f9] | 347 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 348 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[2bc8e278b7] | 349 | for (int r = 0; r < nCities; r++) |
---|
| 350 | for (int c = 0; c < nCities; c++) |
---|
| 351 | if (r != c) |
---|
| 352 | table[r][c] = rand(randMin,randMax); |
---|
| 353 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[2f915f19f2] | 354 | } |
---|