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