[ec54b4490b] | 1 | /* |
---|
[430bd7f7e9] | 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())); |
---|
[b424a7e320] | 89 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) { |
---|
| 90 | setFileName(QCoreApplication::arguments().at(1)); |
---|
[ec54b4490b] | 91 | setWindowModified(false); |
---|
[b424a7e320] | 92 | } else |
---|
| 93 | setFileName(); |
---|
[aecdf994f9] | 94 | #ifdef Q_OS_WINCE |
---|
| 95 | taskView->resizeColumnsToContents(); |
---|
| 96 | taskView->resizeRowsToContents(); |
---|
| 97 | #endif // Q_OS_WINCE |
---|
[003e4193be] | 98 | } |
---|
[052d1b9331] | 99 | |
---|
[430bd7f7e9] | 100 | void MainWindow::enableSolutionActions(bool enable) |
---|
| 101 | { |
---|
| 102 | actionFileSaveAsSolution->setEnabled(enable); |
---|
| 103 | solutionText->setEnabled(enable); |
---|
| 104 | if (!enable) |
---|
| 105 | output.clear(); |
---|
| 106 | } |
---|
| 107 | |
---|
[ac4cb71650] | 108 | bool MainWindow::loadLanguage(QString lang) |
---|
[899d1b8e15] | 109 | { |
---|
| 110 | // i18n |
---|
[ac4cb71650] | 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 | } |
---|
[899d1b8e15] | 116 | static QTranslator *qtTranslator; |
---|
| 117 | if (qtTranslator) { |
---|
| 118 | qApp->removeTranslator(qtTranslator); |
---|
| 119 | delete qtTranslator; |
---|
| 120 | qtTranslator = NULL; |
---|
| 121 | } |
---|
| 122 | qtTranslator = new QTranslator(); |
---|
[ac4cb71650] | 123 | static QTranslator *translator; |
---|
| 124 | if (translator) { |
---|
| 125 | qApp->removeTranslator(translator); |
---|
| 126 | delete translator; |
---|
| 127 | } |
---|
| 128 | translator = new QTranslator(); |
---|
[899d1b8e15] | 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 | } |
---|
[ac4cb71650] | 141 | // Now let's load application translation. |
---|
[899d1b8e15] | 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 | |
---|
[430bd7f7e9] | 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 | |
---|
[b424a7e320] | 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 | |
---|
[899d1b8e15] | 173 | void MainWindow::spinCitiesValueChanged(int n) |
---|
[003e4193be] | 174 | { |
---|
[aecdf994f9] | 175 | #ifdef Q_OS_WINCE |
---|
| 176 | int count = tspmodel->numCities(); |
---|
| 177 | #endif // Q_OS_WINCE |
---|
[2bc8e278b7] | 178 | tspmodel->setNumCities(n); |
---|
[aecdf994f9] | 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 |
---|
[5515c2c2a7] | 186 | } |
---|
| 187 | |
---|
[b424a7e320] | 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 | } |
---|
[899d1b8e15] | 200 | |
---|
| 201 | void MainWindow::actionFileNewTriggered() |
---|
| 202 | { |
---|
[b424a7e320] | 203 | if (!maybeSave()) |
---|
| 204 | return; |
---|
[899d1b8e15] | 205 | tspmodel->clear(); |
---|
[b424a7e320] | 206 | #ifdef Q_OS_WINCE |
---|
| 207 | taskView->resizeColumnsToContents(); |
---|
| 208 | taskView->resizeRowsToContents(); |
---|
| 209 | #endif |
---|
| 210 | setFileName(); |
---|
[690f6939a7] | 211 | setWindowModified(false); |
---|
[430bd7f7e9] | 212 | tabWidget->setCurrentIndex(0); |
---|
| 213 | solutionText->clear(); |
---|
| 214 | enableSolutionActions(false); |
---|
[899d1b8e15] | 215 | } |
---|
| 216 | |
---|
[993d5af6f6] | 217 | void MainWindow::actionFileOpenTriggered() |
---|
| 218 | { |
---|
[b424a7e320] | 219 | if (!maybeSave()) |
---|
| 220 | return; |
---|
[993d5af6f6] | 221 | QFileDialog od(this); |
---|
| 222 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
| 223 | od.setFileMode(QFileDialog::ExistingFile); |
---|
| 224 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
[b424a7e320] | 225 | filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
| 226 | filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
[993d5af6f6] | 227 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 228 | od.setNameFilters(filters); |
---|
| 229 | if (od.exec() != QDialog::Accepted) |
---|
| 230 | return; |
---|
| 231 | QStringList files = od.selectedFiles(); |
---|
[b424a7e320] | 232 | if (files.empty()) |
---|
| 233 | return; |
---|
| 234 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 235 | if (!tspmodel->loadTask(files.first())) { |
---|
| 236 | QApplication::restoreOverrideCursor(); |
---|
[993d5af6f6] | 237 | return; |
---|
[b424a7e320] | 238 | } |
---|
| 239 | setFileName(files.first()); |
---|
| 240 | #ifdef Q_OS_WINCE |
---|
| 241 | taskView->resizeColumnsToContents(); |
---|
| 242 | taskView->resizeRowsToContents(); |
---|
| 243 | #endif |
---|
| 244 | tabWidget->setCurrentIndex(0); |
---|
[690f6939a7] | 245 | setWindowModified(false); |
---|
[430bd7f7e9] | 246 | solutionText->clear(); |
---|
| 247 | enableSolutionActions(false); |
---|
[b424a7e320] | 248 | QApplication::restoreOverrideCursor(); |
---|
[993d5af6f6] | 249 | } |
---|
| 250 | |
---|
[430bd7f7e9] | 251 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
[993d5af6f6] | 252 | { |
---|
[690f6939a7] | 253 | saveTask(); |
---|
| 254 | } |
---|
| 255 | |
---|
[430bd7f7e9] | 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)"); |
---|
[ec54b4490b] | 264 | #if QT_VERSION >= 0x040500 |
---|
[430bd7f7e9] | 265 | filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); |
---|
[ec54b4490b] | 266 | #endif // QT_VERSION >= 0x040500 |
---|
[430bd7f7e9] | 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(); |
---|
[ec54b4490b] | 276 | #if QT_VERSION >= 0x040500 |
---|
[430bd7f7e9] | 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()); |
---|
[ec54b4490b] | 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 |
---|
[430bd7f7e9] | 290 | } |
---|
| 291 | |
---|
[690f6939a7] | 292 | bool MainWindow::saveTask() { |
---|
[993d5af6f6] | 293 | QFileDialog sd(this); |
---|
| 294 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
[b424a7e320] | 295 | QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
[993d5af6f6] | 296 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 297 | sd.setNameFilters(filters); |
---|
| 298 | sd.setDefaultSuffix("tspt"); |
---|
[b424a7e320] | 299 | if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) |
---|
| 300 | sd.selectFile(fileName); |
---|
| 301 | else |
---|
| 302 | sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); |
---|
[993d5af6f6] | 303 | if (sd.exec() != QDialog::Accepted) |
---|
[690f6939a7] | 304 | return false; |
---|
[993d5af6f6] | 305 | QStringList files = sd.selectedFiles(); |
---|
[b424a7e320] | 306 | if (files.empty()) |
---|
[690f6939a7] | 307 | return false; |
---|
[b424a7e320] | 308 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[690f6939a7] | 309 | if (tspmodel->saveTask(files.first())) { |
---|
[b424a7e320] | 310 | setFileName(files.first()); |
---|
[690f6939a7] | 311 | setWindowModified(false); |
---|
[b424a7e320] | 312 | QApplication::restoreOverrideCursor(); |
---|
[690f6939a7] | 313 | return true; |
---|
[b424a7e320] | 314 | } |
---|
| 315 | QApplication::restoreOverrideCursor(); |
---|
| 316 | return false; |
---|
[993d5af6f6] | 317 | } |
---|
| 318 | |
---|
[899d1b8e15] | 319 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
[5515c2c2a7] | 320 | { |
---|
| 321 | SettingsDialog sd(this); |
---|
[430bd7f7e9] | 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 | } |
---|
[5515c2c2a7] | 333 | } |
---|
[bb994a7ff8] | 334 | |
---|
[5354a01311] | 335 | #ifndef Q_OS_WINCE |
---|
[899d1b8e15] | 336 | void MainWindow::actionFilePrintSetupTriggered() |
---|
[5354a01311] | 337 | { |
---|
[899d1b8e15] | 338 | QPrintDialog pd(printer,this); |
---|
[140912822f] | 339 | #if QT_VERSION >= 0x040500 |
---|
| 340 | // No such methods in Qt < 4.5 |
---|
[899d1b8e15] | 341 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 342 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
[140912822f] | 343 | #endif |
---|
[5354a01311] | 344 | pd.exec(); |
---|
| 345 | } |
---|
| 346 | #endif // Q_OS_WINCE |
---|
| 347 | |
---|
[899d1b8e15] | 348 | void MainWindow::buttonRandomClicked() |
---|
[bb994a7ff8] | 349 | { |
---|
[2bc8e278b7] | 350 | tspmodel->randomize(); |
---|
[690f6939a7] | 351 | setWindowModified(true); |
---|
[aecdf994f9] | 352 | #ifdef Q_OS_WINCE |
---|
| 353 | taskView->resizeColumnsToContents(); |
---|
| 354 | taskView->resizeRowsToContents(); |
---|
| 355 | #endif // Q_OS_WINCE |
---|
[bb994a7ff8] | 356 | } |
---|
| 357 | |
---|
[430bd7f7e9] | 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 | |
---|
[899d1b8e15] | 379 | void MainWindow::buttonSolveClicked() |
---|
[bb994a7ff8] | 380 | { |
---|
[e664262f7d] | 381 | tMatrix matrix; |
---|
[430bd7f7e9] | 382 | QList<double> row; |
---|
[2bc8e278b7] | 383 | int n = spinCities->value(); |
---|
[e664262f7d] | 384 | bool ok; |
---|
[2bc8e278b7] | 385 | for (int r = 0; r < n; r++) { |
---|
[430bd7f7e9] | 386 | row.clear(); |
---|
[2bc8e278b7] | 387 | for (int c = 0; c < n; c++) { |
---|
[430bd7f7e9] | 388 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
[2bc8e278b7] | 389 | if (!ok) { |
---|
[b424a7e320] | 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(); |
---|
[2bc8e278b7] | 391 | return; |
---|
[e664262f7d] | 392 | } |
---|
| 393 | } |
---|
| 394 | matrix.append(row); |
---|
| 395 | } |
---|
| 396 | CTSPSolver solver; |
---|
| 397 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
| 398 | if (!root) |
---|
[430bd7f7e9] | 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(); |
---|
[bb994a7ff8] | 437 | } |
---|
[aecdf994f9] | 438 | |
---|
[899d1b8e15] | 439 | void MainWindow::actionHelpAboutTriggered() |
---|
[aecdf994f9] | 440 | { |
---|
| 441 | // TODO: Normal about window :-) |
---|
[ac4cb71650] | 442 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n"); |
---|
[ec54b4490b] | 443 | about += QString::fromUtf8(" Version: "BUILD_VERSION"\n"); |
---|
[430bd7f7e9] | 444 | about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); |
---|
[690f6939a7] | 445 | about += QString::fromUtf8("Target OS: %1\n").arg(OS); |
---|
| 446 | about += "Qt library:\n"; |
---|
[ac4cb71650] | 447 | about += QString::fromUtf8(" Compile time: %1\n").arg(QT_VERSION_STR); |
---|
| 448 | about += QString::fromUtf8(" Runtime: %1\n").arg(qVersion()); |
---|
[b5c9bcb585] | 449 | about += QString::fromUtf8("Built on %1 at %2\n").arg(__DATE__).arg(__TIME__); |
---|
[ac4cb71650] | 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."; |
---|
[5587b87fac] | 452 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
[aecdf994f9] | 453 | } |
---|
| 454 | |
---|
[899d1b8e15] | 455 | void MainWindow::loadLangList() |
---|
| 456 | { |
---|
| 457 | QSettings langinfo("i18n/languages.ini",QSettings::IniFormat); |
---|
[140912822f] | 458 | #if QT_VERSION >= 0x040500 |
---|
| 459 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
[899d1b8e15] | 460 | langinfo.setIniCodec("UTF-8"); |
---|
[140912822f] | 461 | #endif |
---|
[899d1b8e15] | 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); |
---|
[ac4cb71650] | 471 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
[140912822f] | 472 | #if QT_VERSION >= 0x040500 |
---|
[899d1b8e15] | 473 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
[140912822f] | 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 |
---|
[899d1b8e15] | 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 | } |
---|
[b424a7e320] | 506 | bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); |
---|
[ac4cb71650] | 507 | if (loadLanguage(action->data().toString())) { |
---|
[899d1b8e15] | 508 | settings->setValue("Language",action->data().toString()); |
---|
| 509 | retranslateUi(this); |
---|
[b424a7e320] | 510 | if (untitled) |
---|
| 511 | setFileName(); |
---|
[899d1b8e15] | 512 | } |
---|
| 513 | } |
---|
| 514 | |
---|
[aecdf994f9] | 515 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
| 516 | { |
---|
[b424a7e320] | 517 | if (!maybeSave()) { |
---|
| 518 | event->ignore(); |
---|
| 519 | return; |
---|
[690f6939a7] | 520 | } |
---|
[665d32434f] | 521 | settings->setValue("NumCities",spinCities->value()); |
---|
[0621172ec1] | 522 | #ifndef Q_OS_WINCE |
---|
| 523 | // Saving windows state |
---|
[665d32434f] | 524 | if (settings->value("SavePos",false).toBool()) { |
---|
| 525 | settings->beginGroup("MainWindow"); |
---|
| 526 | settings->setValue("Maximized",isMaximized()); |
---|
[aecdf994f9] | 527 | if (!isMaximized()) { |
---|
[665d32434f] | 528 | settings->setValue("Size",size()); |
---|
| 529 | settings->setValue("Position",pos()); |
---|
[aecdf994f9] | 530 | } |
---|
[665d32434f] | 531 | settings->endGroup(); |
---|
[aecdf994f9] | 532 | } |
---|
[0621172ec1] | 533 | #endif // Q_OS_WINCE |
---|
[aecdf994f9] | 534 | QMainWindow::closeEvent(event); |
---|
| 535 | } |
---|
[993d5af6f6] | 536 | |
---|
[690f6939a7] | 537 | void MainWindow::dataChanged() |
---|
| 538 | { |
---|
| 539 | setWindowModified(true); |
---|
| 540 | } |
---|
| 541 | |
---|
[993d5af6f6] | 542 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 543 | { |
---|
| 544 | spinCities->setValue(nCities); |
---|
| 545 | } |
---|