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