Changeset 394216e468 in tspsg for src/mainwindow.cpp
- Timestamp:
- Mar 22, 2010, 9:45:16 PM (14 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- 1babbd6ba3
- Parents:
- e2abfd326f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mainwindow.cpp
re2abfd326f r394216e468 60 60 #endif // QT_NO_PRINTER 61 61 connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); 62 #ifdef Q_OS_WIN32 63 connect(actionHelpCheck4Updates, SIGNAL(triggered()), SLOT(actionHelpCheck4UpdatesTriggered())); 64 #endif // Q_OS_WIN32 62 65 connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); 63 66 connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); … … 134 137 filters.append(tr("All Files") + " (*)"); 135 138 139 QString file = QFileInfo(fileName).canonicalPath(); 136 140 QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; 137 QString file = QFileDialog::getOpenFileName(this, tr("Task Load"), QString(), filters.join(";;"), NULL, opts);141 file = QFileDialog::getOpenFileName(this, tr("Task Load"), file, filters.join(";;"), NULL, opts); 138 142 if (file.isEmpty() || !QFileInfo(file).isFile()) 139 143 return; … … 147 151 } 148 152 149 void MainWindow::actionFileSaveTriggered() 150 { 151 qDebug() << tr("Untitled"); 153 bool MainWindow::actionFileSaveTriggered() 154 { 152 155 if ((fileName == tr("Untitled") + ".tspt") || (!fileName.endsWith(".tspt", Qt::CaseInsensitive))) 153 saveTask();156 return saveTask(); 154 157 else 155 if (tspmodel->saveTask(fileName)) 158 if (tspmodel->saveTask(fileName)) { 156 159 setWindowModified(false); 160 return true; 161 } else 162 return false; 157 163 } 158 164 … … 165 171 { 166 172 static QString selectedFile; 167 if (selectedFile.isEmpty()) { 168 if (fileName == tr("Untitled") + ".tspt") { 169 #ifndef QT_NO_PRINTER 170 selectedFile = "solution.pdf"; 173 if (selectedFile.isEmpty()) 174 selectedFile = QFileInfo(fileName).canonicalPath(); 175 else 176 selectedFile = QFileInfo(selectedFile).canonicalPath(); 177 if (!selectedFile.isEmpty()) 178 selectedFile += "/"; 179 if (fileName == tr("Untitled") + ".tspt") { 180 #ifndef QT_NO_PRINTER 181 selectedFile += "solution.pdf"; 171 182 #else 172 selectedFile= "solution.html";183 selectedFile += "solution.html"; 173 184 #endif // QT_NO_PRINTER 174 175 #ifndef QT_NO_PRINTER 176 selectedFile = QFileInfo(fileName).canonicalPath() + "/" +QFileInfo(fileName).completeBaseName() + ".pdf";185 } else { 186 #ifndef QT_NO_PRINTER 187 selectedFile += QFileInfo(fileName).completeBaseName() + ".pdf"; 177 188 #else 178 selectedFile = QFileInfo(fileName).canonicalPath() + "/" +QFileInfo(fileName).completeBaseName() + ".html";189 selectedFile += QFileInfo(fileName).completeBaseName() + ".html"; 179 190 #endif // QT_NO_PRINTER 180 }181 191 } 182 192 … … 191 201 filters.append(tr("All Files") + " (*)"); 192 202 193 QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog;203 QFileDialog::Options opts(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog); 194 204 QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"), NULL, opts); 195 205 if (file.isEmpty()) … … 211 221 if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) 212 222 dw.setFormat("plaintext"); 213 dw.write(solutionText->document()); 214 #else 223 if (!dw.write(solutionText->document())) 224 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(dw.device()->errorString())); 225 #else // QT_VERSION >= 0x040500 215 226 // Qt < 4.5 has no QTextDocumentWriter class 216 227 QFile file(selectedFile); 217 228 if (!file.open(QFile::WriteOnly)) { 218 229 QApplication::restoreOverrideCursor(); 230 QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(file->errorString())); 219 231 return; 220 232 } … … 258 270 if (sd.colorChanged() || sd.fontChanged()) { 259 271 initDocStyleSheet(); 260 if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,tr("Settings Changed"),tr("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) { 261 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 262 solutionText->clear(); 263 solutionText->setHtml(output.join("")); 264 QApplication::restoreOverrideCursor(); 265 } 266 } 267 if (sd.translucencyChanged() != 0) { 272 if (!solutionText->document()->isEmpty() && sd.colorChanged()) 273 QMessageBox::information(this, tr("Settings Changed"), tr("You have changed color settings.\nThey will be applied to the next solution output.")); 274 } 275 if (sd.translucencyChanged() != 0) 268 276 toggleTranclucency(sd.translucencyChanged() == 1); 269 }270 277 } 271 278 … … 283 290 if (actionSettingsLanguageAutodetect->isChecked()) { 284 291 // We have language autodetection. It needs to be disabled to change language. 285 if (QMessageBox (QMessageBox::Question,tr("Language change"),tr("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes) {292 if (QMessageBox::question(this, tr("Language change"), tr("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { 286 293 actionSettingsLanguageAutodetect->trigger(); 287 294 } else … … 302 309 #endif 303 310 QApplication::restoreOverrideCursor(); 304 } 305 } 311 QMessageBox::information(this, tr("Settings Changed"), tr("You have changed the application language.\nTo get current solution output in the new language\nyou need to re-run the solution process.")); 312 } 313 } 314 315 #ifdef Q_OS_WIN32 316 void MainWindow::actionHelpCheck4UpdatesTriggered() 317 { 318 if (!hasUpdater()) { 319 QMessageBox::warning(this, tr("Unsupported Feature"), tr("Sorry, but this feature is not supported on your platform\nor support for this feature was not installed.")); 320 return; 321 } 322 323 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 324 QProcess::execute("updater/Update.exe -name=\"TSPSG: TSP Solver and Generator\" -check=\"freeupdate\""); 325 QApplication::restoreOverrideCursor(); 326 } 327 #endif // Q_OS_WIN32 306 328 307 329 void MainWindow::actionHelpAboutTriggered() 308 330 { 309 //! \todo TODO: Normal about window :-)310 331 QString title; 311 332 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) … … 314 335 title += QString("<b>TSPSG: TSP Solver and Generator</b><br>"); 315 336 #endif // Q_OS_WINCE || Q_OS_SYMBIAN 316 title += QString("%1: <b>%2</b><br>").arg(tr("Version"), BUILD_VERSION);337 title += QString("%1: <b>%2</b><br>").arg(tr("Version"), QApplication::applicationVersion()); 317 338 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) 318 title += QString("<b>© 2007-%1 Oleksii \"Lёppa\" Serdiuk</b><br>").arg(QDate::currentDate().toString("yyyy"));339 title += QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b><br>").arg(QDate::currentDate().toString("yyyy"), QApplication::organizationDomain(), QApplication::organizationName()); 319 340 title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b>"); 320 341 #else … … 410 431 vb->addLayout(hb2); 411 432 412 dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);433 dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); 413 434 dlg->setWindowTitle(tr("About TSPSG")); 414 435 dlg->setLayout(vb); … … 453 474 row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); 454 475 if (!ok) { 455 QMessageBox (QMessageBox::Critical,tr("Data error"),tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();476 QMessageBox::critical(this, tr("Data error"), tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1)); 456 477 return; 457 478 } … … 459 480 matrix.append(row); 460 481 } 482 483 QProgressDialog pd(this); 484 QProgressBar *pb = new QProgressBar(&pd); 485 pb->setAlignment(Qt::AlignCenter); 486 pb->setFormat(tr("%v of %1 parts found").arg(n)); 487 pd.setBar(pb); 488 pd.setMaximum(n * 2 + 3); 489 pd.setMinimumDuration(1000); 490 pd.setLabelText(tr("Calculating optimal route...")); 491 pd.setWindowTitle(tr("Solution Progress")); 492 pd.setWindowModality(Qt::ApplicationModal); 493 pd.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint); 494 pd.setValue(0); 495 461 496 CTSPSolver solver; 462 SStep *root = solver.solve(n,matrix,this); 463 if (!root) 464 return; 465 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); 466 QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); 467 output.clear(); 468 output.append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>"); 469 output.append("<p>" + tr("Task:") + "</p>"); 470 outputMatrix(matrix, output); 471 output.append("<hr>"); 472 output.append("<p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); 497 connect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); 498 connect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); 499 SStep *root = solver.solve(n, matrix); 500 disconnect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); 501 disconnect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); 502 if (!root) { 503 pd.reset(); 504 if (!solver.wasCanceled()) 505 QMessageBox::warning(this, tr("Solution Result"), tr("Unable to find a solution.\nMaybe, this task has no solution.")); 506 return; 507 } 508 pb->setFormat("%p%"); 509 pd.setLabelText(tr("Generating solution output...")); 510 pd.setValue(n + 1); 511 512 solutionText->clear(); 513 pd.setValue(n + 2); 514 515 solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value())); 516 solutionText->append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>"); 517 solutionText->append("<p>" + tr("Task:") + "</p>"); 518 solutionText->append(outputMatrix(matrix)); 519 solutionText->append("<hr><p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); 473 520 SStep *step = root; 474 521 n = 1; 475 522 while (n <= spinCities->value()) { 523 if (pd.wasCanceled()) { 524 solutionText->clear(); 525 return; 526 } 527 pd.setValue(spinCities->value() + 2 + n); 528 476 529 if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) { 477 530 if (n != spinCities->value()) { 478 output.append("<p>" + tr("Step #%1").arg(n++) + "</p>");531 solutionText->append("<p>" + tr("Step #%1").arg(n++) + "</p>"); 479 532 if (settings->value("Output/ShowMatrix", DEF_SHOW_MATRIX).toBool() && (!settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() || (settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() && (spinCities->value() <= settings->value("Output/ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt())))) { 480 outputMatrix(*step, output);533 solutionText->append(outputMatrix(*step)); 481 534 } 482 output.append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>");535 solutionText->append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>"); 483 536 if (!step->alts.empty()) { 484 537 SCandidate cand; … … 489 542 alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); 490 543 } 491 output.append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>");544 solutionText->append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>"); 492 545 } 493 output.append("<p> </p>");546 solutionText->append("<p> </p>"); 494 547 } 495 548 } … … 501 554 break; 502 555 } 556 pd.setValue(spinCities->value() + 2 + n); 557 503 558 if (solver.isOptimal()) 504 output.append("<p>" + tr("Optimal path:") + "</p>");559 solutionText->append("<p>" + tr("Optimal path:") + "</p>"); 505 560 else 506 output.append("<p>" + tr("Resulting path:") + "</p>");507 output.append("<p> " + solver.getSortedPath() + "</p>");561 solutionText->append("<p>" + tr("Resulting path:") + "</p>"); 562 solutionText->append("<p> " + solver.getSortedPath() + "</p>"); 508 563 if (isInteger(step->price)) 509 output.append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>");564 solutionText->append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>"); 510 565 else 511 output.append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>");566 solutionText->append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>"); 512 567 if (!solver.isOptimal()) { 513 output.append("<p> </p>"); 514 output.append("<p>" + tr("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>"); 515 } 516 output.append("<p></p>"); 517 518 solutionText->setHtml(output.join("")); 519 solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value())); 568 solutionText->append("<p> </p>"); 569 solutionText->append("<p>" + tr("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>"); 570 } 520 571 521 572 if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) { 522 573 // Scrolling to the end of text. 523 574 solutionText->moveCursor(QTextCursor::End); 524 } 525 575 } else 576 solutionText->moveCursor(QTextCursor::Start); 577 578 pd.setLabelText(tr("Cleaning up...")); 579 pd.setMaximum(0); 580 pd.setCancelButton(NULL); 581 pd.adjustSize(); 582 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); 583 solver.cleanup(true); 526 584 toggleSolutionActions(); 527 585 tabWidget->setCurrentIndex(1); 528 QApplication::restoreOverrideCursor();529 586 } 530 587 … … 632 689 } 633 690 691 bool MainWindow::hasUpdater() const 692 { 693 #ifdef Q_OS_WIN32 694 return QFile::exists("updater/Update.exe"); 695 #else // Q_OS_WIN32 696 return false; 697 #endif // Q_OS_WIN32 698 } 699 634 700 void MainWindow::initDocStyleSheet() 635 701 { … … 719 785 if (!ad) { 720 786 settings->remove("Language"); 721 if (QApplication::overrideCursor() != 0) 722 QApplication::restoreOverrideCursor(); 787 QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor)); 723 788 if (isVisible()) 724 789 QMessageBox::warning(this, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); 725 790 else 726 791 QMessageBox::warning(NULL, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); 792 QApplication::restoreOverrideCursor(); 727 793 } 728 794 return false; … … 735 801 if (!isWindowModified()) 736 802 return true; 737 int res = QMessageBox (QMessageBox::Warning,tr("Unsaved Changes"),tr("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec();803 int res = QMessageBox::warning(this, tr("Unsaved Changes"), tr("Would you like to save changes in the current task?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); 738 804 if (res == QMessageBox::Save) 739 return saveTask();805 return actionFileSaveTriggered(); 740 806 else if (res == QMessageBox::Cancel) 741 807 return false; … … 744 810 } 745 811 746 void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output) 812 QString MainWindow::outputMatrix(const TMatrix &matrix) const 747 813 { 748 814 int n = spinCities->value(); 749 QString line="";815 QString output(""), line; 750 816 output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); 751 817 for (int r = 0; r < n; r++) { … … 761 827 } 762 828 output.append("</table>"); 763 } 764 765 void MainWindow::outputMatrix(const SStep &step, QStringList &output) 829 return output; 830 } 831 832 QString MainWindow::outputMatrix(const SStep &step) const 766 833 { 767 834 int n = spinCities->value(); 768 QString line="";835 QString output(""), line; 769 836 output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); 770 837 for (int r = 0; r < n; r++) { … … 789 856 } 790 857 output.append("</table>"); 858 return output; 791 859 } 792 860 … … 816 884 actionFilePrint->setShortcut(QApplication::translate("MainWindow", "Ctrl+P", 0, QApplication::UnicodeUTF8)); 817 885 #endif // QT_NO_PRINTER 886 #ifdef Q_OS_WIN32 887 actionHelpCheck4Updates->setText(tr("Check for &Updates...")); 888 #ifndef QT_NO_TOOLTIP 889 actionHelpCheck4Updates->setToolTip(tr("Check for %1 updates").arg(QApplication::applicationName())); 890 #endif // QT_NO_TOOLTIP 891 #ifndef QT_NO_STATUSTIP 892 actionHelpCheck4Updates->setStatusTip(tr("Check for %1 updates").arg(QApplication::applicationName())); 893 #endif // QT_NO_STATUSTIP 894 #endif // Q_OS_WIN32 818 895 } 819 896 … … 901 978 toolBar->insertAction(actionSettingsPreferences,actionFilePrint); 902 979 #endif // QT_NO_PRINTER 980 #ifdef Q_OS_WIN32 981 actionHelpCheck4Updates = new QAction(this); 982 actionHelpCheck4Updates->setEnabled(hasUpdater()); 983 menuHelp->insertAction(actionHelpAboutQt, actionHelpCheck4Updates); 984 menuHelp->insertSeparator(actionHelpAboutQt); 985 #endif // Q_OS_WIN32 903 986 904 987 groupSettingsLanguageList = new QActionGroup(this); … … 925 1008 actionFileSaveAsSolution->setEnabled(enable); 926 1009 solutionText->setEnabled(enable); 927 if (!enable)928 output.clear();929 1010 #ifndef QT_NO_PRINTER 930 1011 actionFilePrint->setEnabled(enable);
Note: See TracChangeset
for help on using the changeset viewer.