Changeset b424a7e320 in tspsg for src
- Timestamp:
- Aug 2, 2009, 9:47:45 PM (15 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- 6b3d3c1bbb
- Parents:
- 9aa0e521ed
- Location:
- src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mainwindow.cpp
r9aa0e521ed rb424a7e320 87 87 connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged())); 88 88 connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); 89 if ( QCoreApplication::arguments().count() > 1) {90 tspmodel->loadTask(QCoreApplication::arguments().at(1));89 if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) { 90 setFileName(QCoreApplication::arguments().at(1)); 91 91 setWindowModified(false); 92 } 92 } else 93 setFileName(); 93 94 #ifdef Q_OS_WINCE 94 95 taskView->resizeColumnsToContents(); … … 164 165 } 165 166 167 void MainWindow::setFileName(QString fileName) 168 { 169 this->fileName = fileName; 170 setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem"))); 171 } 172 166 173 void MainWindow::spinCitiesValueChanged(int n) 167 174 { … … 179 186 } 180 187 188 bool MainWindow::maybeSave() 189 { 190 if (!isWindowModified()) 191 return true; 192 int res = QMessageBox(QMessageBox::Warning,trUtf8("Unsaved Changes"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec(); 193 if (res == QMessageBox::Save) 194 return saveTask(); 195 else if (res == QMessageBox::Cancel) 196 return false; 197 else 198 return true; 199 } 181 200 182 201 void MainWindow::actionFileNewTriggered() 183 202 { 184 if (isWindowModified()) { 185 int res = QMessageBox(QMessageBox::Warning,trUtf8("New Task"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec(); 186 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask())) 187 return; 188 } 203 if (!maybeSave()) 204 return; 189 205 tspmodel->clear(); 206 #ifdef Q_OS_WINCE 207 taskView->resizeColumnsToContents(); 208 taskView->resizeRowsToContents(); 209 #endif 210 setFileName(); 190 211 setWindowModified(false); 191 212 tabWidget->setCurrentIndex(0); … … 196 217 void MainWindow::actionFileOpenTriggered() 197 218 { 198 if (isWindowModified()) { 199 int res = QMessageBox(QMessageBox::Warning,trUtf8("Task Open"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec(); 200 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask())) 201 return; 202 } 219 if (!maybeSave()) 220 return; 203 221 QFileDialog od(this); 204 222 od.setAcceptMode(QFileDialog::AcceptOpen); 205 223 od.setFileMode(QFileDialog::ExistingFile); 206 224 QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); 207 filters.append( QString(trUtf8("%1 Task Files")).arg("TSPSG") + " (*.tspt)");208 filters.append( QString(trUtf8("%1 Task Files")).arg("ZKomModRd") + " (*.zkt)");225 filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); 226 filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); 209 227 filters.append(trUtf8("All Files") + " (*)"); 210 228 od.setNameFilters(filters); … … 212 230 return; 213 231 QStringList files = od.selectedFiles(); 214 if (files.size() < 1) 215 return; 216 tspmodel->loadTask(files.first()); 232 if (files.empty()) 233 return; 234 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 235 if (!tspmodel->loadTask(files.first())) { 236 QApplication::restoreOverrideCursor(); 237 return; 238 } 239 setFileName(files.first()); 240 #ifdef Q_OS_WINCE 241 taskView->resizeColumnsToContents(); 242 taskView->resizeRowsToContents(); 243 #endif 244 tabWidget->setCurrentIndex(0); 217 245 setWindowModified(false); 218 246 solutionText->clear(); 219 247 enableSolutionActions(false); 248 QApplication::restoreOverrideCursor(); 220 249 } 221 250 … … 264 293 QFileDialog sd(this); 265 294 sd.setAcceptMode(QFileDialog::AcceptSave); 266 QStringList filters( QString(trUtf8("%1 Task File")).arg("TSPSG") + " (*.tspt)");295 QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); 267 296 filters.append(trUtf8("All Files") + " (*)"); 268 297 sd.setNameFilters(filters); 269 298 sd.setDefaultSuffix("tspt"); 299 if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) 300 sd.selectFile(fileName); 301 else 302 sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); 270 303 if (sd.exec() != QDialog::Accepted) 271 304 return false; 272 305 QStringList files = sd.selectedFiles(); 273 if (files. size() < 1)306 if (files.empty()) 274 307 return false; 308 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 275 309 if (tspmodel->saveTask(files.first())) { 310 setFileName(files.first()); 276 311 setWindowModified(false); 312 QApplication::restoreOverrideCursor(); 277 313 return true; 278 } else 279 return false; 314 } 315 QApplication::restoreOverrideCursor(); 316 return false; 280 317 } 281 318 … … 351 388 row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); 352 389 if (!ok) { 353 QMessageBox(QMessageBox::Critical,trUtf8("Data error"), QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();390 QMessageBox(QMessageBox::Critical,trUtf8("Data error"),trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); 354 391 return; 355 392 } … … 467 504 return; 468 505 } 506 bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); 469 507 if (loadLanguage(action->data().toString())) { 470 508 settings->setValue("Language",action->data().toString()); 471 509 retranslateUi(this); 510 if (untitled) 511 setFileName(); 472 512 } 473 513 } … … 475 515 void MainWindow::closeEvent(QCloseEvent *event) 476 516 { 477 if (isWindowModified()) { 478 int res = QMessageBox(QMessageBox::Warning,trUtf8("Application Close"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,this).exec(); 479 if ((res == QMessageBox::Cancel) || ((res == QMessageBox::Yes) && !saveTask())) { 480 event->ignore(); 481 return; 482 } 517 if (!maybeSave()) { 518 event->ignore(); 519 return; 483 520 } 484 521 settings->setValue("NumCities",spinCities->value()); -
src/mainwindow.h
r9aa0e521ed rb424a7e320 38 38 { 39 39 Q_OBJECT 40 40 41 public: 41 42 MainWindow(QWidget *parent = 0); 42 43 void closeEvent(QCloseEvent *); 44 43 45 private slots: 44 46 void actionFileNewTriggered(); … … 58 60 void spinCitiesValueChanged(int); 59 61 void numCitiesChanged(int); 62 60 63 private: 61 64 QSettings *settings; 62 65 QPrinter *printer; 63 66 CTSPModel *tspmodel; 67 QString fileName; 64 68 QActionGroup *groupSettingsLanguageList; 65 69 QStringList output; 70 void enableSolutionActions(bool enable = true); 71 void initDocStyleSheet(); 66 72 bool loadLanguage(QString lang = ""); 67 73 void loadLangList(); 68 void initDocStyleSheet(); 74 bool maybeSave(); 75 void outputMatrix(tMatrix, QStringList &, int nRow = -1, int nCol = -1); 76 void setFileName(QString fileName = trUtf8("Untitled") + ".tspt"); 69 77 bool saveTask(); 70 void outputMatrix(tMatrix, QStringList &, int nRow = -1, int nCol = -1);71 void enableSolutionActions(bool enable = true);72 78 }; 73 79 -
src/tspmodel.cpp
r9aa0e521ed rb424a7e320 154 154 } 155 155 156 voidCTSPModel::loadTask(QString fname)156 bool CTSPModel::loadTask(QString fname) 157 157 { 158 158 QFile f(fname); 159 159 if (!f.open(QIODevice::ReadOnly)) { 160 160 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),QString(trUtf8("Unable to open task file.\nError: %1")).arg(f.errorString()),QMessageBox::Ok).exec(); 161 return ;161 return false; 162 162 } 163 163 QDataStream ds(&f); … … 166 166 ds >> sig; 167 167 if (loadError(ds.status())) 168 return ;168 return false; 169 169 ds.device()->reset(); 170 170 if (sig == TSPT) 171 loadTSPT(&ds); 171 if (!loadTSPT(&ds)) { 172 f.close(); 173 return false; 174 } 172 175 else if ((sig >> 16) == ZKT) 173 loadZKT(&ds); 174 else 176 if (!loadZKT(&ds)) { 177 f.close(); 178 return false; 179 } 180 else { 175 181 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unknown file format or file is corrupted."),QMessageBox::Ok).exec(); 182 f.close(); 183 return false; 184 } 176 185 f.close(); 177 } 178 179 void CTSPModel::loadTSPT(QDataStream *ds) 186 return true; 187 } 188 189 bool CTSPModel::loadTSPT(QDataStream *ds) 180 190 { 181 191 // Skipping signature 182 192 ds->skipRawData(sizeof(TSPT)); 183 193 if (loadError(ds->status())) 184 return ;194 return false; 185 195 // File version 186 196 quint8 version; 187 197 *ds >> version; 188 198 if (loadError(ds->status())) 189 return ;199 return false; 190 200 if (version > TSPT_VERSION) { 191 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(); 192 return ;202 return false; 193 203 } 194 204 // Skipping metadata 195 205 ds->skipRawData(TSPT_META_SIZE); 196 206 if (loadError(ds->status())) 197 return ;207 return false; 198 208 // Cities number 199 209 quint16 size; 200 210 *ds >> size; 201 211 if (loadError(ds->status())) 202 return ;212 return false; 203 213 if (size < 3) { 204 214 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); 205 return ;215 return false; 206 216 } 207 217 if (nCities != size) … … 214 224 if (loadError(ds->status())) { 215 225 clear(); 216 return ;226 return false; 217 227 } 218 228 } 219 229 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 220 } 221 222 void CTSPModel::loadZKT(QDataStream *ds) 230 return true; 231 } 232 233 bool CTSPModel::loadZKT(QDataStream *ds) 223 234 { 224 235 // Skipping signature 225 236 ds->skipRawData(sizeof(ZKT)); 226 237 if (loadError(ds->status())) 227 return ;238 return false; 228 239 // File version 229 240 quint16 version; 230 241 ds->readRawData(reinterpret_cast<char *>(&version),2); 231 242 if (loadError(ds->status())) 232 return ;243 return false; 233 244 if (version > ZKT_VERSION) { 234 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(); 235 return ;246 return false; 236 247 } 237 248 // Cities number … … 239 250 ds->readRawData(reinterpret_cast<char *>(&size),1); 240 251 if (loadError(ds->status())) 241 return ;252 return false; 242 253 if ((size < 3) || (size > 5)) { 243 254 QMessageBox(QMessageBox::Critical,trUtf8("Task Load"),trUtf8("Unable to load task:") + "\n" + trUtf8("Unexpected data read.\nFile is possibly corrupted."),QMessageBox::Ok).exec(); 244 return ;255 return false; 245 256 } 246 257 if (nCities != size) … … 254 265 if (loadError(ds->status())) { 255 266 clear(); 256 return ;267 return false; 257 268 } 258 269 table[r][c] = val; … … 261 272 if (loadError(ds->status())) { 262 273 clear(); 263 return ;274 return false; 264 275 } 265 276 } 266 277 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 278 return true; 267 279 } 268 280 -
src/tspmodel.h
r9aa0e521ed rb424a7e320 30 30 { 31 31 Q_OBJECT 32 32 33 public: 33 34 CTSPModel(QObject *parent = 0); 35 void clear(); 36 int columnCount(const QModelIndex &) const; 37 QVariant data(const QModelIndex &, int) const; 38 Qt::ItemFlags flags(const QModelIndex &) const; 39 QVariant headerData(int, Qt::Orientation, int) const; 40 bool loadTask(QString); 41 quint16 numCities() const; 42 void randomize(); 34 43 int rowCount(const QModelIndex &) const; 35 int columnCount(const QModelIndex &) const; 36 QVariant headerData(int, Qt::Orientation, int) const; 37 QVariant data(const QModelIndex &, int) const; 44 bool saveTask(QString); 38 45 bool setData(const QModelIndex &, const QVariant &, int); 39 Qt::ItemFlags flags(const QModelIndex &) const;40 quint16 numCities() const;41 46 void setNumCities(int); 42 void clear(); 43 void loadTask(QString); 44 bool saveTask(QString); 45 void randomize(); 47 46 48 signals: 47 49 void numCitiesChanged(int); 50 48 51 private: 49 52 QSettings *settings; 50 53 QVector<QVector<double> > table; 51 54 quint16 nCities; 55 bool loadError(QDataStream::Status); 56 bool loadZKT(QDataStream *); 57 bool loadTSPT(QDataStream *); 52 58 int rand(int, int) const; 53 bool loadError(QDataStream::Status);54 void loadZKT(QDataStream *);55 void loadTSPT(QDataStream *);56 59 }; 57 60
Note: See TracChangeset
for help on using the changeset viewer.