[14] | 1 | /* |
---|
[42] | 2 | * TSPSG: TSP Solver and Generator |
---|
[87] | 3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
[14] | 4 | * |
---|
| 5 | * $Id: tspmodel.cpp 89 2010-01-12 15:27:52Z 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 | |
---|
[67] | 26 | /*! |
---|
| 27 | * \brief Class constructor. |
---|
| 28 | * \param parent The parent of the table model. |
---|
| 29 | */ |
---|
[14] | 30 | CTSPModel::CTSPModel(QObject *parent) |
---|
[21] | 31 | : QAbstractTableModel(parent), nCities(0) |
---|
[14] | 32 | { |
---|
[80] | 33 | settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this); |
---|
[14] | 34 | } |
---|
| 35 | |
---|
[67] | 36 | /*! |
---|
| 37 | * \brief Resets the table, setting all its elements to 0. |
---|
| 38 | * |
---|
| 39 | * \sa randomize() |
---|
| 40 | */ |
---|
| 41 | void CTSPModel::clear() |
---|
[14] | 42 | { |
---|
[67] | 43 | for (int r = 0; r < nCities; r++) |
---|
| 44 | for (int c = 0; c < nCities; c++) |
---|
| 45 | if (r != c) |
---|
| 46 | table[r][c] = 0; |
---|
| 47 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[14] | 48 | } |
---|
| 49 | |
---|
[67] | 50 | /*! |
---|
| 51 | * \brief Returns the column count in the table. |
---|
| 52 | * \return Number of columns in the table. |
---|
| 53 | * |
---|
| 54 | * Actually, this function returns the number of cities in the current task. |
---|
| 55 | * |
---|
| 56 | * \sa numCities(), rowCount() |
---|
| 57 | */ |
---|
[36] | 58 | int CTSPModel::columnCount(const QModelIndex &) const |
---|
[14] | 59 | { |
---|
| 60 | return nCities; |
---|
| 61 | } |
---|
| 62 | |
---|
[67] | 63 | /*! |
---|
| 64 | * \brief Returns the data stored under the given \a role for the item referred to by the \a index. |
---|
| 65 | * \param index An item index to get data from. |
---|
| 66 | * \param role The role to get data for. |
---|
| 67 | * \return Corresponding data. |
---|
| 68 | * |
---|
| 69 | * \sa setData(), headerData() |
---|
| 70 | */ |
---|
[14] | 71 | QVariant CTSPModel::data(const QModelIndex &index, int role) const |
---|
| 72 | { |
---|
| 73 | if (!index.isValid()) |
---|
| 74 | return QVariant(); |
---|
| 75 | if (role == Qt::TextAlignmentRole) |
---|
| 76 | return int(Qt::AlignCenter); |
---|
[15] | 77 | else if (role == Qt::FontRole) { |
---|
| 78 | QFont font; |
---|
| 79 | font.setBold(true); |
---|
| 80 | return font; |
---|
| 81 | } else if (role == Qt::DisplayRole || role == Qt::EditRole) { |
---|
[14] | 82 | if (index.row() < nCities && index.column() < nCities) |
---|
[15] | 83 | if (table[index.row()][index.column()] == INFINITY) |
---|
[87] | 84 | return tr(INFSTR); |
---|
[15] | 85 | else |
---|
[67] | 86 | //! \hack HACK: Converting to string to prevent spinbox in edit mode |
---|
[15] | 87 | return QVariant(table[index.row()][index.column()]).toString(); |
---|
[14] | 88 | else |
---|
| 89 | return QVariant(); |
---|
[15] | 90 | } else if (role == Qt::UserRole) |
---|
| 91 | return table[index.row()][index.column()]; |
---|
[14] | 92 | return QVariant(); |
---|
| 93 | } |
---|
| 94 | |
---|
[67] | 95 | /*! |
---|
| 96 | * \brief Returns the item flags for the given \a index. |
---|
| 97 | * \param index An item index to get flags from. |
---|
| 98 | * \return Corresponding item flags. |
---|
| 99 | */ |
---|
| 100 | Qt::ItemFlags CTSPModel::flags(const QModelIndex &index) const |
---|
| 101 | { |
---|
| 102 | Qt::ItemFlags flags = QAbstractItemModel::flags(index); |
---|
| 103 | if (index.row() != index.column()) |
---|
| 104 | flags |= Qt::ItemIsEditable; |
---|
| 105 | return flags; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /*! |
---|
| 109 | * \brief Returns the data for the given \a role and \a section in the header with the specified \a orientation. |
---|
| 110 | * \param section The section to get header data for. |
---|
| 111 | * \param orientation The orientation to get header data for. |
---|
| 112 | * \param role The role to get header data for. |
---|
| 113 | * \return Corresponding header data. |
---|
| 114 | * |
---|
| 115 | * For horizontal headers, the section number corresponds to the column number of items shown beneath it. For vertical headers, the section number typically to the row number of items shown alongside it. |
---|
| 116 | */ |
---|
| 117 | QVariant CTSPModel::headerData(int section, Qt::Orientation orientation, int role) const |
---|
| 118 | { |
---|
| 119 | if (role == Qt::DisplayRole) { |
---|
| 120 | if (orientation == Qt::Vertical) |
---|
[87] | 121 | return tr("City %1").arg(section + 1); |
---|
[67] | 122 | else |
---|
[87] | 123 | return tr("%1").arg(section + 1); |
---|
[67] | 124 | } |
---|
| 125 | return QVariant(); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /*! |
---|
| 129 | * \brief Loads a task from \a fname. |
---|
| 130 | * \param fname The name of the file to be loaded. |
---|
| 131 | * \return \c true on success, otherwise \c false. |
---|
| 132 | * |
---|
| 133 | * \sa saveTask() |
---|
| 134 | */ |
---|
[71] | 135 | bool CTSPModel::loadTask(const QString &fname) |
---|
[67] | 136 | { |
---|
| 137 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 138 | QFile f(fname); |
---|
| 139 | if (!f.open(QIODevice::ReadOnly)) { |
---|
| 140 | QApplication::restoreOverrideCursor(); |
---|
[87] | 141 | QMessageBox(QMessageBox::Critical,tr("Task Load"),QString(tr("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 142 | return false; |
---|
| 143 | } |
---|
| 144 | QDataStream ds(&f); |
---|
| 145 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 146 | quint32 sig; |
---|
| 147 | ds >> sig; |
---|
| 148 | if (loadError(ds.status())) { |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | ds.device()->reset(); |
---|
| 152 | if (sig == TSPT) { |
---|
| 153 | if (!loadTSPT(&ds)) { |
---|
| 154 | f.close(); |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | } else if ((sig >> 16) == ZKT) { |
---|
| 158 | if (!loadZKT(&ds)) { |
---|
| 159 | f.close(); |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | } else { |
---|
| 163 | f.close(); |
---|
| 164 | QApplication::restoreOverrideCursor(); |
---|
[87] | 165 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); |
---|
[67] | 166 | return false; |
---|
| 167 | } |
---|
| 168 | f.close(); |
---|
| 169 | QApplication::restoreOverrideCursor(); |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /*! |
---|
| 174 | * \brief Returns the number of cities. |
---|
| 175 | * \return Number of cities in the current task. |
---|
| 176 | * |
---|
| 177 | * \sa columnCount(), rowCount(), setNumCities() |
---|
| 178 | */ |
---|
| 179 | quint16 CTSPModel::numCities() const |
---|
| 180 | { |
---|
| 181 | return nCities; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /*! |
---|
| 185 | * \brief Randomizes the table by setting all its values to random ones. |
---|
| 186 | * |
---|
| 187 | * Uses TSPSG settings to determine random values range. |
---|
| 188 | * |
---|
| 189 | * \sa clear() |
---|
| 190 | */ |
---|
| 191 | void CTSPModel::randomize() |
---|
| 192 | { |
---|
[87] | 193 | int randMin = settings->value("Task/RandMin",DEF_RAND_MIN).toInt(); |
---|
| 194 | int randMax = settings->value("Task/RandMax",DEF_RAND_MAX).toInt(); |
---|
[67] | 195 | for (int r = 0; r < nCities; r++) |
---|
| 196 | for (int c = 0; c < nCities; c++) |
---|
| 197 | if (r != c) |
---|
| 198 | table[r][c] = rand(randMin,randMax); |
---|
| 199 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /*! |
---|
| 203 | * \brief Returns the row count in the table. |
---|
| 204 | * \return Number of rows in the table. |
---|
| 205 | * |
---|
| 206 | * Actually, this function returns the number of cities in the current task. |
---|
| 207 | * |
---|
| 208 | * \sa columnCount(), numCities() |
---|
| 209 | */ |
---|
| 210 | int CTSPModel::rowCount(const QModelIndex &) const |
---|
| 211 | { |
---|
| 212 | return nCities; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /*! |
---|
| 216 | * \brief Saves current task to \a fname. |
---|
| 217 | * \param fname The name of the file to seve to. |
---|
| 218 | * \return \c true on success, otherwise \c false. |
---|
| 219 | * |
---|
| 220 | * \sa loadTask() |
---|
| 221 | */ |
---|
[71] | 222 | bool CTSPModel::saveTask(const QString &fname) |
---|
[67] | 223 | { |
---|
| 224 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 225 | QFile f(fname); |
---|
| 226 | if (!f.open(QIODevice::WriteOnly)) { |
---|
| 227 | QApplication::restoreOverrideCursor(); |
---|
[87] | 228 | QMessageBox(QMessageBox::Critical,tr("Task Save"),QString(tr("Unable to create task file.\nError: %1\nMaybe, file is read-only?")).arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 229 | return false; |
---|
| 230 | } |
---|
| 231 | QDataStream ds(&f); |
---|
| 232 | ds.setVersion(QDataStream::Qt_4_4); |
---|
| 233 | if (f.error() != QFile::NoError) { |
---|
| 234 | f.close(); |
---|
| 235 | QApplication::restoreOverrideCursor(); |
---|
[87] | 236 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 237 | return false; |
---|
| 238 | } |
---|
| 239 | // File signature |
---|
| 240 | ds << TSPT; |
---|
| 241 | if (f.error() != QFile::NoError) { |
---|
| 242 | f.close(); |
---|
| 243 | QApplication::restoreOverrideCursor(); |
---|
[87] | 244 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 245 | return false; |
---|
| 246 | } |
---|
| 247 | // File version |
---|
| 248 | ds << TSPT_VERSION; |
---|
| 249 | if (f.error() != QFile::NoError) { |
---|
| 250 | f.close(); |
---|
| 251 | QApplication::restoreOverrideCursor(); |
---|
[87] | 252 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 253 | return false; |
---|
| 254 | } |
---|
| 255 | // File metadata version |
---|
| 256 | ds << TSPT_META_VERSION; |
---|
| 257 | if (f.error() != QFile::NoError) { |
---|
| 258 | f.close(); |
---|
| 259 | QApplication::restoreOverrideCursor(); |
---|
[87] | 260 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 261 | return false; |
---|
| 262 | } |
---|
| 263 | // Metadata |
---|
| 264 | ds << OSID; |
---|
| 265 | if (f.error() != QFile::NoError) { |
---|
| 266 | f.close(); |
---|
| 267 | QApplication::restoreOverrideCursor(); |
---|
[87] | 268 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 269 | return false; |
---|
| 270 | } |
---|
| 271 | // Number of cities |
---|
| 272 | ds << nCities; |
---|
| 273 | if (f.error() != QFile::NoError) { |
---|
| 274 | f.close(); |
---|
| 275 | QApplication::restoreOverrideCursor(); |
---|
[87] | 276 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 277 | return false; |
---|
| 278 | } |
---|
| 279 | // Costs |
---|
| 280 | for (int r = 0; r < nCities; r++) |
---|
| 281 | for (int c = 0; c < nCities; c++) |
---|
| 282 | if (r != c) { |
---|
[89] | 283 | ds << static_cast<double>(table[r][c]); // We cast to double because double may be float on some platforms and we store double values in file |
---|
[67] | 284 | if (f.error() != QFile::NoError) { |
---|
| 285 | f.close(); |
---|
| 286 | QApplication::restoreOverrideCursor(); |
---|
[87] | 287 | QMessageBox(QMessageBox::Critical,tr("Task Save"),tr("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); |
---|
[67] | 288 | return false; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | f.close(); |
---|
| 292 | QApplication::restoreOverrideCursor(); |
---|
| 293 | return true; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /*! |
---|
| 297 | * \brief Sets the \a role data for the item at \a index to \a value. |
---|
| 298 | * \param index The index of the item to set data at. |
---|
| 299 | * \param value The value of the item data to be set. |
---|
| 300 | * \param role The role of the item to set data for. |
---|
| 301 | * \return \c true on success, otherwise \c false. |
---|
| 302 | * |
---|
| 303 | * \sa data() |
---|
| 304 | */ |
---|
[14] | 305 | bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) |
---|
| 306 | { |
---|
[15] | 307 | if (!index.isValid()) |
---|
| 308 | return false; |
---|
| 309 | if (role == Qt::EditRole && index.row() != index.column()) { |
---|
| 310 | if (value.toString().compare(INFSTR) == 0) |
---|
| 311 | table[index.row()][index.column()] = INFINITY; |
---|
| 312 | else { |
---|
| 313 | bool ok; |
---|
[89] | 314 | double tmp = value.toDouble(&ok); |
---|
[15] | 315 | if (!ok || tmp < 0) |
---|
| 316 | return false; |
---|
| 317 | else |
---|
| 318 | table[index.row()][index.column()] = tmp; |
---|
| 319 | } |
---|
| 320 | emit dataChanged(index,index); |
---|
| 321 | return true; |
---|
| 322 | } |
---|
| 323 | return false; |
---|
[14] | 324 | } |
---|
| 325 | |
---|
[67] | 326 | /*! |
---|
| 327 | * \brief Sets number of cities in the current task to \a n. |
---|
| 328 | * \param n Number of cities to set to. |
---|
| 329 | * |
---|
| 330 | * \sa numCities() |
---|
| 331 | */ |
---|
[14] | 332 | void CTSPModel::setNumCities(int n) |
---|
| 333 | { |
---|
[15] | 334 | if (n == nCities) |
---|
| 335 | return; |
---|
| 336 | emit layoutAboutToBeChanged(); |
---|
[42] | 337 | table.resize(n); |
---|
| 338 | for (int k = 0; k < n; k++) { |
---|
| 339 | table[k].resize(n); |
---|
[14] | 340 | } |
---|
[42] | 341 | if (n > nCities) |
---|
| 342 | for (int k = nCities; k < n; k++) |
---|
| 343 | table[k][k] = INFINITY; |
---|
[14] | 344 | nCities = n; |
---|
[15] | 345 | emit layoutChanged(); |
---|
[14] | 346 | } |
---|
| 347 | |
---|
[67] | 348 | /* Privates **********************************************************/ |
---|
[29] | 349 | |
---|
[42] | 350 | inline bool CTSPModel::loadError(QDataStream::Status status) |
---|
[35] | 351 | { |
---|
| 352 | QString err; |
---|
| 353 | if (status == QDataStream::Ok) |
---|
| 354 | return false; |
---|
| 355 | else if (status == QDataStream::ReadPastEnd) |
---|
[87] | 356 | err = tr("Unexpected end of file."); |
---|
[35] | 357 | else if (status == QDataStream::ReadCorruptData) |
---|
[87] | 358 | err = tr("Corrupt data read. File possibly corrupted."); |
---|
[35] | 359 | else |
---|
[87] | 360 | err = tr("Unknown error."); |
---|
[59] | 361 | QApplication::restoreOverrideCursor(); |
---|
[87] | 362 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec(); |
---|
[35] | 363 | return true; |
---|
| 364 | } |
---|
| 365 | |
---|
[47] | 366 | bool CTSPModel::loadTSPT(QDataStream *ds) |
---|
[31] | 367 | { |
---|
| 368 | // Skipping signature |
---|
| 369 | ds->skipRawData(sizeof(TSPT)); |
---|
[35] | 370 | if (loadError(ds->status())) |
---|
[47] | 371 | return false; |
---|
[31] | 372 | // File version |
---|
| 373 | quint8 version; |
---|
| 374 | *ds >> version; |
---|
[35] | 375 | if (loadError(ds->status())) |
---|
[47] | 376 | return false; |
---|
[31] | 377 | if (version > TSPT_VERSION) { |
---|
[59] | 378 | QApplication::restoreOverrideCursor(); |
---|
[87] | 379 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
[47] | 380 | return false; |
---|
[31] | 381 | } |
---|
| 382 | // Skipping metadata |
---|
| 383 | ds->skipRawData(TSPT_META_SIZE); |
---|
[35] | 384 | if (loadError(ds->status())) |
---|
[47] | 385 | return false; |
---|
[50] | 386 | // Number of cities |
---|
[31] | 387 | quint16 size; |
---|
| 388 | *ds >> size; |
---|
[35] | 389 | if (loadError(ds->status())) |
---|
[47] | 390 | return false; |
---|
[50] | 391 | if ((size < 3) || (size > MAX_NUM_CITIES)) { |
---|
[59] | 392 | QApplication::restoreOverrideCursor(); |
---|
[87] | 393 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
[47] | 394 | return false; |
---|
[35] | 395 | } |
---|
[50] | 396 | if (nCities != size) { |
---|
| 397 | setNumCities(size); |
---|
[31] | 398 | emit numCitiesChanged(size); |
---|
[50] | 399 | } |
---|
[87] | 400 | |
---|
[89] | 401 | double x; // We need this as double may be float on some platforms and we store double values in file |
---|
[50] | 402 | // Travel costs |
---|
[31] | 403 | for (int r = 0; r < size; r++) |
---|
| 404 | for (int c = 0; c < size; c++) |
---|
[35] | 405 | if (r != c) { |
---|
[87] | 406 | *ds >> x; |
---|
| 407 | table[r][c] = x; |
---|
[35] | 408 | if (loadError(ds->status())) { |
---|
| 409 | clear(); |
---|
[47] | 410 | return false; |
---|
[35] | 411 | } |
---|
| 412 | } |
---|
[31] | 413 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[59] | 414 | QApplication::restoreOverrideCursor(); |
---|
[47] | 415 | return true; |
---|
[31] | 416 | } |
---|
| 417 | |
---|
[47] | 418 | bool CTSPModel::loadZKT(QDataStream *ds) |
---|
[31] | 419 | { |
---|
| 420 | // Skipping signature |
---|
| 421 | ds->skipRawData(sizeof(ZKT)); |
---|
[35] | 422 | if (loadError(ds->status())) |
---|
[47] | 423 | return false; |
---|
[31] | 424 | // File version |
---|
| 425 | quint16 version; |
---|
| 426 | ds->readRawData(reinterpret_cast<char *>(&version),2); |
---|
[35] | 427 | if (loadError(ds->status())) |
---|
[47] | 428 | return false; |
---|
[31] | 429 | if (version > ZKT_VERSION) { |
---|
[59] | 430 | QApplication::restoreOverrideCursor(); |
---|
[87] | 431 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("File version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec(); |
---|
[47] | 432 | return false; |
---|
[31] | 433 | } |
---|
[50] | 434 | // Number of cities |
---|
[31] | 435 | quint8 size; |
---|
| 436 | ds->readRawData(reinterpret_cast<char *>(&size),1); |
---|
[35] | 437 | if (loadError(ds->status())) |
---|
[47] | 438 | return false; |
---|
[35] | 439 | if ((size < 3) || (size > 5)) { |
---|
[59] | 440 | QApplication::restoreOverrideCursor(); |
---|
[87] | 441 | QMessageBox(QMessageBox::Critical,tr("Task Load"),tr("Unable to load task:") + "\n" + tr("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); |
---|
[47] | 442 | return false; |
---|
[35] | 443 | } |
---|
[50] | 444 | if (nCities != size) { |
---|
| 445 | setNumCities(size); |
---|
[31] | 446 | emit numCitiesChanged(size); |
---|
[50] | 447 | } |
---|
| 448 | // Travel costs |
---|
[89] | 449 | double val; |
---|
[42] | 450 | for (int r = 0; r < 5; r++) |
---|
| 451 | for (int c = 0; c < 5; c++) |
---|
[87] | 452 | if ((r != c) && (r < size) && (c < size)) { |
---|
[31] | 453 | ds->readRawData(reinterpret_cast<char *>(&val),8); |
---|
[35] | 454 | if (loadError(ds->status())) { |
---|
| 455 | clear(); |
---|
[47] | 456 | return false; |
---|
[35] | 457 | } |
---|
[31] | 458 | table[r][c] = val; |
---|
[35] | 459 | } else { |
---|
[31] | 460 | ds->skipRawData(8); |
---|
[35] | 461 | if (loadError(ds->status())) { |
---|
| 462 | clear(); |
---|
[47] | 463 | return false; |
---|
[35] | 464 | } |
---|
| 465 | } |
---|
[31] | 466 | emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); |
---|
[59] | 467 | QApplication::restoreOverrideCursor(); |
---|
[47] | 468 | return true; |
---|
[31] | 469 | } |
---|
| 470 | |
---|
[89] | 471 | inline double CTSPModel::rand(int min, int max) const |
---|
[31] | 472 | { |
---|
[89] | 473 | double r; |
---|
[87] | 474 | if (settings->value("Task/FractionalRandom", DEF_FRACTIONAL_RANDOM).toBool()) { |
---|
[89] | 475 | double x = qPow(10, settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 476 | r = (double)qRound((double)qrand() / RAND_MAX * (max - min) * x) / x; |
---|
[87] | 477 | } else |
---|
[89] | 478 | r = qRound((double)qrand() / RAND_MAX * (max - min)); |
---|
[81] | 479 | return min + r; |
---|
[31] | 480 | } |
---|