Changeset 9b7064d770 in tspsg for src/tspmodel.cpp
- Timestamp:
- Jul 1, 2009, 7:01:23 PM (15 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- 4c96f94558
- Parents:
- c02b4903bd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tspmodel.cpp
rc02b4903bd r9b7064d770 30 30 } 31 31 32 in t CTSPModel::rand(int min, int max)32 inline int CTSPModel::rand(int min, int max) const 33 33 { 34 34 return min + (int)(((float)qrand() / RAND_MAX) * max); 35 35 } 36 36 37 in t CTSPModel::rowCount(const QModelIndex &) const37 inline int CTSPModel::rowCount(const QModelIndex &) const 38 38 { 39 39 return nCities; 40 40 } 41 41 42 in t CTSPModel::columnCount(const QModelIndex &) const42 inline int CTSPModel::columnCount(const QModelIndex &) const 43 43 { 44 44 return nCities; … … 116 116 void CTSPModel::setNumCities(int n) 117 117 { 118 // int randMin = settings->value("MinCost",DEF_RAND_MIN).toInt();119 // int randMax = settings->value("MaxCost",DEF_RAND_MAX).toInt();120 118 if (n == nCities) 121 119 return; … … 127 125 table[r][c] = INFINITY; 128 126 else 129 table[r][c] = 0; // rand(randMin,randMax);127 table[r][c] = 0; 130 128 } 131 129 for (int r = nCities; r < n; r++) { … … 134 132 table[r][c] = INFINITY; 135 133 else 136 table[r][c] = 0; // rand(randMin,randMax);134 table[r][c] = 0; 137 135 } 138 136 } … … 150 148 } 151 149 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 152 165 void CTSPModel::loadTask(QString fname) 153 166 { 154 167 QFile f(fname); 155 f.open(QIODevice::ReadOnly); 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 } 156 172 QDataStream ds(&f); 157 173 ds.setVersion(QDataStream::Qt_4_4); 158 174 quint32 sig; 159 175 ds >> sig; 176 if (loadError(ds.status())) 177 return; 160 178 ds.device()->reset(); 161 179 if (sig == TSPT) … … 164 182 loadZKT(&ds); 165 183 else 166 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task: \nUnknown file format or file is corrupted."),QMessageBox::Ok).exec();184 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); 167 185 f.close(); 168 186 } … … 172 190 // Skipping signature 173 191 ds->skipRawData(sizeof(TSPT)); 192 if (loadError(ds->status())) 193 return; 174 194 // File version 175 195 quint8 version; 176 196 *ds >> version; 197 if (loadError(ds->status())) 198 return; 177 199 if (version > TSPT_VERSION) { 178 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task: \nFile version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();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(); 179 201 return; 180 202 } 181 203 // Skipping metadata 182 204 ds->skipRawData(TSPT_META_SIZE); 205 if (loadError(ds->status())) 206 return; 183 207 // Cities number 184 208 quint16 size; 185 209 *ds >> size; 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 } 186 216 if (nCities != size) 187 217 emit numCitiesChanged(size); … … 189 219 for (int r = 0; r < size; r++) 190 220 for (int c = 0; c < size; c++) 191 if (r != c) 221 if (r != c) { 192 222 *ds >> table[r][c]; 223 if (loadError(ds->status())) { 224 clear(); 225 return; 226 } 227 } 193 228 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 194 229 } … … 198 233 // Skipping signature 199 234 ds->skipRawData(sizeof(ZKT)); 235 if (loadError(ds->status())) 236 return; 200 237 // File version 201 238 quint16 version; 202 239 ds->readRawData(reinterpret_cast<char *>(&version),2); 240 if (loadError(ds->status())) 241 return; 203 242 if (version > ZKT_VERSION) { 204 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task: \nFile version is newer than application supports.\nPlease, try to update application."),QMessageBox::Ok).exec();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(); 205 244 return; 206 245 } … … 208 247 quint8 size; 209 248 ds->readRawData(reinterpret_cast<char *>(&size),1); 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 } 210 255 if (nCities != size) 211 256 emit numCitiesChanged(size); … … 216 261 if (r != c) { 217 262 ds->readRawData(reinterpret_cast<char *>(&val),8); 263 if (loadError(ds->status())) { 264 clear(); 265 return; 266 } 218 267 table[r][c] = val; 219 } else 268 } else { 220 269 ds->skipRawData(8); 270 if (loadError(ds->status())) { 271 clear(); 272 return; 273 } 274 } 221 275 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 222 276 } … … 225 279 { 226 280 QFile f(fname); 227 f.open(QIODevice::WriteOnly); 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(); 283 return; 284 } 228 285 QDataStream ds(&f); 229 286 ds.setVersion(QDataStream::Qt_4_4); 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(); 290 return; 291 } 230 292 // File signature 231 293 ds << TSPT; 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(); 297 return; 298 } 232 299 // File version 233 300 ds << TSPT_VERSION; 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(); 304 return; 305 } 234 306 // File metadata version 235 307 ds << TSPT_META_VERSION; 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(); 311 return; 312 } 236 313 // Metadata 237 314 ds << OSID; 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(); 318 return; 319 } 238 320 // Number of cities 239 321 ds << nCities; 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(); 325 return; 326 } 240 327 // Costs 241 328 for (int r = 0; r < nCities; r++) 242 329 for (int c = 0; c < nCities; c++) 243 if (r != c) 330 if (r != c) { 244 331 ds << table[r][c]; 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(); 335 return; 336 } 337 } 245 338 f.close(); 246 339 }
Note: See TracChangeset
for help on using the changeset viewer.