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