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