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