[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 74 2009-12-16 22:22:05Z 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 | |
---|
[65] | 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 | */ |
---|
[1] | 33 | MainWindow::MainWindow(QWidget *parent) |
---|
[21] | 34 | : QMainWindow(parent) |
---|
[1] | 35 | { |
---|
[29] | 36 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
| 37 | loadLanguage(); |
---|
[1] | 38 | setupUi(this); |
---|
[54] | 39 | #ifndef Q_OS_WINCE |
---|
| 40 | QStatusBar *statusbar = new QStatusBar(this); |
---|
| 41 | statusbar->setObjectName("statusbar"); |
---|
| 42 | setStatusBar(statusbar); |
---|
| 43 | #endif // Q_OS_WINCE |
---|
[42] | 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); |
---|
[38] | 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 |
---|
[54] | 53 | #ifndef QT_NO_PRINTER |
---|
[52] | 54 | printer = new QPrinter(QPrinter::HighResolution); |
---|
[54] | 55 | #endif // QT_NO_PRINTER |
---|
[29] | 56 | groupSettingsLanguageList = new QActionGroup(this); |
---|
[30] | 57 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 58 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
[29] | 59 | loadLangList(); |
---|
[50] | 60 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
[29] | 61 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
| 62 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
[31] | 63 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
[50] | 64 | connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered())); |
---|
[42] | 65 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
| 66 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
[29] | 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 *))); |
---|
[37] | 70 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
[29] | 71 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
[54] | 72 | #ifndef QT_NO_PRINTER |
---|
| 73 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
| 74 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
| 75 | menuFile->insertSeparator(actionFileExit); |
---|
| 76 | toolBar->insertAction(actionSettingsPreferences,actionFilePrint); |
---|
[52] | 77 | connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered())); |
---|
| 78 | connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered())); |
---|
[54] | 79 | #endif // QT_NO_PRINTER |
---|
[29] | 80 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
| 81 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
[50] | 82 | connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked())); |
---|
[29] | 83 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
[50] | 84 | setCentralWidget(tabWidget); |
---|
[71] | 85 | |
---|
| 86 | if (settings->value("SavePos", false).toBool()) { |
---|
[21] | 87 | // Loading of saved window state |
---|
[23] | 88 | settings->beginGroup("MainWindow"); |
---|
[71] | 89 | #ifndef Q_OS_WINCE |
---|
| 90 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
| 91 | #endif // Q_OS_WINCE |
---|
| 92 | restoreState(settings->value("State").toByteArray()); |
---|
[23] | 93 | settings->endGroup(); |
---|
[71] | 94 | #ifndef Q_OS_WINCE |
---|
[21] | 95 | } else { |
---|
| 96 | // Centering main window |
---|
[71] | 97 | QRect rect = geometry(); |
---|
[21] | 98 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 99 | setGeometry(rect); |
---|
[71] | 100 | #endif // Q_OS_WINCE |
---|
[21] | 101 | } |
---|
[71] | 102 | |
---|
[11] | 103 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[71] | 104 | tspmodel = new CTSPModel(this); |
---|
[52] | 105 | taskView->setModel(tspmodel); |
---|
[31] | 106 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
[57] | 107 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
[37] | 108 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
[50] | 109 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
[47] | 110 | setFileName(QCoreApplication::arguments().at(1)); |
---|
[50] | 111 | else { |
---|
[47] | 112 | setFileName(); |
---|
[50] | 113 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
[52] | 114 | spinCitiesValueChanged(spinCities->value()); |
---|
[50] | 115 | } |
---|
| 116 | setWindowModified(false); |
---|
[6] | 117 | } |
---|
| 118 | |
---|
[67] | 119 | /* Privates **********************************************************/ |
---|
[42] | 120 | |
---|
[29] | 121 | void MainWindow::actionFileNewTriggered() |
---|
[1] | 122 | { |
---|
[47] | 123 | if (!maybeSave()) |
---|
| 124 | return; |
---|
[54] | 125 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[29] | 126 | tspmodel->clear(); |
---|
[47] | 127 | setFileName(); |
---|
[37] | 128 | setWindowModified(false); |
---|
[42] | 129 | tabWidget->setCurrentIndex(0); |
---|
| 130 | solutionText->clear(); |
---|
| 131 | enableSolutionActions(false); |
---|
[54] | 132 | QApplication::restoreOverrideCursor(); |
---|
[29] | 133 | } |
---|
| 134 | |
---|
[31] | 135 | void MainWindow::actionFileOpenTriggered() |
---|
| 136 | { |
---|
[47] | 137 | if (!maybeSave()) |
---|
| 138 | return; |
---|
[31] | 139 | QFileDialog od(this); |
---|
| 140 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
| 141 | od.setFileMode(QFileDialog::ExistingFile); |
---|
| 142 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
[47] | 143 | filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
| 144 | filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
[31] | 145 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 146 | od.setNameFilters(filters); |
---|
| 147 | if (od.exec() != QDialog::Accepted) |
---|
| 148 | return; |
---|
| 149 | QStringList files = od.selectedFiles(); |
---|
[47] | 150 | if (files.empty()) |
---|
[31] | 151 | return; |
---|
[59] | 152 | if (!tspmodel->loadTask(files.first())) |
---|
[47] | 153 | return; |
---|
| 154 | setFileName(files.first()); |
---|
| 155 | tabWidget->setCurrentIndex(0); |
---|
[37] | 156 | setWindowModified(false); |
---|
[42] | 157 | solutionText->clear(); |
---|
| 158 | enableSolutionActions(false); |
---|
[31] | 159 | } |
---|
| 160 | |
---|
[50] | 161 | void MainWindow::actionFileSaveTriggered() |
---|
| 162 | { |
---|
| 163 | if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive))) |
---|
| 164 | saveTask(); |
---|
[59] | 165 | else |
---|
[50] | 166 | if (tspmodel->saveTask(fileName)) |
---|
| 167 | setWindowModified(false); |
---|
| 168 | } |
---|
| 169 | |
---|
[42] | 170 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
[31] | 171 | { |
---|
[37] | 172 | saveTask(); |
---|
| 173 | } |
---|
| 174 | |
---|
[42] | 175 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
| 176 | { |
---|
| 177 | static QString selectedFile; |
---|
| 178 | if (selectedFile.isEmpty()) |
---|
[55] | 179 | #ifndef QT_NO_PRINTER |
---|
| 180 | selectedFile = "solution.pdf"; |
---|
| 181 | #else |
---|
[42] | 182 | selectedFile = "solution.html"; |
---|
[55] | 183 | #endif // QT_NO_PRINTER |
---|
[42] | 184 | QFileDialog sd(this); |
---|
| 185 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
[55] | 186 | QStringList filters; |
---|
| 187 | #ifndef QT_NO_PRINTER |
---|
| 188 | filters.append(trUtf8("PDF Files") + "(*.pdf)"); |
---|
| 189 | #endif |
---|
| 190 | filters.append(trUtf8("HTML Files") + " (*.html *.htm)"); |
---|
[45] | 191 | #if QT_VERSION >= 0x040500 |
---|
[42] | 192 | filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); |
---|
[45] | 193 | #endif // QT_VERSION >= 0x040500 |
---|
[42] | 194 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 195 | sd.setNameFilters(filters); |
---|
| 196 | sd.selectFile(selectedFile); |
---|
| 197 | if (sd.exec() != QDialog::Accepted) |
---|
| 198 | return; |
---|
| 199 | QStringList files = sd.selectedFiles(); |
---|
| 200 | if (files.empty()) |
---|
| 201 | return; |
---|
| 202 | selectedFile = files.first(); |
---|
[51] | 203 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[55] | 204 | #ifndef QT_NO_PRINTER |
---|
| 205 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
| 206 | QPrinter printer(QPrinter::HighResolution); |
---|
| 207 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
| 208 | printer.setOutputFileName(selectedFile); |
---|
| 209 | solutionText->document()->print(&printer); |
---|
| 210 | QApplication::restoreOverrideCursor(); |
---|
| 211 | return; |
---|
| 212 | } |
---|
| 213 | #endif |
---|
[45] | 214 | #if QT_VERSION >= 0x040500 |
---|
[42] | 215 | QTextDocumentWriter dw(selectedFile); |
---|
| 216 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
| 217 | dw.setFormat("plaintext"); |
---|
| 218 | dw.write(solutionText->document()); |
---|
[45] | 219 | #else |
---|
| 220 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
| 221 | QFile file(selectedFile); |
---|
[51] | 222 | if (!file.open(QFile::WriteOnly)) { |
---|
| 223 | QApplication::restoreOverrideCursor(); |
---|
[45] | 224 | return; |
---|
[51] | 225 | } |
---|
[45] | 226 | QTextStream ts(&file); |
---|
| 227 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
| 228 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
[51] | 229 | file.close(); |
---|
[45] | 230 | #endif // QT_VERSION >= 0x040500 |
---|
[51] | 231 | QApplication::restoreOverrideCursor(); |
---|
[42] | 232 | } |
---|
| 233 | |
---|
[67] | 234 | #ifndef QT_NO_PRINTER |
---|
| 235 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
| 236 | { |
---|
| 237 | QPrintPreviewDialog ppd(printer, this); |
---|
| 238 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
| 239 | ppd.exec(); |
---|
[31] | 240 | } |
---|
| 241 | |
---|
[67] | 242 | void MainWindow::actionFilePrintTriggered() |
---|
| 243 | { |
---|
| 244 | QPrintDialog pd(printer,this); |
---|
| 245 | #if QT_VERSION >= 0x040500 |
---|
| 246 | // No such methods in Qt < 4.5 |
---|
| 247 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 248 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
| 249 | #endif |
---|
| 250 | if (pd.exec() != QDialog::Accepted) |
---|
| 251 | return; |
---|
| 252 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 253 | solutionText->document()->print(printer); |
---|
| 254 | QApplication::restoreOverrideCursor(); |
---|
| 255 | } |
---|
| 256 | #endif // QT_NO_PRINTER |
---|
| 257 | |
---|
[29] | 258 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
| 259 | { |
---|
[1] | 260 | SettingsDialog sd(this); |
---|
[42] | 261 | if (sd.exec() != QDialog::Accepted) |
---|
| 262 | return; |
---|
| 263 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
| 264 | initDocStyleSheet(); |
---|
| 265 | 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)) { |
---|
| 266 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 267 | solutionText->clear(); |
---|
| 268 | solutionText->setHtml(output.join("")); |
---|
| 269 | QApplication::restoreOverrideCursor(); |
---|
| 270 | } |
---|
| 271 | } |
---|
[1] | 272 | } |
---|
[6] | 273 | |
---|
[67] | 274 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
[17] | 275 | { |
---|
[67] | 276 | if (checked) { |
---|
| 277 | settings->remove("Language"); |
---|
| 278 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
| 279 | } else |
---|
| 280 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
[52] | 281 | } |
---|
| 282 | |
---|
[67] | 283 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
[52] | 284 | { |
---|
[67] | 285 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 286 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 287 | 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) { |
---|
| 288 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 289 | } else |
---|
| 290 | return; |
---|
| 291 | } |
---|
| 292 | bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); |
---|
| 293 | if (loadLanguage(action->data().toString())) { |
---|
| 294 | settings->setValue("Language",action->data().toString()); |
---|
| 295 | retranslateUi(this); |
---|
| 296 | if (untitled) |
---|
| 297 | setFileName(); |
---|
| 298 | } |
---|
[52] | 299 | } |
---|
| 300 | |
---|
[67] | 301 | void MainWindow::actionHelpAboutTriggered() |
---|
[52] | 302 | { |
---|
[67] | 303 | //! \todo TODO: Normal about window :-) |
---|
[74] | 304 | QString about = QString::fromUtf8("<b>TSPSG: TSP Solver and Generator</b><br>"); |
---|
| 305 | about += QString::fromUtf8(" Version: <b>"BUILD_VERSION"</b><br>"); |
---|
| 306 | about += QString::fromUtf8(" Copyright: <b>© 2007-%1 Lёppa</b><br>").arg(QDate::currentDate().toString("yyyy")); |
---|
| 307 | about += QString::fromUtf8(" <b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b><br>"); |
---|
| 308 | about += "<br>"; |
---|
| 309 | about += QString::fromUtf8("Target OS: <b>%1</b><br>").arg(OS); |
---|
| 310 | about += "Qt library:<br>"; |
---|
| 311 | about += QString::fromUtf8(" Build time: <b>%1</b><br>").arg(QT_VERSION_STR); |
---|
| 312 | about += QString::fromUtf8(" Runtime: <b>%1</b><br>").arg(qVersion()); |
---|
| 313 | about += QString::fromUtf8("Built on <b>%1</b> at <b>%2</b><br>").arg(__DATE__).arg(__TIME__); |
---|
| 314 | about += QString::fromUtf8("Id: <b>"VERSIONID"</b><br>"); |
---|
| 315 | about += QString::fromUtf8("Algorithm: <b>%1</b><br>").arg(CTSPSolver::getVersionId()); |
---|
| 316 | about += "<br>"; |
---|
| 317 | about += "TSPSG is free software: you can redistribute it and/or modify it<br>" |
---|
| 318 | "under the terms of the GNU General Public License as published<br>" |
---|
| 319 | "by the Free Software Foundation, either version 3 of the License,<br>" |
---|
| 320 | "or (at your option) any later version.<br>" |
---|
| 321 | "<br>" |
---|
| 322 | "TSPSG is distributed in the hope that it will be useful, but<br>" |
---|
| 323 | "WITHOUT ANY WARRANTY; without even the implied warranty of<br>" |
---|
| 324 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>" |
---|
| 325 | "GNU General Public License for more details.<br>" |
---|
| 326 | "<br>" |
---|
| 327 | "You should have received a copy of the GNU General Public License<br>" |
---|
| 328 | "along with TSPSG. If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>."; |
---|
| 329 | |
---|
| 330 | QDialog *dlg = new QDialog(this); |
---|
| 331 | QLabel *lblIcon = new QLabel(dlg); |
---|
| 332 | QTextBrowser *txtAbout = new QTextBrowser(dlg); |
---|
| 333 | QVBoxLayout *vb1 = new QVBoxLayout(), |
---|
| 334 | *vb2 = new QVBoxLayout(); |
---|
| 335 | QHBoxLayout *hb = new QHBoxLayout(); |
---|
| 336 | QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg); |
---|
| 337 | |
---|
| 338 | lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(64, Qt::SmoothTransformation)); |
---|
| 339 | |
---|
| 340 | vb1->addWidget(lblIcon); |
---|
| 341 | vb1->addStretch(); |
---|
| 342 | |
---|
| 343 | // txtAbout->setTextInteractionFlags(txtAbout->textInteractionFlags() ^ Qt::TextEditable); |
---|
| 344 | txtAbout->setWordWrapMode(QTextOption::NoWrap); |
---|
| 345 | txtAbout->setOpenExternalLinks(true); |
---|
| 346 | txtAbout->setHtml(about); |
---|
| 347 | txtAbout->moveCursor(QTextCursor::Start); |
---|
| 348 | |
---|
| 349 | hb->addLayout(vb1); |
---|
| 350 | hb->addWidget(txtAbout); |
---|
| 351 | |
---|
| 352 | vb2->addLayout(hb); |
---|
| 353 | vb2->addWidget(bb); |
---|
| 354 | |
---|
| 355 | dlg->setWindowTitle(trUtf8("About TSPSG")); |
---|
| 356 | dlg->setLayout(vb2); |
---|
| 357 | |
---|
| 358 | connect(bb, SIGNAL(accepted()), dlg, SLOT(accept())); |
---|
| 359 | |
---|
| 360 | dlg->resize(475, 350); |
---|
| 361 | dlg->exec(); |
---|
| 362 | |
---|
| 363 | delete dlg; |
---|
[17] | 364 | } |
---|
| 365 | |
---|
[50] | 366 | void MainWindow::buttonBackToTaskClicked() |
---|
| 367 | { |
---|
| 368 | tabWidget->setCurrentIndex(0); |
---|
| 369 | } |
---|
| 370 | |
---|
[67] | 371 | void MainWindow::buttonRandomClicked() |
---|
[42] | 372 | { |
---|
[67] | 373 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 374 | tspmodel->randomize(); |
---|
| 375 | QApplication::restoreOverrideCursor(); |
---|
[42] | 376 | } |
---|
| 377 | |
---|
[29] | 378 | void MainWindow::buttonSolveClicked() |
---|
[6] | 379 | { |
---|
[74] | 380 | TMatrix matrix; |
---|
[42] | 381 | QList<double> row; |
---|
[15] | 382 | int n = spinCities->value(); |
---|
[13] | 383 | bool ok; |
---|
[15] | 384 | for (int r = 0; r < n; r++) { |
---|
[42] | 385 | row.clear(); |
---|
[15] | 386 | for (int c = 0; c < n; c++) { |
---|
[42] | 387 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
[15] | 388 | if (!ok) { |
---|
[47] | 389 | 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] | 390 | return; |
---|
[13] | 391 | } |
---|
| 392 | } |
---|
| 393 | matrix.append(row); |
---|
| 394 | } |
---|
| 395 | CTSPSolver solver; |
---|
[74] | 396 | SStep *root = solver.solve(n,matrix,this); |
---|
[13] | 397 | if (!root) |
---|
[42] | 398 | return; |
---|
| 399 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 400 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
| 401 | output.clear(); |
---|
| 402 | output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
| 403 | output.append("<p>" + trUtf8("Task:") + "</p>"); |
---|
| 404 | outputMatrix(matrix,output); |
---|
| 405 | output.append("<hr>"); |
---|
| 406 | output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
[74] | 407 | SStep *step = root; |
---|
[42] | 408 | n = 1; |
---|
| 409 | while (n <= spinCities->value()) { |
---|
[74] | 410 | if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) { |
---|
[42] | 411 | if (n != spinCities->value()) { |
---|
| 412 | output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>"); |
---|
[74] | 413 | outputMatrix(*step, output); |
---|
| 414 | output.append("<p>" + trUtf8("Selected candidate for branching: %1.").arg(trUtf8("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>"); |
---|
| 415 | if (!step->alts.empty()) { |
---|
| 416 | TCandidate cand; |
---|
| 417 | QString alts; |
---|
| 418 | foreach(cand, step->alts) { |
---|
| 419 | if (!alts.isEmpty()) |
---|
| 420 | alts += ", "; |
---|
| 421 | alts += trUtf8("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); |
---|
| 422 | } |
---|
| 423 | output.append("<p class=\"hasalts\">" + trUtf8("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>"); |
---|
| 424 | } |
---|
[42] | 425 | output.append("<p> </p>"); |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | if (step->prNode->prNode != NULL) |
---|
| 429 | step = step->prNode; |
---|
| 430 | else if (step->plNode->prNode != NULL) |
---|
| 431 | step = step->plNode; |
---|
| 432 | else |
---|
| 433 | break; |
---|
| 434 | } |
---|
[60] | 435 | if (solver.isOptimal()) |
---|
| 436 | output.append("<p>" + trUtf8("Optimal path:") + "</p>"); |
---|
| 437 | else |
---|
| 438 | output.append("<p>" + trUtf8("Resulting path:") + "</p>"); |
---|
| 439 | output.append("<p> " + solver.getSortedPath() + "</p>"); |
---|
[42] | 440 | output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); |
---|
[60] | 441 | if (!solver.isOptimal()) { |
---|
| 442 | output.append("<p> </p>"); |
---|
| 443 | 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>"); |
---|
| 444 | } |
---|
[65] | 445 | output.append("<p></p>"); |
---|
[42] | 446 | solutionText->setHtml(output.join("")); |
---|
| 447 | solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
[65] | 448 | |
---|
| 449 | // Scrolling to the end of text. |
---|
[74] | 450 | solutionText->moveCursor(QTextCursor::End); |
---|
[65] | 451 | |
---|
[42] | 452 | enableSolutionActions(); |
---|
| 453 | tabWidget->setCurrentIndex(1); |
---|
| 454 | QApplication::restoreOverrideCursor(); |
---|
[6] | 455 | } |
---|
[21] | 456 | |
---|
[67] | 457 | void MainWindow::dataChanged() |
---|
[21] | 458 | { |
---|
[67] | 459 | setWindowModified(true); |
---|
[21] | 460 | } |
---|
| 461 | |
---|
[67] | 462 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
| 463 | { |
---|
| 464 | setWindowModified(true); |
---|
| 465 | if (settings->value("Autosize",true).toBool()) { |
---|
| 466 | for (int k = tl.row(); k <= br.row(); k++) |
---|
| 467 | taskView->resizeRowToContents(k); |
---|
| 468 | for (int k = tl.column(); k <= br.column(); k++) |
---|
| 469 | taskView->resizeColumnToContents(k); |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 474 | { |
---|
| 475 | blockSignals(true); |
---|
| 476 | spinCities->setValue(nCities); |
---|
| 477 | blockSignals(false); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | #ifndef QT_NO_PRINTER |
---|
| 481 | void MainWindow::printPreview(QPrinter *printer) |
---|
| 482 | { |
---|
| 483 | solutionText->print(printer); |
---|
| 484 | } |
---|
| 485 | #endif // QT_NO_PRINTER |
---|
| 486 | |
---|
| 487 | void MainWindow::spinCitiesValueChanged(int n) |
---|
| 488 | { |
---|
| 489 | int count = tspmodel->numCities(); |
---|
| 490 | tspmodel->setNumCities(n); |
---|
| 491 | if ((n > count) && settings->value("Autosize",true).toBool()) |
---|
| 492 | for (int k = count; k < n; k++) { |
---|
| 493 | taskView->resizeColumnToContents(k); |
---|
| 494 | taskView->resizeRowToContents(k); |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | |
---|
[71] | 498 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
[69] | 499 | { |
---|
| 500 | if (!maybeSave()) { |
---|
[71] | 501 | ev->ignore(); |
---|
[69] | 502 | return; |
---|
| 503 | } |
---|
[71] | 504 | settings->setValue("NumCities", spinCities->value()); |
---|
| 505 | |
---|
| 506 | // Saving Main Window state |
---|
| 507 | if (settings->value("SavePos", false).toBool()) { |
---|
| 508 | settings->beginGroup("MainWindow"); |
---|
[69] | 509 | #ifndef Q_OS_WINCE |
---|
[71] | 510 | settings->setValue("Geometry", saveGeometry()); |
---|
| 511 | #endif // Q_OS_WINCE |
---|
| 512 | settings->setValue("State", saveState()); |
---|
[69] | 513 | settings->endGroup(); |
---|
| 514 | } |
---|
[71] | 515 | |
---|
| 516 | QMainWindow::closeEvent(ev); |
---|
[69] | 517 | } |
---|
| 518 | |
---|
[67] | 519 | void MainWindow::enableSolutionActions(bool enable) |
---|
| 520 | { |
---|
| 521 | buttonSaveSolution->setEnabled(enable); |
---|
| 522 | actionFileSaveAsSolution->setEnabled(enable); |
---|
| 523 | solutionText->setEnabled(enable); |
---|
| 524 | if (!enable) |
---|
| 525 | output.clear(); |
---|
| 526 | #ifndef QT_NO_PRINTER |
---|
| 527 | actionFilePrint->setEnabled(enable); |
---|
| 528 | actionFilePrintPreview->setEnabled(enable); |
---|
| 529 | #endif // QT_NO_PRINTER |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | void MainWindow::initDocStyleSheet() |
---|
| 533 | { |
---|
| 534 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
| 535 | QColor hilight; |
---|
| 536 | if (color.value() < 192) |
---|
| 537 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
| 538 | else |
---|
| 539 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
| 540 | 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;}"); |
---|
| 541 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
| 542 | } |
---|
| 543 | |
---|
[29] | 544 | void MainWindow::loadLangList() |
---|
| 545 | { |
---|
[57] | 546 | QSettings langinfo(PATH_I18N"/languages.ini",QSettings::IniFormat); |
---|
[33] | 547 | #if QT_VERSION >= 0x040500 |
---|
| 548 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
[29] | 549 | langinfo.setIniCodec("UTF-8"); |
---|
[33] | 550 | #endif |
---|
[57] | 551 | QDir dir(PATH_I18N,"*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
[29] | 552 | if (!dir.exists()) |
---|
| 553 | return; |
---|
| 554 | QFileInfoList langs = dir.entryInfoList(); |
---|
| 555 | if (langs.size() <= 0) |
---|
| 556 | return; |
---|
| 557 | QAction *a; |
---|
| 558 | for (int k = 0; k < langs.size(); k++) { |
---|
| 559 | QFileInfo lang = langs.at(k); |
---|
[30] | 560 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
[33] | 561 | #if QT_VERSION >= 0x040500 |
---|
[29] | 562 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
[33] | 563 | #else |
---|
| 564 | // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings |
---|
| 565 | // reads .ini file as ASCII and there is no way to set file encoding. |
---|
| 566 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString()); |
---|
| 567 | #endif |
---|
[29] | 568 | a->setData(lang.completeBaseName()); |
---|
| 569 | a->setCheckable(true); |
---|
| 570 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 571 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
| 572 | a->setChecked(true); |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | |
---|
[71] | 577 | bool MainWindow::loadLanguage(const QString &lang) |
---|
[29] | 578 | { |
---|
[67] | 579 | // i18n |
---|
| 580 | bool ad = false; |
---|
[71] | 581 | QString lng = lang; |
---|
| 582 | if (lng.isEmpty()) { |
---|
[67] | 583 | ad = settings->value("Language","").toString().isEmpty(); |
---|
[71] | 584 | lng = settings->value("Language",QLocale::system().name()).toString(); |
---|
[29] | 585 | } |
---|
[67] | 586 | static QTranslator *qtTranslator; // Qt library translator |
---|
| 587 | if (qtTranslator) { |
---|
| 588 | qApp->removeTranslator(qtTranslator); |
---|
| 589 | delete qtTranslator; |
---|
| 590 | qtTranslator = NULL; |
---|
[29] | 591 | } |
---|
[67] | 592 | static QTranslator *translator; // Application translator |
---|
| 593 | if (translator) { |
---|
| 594 | qApp->removeTranslator(translator); |
---|
| 595 | delete translator; |
---|
[37] | 596 | } |
---|
[67] | 597 | translator = new QTranslator(); |
---|
[74] | 598 | if ((lng.compare("en") != 0) && !lng.startsWith("en_")) { |
---|
[67] | 599 | // Trying to load system Qt library translation... |
---|
[74] | 600 | qtTranslator = new QTranslator(); |
---|
[71] | 601 | if (qtTranslator->load("qt_" + lng,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
[67] | 602 | qApp->installTranslator(qtTranslator); |
---|
[74] | 603 | else { |
---|
| 604 | // No luck. Let's try to load a bundled one. |
---|
[71] | 605 | if (qtTranslator->load("qt_" + lng,PATH_I18N)) |
---|
[67] | 606 | qApp->installTranslator(qtTranslator); |
---|
| 607 | else { |
---|
| 608 | // Qt library translation unavailable |
---|
| 609 | delete qtTranslator; |
---|
| 610 | qtTranslator = NULL; |
---|
| 611 | } |
---|
[74] | 612 | } |
---|
| 613 | } |
---|
| 614 | // Now let's load application translation. |
---|
| 615 | if (translator->load(lng,PATH_I18N)) |
---|
| 616 | qApp->installTranslator(translator); |
---|
| 617 | else { |
---|
| 618 | delete translator; |
---|
| 619 | translator = NULL; |
---|
| 620 | if ((lng.compare("en") != 0) && !lng.startsWith("en_")) { |
---|
[67] | 621 | if (!ad) |
---|
| 622 | QMessageBox(QMessageBox::Warning,trUtf8("Language Change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); |
---|
| 623 | return false; |
---|
[21] | 624 | } |
---|
| 625 | } |
---|
[67] | 626 | return true; |
---|
[21] | 627 | } |
---|
[31] | 628 | |
---|
[67] | 629 | bool MainWindow::maybeSave() |
---|
[37] | 630 | { |
---|
[67] | 631 | if (!isWindowModified()) |
---|
| 632 | return true; |
---|
| 633 | 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(); |
---|
| 634 | if (res == QMessageBox::Save) |
---|
| 635 | return saveTask(); |
---|
| 636 | else if (res == QMessageBox::Cancel) |
---|
| 637 | return false; |
---|
| 638 | else |
---|
| 639 | return true; |
---|
[37] | 640 | } |
---|
| 641 | |
---|
[74] | 642 | void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output) |
---|
[57] | 643 | { |
---|
[67] | 644 | int n = spinCities->value(); |
---|
| 645 | QString line=""; |
---|
| 646 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
| 647 | for (int r = 0; r < n; r++) { |
---|
| 648 | line = "<tr>"; |
---|
| 649 | for (int c = 0; c < n; c++) { |
---|
[71] | 650 | if (matrix.at(r).at(c) == INFINITY) |
---|
[67] | 651 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
| 652 | else |
---|
[71] | 653 | line += "<td align=\"center\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>"; |
---|
[67] | 654 | } |
---|
| 655 | line += "</tr>"; |
---|
| 656 | output.append(line); |
---|
[57] | 657 | } |
---|
[67] | 658 | output.append("</table>"); |
---|
[57] | 659 | } |
---|
| 660 | |
---|
[74] | 661 | void MainWindow::outputMatrix(const SStep &step, QStringList &output) |
---|
| 662 | { |
---|
| 663 | int n = spinCities->value(); |
---|
| 664 | QString line=""; |
---|
| 665 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
| 666 | for (int r = 0; r < n; r++) { |
---|
| 667 | line = "<tr>"; |
---|
| 668 | for (int c = 0; c < n; c++) { |
---|
| 669 | if (step.matrix.at(r).at(c) == INFINITY) |
---|
| 670 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
| 671 | else if ((r == step.candidate.nRow) && (c == step.candidate.nCol)) |
---|
| 672 | line += "<td align=\"center\" class=\"selected\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
| 673 | else { |
---|
| 674 | TCandidate cand; |
---|
| 675 | cand.nRow = r; |
---|
| 676 | cand.nCol = c; |
---|
| 677 | if (step.alts.contains(cand)) |
---|
| 678 | line += "<td align=\"center\" class=\"alternate\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
| 679 | else |
---|
| 680 | line += "<td align=\"center\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
| 681 | } |
---|
| 682 | } |
---|
| 683 | line += "</tr>"; |
---|
| 684 | output.append(line); |
---|
| 685 | } |
---|
| 686 | output.append("</table>"); |
---|
| 687 | } |
---|
| 688 | |
---|
[67] | 689 | bool MainWindow::saveTask() { |
---|
| 690 | QFileDialog sd(this); |
---|
| 691 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
| 692 | QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
| 693 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 694 | sd.setNameFilters(filters); |
---|
| 695 | sd.setDefaultSuffix("tspt"); |
---|
| 696 | if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) |
---|
| 697 | sd.selectFile(fileName); |
---|
| 698 | else |
---|
| 699 | sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); |
---|
| 700 | if (sd.exec() != QDialog::Accepted) |
---|
| 701 | return false; |
---|
| 702 | QStringList files = sd.selectedFiles(); |
---|
| 703 | if (files.empty()) |
---|
| 704 | return false; |
---|
| 705 | if (tspmodel->saveTask(files.first())) { |
---|
| 706 | setFileName(files.first()); |
---|
| 707 | setWindowModified(false); |
---|
| 708 | return true; |
---|
| 709 | } |
---|
| 710 | return false; |
---|
| 711 | } |
---|
| 712 | |
---|
[71] | 713 | void MainWindow::setFileName(const QString &fileName) |
---|
[31] | 714 | { |
---|
[67] | 715 | this->fileName = fileName; |
---|
| 716 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem"))); |
---|
[31] | 717 | } |
---|