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