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