Changeset e0fcac5f2c in tspsg for src/tspmodel.cpp
- Timestamp:
- Oct 24, 2009, 3:37:48 PM (15 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- 8af59c4aa3
- Parents:
- 85ad815b0b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tspmodel.cpp
r85ad815b0b re0fcac5f2c 24 24 #include "tspmodel.h" 25 25 26 /*! 27 * \brief Class constructor. 28 * \param parent The parent of the table model. 29 */ 26 30 CTSPModel::CTSPModel(QObject *parent) 27 31 : QAbstractTableModel(parent), nCities(0) … … 30 34 } 31 35 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 36 /*! 37 * \brief Resets the table, setting all its elements to 0. 38 * 39 * \sa randomize() 40 */ 41 void CTSPModel::clear() 42 { 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)); 48 } 49 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 */ 58 int CTSPModel::columnCount(const QModelIndex &) const 38 59 { 39 60 return nCities; 40 61 } 41 62 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 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 */ 58 71 QVariant CTSPModel::data(const QModelIndex &index, int role) const 59 72 { … … 71 84 return trUtf8(INFSTR); 72 85 else 73 //HACK: Converting to string to prevent spinbox in edit mode86 //! \hack HACK: Converting to string to prevent spinbox in edit mode 74 87 return QVariant(table[index.row()][index.column()]).toString(); 75 88 else … … 80 93 } 81 94 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) 121 return trUtf8("City %1").arg(section + 1); 122 else 123 return trUtf8("%1").arg(section + 1); 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 */ 135 bool CTSPModel::loadTask(QString fname) 136 { 137 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 138 QFile f(fname); 139 if (!f.open(QIODevice::ReadOnly)) { 140 QApplication::restoreOverrideCursor(); 141 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec(); 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(); 165 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); 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 { 193 int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); 194 int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); 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 */ 222 bool CTSPModel::saveTask(QString fname) 223 { 224 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 225 QFile f(fname); 226 if (!f.open(QIODevice::WriteOnly)) { 227 QApplication::restoreOverrideCursor(); 228 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(); 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(); 236 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 237 return false; 238 } 239 // File signature 240 ds << TSPT; 241 if (f.error() != QFile::NoError) { 242 f.close(); 243 QApplication::restoreOverrideCursor(); 244 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 245 return false; 246 } 247 // File version 248 ds << TSPT_VERSION; 249 if (f.error() != QFile::NoError) { 250 f.close(); 251 QApplication::restoreOverrideCursor(); 252 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 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(); 260 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 261 return false; 262 } 263 // Metadata 264 ds << OSID; 265 if (f.error() != QFile::NoError) { 266 f.close(); 267 QApplication::restoreOverrideCursor(); 268 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 269 return false; 270 } 271 // Number of cities 272 ds << nCities; 273 if (f.error() != QFile::NoError) { 274 f.close(); 275 QApplication::restoreOverrideCursor(); 276 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 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) { 283 ds << table[r][c]; 284 if (f.error() != QFile::NoError) { 285 f.close(); 286 QApplication::restoreOverrideCursor(); 287 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 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 */ 82 305 bool CTSPModel::setData(const QModelIndex &index, const QVariant &value, int role) 83 306 { … … 101 324 } 102 325 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 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 */ 116 332 void CTSPModel::setNumCities(int n) 117 333 { … … 130 346 } 131 347 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 } 348 /* Privates **********************************************************/ 140 349 141 350 inline bool CTSPModel::loadError(QDataStream::Status status) … … 152 361 QApplication::restoreOverrideCursor(); 153 362 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + err,QMessageBox::Ok).exec(); 154 return true;155 }156 157 bool CTSPModel::loadTask(QString fname)158 {159 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));160 QFile f(fname);161 if (!f.open(QIODevice::ReadOnly)) {162 QApplication::restoreOverrideCursor();163 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec();164 return false;165 }166 QDataStream ds(&f);167 ds.setVersion(QDataStream::Qt_4_4);168 quint32 sig;169 ds >> sig;170 if (loadError(ds.status())) {171 return false;172 }173 ds.device()->reset();174 if (sig == TSPT) {175 if (!loadTSPT(&ds)) {176 f.close();177 return false;178 }179 } else if ((sig >> 16) == ZKT) {180 if (!loadZKT(&ds)) {181 f.close();182 return false;183 }184 } else {185 f.close();186 QApplication::restoreOverrideCursor();187 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec();188 return false;189 }190 f.close();191 QApplication::restoreOverrideCursor();192 363 return true; 193 364 } … … 295 466 } 296 467 297 bool CTSPModel::saveTask(QString fname) 298 { 299 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 300 QFile f(fname); 301 if (!f.open(QIODevice::WriteOnly)) { 302 QApplication::restoreOverrideCursor(); 303 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(); 304 return false; 305 } 306 QDataStream ds(&f); 307 ds.setVersion(QDataStream::Qt_4_4); 308 if (f.error() != QFile::NoError) { 309 f.close(); 310 QApplication::restoreOverrideCursor(); 311 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 312 return false; 313 } 314 // File signature 315 ds << TSPT; 316 if (f.error() != QFile::NoError) { 317 f.close(); 318 QApplication::restoreOverrideCursor(); 319 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 320 return false; 321 } 322 // File version 323 ds << TSPT_VERSION; 324 if (f.error() != QFile::NoError) { 325 f.close(); 326 QApplication::restoreOverrideCursor(); 327 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 328 return false; 329 } 330 // File metadata version 331 ds << TSPT_META_VERSION; 332 if (f.error() != QFile::NoError) { 333 f.close(); 334 QApplication::restoreOverrideCursor(); 335 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 336 return false; 337 } 338 // Metadata 339 ds << OSID; 340 if (f.error() != QFile::NoError) { 341 f.close(); 342 QApplication::restoreOverrideCursor(); 343 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 344 return false; 345 } 346 // Number of cities 347 ds << nCities; 348 if (f.error() != QFile::NoError) { 349 f.close(); 350 QApplication::restoreOverrideCursor(); 351 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 352 return false; 353 } 354 // Costs 355 for (int r = 0; r < nCities; r++) 356 for (int c = 0; c < nCities; c++) 357 if (r != c) { 358 ds << table[r][c]; 359 if (f.error() != QFile::NoError) { 360 f.close(); 361 QApplication::restoreOverrideCursor(); 362 QMessageBox(QMessageBox::Critical,trUtf8("Task Save"),trUtf8("Unable to save task.\nError: %1").arg(f.errorString()),QMessageBox::Ok).exec(); 363 return false; 364 } 365 } 366 f.close(); 367 QApplication::restoreOverrideCursor(); 368 return true; 369 } 370 371 void CTSPModel::randomize() 372 { 373 int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt(); 374 int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt(); 375 for (int r = 0; r < nCities; r++) 376 for (int c = 0; c < nCities; c++) 377 if (r != c) 378 table[r][c] = rand(randMin,randMax); 379 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 380 } 468 inline int CTSPModel::rand(int min, int max) const 469 { 470 return min + (int)(((float)qrand() / RAND_MAX) * max); 471 }
Note: See TracChangeset
for help on using the changeset viewer.