[1babbd6ba3] | 1 | /* |
---|
| 2 | * TSPSG: TSP Solver and Generator |
---|
| 3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
| 4 | * |
---|
| 5 | * $Id$ |
---|
| 6 | * $URL$ |
---|
| 7 | * |
---|
| 8 | * This file is part of TSPSG. |
---|
| 9 | * |
---|
| 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. |
---|
| 14 | * |
---|
| 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. |
---|
| 19 | * |
---|
| 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/>. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include "mainwindow.h" |
---|
| 25 | |
---|
| 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 | */ |
---|
| 33 | MainWindow::MainWindow(QWidget *parent) |
---|
| 34 | : QMainWindow(parent) |
---|
| 35 | { |
---|
| 36 | settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this); |
---|
| 37 | |
---|
| 38 | loadLanguage(); |
---|
| 39 | setupUi(); |
---|
| 40 | |
---|
| 41 | initDocStyleSheet(); |
---|
| 42 | |
---|
| 43 | #ifndef QT_NO_PRINTER |
---|
| 44 | printer = new QPrinter(QPrinter::HighResolution); |
---|
| 45 | #endif // QT_NO_PRINTER |
---|
| 46 | |
---|
| 47 | #ifdef Q_OS_WINCE_WM |
---|
| 48 | currentGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 49 | // We need to react to SIP show/hide and resize the window appropriately |
---|
| 50 | connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int))); |
---|
| 51 | #endif // Q_OS_WINCE_WM |
---|
| 52 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
| 53 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
| 54 | connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered())); |
---|
| 55 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
| 56 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
| 57 | #ifndef QT_NO_PRINTER |
---|
| 58 | connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered())); |
---|
| 59 | connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered())); |
---|
| 60 | #endif // QT_NO_PRINTER |
---|
| 61 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
| 62 | #ifdef Q_OS_WIN32 |
---|
| 63 | connect(actionHelpCheck4Updates, SIGNAL(triggered()), SLOT(actionHelpCheck4UpdatesTriggered())); |
---|
| 64 | #endif // Q_OS_WIN32 |
---|
| 65 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
| 66 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
| 67 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
| 68 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
| 69 | |
---|
| 70 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
| 71 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
| 72 | connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked())); |
---|
| 73 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
| 74 | |
---|
| 75 | #ifndef HANDHELD |
---|
| 76 | // Centering main window |
---|
| 77 | QRect rect = geometry(); |
---|
| 78 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 79 | setGeometry(rect); |
---|
| 80 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
| 81 | // Loading of saved window state |
---|
| 82 | settings->beginGroup("MainWindow"); |
---|
| 83 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
| 84 | restoreState(settings->value("State").toByteArray()); |
---|
| 85 | settings->endGroup(); |
---|
| 86 | } |
---|
| 87 | #else |
---|
| 88 | setWindowState(Qt::WindowMaximized); |
---|
| 89 | #endif // HANDHELD |
---|
| 90 | |
---|
| 91 | tspmodel = new CTSPModel(this); |
---|
| 92 | taskView->setModel(tspmodel); |
---|
| 93 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
| 94 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
| 95 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
| 96 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
| 97 | setFileName(QCoreApplication::arguments().at(1)); |
---|
| 98 | else { |
---|
| 99 | setFileName(); |
---|
| 100 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
| 101 | spinCitiesValueChanged(spinCities->value()); |
---|
| 102 | } |
---|
| 103 | setWindowModified(false); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | MainWindow::~MainWindow() |
---|
| 107 | { |
---|
| 108 | #ifndef QT_NO_PRINTER |
---|
| 109 | delete printer; |
---|
| 110 | #endif |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /* Privates **********************************************************/ |
---|
| 114 | |
---|
| 115 | void MainWindow::actionFileNewTriggered() |
---|
| 116 | { |
---|
| 117 | if (!maybeSave()) |
---|
| 118 | return; |
---|
| 119 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 120 | tspmodel->clear(); |
---|
| 121 | setFileName(); |
---|
| 122 | setWindowModified(false); |
---|
| 123 | tabWidget->setCurrentIndex(0); |
---|
| 124 | solutionText->clear(); |
---|
| 125 | toggleSolutionActions(false); |
---|
| 126 | QApplication::restoreOverrideCursor(); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void MainWindow::actionFileOpenTriggered() |
---|
| 130 | { |
---|
| 131 | if (!maybeSave()) |
---|
| 132 | return; |
---|
| 133 | |
---|
| 134 | QStringList filters(tr("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
| 135 | filters.append(tr("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
| 136 | filters.append(tr("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
| 137 | filters.append(tr("All Files") + " (*)"); |
---|
| 138 | |
---|
| 139 | QString file = QFileInfo(fileName).canonicalPath(); |
---|
| 140 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
| 141 | file = QFileDialog::getOpenFileName(this, tr("Task Load"), file, filters.join(";;"), NULL, opts); |
---|
| 142 | if (file.isEmpty() || !QFileInfo(file).isFile()) |
---|
| 143 | return; |
---|
| 144 | if (!tspmodel->loadTask(file)) |
---|
| 145 | return; |
---|
| 146 | setFileName(file); |
---|
| 147 | tabWidget->setCurrentIndex(0); |
---|
| 148 | setWindowModified(false); |
---|
| 149 | solutionText->clear(); |
---|
| 150 | toggleSolutionActions(false); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | bool MainWindow::actionFileSaveTriggered() |
---|
| 154 | { |
---|
| 155 | if ((fileName == tr("Untitled") + ".tspt") || (!fileName.endsWith(".tspt", Qt::CaseInsensitive))) |
---|
| 156 | return saveTask(); |
---|
| 157 | else |
---|
| 158 | if (tspmodel->saveTask(fileName)) { |
---|
| 159 | setWindowModified(false); |
---|
| 160 | return true; |
---|
| 161 | } else |
---|
| 162 | return false; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
| 166 | { |
---|
| 167 | saveTask(); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
| 171 | { |
---|
| 172 | static QString selectedFile; |
---|
| 173 | if (selectedFile.isEmpty()) |
---|
| 174 | selectedFile = QFileInfo(fileName).canonicalPath(); |
---|
| 175 | else |
---|
| 176 | selectedFile = QFileInfo(selectedFile).canonicalPath(); |
---|
| 177 | if (!selectedFile.isEmpty()) |
---|
| 178 | selectedFile += "/"; |
---|
| 179 | if (fileName == tr("Untitled") + ".tspt") { |
---|
| 180 | #ifndef QT_NO_PRINTER |
---|
| 181 | selectedFile += "solution.pdf"; |
---|
| 182 | #else |
---|
| 183 | selectedFile += "solution.html"; |
---|
| 184 | #endif // QT_NO_PRINTER |
---|
| 185 | } else { |
---|
| 186 | #ifndef QT_NO_PRINTER |
---|
| 187 | selectedFile += QFileInfo(fileName).completeBaseName() + ".pdf"; |
---|
| 188 | #else |
---|
| 189 | selectedFile += QFileInfo(fileName).completeBaseName() + ".html"; |
---|
| 190 | #endif // QT_NO_PRINTER |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | QStringList filters; |
---|
| 194 | #ifndef QT_NO_PRINTER |
---|
| 195 | filters.append(tr("PDF Files") + " (*.pdf)"); |
---|
| 196 | #endif |
---|
| 197 | filters.append(tr("HTML Files") + " (*.html *.htm)"); |
---|
| 198 | #if QT_VERSION >= 0x040500 |
---|
| 199 | filters.append(tr("OpenDocument Files") + " (*.odt)"); |
---|
| 200 | #endif // QT_VERSION >= 0x040500 |
---|
| 201 | filters.append(tr("All Files") + " (*)"); |
---|
| 202 | |
---|
| 203 | QFileDialog::Options opts(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog); |
---|
| 204 | QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"), NULL, opts); |
---|
| 205 | if (file.isEmpty()) |
---|
| 206 | return; |
---|
| 207 | selectedFile = file; |
---|
| 208 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 209 | #ifndef QT_NO_PRINTER |
---|
| 210 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
| 211 | QPrinter printer(QPrinter::HighResolution); |
---|
| 212 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
| 213 | printer.setOutputFileName(selectedFile); |
---|
| 214 | solutionText->document()->print(&printer); |
---|
| 215 | QApplication::restoreOverrideCursor(); |
---|
| 216 | return; |
---|
| 217 | } |
---|
| 218 | #endif |
---|
| 219 | #if QT_VERSION >= 0x040500 |
---|
| 220 | QTextDocumentWriter dw(selectedFile); |
---|
| 221 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
| 222 | dw.setFormat("plaintext"); |
---|
| 223 | if (!dw.write(solutionText->document())) |
---|
| 224 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(dw.device()->errorString())); |
---|
| 225 | #else // QT_VERSION >= 0x040500 |
---|
| 226 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
| 227 | QFile file(selectedFile); |
---|
| 228 | if (!file.open(QFile::WriteOnly)) { |
---|
| 229 | QApplication::restoreOverrideCursor(); |
---|
| 230 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(file->errorString())); |
---|
| 231 | return; |
---|
| 232 | } |
---|
| 233 | QTextStream ts(&file); |
---|
| 234 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
| 235 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
| 236 | file.close(); |
---|
| 237 | #endif // QT_VERSION >= 0x040500 |
---|
| 238 | QApplication::restoreOverrideCursor(); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | #ifndef QT_NO_PRINTER |
---|
| 242 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
| 243 | { |
---|
| 244 | QPrintPreviewDialog ppd(printer, this); |
---|
| 245 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
| 246 | ppd.exec(); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | void MainWindow::actionFilePrintTriggered() |
---|
| 250 | { |
---|
| 251 | QPrintDialog pd(printer,this); |
---|
| 252 | #if QT_VERSION >= 0x040500 |
---|
| 253 | // No such methods in Qt < 4.5 |
---|
| 254 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 255 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
| 256 | #endif |
---|
| 257 | if (pd.exec() != QDialog::Accepted) |
---|
| 258 | return; |
---|
| 259 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 260 | solutionText->document()->print(printer); |
---|
| 261 | QApplication::restoreOverrideCursor(); |
---|
| 262 | } |
---|
| 263 | #endif // QT_NO_PRINTER |
---|
| 264 | |
---|
| 265 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
| 266 | { |
---|
| 267 | SettingsDialog sd(this); |
---|
| 268 | if (sd.exec() != QDialog::Accepted) |
---|
| 269 | return; |
---|
| 270 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
| 271 | if (!solutionText->document()->isEmpty() && sd.colorChanged()) |
---|
| 272 | QMessageBox::information(this, tr("Settings Changed"), tr("You have changed color settings.\nThey will be applied to the next solution output.")); |
---|
| 273 | initDocStyleSheet(); |
---|
| 274 | } |
---|
| 275 | if (sd.translucencyChanged() != 0) |
---|
| 276 | toggleTranclucency(sd.translucencyChanged() == 1); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
| 280 | { |
---|
| 281 | if (checked) { |
---|
| 282 | settings->remove("Language"); |
---|
| 283 | QMessageBox::information(this, tr("Language change"), tr("Language will be autodetected on the next application start.")); |
---|
| 284 | } else |
---|
| 285 | settings->setValue("Language", groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
| 289 | { |
---|
| 290 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 291 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 292 | if (QMessageBox::question(this, tr("Language change"), tr("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { |
---|
| 293 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 294 | } else |
---|
| 295 | return; |
---|
| 296 | } |
---|
| 297 | bool untitled = (fileName == tr("Untitled") + ".tspt"); |
---|
| 298 | if (loadLanguage(action->data().toString())) { |
---|
| 299 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 300 | settings->setValue("Language",action->data().toString()); |
---|
| 301 | retranslateUi(); |
---|
| 302 | if (untitled) |
---|
| 303 | setFileName(); |
---|
| 304 | #ifdef Q_OS_WIN32 |
---|
| 305 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
| 306 | toggleStyle(labelVariant, true); |
---|
| 307 | toggleStyle(labelCities, true); |
---|
| 308 | } |
---|
| 309 | #endif |
---|
| 310 | QApplication::restoreOverrideCursor(); |
---|
| 311 | if (!solutionText->document()->isEmpty()) |
---|
| 312 | QMessageBox::information(this, tr("Settings Changed"), tr("You have changed the application language.\nTo get current solution output in the new language\nyou need to re-run the solution process.")); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | #ifdef Q_OS_WIN32 |
---|
| 317 | void MainWindow::actionHelpCheck4UpdatesTriggered() |
---|
| 318 | { |
---|
| 319 | if (!hasUpdater()) { |
---|
| 320 | QMessageBox::warning(this, tr("Unsupported Feature"), tr("Sorry, but this feature is not supported on your platform\nor support for this feature was not installed.")); |
---|
| 321 | return; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 325 | QProcess::execute("updater/Update.exe -name=\"TSPSG: TSP Solver and Generator\" -check=\"freeupdate\""); |
---|
| 326 | QApplication::restoreOverrideCursor(); |
---|
| 327 | } |
---|
| 328 | #endif // Q_OS_WIN32 |
---|
| 329 | |
---|
| 330 | void MainWindow::actionHelpAboutTriggered() |
---|
| 331 | { |
---|
| 332 | QString title; |
---|
| 333 | #ifdef HANDHELD |
---|
| 334 | title += QString("<b>TSPSG<br>TSP Solver and Generator</b><br>"); |
---|
| 335 | #else |
---|
| 336 | title += QString("<b>TSPSG: TSP Solver and Generator</b><br>"); |
---|
| 337 | #endif // HANDHELD |
---|
| 338 | title += QString("%1: <b>%2</b><br>").arg(tr("Version"), QApplication::applicationVersion()); |
---|
| 339 | #ifndef HANDHELD |
---|
| 340 | title += QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b><br>").arg(QDate::currentDate().toString("yyyy"), QApplication::organizationDomain(), QApplication::organizationName()); |
---|
| 341 | title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b>"); |
---|
| 342 | #else |
---|
| 343 | title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sf.net/</a></b>"); |
---|
| 344 | #endif // Q_OS_WINCE_WM && Q_OS_SYMBIAN |
---|
| 345 | |
---|
| 346 | QString about; |
---|
| 347 | about += QString("%1: <b>%2</b><br>").arg(tr("Target OS (ARCH)"), OS); |
---|
| 348 | #ifndef STATIC_BUILD |
---|
| 349 | about += QString("%1 (%2):<br>").arg(tr("Qt library"), tr("shared")); |
---|
| 350 | about += QString(" %1: <b>%2</b><br>").arg(tr("Build time"), QT_VERSION_STR); |
---|
| 351 | about += QString(" %1: <b>%2</b><br>").arg(tr("Runtime"), qVersion()); |
---|
| 352 | #else |
---|
| 353 | about += QString("%1: <b>%2</b> (%3)<br>").arg(tr("Qt library"), QT_VERSION_STR, tr("static")); |
---|
| 354 | #endif // STATIC_BUILD |
---|
| 355 | about += tr("Buid <b>%1</b>, built on <b>%2</b> at <b>%3</b>").arg(BUILD_NUMBER).arg(__DATE__).arg(__TIME__) + "<br>"; |
---|
| 356 | about += QString("%1: <b>%2</b><br>").arg(tr("Algorithm"), CTSPSolver::getVersionId()); |
---|
| 357 | about += "<br>"; |
---|
| 358 | about += tr("TSPSG is free software: you can redistribute it and/or modify it<br>" |
---|
| 359 | "under the terms of the GNU General Public License as published<br>" |
---|
| 360 | "by the Free Software Foundation, either version 3 of the License,<br>" |
---|
| 361 | "or (at your option) any later version.<br>" |
---|
| 362 | "<br>" |
---|
| 363 | "TSPSG is distributed in the hope that it will be useful, but<br>" |
---|
| 364 | "WITHOUT ANY WARRANTY; without even the implied warranty of<br>" |
---|
| 365 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>" |
---|
| 366 | "GNU General Public License for more details.<br>" |
---|
| 367 | "<br>" |
---|
| 368 | "You should have received a copy of the GNU General Public License<br>" |
---|
| 369 | "along with TSPSG. If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>."); |
---|
| 370 | |
---|
| 371 | QDialog *dlg = new QDialog(this); |
---|
| 372 | QLabel *lblIcon = new QLabel(dlg), |
---|
| 373 | *lblTitle = new QLabel(dlg), |
---|
| 374 | *lblTranslated = new QLabel(dlg); |
---|
| 375 | #ifdef HANDHELD |
---|
| 376 | QLabel *lblSubTitle = new QLabel(QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b>").arg(QDate::currentDate().toString("yyyy"), QApplication::organizationDomain(), QApplication::organizationName()), dlg); |
---|
| 377 | #endif // HANDHELD |
---|
| 378 | QTextBrowser *txtAbout = new QTextBrowser(dlg); |
---|
| 379 | QVBoxLayout *vb = new QVBoxLayout(); |
---|
| 380 | QHBoxLayout *hb1 = new QHBoxLayout(), |
---|
| 381 | *hb2 = new QHBoxLayout(); |
---|
| 382 | QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg); |
---|
| 383 | |
---|
| 384 | lblTitle->setOpenExternalLinks(true); |
---|
| 385 | lblTitle->setText(title); |
---|
| 386 | lblTitle->setAlignment(Qt::AlignTop); |
---|
| 387 | lblTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
---|
| 388 | #ifndef HANDHELD |
---|
| 389 | lblTitle->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name())); |
---|
| 390 | #endif // HANDHELD |
---|
| 391 | |
---|
| 392 | lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToHeight(lblTitle->sizeHint().height(), Qt::SmoothTransformation)); |
---|
| 393 | lblIcon->setAlignment(Qt::AlignVCenter); |
---|
| 394 | #ifndef HANDHELD |
---|
| 395 | lblIcon->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().windowText().color().name())); |
---|
| 396 | #endif // HANDHELD |
---|
| 397 | |
---|
| 398 | hb1->addWidget(lblIcon); |
---|
| 399 | hb1->addWidget(lblTitle); |
---|
| 400 | |
---|
| 401 | txtAbout->setWordWrapMode(QTextOption::NoWrap); |
---|
| 402 | txtAbout->setOpenExternalLinks(true); |
---|
| 403 | txtAbout->setHtml(about); |
---|
| 404 | txtAbout->moveCursor(QTextCursor::Start); |
---|
| 405 | #ifndef HANDHELD |
---|
| 406 | txtAbout->setStyleSheet(QString("QTextBrowser {border-color: %1; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().shadow().color().name())); |
---|
| 407 | #endif // HANDHELD |
---|
| 408 | |
---|
| 409 | bb->button(QDialogButtonBox::Ok)->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
| 410 | |
---|
| 411 | lblTranslated->setText(QApplication::translate("--------", "TRANSLATION", "Please, provide translator credits here.")); |
---|
| 412 | if (lblTranslated->text() == "TRANSLATION") |
---|
| 413 | lblTranslated->hide(); |
---|
| 414 | else { |
---|
| 415 | lblTranslated->setOpenExternalLinks(true); |
---|
| 416 | #ifndef HANDHELD |
---|
| 417 | lblTranslated->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name())); |
---|
| 418 | #endif // HANDHELD |
---|
| 419 | hb2->addWidget(lblTranslated); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | hb2->addWidget(bb); |
---|
| 423 | |
---|
| 424 | #ifdef Q_OS_WINCE_WM |
---|
| 425 | vb->setMargin(3); |
---|
| 426 | #endif // Q_OS_WINCE_WM |
---|
| 427 | vb->addLayout(hb1); |
---|
| 428 | #ifdef HANDHELD |
---|
| 429 | vb->addWidget(lblSubTitle); |
---|
| 430 | #endif // HANDHELD |
---|
| 431 | vb->addWidget(txtAbout); |
---|
| 432 | vb->addLayout(hb2); |
---|
| 433 | |
---|
| 434 | dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); |
---|
| 435 | dlg->setWindowTitle(tr("About TSPSG")); |
---|
| 436 | dlg->setLayout(vb); |
---|
| 437 | |
---|
| 438 | connect(bb, SIGNAL(accepted()), dlg, SLOT(accept())); |
---|
| 439 | |
---|
| 440 | #ifdef Q_OS_WIN32 |
---|
| 441 | // Adding some eyecandy in Vista and 7 :-) |
---|
| 442 | if (QtWin::isCompositionEnabled()) { |
---|
| 443 | QtWin::enableBlurBehindWindow(dlg, true); |
---|
| 444 | } |
---|
| 445 | #endif // Q_OS_WIN32 |
---|
| 446 | |
---|
| 447 | dlg->resize(450, 350); |
---|
| 448 | |
---|
| 449 | dlg->exec(); |
---|
| 450 | |
---|
| 451 | delete dlg; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | void MainWindow::buttonBackToTaskClicked() |
---|
| 455 | { |
---|
| 456 | tabWidget->setCurrentIndex(0); |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | void MainWindow::buttonRandomClicked() |
---|
| 460 | { |
---|
| 461 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 462 | tspmodel->randomize(); |
---|
| 463 | QApplication::restoreOverrideCursor(); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | void MainWindow::buttonSolveClicked() |
---|
| 467 | { |
---|
| 468 | TMatrix matrix; |
---|
| 469 | QList<double> row; |
---|
| 470 | int n = spinCities->value(); |
---|
| 471 | bool ok; |
---|
| 472 | for (int r = 0; r < n; r++) { |
---|
| 473 | row.clear(); |
---|
| 474 | for (int c = 0; c < n; c++) { |
---|
| 475 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
| 476 | if (!ok) { |
---|
| 477 | QMessageBox::critical(this, tr("Data error"), tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1)); |
---|
| 478 | return; |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | matrix.append(row); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | QProgressDialog pd(this); |
---|
| 485 | QProgressBar *pb = new QProgressBar(&pd); |
---|
| 486 | pb->setAlignment(Qt::AlignCenter); |
---|
| 487 | pb->setFormat(tr("%v of %1 parts found").arg(n)); |
---|
| 488 | pd.setBar(pb); |
---|
| 489 | pd.setMaximum(n); |
---|
| 490 | pd.setAutoReset(false); |
---|
| 491 | pd.setLabelText(tr("Calculating optimal route...")); |
---|
| 492 | pd.setWindowTitle(tr("Solution Progress")); |
---|
| 493 | pd.setWindowModality(Qt::ApplicationModal); |
---|
| 494 | pd.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint); |
---|
| 495 | pd.show(); |
---|
| 496 | |
---|
| 497 | CTSPSolver solver; |
---|
| 498 | connect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
| 499 | connect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
| 500 | SStep *root = solver.solve(n, matrix); |
---|
| 501 | disconnect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
| 502 | disconnect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
| 503 | if (!root) { |
---|
| 504 | pd.reset(); |
---|
| 505 | if (!solver.wasCanceled()) |
---|
| 506 | QMessageBox::warning(this, tr("Solution Result"), tr("Unable to find a solution.\nMaybe, this task has no solution.")); |
---|
| 507 | return; |
---|
| 508 | } |
---|
| 509 | pb->setFormat(tr("Generating header")); |
---|
| 510 | pd.setLabelText(tr("Generating solution output...")); |
---|
| 511 | pd.setMaximum(n); |
---|
| 512 | pd.setValue(0); |
---|
| 513 | |
---|
| 514 | solutionText->clear(); |
---|
| 515 | solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
| 516 | solutionText->append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
| 517 | solutionText->append("<p>" + tr("Task:") + "</p>"); |
---|
| 518 | solutionText->append(outputMatrix(matrix)); |
---|
| 519 | solutionText->append("<hr><p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
| 520 | SStep *step = root; |
---|
| 521 | n = 1; |
---|
| 522 | pb->setFormat(tr("Generating step %v")); |
---|
| 523 | while (n < spinCities->value()) { |
---|
| 524 | if (pd.wasCanceled()) { |
---|
| 525 | solutionText->clear(); |
---|
| 526 | return; |
---|
| 527 | } |
---|
| 528 | pd.setValue(n); |
---|
| 529 | |
---|
| 530 | if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) { |
---|
| 531 | if (n != spinCities->value()) { |
---|
| 532 | solutionText->append("<p>" + tr("Step #%1").arg(n++) + "</p>"); |
---|
| 533 | if (settings->value("Output/ShowMatrix", DEF_SHOW_MATRIX).toBool() && (!settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() || (settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() && (spinCities->value() <= settings->value("Output/ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt())))) { |
---|
| 534 | solutionText->append(outputMatrix(*step)); |
---|
| 535 | } |
---|
| 536 | solutionText->append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>"); |
---|
| 537 | if (!step->alts.empty()) { |
---|
| 538 | SCandidate cand; |
---|
| 539 | QString alts; |
---|
| 540 | foreach(cand, step->alts) { |
---|
| 541 | if (!alts.isEmpty()) |
---|
| 542 | alts += ", "; |
---|
| 543 | alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); |
---|
| 544 | } |
---|
| 545 | solutionText->append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>"); |
---|
| 546 | } |
---|
| 547 | solutionText->append("<p> </p>"); |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | if (step->prNode->prNode != NULL) |
---|
| 551 | step = step->prNode; |
---|
| 552 | else if (step->plNode->prNode != NULL) |
---|
| 553 | step = step->plNode; |
---|
| 554 | else |
---|
| 555 | break; |
---|
| 556 | } |
---|
| 557 | pb->setFormat(tr("Generating footer")); |
---|
| 558 | pd.setValue(n); |
---|
| 559 | |
---|
| 560 | if (solver.isOptimal()) |
---|
| 561 | solutionText->append("<p>" + tr("Optimal path:") + "</p>"); |
---|
| 562 | else |
---|
| 563 | solutionText->append("<p>" + tr("Resulting path:") + "</p>"); |
---|
| 564 | solutionText->append("<p> " + solver.getSortedPath() + "</p>"); |
---|
| 565 | if (isInteger(step->price)) |
---|
| 566 | solutionText->append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>"); |
---|
| 567 | else |
---|
| 568 | solutionText->append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>"); |
---|
| 569 | if (!solver.isOptimal()) { |
---|
| 570 | solutionText->append("<p> </p>"); |
---|
| 571 | solutionText->append("<p>" + tr("<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>"); |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) { |
---|
| 575 | // Scrolling to the end of text. |
---|
| 576 | solutionText->moveCursor(QTextCursor::End); |
---|
| 577 | } else |
---|
| 578 | solutionText->moveCursor(QTextCursor::Start); |
---|
| 579 | |
---|
| 580 | pd.setLabelText(tr("Cleaning up...")); |
---|
| 581 | pd.setMaximum(0); |
---|
| 582 | pd.setCancelButton(NULL); |
---|
| 583 | QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
| 584 | solver.cleanup(true); |
---|
| 585 | toggleSolutionActions(); |
---|
| 586 | tabWidget->setCurrentIndex(1); |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | void MainWindow::dataChanged() |
---|
| 590 | { |
---|
| 591 | setWindowModified(true); |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
| 595 | { |
---|
| 596 | setWindowModified(true); |
---|
| 597 | if (settings->value("Autosize", DEF_AUTOSIZE).toBool()) { |
---|
| 598 | for (int k = tl.row(); k <= br.row(); k++) |
---|
| 599 | taskView->resizeRowToContents(k); |
---|
| 600 | for (int k = tl.column(); k <= br.column(); k++) |
---|
| 601 | taskView->resizeColumnToContents(k); |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | #ifdef Q_OS_WINCE_WM |
---|
| 606 | void MainWindow::changeEvent(QEvent *ev) |
---|
| 607 | { |
---|
| 608 | if ((ev->type() == QEvent::ActivationChange) && isActiveWindow()) |
---|
| 609 | desktopResized(0); |
---|
| 610 | |
---|
| 611 | QWidget::changeEvent(ev); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | void MainWindow::desktopResized(int screen) |
---|
| 615 | { |
---|
| 616 | if ((screen != 0) || !isActiveWindow()) |
---|
| 617 | return; |
---|
| 618 | |
---|
| 619 | QRect availableGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 620 | if (currentGeometry != availableGeometry) { |
---|
| 621 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 622 | /*! |
---|
| 623 | * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height. |
---|
| 624 | * If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the |
---|
| 625 | * window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile? |
---|
| 626 | */ |
---|
| 627 | if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) { |
---|
| 628 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
| 629 | } else { |
---|
| 630 | if (windowState() & Qt::WindowMaximized) |
---|
| 631 | setWindowState(windowState() ^ Qt::WindowMaximized); |
---|
| 632 | setGeometry(availableGeometry); |
---|
| 633 | } |
---|
| 634 | currentGeometry = availableGeometry; |
---|
| 635 | QApplication::restoreOverrideCursor(); |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | #endif // Q_OS_WINCE_WM |
---|
| 639 | |
---|
| 640 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 641 | { |
---|
| 642 | blockSignals(true); |
---|
| 643 | spinCities->setValue(nCities); |
---|
| 644 | blockSignals(false); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | #ifndef QT_NO_PRINTER |
---|
| 648 | void MainWindow::printPreview(QPrinter *printer) |
---|
| 649 | { |
---|
| 650 | solutionText->print(printer); |
---|
| 651 | } |
---|
| 652 | #endif // QT_NO_PRINTER |
---|
| 653 | |
---|
| 654 | void MainWindow::spinCitiesValueChanged(int n) |
---|
| 655 | { |
---|
| 656 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 657 | int count = tspmodel->numCities(); |
---|
| 658 | tspmodel->setNumCities(n); |
---|
| 659 | if ((n > count) && settings->value("Autosize", DEF_AUTOSIZE).toBool()) |
---|
| 660 | for (int k = count; k < n; k++) { |
---|
| 661 | taskView->resizeColumnToContents(k); |
---|
| 662 | taskView->resizeRowToContents(k); |
---|
| 663 | } |
---|
| 664 | QApplication::restoreOverrideCursor(); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
| 668 | { |
---|
| 669 | if (!maybeSave()) { |
---|
| 670 | ev->ignore(); |
---|
| 671 | return; |
---|
| 672 | } |
---|
| 673 | if (!settings->value("SettingsReset", false).toBool()) { |
---|
| 674 | settings->setValue("NumCities", spinCities->value()); |
---|
| 675 | |
---|
| 676 | // Saving Main Window state |
---|
| 677 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
| 678 | settings->beginGroup("MainWindow"); |
---|
| 679 | #ifndef HANDHELD |
---|
| 680 | settings->setValue("Geometry", saveGeometry()); |
---|
| 681 | #endif // HANDHELD |
---|
| 682 | settings->setValue("State", saveState()); |
---|
| 683 | settings->endGroup(); |
---|
| 684 | } |
---|
| 685 | } else { |
---|
| 686 | settings->remove("SettingsReset"); |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | QMainWindow::closeEvent(ev); |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | bool MainWindow::hasUpdater() const |
---|
| 693 | { |
---|
| 694 | #ifdef Q_OS_WIN32 |
---|
| 695 | return QFile::exists("updater/Update.exe"); |
---|
| 696 | #else // Q_OS_WIN32 |
---|
| 697 | return false; |
---|
| 698 | #endif // Q_OS_WIN32 |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | void MainWindow::initDocStyleSheet() |
---|
| 702 | { |
---|
| 703 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
| 704 | QColor hilight; |
---|
| 705 | if (color.value() < 192) |
---|
| 706 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
| 707 | else |
---|
| 708 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
| 709 | 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;}"); |
---|
| 710 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | void MainWindow::loadLangList() |
---|
| 714 | { |
---|
| 715 | QDir dir(PATH_L10N, "tspsg_*.qm", QDir::Name | QDir::IgnoreCase, QDir::Files); |
---|
| 716 | if (!dir.exists()) |
---|
| 717 | return; |
---|
| 718 | QFileInfoList langs = dir.entryInfoList(); |
---|
| 719 | if (langs.size() <= 0) |
---|
| 720 | return; |
---|
| 721 | QAction *a; |
---|
| 722 | QTranslator t; |
---|
| 723 | QString name; |
---|
| 724 | for (int k = 0; k < langs.size(); k++) { |
---|
| 725 | QFileInfo lang = langs.at(k); |
---|
| 726 | if (lang.completeBaseName().compare("tspsg_en", Qt::CaseInsensitive) && t.load(lang.completeBaseName(), PATH_L10N)) { |
---|
| 727 | name = t.translate("--------", "LANGNAME", "Please, provide a native name of your translation language here."); |
---|
| 728 | a = menuSettingsLanguage->addAction(name); |
---|
| 729 | a->setStatusTip(QString("Set application language to %1").arg(name)); |
---|
| 730 | a->setData(lang.completeBaseName().mid(6)); |
---|
| 731 | a->setCheckable(true); |
---|
| 732 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 733 | if (settings->value("Language", QLocale::system().name()).toString().startsWith(lang.completeBaseName().mid(6))) |
---|
| 734 | a->setChecked(true); |
---|
| 735 | } |
---|
| 736 | } |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | bool MainWindow::loadLanguage(const QString &lang) |
---|
| 740 | { |
---|
| 741 | // i18n |
---|
| 742 | bool ad = false; |
---|
| 743 | QString lng = lang; |
---|
| 744 | if (lng.isEmpty()) { |
---|
| 745 | ad = settings->value("Language", "").toString().isEmpty(); |
---|
| 746 | lng = settings->value("Language", QLocale::system().name()).toString(); |
---|
| 747 | } |
---|
| 748 | static QTranslator *qtTranslator; // Qt library translator |
---|
| 749 | if (qtTranslator) { |
---|
| 750 | qApp->removeTranslator(qtTranslator); |
---|
| 751 | delete qtTranslator; |
---|
| 752 | qtTranslator = NULL; |
---|
| 753 | } |
---|
| 754 | static QTranslator *translator; // Application translator |
---|
| 755 | if (translator) { |
---|
| 756 | qApp->removeTranslator(translator); |
---|
| 757 | delete translator; |
---|
| 758 | translator = NULL; |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | if (lng == "en") |
---|
| 762 | return true; |
---|
| 763 | |
---|
| 764 | // Trying to load system Qt library translation... |
---|
| 765 | qtTranslator = new QTranslator(this); |
---|
| 766 | if (qtTranslator->load("qt_" + lng, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
| 767 | qApp->installTranslator(qtTranslator); |
---|
| 768 | else { |
---|
| 769 | // No luck. Let's try to load a bundled one. |
---|
| 770 | if (qtTranslator->load("qt_" + lng, PATH_L10N)) |
---|
| 771 | qApp->installTranslator(qtTranslator); |
---|
| 772 | else { |
---|
| 773 | // Qt library translation unavailable |
---|
| 774 | delete qtTranslator; |
---|
| 775 | qtTranslator = NULL; |
---|
| 776 | } |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | // Now let's load application translation. |
---|
| 780 | translator = new QTranslator(this); |
---|
| 781 | if (translator->load("tspsg_" + lng, PATH_L10N)) |
---|
| 782 | qApp->installTranslator(translator); |
---|
| 783 | else { |
---|
| 784 | delete translator; |
---|
| 785 | translator = NULL; |
---|
| 786 | if (!ad) { |
---|
| 787 | settings->remove("Language"); |
---|
| 788 | QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor)); |
---|
| 789 | if (isVisible()) |
---|
| 790 | QMessageBox::warning(this, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); |
---|
| 791 | else |
---|
| 792 | QMessageBox::warning(NULL, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); |
---|
| 793 | QApplication::restoreOverrideCursor(); |
---|
| 794 | } |
---|
| 795 | return false; |
---|
| 796 | } |
---|
| 797 | return true; |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | bool MainWindow::maybeSave() |
---|
| 801 | { |
---|
| 802 | if (!isWindowModified()) |
---|
| 803 | return true; |
---|
| 804 | int res = QMessageBox::warning(this, tr("Unsaved Changes"), tr("Would you like to save changes in the current task?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); |
---|
| 805 | if (res == QMessageBox::Save) |
---|
| 806 | return actionFileSaveTriggered(); |
---|
| 807 | else if (res == QMessageBox::Cancel) |
---|
| 808 | return false; |
---|
| 809 | else |
---|
| 810 | return true; |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | QString MainWindow::outputMatrix(const TMatrix &matrix) const |
---|
| 814 | { |
---|
| 815 | int n = spinCities->value(); |
---|
| 816 | QString output(""), line; |
---|
| 817 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
| 818 | for (int r = 0; r < n; r++) { |
---|
| 819 | line = "<tr>"; |
---|
| 820 | for (int c = 0; c < n; c++) { |
---|
| 821 | if (matrix.at(r).at(c) == INFINITY) |
---|
| 822 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
| 823 | else |
---|
| 824 | line += isInteger(matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 825 | } |
---|
| 826 | line += "</tr>"; |
---|
| 827 | output.append(line); |
---|
| 828 | } |
---|
| 829 | output.append("</table>"); |
---|
| 830 | return output; |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | QString MainWindow::outputMatrix(const SStep &step) const |
---|
| 834 | { |
---|
| 835 | int n = spinCities->value(); |
---|
| 836 | QString output(""), line; |
---|
| 837 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
| 838 | for (int r = 0; r < n; r++) { |
---|
| 839 | line = "<tr>"; |
---|
| 840 | for (int c = 0; c < n; c++) { |
---|
| 841 | if (step.matrix.at(r).at(c) == INFINITY) |
---|
| 842 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
| 843 | else if ((r == step.candidate.nRow) && (c == step.candidate.nCol)) |
---|
| 844 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 845 | else { |
---|
| 846 | SCandidate cand; |
---|
| 847 | cand.nRow = r; |
---|
| 848 | cand.nCol = c; |
---|
| 849 | if (step.alts.contains(cand)) |
---|
| 850 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 851 | else |
---|
| 852 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
| 853 | } |
---|
| 854 | } |
---|
| 855 | line += "</tr>"; |
---|
| 856 | output.append(line); |
---|
| 857 | } |
---|
| 858 | output.append("</table>"); |
---|
| 859 | return output; |
---|
| 860 | } |
---|
| 861 | |
---|
| 862 | void MainWindow::retranslateUi(bool all) |
---|
| 863 | { |
---|
| 864 | if (all) |
---|
| 865 | Ui::MainWindow::retranslateUi(this); |
---|
| 866 | |
---|
| 867 | actionSettingsLanguageEnglish->setStatusTip(tr("Set application language to %1").arg("English")); |
---|
| 868 | |
---|
| 869 | #ifndef QT_NO_PRINTER |
---|
| 870 | actionFilePrintPreview->setText(QApplication::translate("MainWindow", "P&rint Preview...", 0, QApplication::UnicodeUTF8)); |
---|
| 871 | #ifndef QT_NO_TOOLTIP |
---|
| 872 | actionFilePrintPreview->setToolTip(QApplication::translate("MainWindow", "Preview solution results", 0, QApplication::UnicodeUTF8)); |
---|
| 873 | #endif // QT_NO_TOOLTIP |
---|
| 874 | #ifndef QT_NO_STATUSTIP |
---|
| 875 | actionFilePrintPreview->setStatusTip(QApplication::translate("MainWindow", "Preview current solution results before printing", 0, QApplication::UnicodeUTF8)); |
---|
| 876 | #endif // QT_NO_STATUSTIP |
---|
| 877 | |
---|
| 878 | actionFilePrint->setText(QApplication::translate("MainWindow", "&Print...", 0, QApplication::UnicodeUTF8)); |
---|
| 879 | #ifndef QT_NO_TOOLTIP |
---|
| 880 | actionFilePrint->setToolTip(QApplication::translate("MainWindow", "Print solution", 0, QApplication::UnicodeUTF8)); |
---|
| 881 | #endif // QT_NO_TOOLTIP |
---|
| 882 | #ifndef QT_NO_STATUSTIP |
---|
| 883 | actionFilePrint->setStatusTip(QApplication::translate("MainWindow", "Print current solution results", 0, QApplication::UnicodeUTF8)); |
---|
| 884 | #endif // QT_NO_STATUSTIP |
---|
| 885 | actionFilePrint->setShortcut(QApplication::translate("MainWindow", "Ctrl+P", 0, QApplication::UnicodeUTF8)); |
---|
| 886 | #endif // QT_NO_PRINTER |
---|
| 887 | #ifdef Q_OS_WIN32 |
---|
| 888 | actionHelpCheck4Updates->setText(tr("Check for &Updates...")); |
---|
| 889 | #ifndef QT_NO_TOOLTIP |
---|
| 890 | actionHelpCheck4Updates->setToolTip(tr("Check for %1 updates").arg(QApplication::applicationName())); |
---|
| 891 | #endif // QT_NO_TOOLTIP |
---|
| 892 | #ifndef QT_NO_STATUSTIP |
---|
| 893 | actionHelpCheck4Updates->setStatusTip(tr("Check for %1 updates").arg(QApplication::applicationName())); |
---|
| 894 | #endif // QT_NO_STATUSTIP |
---|
| 895 | #endif // Q_OS_WIN32 |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | bool MainWindow::saveTask() { |
---|
| 899 | QStringList filters(tr("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
| 900 | filters.append(tr("All Files") + " (*)"); |
---|
| 901 | QString file; |
---|
| 902 | if (fileName.endsWith(".tspt", Qt::CaseInsensitive)) |
---|
| 903 | file = fileName; |
---|
| 904 | else |
---|
| 905 | file = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"; |
---|
| 906 | |
---|
| 907 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
| 908 | file = QFileDialog::getSaveFileName(this, tr("Task Save"), file, filters.join(";;"), NULL, opts); |
---|
| 909 | |
---|
| 910 | if (file.isEmpty()) |
---|
| 911 | return false; |
---|
| 912 | if (tspmodel->saveTask(file)) { |
---|
| 913 | setFileName(file); |
---|
| 914 | setWindowModified(false); |
---|
| 915 | return true; |
---|
| 916 | } |
---|
| 917 | return false; |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | void MainWindow::setFileName(const QString &fileName) |
---|
| 921 | { |
---|
| 922 | this->fileName = fileName; |
---|
| 923 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(tr("Travelling Salesman Problem"))); |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | void MainWindow::setupUi() |
---|
| 927 | { |
---|
| 928 | Ui::MainWindow::setupUi(this); |
---|
| 929 | |
---|
| 930 | #if QT_VERSION >= 0x040600 |
---|
| 931 | setToolButtonStyle(Qt::ToolButtonFollowStyle); |
---|
| 932 | #endif |
---|
| 933 | |
---|
| 934 | #ifndef HANDHELD |
---|
| 935 | QStatusBar *statusbar = new QStatusBar(this); |
---|
| 936 | statusbar->setObjectName("statusbar"); |
---|
| 937 | setStatusBar(statusbar); |
---|
| 938 | #endif // HANDHELD |
---|
| 939 | |
---|
| 940 | #ifdef Q_OS_WINCE_WM |
---|
| 941 | menuBar()->setDefaultAction(menuFile->menuAction()); |
---|
| 942 | |
---|
| 943 | QScrollArea *scrollArea = new QScrollArea(this); |
---|
| 944 | scrollArea->setFrameShape(QFrame::NoFrame); |
---|
| 945 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
---|
| 946 | scrollArea->setWidgetResizable(true); |
---|
| 947 | scrollArea->setWidget(tabWidget); |
---|
| 948 | setCentralWidget(scrollArea); |
---|
| 949 | #else |
---|
| 950 | setCentralWidget(tabWidget); |
---|
| 951 | #endif // Q_OS_WINCE_WM |
---|
| 952 | |
---|
| 953 | //! \hack HACK: A little hack for toolbar icons to have a sane size. |
---|
| 954 | #ifdef Q_OS_WINCE_WM |
---|
| 955 | toolBar->setIconSize(QSize(logicalDpiX() / 4, logicalDpiY() / 4)); |
---|
| 956 | #elif defined(Q_OS_SYMBIAN) |
---|
| 957 | toolBar->setIconSize(QSize(logicalDpiX() / 5, logicalDpiY() / 5)); |
---|
| 958 | #endif // Q_OS_WINCE_WM |
---|
| 959 | |
---|
| 960 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
| 961 | solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); |
---|
| 962 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
| 963 | |
---|
| 964 | #ifndef QT_NO_PRINTER |
---|
| 965 | actionFilePrintPreview = new QAction(this); |
---|
| 966 | actionFilePrintPreview->setObjectName("actionFilePrintPreview"); |
---|
| 967 | actionFilePrintPreview->setEnabled(false); |
---|
| 968 | actionFilePrintPreview->setIcon(QIcon(":/images/icons/document_preview.png")); |
---|
| 969 | |
---|
| 970 | actionFilePrint = new QAction(this); |
---|
| 971 | actionFilePrint->setObjectName("actionFilePrint"); |
---|
| 972 | actionFilePrint->setEnabled(false); |
---|
| 973 | actionFilePrint->setIcon(QIcon(":/images/icons/fileprint.png")); |
---|
| 974 | |
---|
| 975 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
| 976 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
| 977 | menuFile->insertSeparator(actionFileExit); |
---|
| 978 | |
---|
| 979 | toolBar->insertAction(actionSettingsPreferences,actionFilePrint); |
---|
| 980 | #endif // QT_NO_PRINTER |
---|
| 981 | #ifdef Q_OS_WIN32 |
---|
| 982 | actionHelpCheck4Updates = new QAction(this); |
---|
| 983 | actionHelpCheck4Updates->setEnabled(hasUpdater()); |
---|
| 984 | menuHelp->insertAction(actionHelpAboutQt, actionHelpCheck4Updates); |
---|
| 985 | menuHelp->insertSeparator(actionHelpAboutQt); |
---|
| 986 | #endif // Q_OS_WIN32 |
---|
| 987 | |
---|
| 988 | groupSettingsLanguageList = new QActionGroup(this); |
---|
| 989 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 990 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
| 991 | loadLangList(); |
---|
| 992 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language", "").toString().isEmpty()); |
---|
| 993 | |
---|
| 994 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
| 995 | |
---|
| 996 | retranslateUi(false); |
---|
| 997 | |
---|
| 998 | #ifdef Q_OS_WIN32 |
---|
| 999 | // Adding some eyecandy in Vista and 7 :-) |
---|
| 1000 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
| 1001 | toggleTranclucency(true); |
---|
| 1002 | } |
---|
| 1003 | #endif // Q_OS_WIN32 |
---|
| 1004 | } |
---|
| 1005 | |
---|
| 1006 | void MainWindow::toggleSolutionActions(bool enable) |
---|
| 1007 | { |
---|
| 1008 | buttonSaveSolution->setEnabled(enable); |
---|
| 1009 | actionFileSaveAsSolution->setEnabled(enable); |
---|
| 1010 | solutionText->setEnabled(enable); |
---|
| 1011 | #ifndef QT_NO_PRINTER |
---|
| 1012 | actionFilePrint->setEnabled(enable); |
---|
| 1013 | actionFilePrintPreview->setEnabled(enable); |
---|
| 1014 | #endif // QT_NO_PRINTER |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | void MainWindow::toggleTranclucency(bool enable) |
---|
| 1018 | { |
---|
| 1019 | #ifdef Q_OS_WIN32 |
---|
| 1020 | toggleStyle(labelVariant, enable); |
---|
| 1021 | toggleStyle(labelCities, enable); |
---|
| 1022 | toggleStyle(statusBar(), enable); |
---|
| 1023 | tabWidget->setDocumentMode(enable); |
---|
| 1024 | QtWin::enableBlurBehindWindow(this, enable); |
---|
| 1025 | #else |
---|
| 1026 | Q_UNUSED(enable); |
---|
| 1027 | #endif // Q_OS_WIN32 |
---|
| 1028 | } |
---|