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