[2f915f19f2] | 1 | /* |
---|
| 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(); |
---|
[2f915f19f2] | 121 | if (n > nCities) { |
---|
[2bc8e278b7] | 122 | for (int r = 0; r < nCities; r++) { |
---|
| 123 | for (int c = nCities; c < n; c++) |
---|
| 124 | if (r == c) |
---|
| 125 | table[r][c] = INFINITY; |
---|
| 126 | else |
---|
[9b7064d770] | 127 | table[r][c] = 0; |
---|
[2f915f19f2] | 128 | } |
---|
[2bc8e278b7] | 129 | for (int r = nCities; r < n; r++) { |
---|
| 130 | for (int c = 0; c < n; c++) |
---|
| 131 | if (r == c) |
---|
| 132 | table[r][c] = INFINITY; |
---|
| 133 | else |
---|
[9b7064d770] | 134 | table[r][c] = 0; |
---|
[2f915f19f2] | 135 | } |
---|
| 136 | } |
---|
| 137 | nCities = n; |
---|
[2bc8e278b7] | 138 | emit layoutChanged(); |
---|
| 139 | } |
---|
| 140 | |
---|
[899d1b8e15] | 141 | void CTSPModel::clear() |
---|
| 142 | { |
---|
| 143 | for (int r = 0; r < nCities; r++) |
---|
| 144 | for (int c = 0; c < nCities; c++) |
---|
| 145 | if (r != c) |
---|
| 146 | table[r][c] = 0; |
---|
| 147 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 148 | } |
---|
| 149 | |
---|
[9b7064d770] | 150 | inline bool CTSPModel::loadError(QDataStream::Status status) const |
---|
| 151 | { |
---|
| 152 | QString err; |
---|
| 153 | if (status == QDataStream::Ok) |
---|
| 154 | return false; |
---|
| 155 | else if (status == QDataStream::ReadPastEnd) |
---|
| 156 | err = trUtf8("Unexpected end of file."); |
---|
| 157 | else if (status == QDataStream::ReadCorruptData) |
---|
| 158 | err = trUtf8("Corrupt data read. File possibly corrupted."); |
---|
| 159 | else |
---|
| 160 | err = trUtf8("Unknown error."); |
---|
| 161 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec(); |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | |
---|
[993d5af6f6] | 165 | void CTSPModel::loadTask(QString fname) |
---|
| 166 | { |
---|
| 167 | QFile f(fname); |
---|
[9b7064d770] | 168 | if (!f.open(QIODevice::ReadOnly)) { |
---|
| 169 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 170 | return; |
---|
| 171 | } |
---|
[993d5af6f6] | 172 | QDataStream ds(&f); |
---|
| 173 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 174 | quint32 sig; |
---|
| 175 | ds >> sig; |
---|
[9b7064d770] | 176 | if (loadError(ds.status())) |
---|
| 177 | return; |
---|
[993d5af6f6] | 178 | ds.device()->reset(); |
---|
| 179 | if (sig == TSPT) |
---|
| 180 | loadTSPT(&ds); |
---|
| 181 | else if ((sig >> 16) == ZKT) |
---|
| 182 | loadZKT(&ds); |
---|
| 183 | else |
---|
[9b7064d770] | 184 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); |
---|
[993d5af6f6] | 185 | f.close(); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void CTSPModel::loadTSPT(QDataStream *ds) |
---|
| 189 | { |
---|
| 190 | // Skipping signature |
---|
| 191 | ds->skipRawData(sizeof(TSPT)); |
---|
[9b7064d770] | 192 | if (loadError(ds->status())) |
---|
| 193 | return; |
---|
[993d5af6f6] | 194 | // File version |
---|
| 195 | quint8 version; |
---|
| 196 | *ds >> version; |
---|
[9b7064d770] | 197 | if (loadError(ds->status())) |
---|
| 198 | return; |
---|
[993d5af6f6] | 199 | if (version > TSPT_VERSION) { |
---|
[9b7064d770] | 200 | 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(); |
---|
[993d5af6f6] | 201 | return; |
---|
| 202 | } |
---|
| 203 | // Skipping metadata |
---|
| 204 | ds->skipRawData(TSPT_META_SIZE); |
---|
[9b7064d770] | 205 | if (loadError(ds->status())) |
---|
| 206 | return; |
---|
[993d5af6f6] | 207 | // Cities number |
---|
| 208 | quint16 size; |
---|
| 209 | *ds >> size; |
---|
[9b7064d770] | 210 | if (loadError(ds->status())) |
---|
| 211 | return; |
---|
| 212 | if (size < 3) { |
---|
| 213 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
| 214 | return; |
---|
| 215 | } |
---|
[993d5af6f6] | 216 | if (nCities != size) |
---|
| 217 | emit numCitiesChanged(size); |
---|
| 218 | // Costs |
---|
| 219 | for (int r = 0; r < size; r++) |
---|
| 220 | for (int c = 0; c < size; c++) |
---|
[9b7064d770] | 221 | if (r != c) { |
---|
[993d5af6f6] | 222 | *ds >> table[r][c]; |
---|
[9b7064d770] | 223 | if (loadError(ds->status())) { |
---|
| 224 | clear(); |
---|
| 225 | return; |
---|
| 226 | } |
---|
| 227 | } |
---|
[993d5af6f6] | 228 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | void CTSPModel::loadZKT(QDataStream *ds) |
---|
| 232 | { |
---|
| 233 | // Skipping signature |
---|
| 234 | ds->skipRawData(sizeof(ZKT)); |
---|
[9b7064d770] | 235 | if (loadError(ds->status())) |
---|
| 236 | return; |
---|
[993d5af6f6] | 237 | // File version |
---|
| 238 | quint16 version; |
---|
| 239 | ds->readRawData(reinterpret_cast<char *>(&version),2); |
---|
[9b7064d770] | 240 | if (loadError(ds->status())) |
---|
| 241 | return; |
---|
[993d5af6f6] | 242 | if (version > ZKT_VERSION) { |
---|
[9b7064d770] | 243 | 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(); |
---|
[993d5af6f6] | 244 | return; |
---|
| 245 | } |
---|
| 246 | // Cities number |
---|
| 247 | quint8 size; |
---|
| 248 | ds->readRawData(reinterpret_cast<char *>(&size),1); |
---|
[9b7064d770] | 249 | if (loadError(ds->status())) |
---|
| 250 | return; |
---|
| 251 | if ((size < 3) || (size > 5)) { |
---|
| 252 | QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
| 253 | return; |
---|
| 254 | } |
---|
[993d5af6f6] | 255 | if (nCities != size) |
---|
| 256 | emit numCitiesChanged(size); |
---|
| 257 | // Costs |
---|
| 258 | double val; |
---|
| 259 | for (int r = 0; r < size; r++) |
---|
| 260 | for (int c = 0; c < size; c++) |
---|
| 261 | if (r != c) { |
---|
| 262 | ds->readRawData(reinterpret_cast<char *>(&val),8); |
---|
[9b7064d770] | 263 | if (loadError(ds->status())) { |
---|
| 264 | clear(); |
---|
| 265 | return; |
---|
| 266 | } |
---|
[993d5af6f6] | 267 | table[r][c] = val; |
---|
[9b7064d770] | 268 | } else { |
---|
[993d5af6f6] | 269 | ds->skipRawData(8); |
---|
[9b7064d770] | 270 | if (loadError(ds->status())) { |
---|
| 271 | clear(); |
---|
| 272 | return; |
---|
| 273 | } |
---|
| 274 | } |
---|
[993d5af6f6] | 275 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 276 | } |
---|
| 277 | |
---|
[690f6939a7] | 278 | bool CTSPModel::saveTask(QString fname) |
---|
[993d5af6f6] | 279 | { |
---|
| 280 | QFile f(fname); |
---|
[9b7064d770] | 281 | if (!f.open(QIODevice::WriteOnly)) { |
---|
| 282 | 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] | 283 | return false; |
---|
[9b7064d770] | 284 | } |
---|
[993d5af6f6] | 285 | QDataStream ds(&f); |
---|
| 286 | ds.setVersion(QDataStream::Qt_4_4); |
---|
[9b7064d770] | 287 | if (f.error() != QFile::NoError) { |
---|
| 288 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 289 | f.close(); |
---|
[690f6939a7] | 290 | return false; |
---|
[9b7064d770] | 291 | } |
---|
[993d5af6f6] | 292 | // File signature |
---|
| 293 | ds << TSPT; |
---|
[9b7064d770] | 294 | if (f.error() != QFile::NoError) { |
---|
| 295 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 296 | f.close(); |
---|
[690f6939a7] | 297 | return false; |
---|
[9b7064d770] | 298 | } |
---|
[993d5af6f6] | 299 | // File version |
---|
| 300 | ds << TSPT_VERSION; |
---|
[9b7064d770] | 301 | if (f.error() != QFile::NoError) { |
---|
| 302 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 303 | f.close(); |
---|
[690f6939a7] | 304 | return false; |
---|
[9b7064d770] | 305 | } |
---|
[993d5af6f6] | 306 | // File metadata version |
---|
| 307 | ds << TSPT_META_VERSION; |
---|
[9b7064d770] | 308 | if (f.error() != QFile::NoError) { |
---|
| 309 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 310 | f.close(); |
---|
[690f6939a7] | 311 | return false; |
---|
[9b7064d770] | 312 | } |
---|
[993d5af6f6] | 313 | // Metadata |
---|
| 314 | ds << OSID; |
---|
[9b7064d770] | 315 | if (f.error() != QFile::NoError) { |
---|
| 316 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 317 | f.close(); |
---|
[690f6939a7] | 318 | return false; |
---|
[9b7064d770] | 319 | } |
---|
[993d5af6f6] | 320 | // Number of cities |
---|
| 321 | ds << nCities; |
---|
[9b7064d770] | 322 | if (f.error() != QFile::NoError) { |
---|
| 323 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 324 | f.close(); |
---|
[690f6939a7] | 325 | return false; |
---|
[9b7064d770] | 326 | } |
---|
[993d5af6f6] | 327 | // Costs |
---|
| 328 | for (int r = 0; r < nCities; r++) |
---|
| 329 | for (int c = 0; c < nCities; c++) |
---|
[9b7064d770] | 330 | if (r != c) { |
---|
[993d5af6f6] | 331 | ds << table[r][c]; |
---|
[9b7064d770] | 332 | if (f.error() != QFile::NoError) { |
---|
| 333 | QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
| 334 | f.close(); |
---|
[690f6939a7] | 335 | return false; |
---|
[9b7064d770] | 336 | } |
---|
| 337 | } |
---|
[993d5af6f6] | 338 | f.close(); |
---|
[690f6939a7] | 339 | return true; |
---|
[993d5af6f6] | 340 | } |
---|
| 341 | |
---|
[2bc8e278b7] | 342 | void CTSPModel::randomize() |
---|
| 343 | { |
---|
[aecdf994f9] | 344 | int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); |
---|
| 345 | int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); |
---|
[2bc8e278b7] | 346 | for (int r = 0; r < nCities; r++) |
---|
| 347 | for (int c = 0; c < nCities; c++) |
---|
| 348 | if (r != c) |
---|
| 349 | table[r][c] = rand(randMin,randMax); |
---|
| 350 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[2f915f19f2] | 351 | } |
---|