[45] | 1 | /* |
---|
[42] | 2 | * TSPSG: TSP Solver and Generator |
---|
[17] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[1] | 4 | * |
---|
[6] | 5 | * $Id: mainwindow.cpp 71 2009-12-07 16:06:44Z laleppa $ |
---|
| 6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/mainwindow.cpp $ |
---|
[4] | 7 | * |
---|
[6] | 8 | * This file is part of TSPSG. |
---|
[1] | 9 | * |
---|
[6] | 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. |
---|
[1] | 14 | * |
---|
[6] | 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. |
---|
[1] | 19 | * |
---|
[6] | 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/>. |
---|
[1] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include "mainwindow.h" |
---|
| 25 | |
---|
[65] | 26 | /*! |
---|
| 27 | * \brief Class constructor. |
---|
| 28 | * \param parent Main Window parent widget. |
---|
| 29 | * |
---|
| 30 | * Initializes Main Window and creates its layout based on target OS. |
---|
| 31 | * Loads TSPSG settings and opens a task file if it was specified as a commandline parameter. |
---|
| 32 | */ |
---|
[1] | 33 | MainWindow::MainWindow(QWidget *parent) |
---|
[21] | 34 | : QMainWindow(parent) |
---|
[1] | 35 | { |
---|
[29] | 36 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
| 37 | loadLanguage(); |
---|
[1] | 38 | setupUi(this); |
---|
[54] | 39 | #ifndef Q_OS_WINCE |
---|
| 40 | QStatusBar *statusbar = new QStatusBar(this); |
---|
| 41 | statusbar->setObjectName("statusbar"); |
---|
| 42 | setStatusBar(statusbar); |
---|
| 43 | #endif // Q_OS_WINCE |
---|
[42] | 44 | initDocStyleSheet(); |
---|
| 45 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
| 46 | solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); |
---|
| 47 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
[38] | 48 | #ifdef Q_OS_WINCE |
---|
| 49 | // A little hack for toolbar icons to have sane size. |
---|
| 50 | int s = qMin(QApplication::desktop()->screenGeometry().width(),QApplication::desktop()->screenGeometry().height()); |
---|
| 51 | toolBar->setIconSize(QSize(s / 10,s / 10)); |
---|
| 52 | #endif |
---|
[54] | 53 | #ifndef QT_NO_PRINTER |
---|
[52] | 54 | printer = new QPrinter(QPrinter::HighResolution); |
---|
[54] | 55 | #endif // QT_NO_PRINTER |
---|
[29] | 56 | groupSettingsLanguageList = new QActionGroup(this); |
---|
[30] | 57 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 58 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
[29] | 59 | loadLangList(); |
---|
[50] | 60 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
[29] | 61 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
| 62 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
[31] | 63 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
[50] | 64 | connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered())); |
---|
[42] | 65 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
| 66 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
[29] | 67 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
| 68 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
| 69 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
[37] | 70 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
[29] | 71 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
[54] | 72 | #ifndef QT_NO_PRINTER |
---|
| 73 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
| 74 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
| 75 | menuFile->insertSeparator(actionFileExit); |
---|
| 76 | toolBar->insertAction(actionSettingsPreferences,actionFilePrint); |
---|
[52] | 77 | connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered())); |
---|
| 78 | connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered())); |
---|
[54] | 79 | #endif // QT_NO_PRINTER |
---|
[29] | 80 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
| 81 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
[50] | 82 | connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked())); |
---|
[29] | 83 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
[50] | 84 | setCentralWidget(tabWidget); |
---|
[71] | 85 | |
---|
| 86 | if (settings->value("SavePos", false).toBool()) { |
---|
[21] | 87 | // Loading of saved window state |
---|
[23] | 88 | settings->beginGroup("MainWindow"); |
---|
[71] | 89 | #ifndef Q_OS_WINCE |
---|
| 90 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
| 91 | #endif // Q_OS_WINCE |
---|
| 92 | restoreState(settings->value("State").toByteArray()); |
---|
[23] | 93 | settings->endGroup(); |
---|
[71] | 94 | #ifndef Q_OS_WINCE |
---|
[21] | 95 | } else { |
---|
| 96 | // Centering main window |
---|
[71] | 97 | QRect rect = geometry(); |
---|
[21] | 98 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 99 | setGeometry(rect); |
---|
[71] | 100 | #endif // Q_OS_WINCE |
---|
[21] | 101 | } |
---|
[71] | 102 | |
---|
[11] | 103 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[71] | 104 | tspmodel = new CTSPModel(this); |
---|
[52] | 105 | taskView->setModel(tspmodel); |
---|
[31] | 106 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
[57] | 107 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
[37] | 108 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
[50] | 109 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
[47] | 110 | setFileName(QCoreApplication::arguments().at(1)); |
---|
[50] | 111 | else { |
---|
[47] | 112 | setFileName(); |
---|
[50] | 113 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
[52] | 114 | spinCitiesValueChanged(spinCities->value()); |
---|
[50] | 115 | } |
---|
| 116 | setWindowModified(false); |
---|
[6] | 117 | } |
---|
| 118 | |
---|
[67] | 119 | /* Privates **********************************************************/ |
---|
[42] | 120 | |
---|
[29] | 121 | void MainWindow::actionFileNewTriggered() |
---|
[1] | 122 | { |
---|
[47] | 123 | if (!maybeSave()) |
---|
| 124 | return; |
---|
[54] | 125 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[29] | 126 | tspmodel->clear(); |
---|
[47] | 127 | setFileName(); |
---|
[37] | 128 | setWindowModified(false); |
---|
[42] | 129 | tabWidget->setCurrentIndex(0); |
---|
| 130 | solutionText->clear(); |
---|
| 131 | enableSolutionActions(false); |
---|
[54] | 132 | QApplication::restoreOverrideCursor(); |
---|
[29] | 133 | } |
---|
| 134 | |
---|
[31] | 135 | void MainWindow::actionFileOpenTriggered() |
---|
| 136 | { |
---|
[47] | 137 | if (!maybeSave()) |
---|
| 138 | return; |
---|
[31] | 139 | QFileDialog od(this); |
---|
| 140 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
| 141 | od.setFileMode(QFileDialog::ExistingFile); |
---|
| 142 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
[47] | 143 | filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
| 144 | filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
[31] | 145 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 146 | od.setNameFilters(filters); |
---|
| 147 | if (od.exec() != QDialog::Accepted) |
---|
| 148 | return; |
---|
| 149 | QStringList files = od.selectedFiles(); |
---|
[47] | 150 | if (files.empty()) |
---|
[31] | 151 | return; |
---|
[59] | 152 | if (!tspmodel->loadTask(files.first())) |
---|
[47] | 153 | return; |
---|
| 154 | setFileName(files.first()); |
---|
| 155 | tabWidget->setCurrentIndex(0); |
---|
[37] | 156 | setWindowModified(false); |
---|
[42] | 157 | solutionText->clear(); |
---|
| 158 | enableSolutionActions(false); |
---|
[31] | 159 | } |
---|
| 160 | |
---|
[50] | 161 | void MainWindow::actionFileSaveTriggered() |
---|
| 162 | { |
---|
| 163 | if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive))) |
---|
| 164 | saveTask(); |
---|
[59] | 165 | else |
---|
[50] | 166 | if (tspmodel->saveTask(fileName)) |
---|
| 167 | setWindowModified(false); |
---|
| 168 | } |
---|
| 169 | |
---|
[42] | 170 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
[31] | 171 | { |
---|
[37] | 172 | saveTask(); |
---|
| 173 | } |
---|
| 174 | |
---|
[42] | 175 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
| 176 | { |
---|
| 177 | static QString selectedFile; |
---|
| 178 | if (selectedFile.isEmpty()) |
---|
[55] | 179 | #ifndef QT_NO_PRINTER |
---|
| 180 | selectedFile = "solution.pdf"; |
---|
| 181 | #else |
---|
[42] | 182 | selectedFile = "solution.html"; |
---|
[55] | 183 | #endif // QT_NO_PRINTER |
---|
[42] | 184 | QFileDialog sd(this); |
---|
| 185 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
[55] | 186 | QStringList filters; |
---|
| 187 | #ifndef QT_NO_PRINTER |
---|
| 188 | filters.append(trUtf8("PDF Files") + "(*.pdf)"); |
---|
| 189 | #endif |
---|
| 190 | filters.append(trUtf8("HTML Files") + " (*.html *.htm)"); |
---|
[45] | 191 | #if QT_VERSION >= 0x040500 |
---|
[42] | 192 | filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); |
---|
[45] | 193 | #endif // QT_VERSION >= 0x040500 |
---|
[42] | 194 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 195 | sd.setNameFilters(filters); |
---|
| 196 | sd.selectFile(selectedFile); |
---|
| 197 | if (sd.exec() != QDialog::Accepted) |
---|
| 198 | return; |
---|
| 199 | QStringList files = sd.selectedFiles(); |
---|
| 200 | if (files.empty()) |
---|
| 201 | return; |
---|
| 202 | selectedFile = files.first(); |
---|
[51] | 203 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[55] | 204 | #ifndef QT_NO_PRINTER |
---|
| 205 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
| 206 | QPrinter printer(QPrinter::HighResolution); |
---|
| 207 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
| 208 | printer.setOutputFileName(selectedFile); |
---|
| 209 | solutionText->document()->print(&printer); |
---|
| 210 | QApplication::restoreOverrideCursor(); |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | #endif |
---|
[45] | 214 | #if QT_VERSION >= 0x040500 |
---|
[42] | 215 | QTextDocumentWriter dw(selectedFile); |
---|
| 216 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
| 217 | dw.setFormat("plaintext"); |
---|
| 218 | dw.write(solutionText->document()); |
---|
[45] | 219 | #else |
---|
| 220 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
| 221 | QFile file(selectedFile); |
---|
[51] | 222 | if (!file.open(QFile::WriteOnly)) { |
---|
| 223 | QApplication::restoreOverrideCursor(); |
---|
[45] | 224 | return; |
---|
[51] | 225 | } |
---|
[45] | 226 | QTextStream ts(&file); |
---|
| 227 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
| 228 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
[51] | 229 | file.close(); |
---|
[45] | 230 | #endif // QT_VERSION >= 0x040500 |
---|
[51] | 231 | QApplication::restoreOverrideCursor(); |
---|
[42] | 232 | } |
---|
| 233 | |
---|
[67] | 234 | #ifndef QT_NO_PRINTER |
---|
| 235 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
| 236 | { |
---|
| 237 | QPrintPreviewDialog ppd(printer, this); |
---|
| 238 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
| 239 | ppd.exec(); |
---|
[31] | 240 | } |
---|
| 241 | |
---|
[67] | 242 | void MainWindow::actionFilePrintTriggered() |
---|
| 243 | { |
---|
| 244 | QPrintDialog pd(printer,this); |
---|
| 245 | #if QT_VERSION >= 0x040500 |
---|
| 246 | // No such methods in Qt < 4.5 |
---|
| 247 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 248 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
| 249 | #endif |
---|
| 250 | if (pd.exec() != QDialog::Accepted) |
---|
| 251 | return; |
---|
| 252 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 253 | solutionText->document()->print(printer); |
---|
| 254 | QApplication::restoreOverrideCursor(); |
---|
| 255 | } |
---|
| 256 | #endif // QT_NO_PRINTER |
---|
| 257 | |
---|
[29] | 258 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
| 259 | { |
---|
[1] | 260 | SettingsDialog sd(this); |
---|
[42] | 261 | if (sd.exec() != QDialog::Accepted) |
---|
| 262 | return; |
---|
| 263 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
| 264 | initDocStyleSheet(); |
---|
| 265 | if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,trUtf8("Settings Changed"),trUtf8("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) { |
---|
| 266 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 267 | solutionText->clear(); |
---|
| 268 | solutionText->setHtml(output.join("")); |
---|
| 269 | QApplication::restoreOverrideCursor(); |
---|
| 270 | } |
---|
| 271 | } |
---|
[1] | 272 | } |
---|
[6] | 273 | |
---|
[67] | 274 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
[17] | 275 | { |
---|
[67] | 276 | if (checked) { |
---|
| 277 | settings->remove("Language"); |
---|
| 278 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
| 279 | } else |
---|
| 280 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
[52] | 281 | } |
---|
| 282 | |
---|
[67] | 283 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
[52] | 284 | { |
---|
[67] | 285 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 286 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 287 | if (QMessageBox(QMessageBox::Question,trUtf8("Language change"),trUtf8("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) { |
---|
| 288 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 289 | } else |
---|
| 290 | return; |
---|
| 291 | } |
---|
| 292 | bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); |
---|
| 293 | if (loadLanguage(action->data().toString())) { |
---|
| 294 | settings->setValue("Language",action->data().toString()); |
---|
| 295 | retranslateUi(this); |
---|
| 296 | if (untitled) |
---|
| 297 | setFileName(); |
---|
| 298 | } |
---|
[52] | 299 | } |
---|
| 300 | |
---|
[67] | 301 | void MainWindow::actionHelpAboutTriggered() |
---|
[52] | 302 | { |
---|
[67] | 303 | //! \todo TODO: Normal about window :-) |
---|
| 304 | QString about = QString::fromUtf8("TSPSG: TSP Solver and Generator\n"); |
---|
| 305 | about += QString::fromUtf8(" Version: "BUILD_VERSION"\n"); |
---|
| 306 | about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); |
---|
| 307 | about += QString::fromUtf8("Target OS: %1\n").arg(OS); |
---|
| 308 | about += "Qt library:\n"; |
---|
| 309 | about += QString::fromUtf8(" Compile time: %1\n").arg(QT_VERSION_STR); |
---|
| 310 | about += QString::fromUtf8(" Runtime: %1\n").arg(qVersion()); |
---|
| 311 | about += QString::fromUtf8("Built on %1 at %2\n").arg(__DATE__).arg(__TIME__); |
---|
| 312 | about += QString::fromUtf8(VERSIONID"\n\n"); |
---|
| 313 | about += QString::fromUtf8("Algorithm: %1\n").arg(CTSPSolver::getVersionId()); |
---|
| 314 | about += "\n"; |
---|
| 315 | about += "TSPSG is licensed under the terms of the GNU General Public License. You should have received a copy of the GNU General Public License along with TSPSG."; |
---|
| 316 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
[17] | 317 | } |
---|
| 318 | |
---|
[50] | 319 | void MainWindow::buttonBackToTaskClicked() |
---|
| 320 | { |
---|
| 321 | tabWidget->setCurrentIndex(0); |
---|
| 322 | } |
---|
| 323 | |
---|
[67] | 324 | void MainWindow::buttonRandomClicked() |
---|
[42] | 325 | { |
---|
[67] | 326 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 327 | tspmodel->randomize(); |
---|
| 328 | QApplication::restoreOverrideCursor(); |
---|
[42] | 329 | } |
---|
| 330 | |
---|
[29] | 331 | void MainWindow::buttonSolveClicked() |
---|
[6] | 332 | { |
---|
[13] | 333 | tMatrix matrix; |
---|
[42] | 334 | QList<double> row; |
---|
[15] | 335 | int n = spinCities->value(); |
---|
[13] | 336 | bool ok; |
---|
[15] | 337 | for (int r = 0; r < n; r++) { |
---|
[42] | 338 | row.clear(); |
---|
[15] | 339 | for (int c = 0; c < n; c++) { |
---|
[42] | 340 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
[15] | 341 | if (!ok) { |
---|
[47] | 342 | 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(); |
---|
[15] | 343 | return; |
---|
[13] | 344 | } |
---|
| 345 | } |
---|
| 346 | matrix.append(row); |
---|
| 347 | } |
---|
| 348 | CTSPSolver solver; |
---|
[50] | 349 | sStep *root = solver.solve(n,matrix,this); |
---|
[13] | 350 | if (!root) |
---|
[42] | 351 | return; |
---|
| 352 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 353 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
| 354 | output.clear(); |
---|
| 355 | output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
| 356 | output.append("<p>" + trUtf8("Task:") + "</p>"); |
---|
| 357 | outputMatrix(matrix,output); |
---|
| 358 | output.append("<hr>"); |
---|
| 359 | output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
| 360 | sStep *step = root; |
---|
| 361 | n = 1; |
---|
| 362 | while (n <= spinCities->value()) { |
---|
| 363 | if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) { |
---|
| 364 | if (n != spinCities->value()) { |
---|
| 365 | output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>"); |
---|
| 366 | outputMatrix(step->matrix,output,step->candidate.nRow,step->candidate.nCol); |
---|
| 367 | if (step->alts) |
---|
| 368 | output.append("<p class=\"hasalts\">" + trUtf8("This step has alternate candidates for branching.") + "</p>"); |
---|
| 369 | output.append("<p> </p>"); |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | if (step->prNode->prNode != NULL) |
---|
| 373 | step = step->prNode; |
---|
| 374 | else if (step->plNode->prNode != NULL) |
---|
| 375 | step = step->plNode; |
---|
| 376 | else |
---|
| 377 | break; |
---|
| 378 | } |
---|
[60] | 379 | if (solver.isOptimal()) |
---|
| 380 | output.append("<p>" + trUtf8("Optimal path:") + "</p>"); |
---|
| 381 | else |
---|
| 382 | output.append("<p>" + trUtf8("Resulting path:") + "</p>"); |
---|
| 383 | output.append("<p> " + solver.getSortedPath() + "</p>"); |
---|
[42] | 384 | output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); |
---|
[60] | 385 | if (!solver.isOptimal()) { |
---|
| 386 | output.append("<p> </p>"); |
---|
| 387 | output.append("<p>" + trUtf8("<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>"); |
---|
| 388 | } |
---|
[65] | 389 | output.append("<p></p>"); |
---|
[42] | 390 | solutionText->setHtml(output.join("")); |
---|
| 391 | solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
[65] | 392 | |
---|
| 393 | // Scrolling to the end of text. |
---|
| 394 | QTextCursor cursor(solutionText->textCursor()); |
---|
| 395 | cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); |
---|
| 396 | solutionText->setTextCursor(cursor); |
---|
| 397 | |
---|
[42] | 398 | enableSolutionActions(); |
---|
| 399 | tabWidget->setCurrentIndex(1); |
---|
| 400 | QApplication::restoreOverrideCursor(); |
---|
[6] | 401 | } |
---|
[21] | 402 | |
---|
[67] | 403 | void MainWindow::dataChanged() |
---|
[21] | 404 | { |
---|
[67] | 405 | setWindowModified(true); |
---|
[21] | 406 | } |
---|
| 407 | |
---|
[67] | 408 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
| 409 | { |
---|
| 410 | setWindowModified(true); |
---|
| 411 | if (settings->value("Autosize",true).toBool()) { |
---|
| 412 | for (int k = tl.row(); k <= br.row(); k++) |
---|
| 413 | taskView->resizeRowToContents(k); |
---|
| 414 | for (int k = tl.column(); k <= br.column(); k++) |
---|
| 415 | taskView->resizeColumnToContents(k); |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 420 | { |
---|
| 421 | blockSignals(true); |
---|
| 422 | spinCities->setValue(nCities); |
---|
| 423 | blockSignals(false); |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | #ifndef QT_NO_PRINTER |
---|
| 427 | void MainWindow::printPreview(QPrinter *printer) |
---|
| 428 | { |
---|
| 429 | solutionText->print(printer); |
---|
| 430 | } |
---|
| 431 | #endif // QT_NO_PRINTER |
---|
| 432 | |
---|
| 433 | void MainWindow::spinCitiesValueChanged(int n) |
---|
| 434 | { |
---|
| 435 | int count = tspmodel->numCities(); |
---|
| 436 | tspmodel->setNumCities(n); |
---|
| 437 | if ((n > count) && settings->value("Autosize",true).toBool()) |
---|
| 438 | for (int k = count; k < n; k++) { |
---|
| 439 | taskView->resizeColumnToContents(k); |
---|
| 440 | taskView->resizeRowToContents(k); |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
[71] | 444 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
[69] | 445 | { |
---|
| 446 | if (!maybeSave()) { |
---|
[71] | 447 | ev->ignore(); |
---|
[69] | 448 | return; |
---|
| 449 | } |
---|
[71] | 450 | settings->setValue("NumCities", spinCities->value()); |
---|
| 451 | |
---|
| 452 | // Saving Main Window state |
---|
| 453 | if (settings->value("SavePos", false).toBool()) { |
---|
| 454 | settings->beginGroup("MainWindow"); |
---|
[69] | 455 | #ifndef Q_OS_WINCE |
---|
[71] | 456 | settings->setValue("Geometry", saveGeometry()); |
---|
| 457 | #endif // Q_OS_WINCE |
---|
| 458 | settings->setValue("State", saveState()); |
---|
[69] | 459 | settings->endGroup(); |
---|
| 460 | } |
---|
[71] | 461 | |
---|
| 462 | QMainWindow::closeEvent(ev); |
---|
[69] | 463 | } |
---|
| 464 | |
---|
[67] | 465 | void MainWindow::enableSolutionActions(bool enable) |
---|
| 466 | { |
---|
| 467 | buttonSaveSolution->setEnabled(enable); |
---|
| 468 | actionFileSaveAsSolution->setEnabled(enable); |
---|
| 469 | solutionText->setEnabled(enable); |
---|
| 470 | if (!enable) |
---|
| 471 | output.clear(); |
---|
| 472 | #ifndef QT_NO_PRINTER |
---|
| 473 | actionFilePrint->setEnabled(enable); |
---|
| 474 | actionFilePrintPreview->setEnabled(enable); |
---|
| 475 | #endif // QT_NO_PRINTER |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | void MainWindow::initDocStyleSheet() |
---|
| 479 | { |
---|
| 480 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
| 481 | QColor hilight; |
---|
| 482 | if (color.value() < 192) |
---|
| 483 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
| 484 | else |
---|
| 485 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
| 486 | solutionText->document()->setDefaultStyleSheet("* {color: " + color.name() +";} p {margin: 0px 10px;} table {margin: 5px;} td {padding: 1px 5px;} .hasalts {color: " + hilight.name() + ";} .selected {color: #A00000; font-weight: bold;} .alternate {color: #008000; font-weight: bold;}"); |
---|
| 487 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
| 488 | } |
---|
| 489 | |
---|
[29] | 490 | void MainWindow::loadLangList() |
---|
| 491 | { |
---|
[57] | 492 | QSettings langinfo(PATH_I18N"/languages.ini",QSettings::IniFormat); |
---|
[33] | 493 | #if QT_VERSION >= 0x040500 |
---|
| 494 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
[29] | 495 | langinfo.setIniCodec("UTF-8"); |
---|
[33] | 496 | #endif |
---|
[57] | 497 | QDir dir(PATH_I18N,"*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
[29] | 498 | if (!dir.exists()) |
---|
| 499 | return; |
---|
| 500 | QFileInfoList langs = dir.entryInfoList(); |
---|
| 501 | if (langs.size() <= 0) |
---|
| 502 | return; |
---|
| 503 | QAction *a; |
---|
| 504 | for (int k = 0; k < langs.size(); k++) { |
---|
| 505 | QFileInfo lang = langs.at(k); |
---|
[30] | 506 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
[33] | 507 | #if QT_VERSION >= 0x040500 |
---|
[29] | 508 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
[33] | 509 | #else |
---|
| 510 | // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings |
---|
| 511 | // reads .ini file as ASCII and there is no way to set file encoding. |
---|
| 512 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString()); |
---|
| 513 | #endif |
---|
[29] | 514 | a->setData(lang.completeBaseName()); |
---|
| 515 | a->setCheckable(true); |
---|
| 516 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 517 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
| 518 | a->setChecked(true); |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
[71] | 523 | bool MainWindow::loadLanguage(const QString &lang) |
---|
[29] | 524 | { |
---|
[67] | 525 | // i18n |
---|
| 526 | bool ad = false; |
---|
[71] | 527 | QString lng = lang; |
---|
| 528 | if (lng.isEmpty()) { |
---|
[67] | 529 | ad = settings->value("Language","").toString().isEmpty(); |
---|
[71] | 530 | lng = settings->value("Language",QLocale::system().name()).toString(); |
---|
[29] | 531 | } |
---|
[67] | 532 | static QTranslator *qtTranslator; // Qt library translator |
---|
| 533 | if (qtTranslator) { |
---|
| 534 | qApp->removeTranslator(qtTranslator); |
---|
| 535 | delete qtTranslator; |
---|
| 536 | qtTranslator = NULL; |
---|
[29] | 537 | } |
---|
[67] | 538 | qtTranslator = new QTranslator(); |
---|
| 539 | static QTranslator *translator; // Application translator |
---|
| 540 | if (translator) { |
---|
| 541 | qApp->removeTranslator(translator); |
---|
| 542 | delete translator; |
---|
[37] | 543 | } |
---|
[67] | 544 | translator = new QTranslator(); |
---|
[71] | 545 | if (lng.compare("en") && !lng.startsWith("en_")) { |
---|
[67] | 546 | // Trying to load system Qt library translation... |
---|
[71] | 547 | if (qtTranslator->load("qt_" + lng,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
[67] | 548 | qApp->installTranslator(qtTranslator); |
---|
| 549 | else |
---|
| 550 | // No luck. Let's try to load bundled one. |
---|
[71] | 551 | if (qtTranslator->load("qt_" + lng,PATH_I18N)) |
---|
[67] | 552 | qApp->installTranslator(qtTranslator); |
---|
| 553 | else { |
---|
| 554 | // Qt library translation unavailable |
---|
| 555 | delete qtTranslator; |
---|
| 556 | qtTranslator = NULL; |
---|
| 557 | } |
---|
| 558 | // Now let's load application translation. |
---|
[71] | 559 | if (translator->load(lng,PATH_I18N)) |
---|
[67] | 560 | qApp->installTranslator(translator); |
---|
| 561 | else { |
---|
| 562 | if (!ad) |
---|
| 563 | QMessageBox(QMessageBox::Warning,trUtf8("Language Change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); |
---|
| 564 | delete translator; |
---|
| 565 | translator = NULL; |
---|
| 566 | return false; |
---|
[21] | 567 | } |
---|
| 568 | } |
---|
[67] | 569 | return true; |
---|
[21] | 570 | } |
---|
[31] | 571 | |
---|
[67] | 572 | bool MainWindow::maybeSave() |
---|
[37] | 573 | { |
---|
[67] | 574 | if (!isWindowModified()) |
---|
| 575 | return true; |
---|
| 576 | 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(); |
---|
| 577 | if (res == QMessageBox::Save) |
---|
| 578 | return saveTask(); |
---|
| 579 | else if (res == QMessageBox::Cancel) |
---|
| 580 | return false; |
---|
| 581 | else |
---|
| 582 | return true; |
---|
[37] | 583 | } |
---|
| 584 | |
---|
[71] | 585 | void MainWindow::outputMatrix(const tMatrix &matrix, QStringList &output, int nRow, int nCol) |
---|
[57] | 586 | { |
---|
[67] | 587 | int n = spinCities->value(); |
---|
| 588 | QString line=""; |
---|
| 589 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
| 590 | for (int r = 0; r < n; r++) { |
---|
| 591 | line = "<tr>"; |
---|
| 592 | for (int c = 0; c < n; c++) { |
---|
[71] | 593 | if (matrix.at(r).at(c) == INFINITY) |
---|
[67] | 594 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
| 595 | else if ((r == nRow) && (c == nCol)) |
---|
[71] | 596 | line += "<td align=\"center\" class=\"selected\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>"; |
---|
[67] | 597 | else |
---|
[71] | 598 | line += "<td align=\"center\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>"; |
---|
[67] | 599 | } |
---|
| 600 | line += "</tr>"; |
---|
| 601 | output.append(line); |
---|
[57] | 602 | } |
---|
[67] | 603 | output.append("</table>"); |
---|
[57] | 604 | } |
---|
| 605 | |
---|
[67] | 606 | bool MainWindow::saveTask() { |
---|
| 607 | QFileDialog sd(this); |
---|
| 608 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
| 609 | QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
| 610 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 611 | sd.setNameFilters(filters); |
---|
| 612 | sd.setDefaultSuffix("tspt"); |
---|
| 613 | if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) |
---|
| 614 | sd.selectFile(fileName); |
---|
| 615 | else |
---|
| 616 | sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); |
---|
| 617 | if (sd.exec() != QDialog::Accepted) |
---|
| 618 | return false; |
---|
| 619 | QStringList files = sd.selectedFiles(); |
---|
| 620 | if (files.empty()) |
---|
| 621 | return false; |
---|
| 622 | if (tspmodel->saveTask(files.first())) { |
---|
| 623 | setFileName(files.first()); |
---|
| 624 | setWindowModified(false); |
---|
| 625 | return true; |
---|
| 626 | } |
---|
| 627 | return false; |
---|
| 628 | } |
---|
| 629 | |
---|
[71] | 630 | void MainWindow::setFileName(const QString &fileName) |
---|
[31] | 631 | { |
---|
[67] | 632 | this->fileName = fileName; |
---|
| 633 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem"))); |
---|
[31] | 634 | } |
---|