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