[100] | 1 | /* |
---|
| 2 | * TSPSG: TSP Solver and Generator |
---|
| 3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
| 4 | * |
---|
| 5 | * $Id: mainwindow.cpp 142 2010-10-05 19:44:54Z laleppa $ |
---|
| 6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/mainwindow.cpp $ |
---|
| 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 | |
---|
[141] | 26 | #ifdef Q_WS_WIN32 |
---|
[124] | 27 | #include "shobjidl.h" |
---|
| 28 | #endif |
---|
| 29 | |
---|
[121] | 30 | #ifdef _T_T_L_ |
---|
| 31 | #include "_.h" |
---|
[122] | 32 | _C_ _R_ _Y_ _P_ _T_ |
---|
[121] | 33 | #endif |
---|
| 34 | |
---|
[100] | 35 | /*! |
---|
| 36 | * \brief Class constructor. |
---|
| 37 | * \param parent Main Window parent widget. |
---|
| 38 | * |
---|
| 39 | * Initializes Main Window and creates its layout based on target OS. |
---|
| 40 | * Loads TSPSG settings and opens a task file if it was specified as a commandline parameter. |
---|
| 41 | */ |
---|
| 42 | MainWindow::MainWindow(QWidget *parent) |
---|
| 43 | : QMainWindow(parent) |
---|
| 44 | { |
---|
| 45 | settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this); |
---|
| 46 | |
---|
[115] | 47 | if (settings->contains("Style")) { |
---|
| 48 | QStyle *s = QStyleFactory::create(settings->value("Style").toString()); |
---|
| 49 | if (s != NULL) |
---|
| 50 | QApplication::setStyle(s); |
---|
| 51 | else |
---|
| 52 | settings->remove("Style"); |
---|
| 53 | } |
---|
| 54 | |
---|
[100] | 55 | loadLanguage(); |
---|
| 56 | setupUi(); |
---|
[105] | 57 | setAcceptDrops(true); |
---|
[100] | 58 | |
---|
| 59 | initDocStyleSheet(); |
---|
| 60 | |
---|
| 61 | #ifndef QT_NO_PRINTER |
---|
| 62 | printer = new QPrinter(QPrinter::HighResolution); |
---|
| 63 | #endif // QT_NO_PRINTER |
---|
| 64 | |
---|
[141] | 65 | #ifdef Q_WS_WINCE_WM |
---|
[100] | 66 | currentGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 67 | // We need to react to SIP show/hide and resize the window appropriately |
---|
| 68 | connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int))); |
---|
[141] | 69 | #endif // Q_WS_WINCE_WM |
---|
[114] | 70 | connect(actionFileNew, SIGNAL(triggered()), SLOT(actionFileNewTriggered())); |
---|
| 71 | connect(actionFileOpen, SIGNAL(triggered()), SLOT(actionFileOpenTriggered())); |
---|
| 72 | connect(actionFileSave, SIGNAL(triggered()), SLOT(actionFileSaveTriggered())); |
---|
| 73 | connect(actionFileSaveAsTask, SIGNAL(triggered()), SLOT(actionFileSaveAsTaskTriggered())); |
---|
| 74 | connect(actionFileSaveAsSolution, SIGNAL(triggered()), SLOT(actionFileSaveAsSolutionTriggered())); |
---|
[100] | 75 | #ifndef QT_NO_PRINTER |
---|
[114] | 76 | connect(actionFilePrintPreview, SIGNAL(triggered()), SLOT(actionFilePrintPreviewTriggered())); |
---|
| 77 | connect(actionFilePrint, SIGNAL(triggered()), SLOT(actionFilePrintTriggered())); |
---|
[100] | 78 | #endif // QT_NO_PRINTER |
---|
[118] | 79 | #ifndef HANDHELD |
---|
| 80 | connect(actionSettingsToolbarsConfigure, SIGNAL(triggered()), SLOT(actionSettingsToolbarsConfigureTriggered())); |
---|
| 81 | #endif // HANDHELD |
---|
[114] | 82 | connect(actionSettingsPreferences, SIGNAL(triggered()), SLOT(actionSettingsPreferencesTriggered())); |
---|
[139] | 83 | if (actionHelpCheck4Updates != NULL) |
---|
| 84 | connect(actionHelpCheck4Updates, SIGNAL(triggered()), SLOT(actionHelpCheck4UpdatesTriggered())); |
---|
[114] | 85 | connect(actionSettingsLanguageAutodetect, SIGNAL(triggered(bool)), SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
| 86 | connect(groupSettingsLanguageList, SIGNAL(triggered(QAction *)), SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
[115] | 87 | connect(actionSettingsStyleSystem, SIGNAL(triggered(bool)), SLOT(actionSettingsStyleSystemTriggered(bool))); |
---|
| 88 | connect(groupSettingsStyleList, SIGNAL(triggered(QAction*)), SLOT(groupSettingsStyleListTriggered(QAction*))); |
---|
[142] | 89 | connect(actionHelpOnlineSupport, SIGNAL(triggered()), SLOT(actionHelpOnlineSupportTriggered())); |
---|
| 90 | connect(actionHelpReportBug, SIGNAL(triggered()), SLOT(actionHelpReportBugTriggered())); |
---|
[114] | 91 | connect(actionHelpAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
---|
| 92 | connect(actionHelpAbout, SIGNAL(triggered()), SLOT(actionHelpAboutTriggered())); |
---|
[100] | 93 | |
---|
[114] | 94 | connect(buttonSolve, SIGNAL(clicked()), SLOT(buttonSolveClicked())); |
---|
| 95 | connect(buttonRandom, SIGNAL(clicked()), SLOT(buttonRandomClicked())); |
---|
| 96 | connect(buttonBackToTask, SIGNAL(clicked()), SLOT(buttonBackToTaskClicked())); |
---|
| 97 | connect(spinCities, SIGNAL(valueChanged(int)), SLOT(spinCitiesValueChanged(int))); |
---|
[100] | 98 | |
---|
| 99 | #ifndef HANDHELD |
---|
| 100 | // Centering main window |
---|
| 101 | QRect rect = geometry(); |
---|
| 102 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 103 | setGeometry(rect); |
---|
| 104 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
| 105 | // Loading of saved window state |
---|
| 106 | settings->beginGroup("MainWindow"); |
---|
| 107 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
| 108 | restoreState(settings->value("State").toByteArray()); |
---|
| 109 | settings->endGroup(); |
---|
| 110 | } |
---|
| 111 | #endif // HANDHELD |
---|
| 112 | |
---|
| 113 | tspmodel = new CTSPModel(this); |
---|
| 114 | taskView->setModel(tspmodel); |
---|
[114] | 115 | connect(tspmodel, SIGNAL(numCitiesChanged(int)), SLOT(numCitiesChanged(int))); |
---|
| 116 | connect(tspmodel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
| 117 | connect(tspmodel, SIGNAL(layoutChanged()), SLOT(dataChanged())); |
---|
[100] | 118 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
| 119 | setFileName(QCoreApplication::arguments().at(1)); |
---|
| 120 | else { |
---|
| 121 | setFileName(); |
---|
| 122 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
| 123 | spinCitiesValueChanged(spinCities->value()); |
---|
| 124 | } |
---|
| 125 | setWindowModified(false); |
---|
[139] | 126 | |
---|
| 127 | if ((actionHelpCheck4Updates != NULL) |
---|
| 128 | && (settings->value("Check4Updates/Enabled", DEF_CHECK_FOR_UPDATES).toBool()) |
---|
| 129 | && (QDate(qvariant_cast<QDate>(settings->value("Check4Updates/LastAttempt"))).daysTo(QDate::currentDate()) >= settings->value("Check4Updates/Interval", DEF_UPDATE_CHECK_INTERVAL).toInt())) { |
---|
| 130 | check4Updates(true); |
---|
| 131 | } |
---|
[100] | 132 | } |
---|
| 133 | |
---|
| 134 | MainWindow::~MainWindow() |
---|
| 135 | { |
---|
| 136 | #ifndef QT_NO_PRINTER |
---|
| 137 | delete printer; |
---|
| 138 | #endif |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /* Privates **********************************************************/ |
---|
| 142 | |
---|
| 143 | void MainWindow::actionFileNewTriggered() |
---|
| 144 | { |
---|
| 145 | if (!maybeSave()) |
---|
| 146 | return; |
---|
| 147 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 148 | tspmodel->clear(); |
---|
| 149 | setFileName(); |
---|
| 150 | setWindowModified(false); |
---|
| 151 | tabWidget->setCurrentIndex(0); |
---|
| 152 | solutionText->clear(); |
---|
| 153 | toggleSolutionActions(false); |
---|
| 154 | QApplication::restoreOverrideCursor(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void MainWindow::actionFileOpenTriggered() |
---|
| 158 | { |
---|
| 159 | if (!maybeSave()) |
---|
| 160 | return; |
---|
| 161 | |
---|
| 162 | QStringList filters(tr("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
| 163 | filters.append(tr("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
| 164 | filters.append(tr("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
| 165 | filters.append(tr("All Files") + " (*)"); |
---|
| 166 | |
---|
[128] | 167 | QString file; |
---|
| 168 | if ((fileName == tr("Untitled") + ".tspt") && settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) |
---|
[133] | 169 | file = settings->value(OS"/LastUsed/TaskLoadPath").toString(); |
---|
[128] | 170 | else |
---|
| 171 | file = QFileInfo(fileName).path(); |
---|
[100] | 172 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
| 173 | file = QFileDialog::getOpenFileName(this, tr("Task Load"), file, filters.join(";;"), NULL, opts); |
---|
| 174 | if (file.isEmpty() || !QFileInfo(file).isFile()) |
---|
| 175 | return; |
---|
[128] | 176 | if (settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) |
---|
[133] | 177 | settings->setValue(OS"/LastUsed/TaskLoadPath", QFileInfo(file).path()); |
---|
[128] | 178 | |
---|
[100] | 179 | if (!tspmodel->loadTask(file)) |
---|
| 180 | return; |
---|
| 181 | setFileName(file); |
---|
| 182 | tabWidget->setCurrentIndex(0); |
---|
| 183 | setWindowModified(false); |
---|
| 184 | solutionText->clear(); |
---|
| 185 | toggleSolutionActions(false); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | bool MainWindow::actionFileSaveTriggered() |
---|
| 189 | { |
---|
[128] | 190 | if ((fileName == tr("Untitled") + ".tspt") || !fileName.endsWith(".tspt", Qt::CaseInsensitive)) |
---|
[100] | 191 | return saveTask(); |
---|
| 192 | else |
---|
| 193 | if (tspmodel->saveTask(fileName)) { |
---|
| 194 | setWindowModified(false); |
---|
| 195 | return true; |
---|
| 196 | } else |
---|
| 197 | return false; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
| 201 | { |
---|
| 202 | saveTask(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
| 206 | { |
---|
| 207 | static QString selectedFile; |
---|
[128] | 208 | if (selectedFile.isEmpty()) { |
---|
| 209 | if (settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) { |
---|
[133] | 210 | selectedFile = settings->value(OS"/LastUsed/SolutionSavePath").toString(); |
---|
[128] | 211 | } |
---|
| 212 | } else |
---|
| 213 | selectedFile = QFileInfo(selectedFile).path(); |
---|
[100] | 214 | if (!selectedFile.isEmpty()) |
---|
[128] | 215 | selectedFile.append("/"); |
---|
[100] | 216 | if (fileName == tr("Untitled") + ".tspt") { |
---|
| 217 | #ifndef QT_NO_PRINTER |
---|
| 218 | selectedFile += "solution.pdf"; |
---|
| 219 | #else |
---|
| 220 | selectedFile += "solution.html"; |
---|
| 221 | #endif // QT_NO_PRINTER |
---|
| 222 | } else { |
---|
| 223 | #ifndef QT_NO_PRINTER |
---|
| 224 | selectedFile += QFileInfo(fileName).completeBaseName() + ".pdf"; |
---|
| 225 | #else |
---|
| 226 | selectedFile += QFileInfo(fileName).completeBaseName() + ".html"; |
---|
| 227 | #endif // QT_NO_PRINTER |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | QStringList filters; |
---|
| 231 | #ifndef QT_NO_PRINTER |
---|
| 232 | filters.append(tr("PDF Files") + " (*.pdf)"); |
---|
| 233 | #endif |
---|
| 234 | filters.append(tr("HTML Files") + " (*.html *.htm)"); |
---|
| 235 | filters.append(tr("OpenDocument Files") + " (*.odt)"); |
---|
| 236 | filters.append(tr("All Files") + " (*)"); |
---|
| 237 | |
---|
| 238 | QFileDialog::Options opts(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog); |
---|
| 239 | QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"), NULL, opts); |
---|
| 240 | if (file.isEmpty()) |
---|
| 241 | return; |
---|
| 242 | selectedFile = file; |
---|
[128] | 243 | if (settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) |
---|
[133] | 244 | settings->setValue(OS"/LastUsed/SolutionSavePath", QFileInfo(selectedFile).path()); |
---|
[100] | 245 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 246 | #ifndef QT_NO_PRINTER |
---|
| 247 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
| 248 | QPrinter printer(QPrinter::HighResolution); |
---|
| 249 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
| 250 | printer.setOutputFileName(selectedFile); |
---|
| 251 | solutionText->document()->print(&printer); |
---|
| 252 | QApplication::restoreOverrideCursor(); |
---|
| 253 | return; |
---|
| 254 | } |
---|
| 255 | #endif |
---|
[108] | 256 | if (selectedFile.endsWith(".htm", Qt::CaseInsensitive) || selectedFile.endsWith(".html", Qt::CaseInsensitive)) { |
---|
| 257 | QFile file(selectedFile); |
---|
| 258 | if (!file.open(QFile::WriteOnly)) { |
---|
| 259 | QApplication::restoreOverrideCursor(); |
---|
| 260 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(file.errorString())); |
---|
| 261 | return; |
---|
| 262 | } |
---|
| 263 | QFileInfo fi(selectedFile); |
---|
[109] | 264 | QString format = settings->value("Output/GraphImageFormat", DEF_GRAPH_IMAGE_FORMAT).toString(); |
---|
[134] | 265 | #if !defined(NOSVG) |
---|
[109] | 266 | if (!QImageWriter::supportedImageFormats().contains(format.toAscii()) && (format != "svg")) { |
---|
[134] | 267 | #else // NOSVG |
---|
[109] | 268 | if (!QImageWriter::supportedImageFormats().contains(format.toAscii())) { |
---|
[134] | 269 | #endif // NOSVG |
---|
[109] | 270 | format = DEF_GRAPH_IMAGE_FORMAT; |
---|
| 271 | settings->remove("Output/GraphImageFormat"); |
---|
| 272 | } |
---|
[108] | 273 | QString html = solutionText->document()->toHtml("UTF-8"), |
---|
[109] | 274 | img = fi.completeBaseName() + "." + format; |
---|
[138] | 275 | html.replace(QRegExp("font-family:([^;]*);"), "font-family:\\1, 'DejaVu Sans Mono', 'Courier New', Courier, monospace;"); |
---|
[125] | 276 | html.replace(QRegExp("<img\\s+src=\"tspsg://graph.pic\""), QString("<img src=\"%1\" alt=\"%2\"").arg(img, tr("Solution Graph"))); |
---|
[109] | 277 | |
---|
[108] | 278 | // Saving solution text as HTML |
---|
| 279 | QTextStream ts(&file); |
---|
| 280 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
| 281 | ts << html; |
---|
| 282 | file.close(); |
---|
[109] | 283 | |
---|
| 284 | // Saving solution graph as SVG or PNG (depending on settings and SVG support) |
---|
[134] | 285 | #if !defined(NOSVG) |
---|
[109] | 286 | if (format == "svg") { |
---|
[108] | 287 | QSvgGenerator svg; |
---|
[138] | 288 | svg.setSize(QSize(graph.width() + 2, graph.height() + 2)); |
---|
[109] | 289 | svg.setResolution(graph.logicalDpiX()); |
---|
| 290 | svg.setFileName(fi.path() + "/" + img); |
---|
| 291 | svg.setTitle(tr("Solution Graph")); |
---|
[142] | 292 | svg.setDescription(tr("Generated with %1").arg(QCoreApplication::applicationName())); |
---|
[108] | 293 | QPainter p; |
---|
[109] | 294 | p.begin(&svg); |
---|
| 295 | p.drawPicture(1, 1, graph); |
---|
| 296 | p.end(); |
---|
| 297 | } else { |
---|
[134] | 298 | #endif // NOSVG |
---|
[138] | 299 | QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_ARGB32); |
---|
[109] | 300 | i.fill(0x00FFFFFF); |
---|
| 301 | QPainter p; |
---|
| 302 | p.begin(&i); |
---|
| 303 | p.drawPicture(1, 1, graph); |
---|
| 304 | p.end(); |
---|
| 305 | QImageWriter pic(fi.path() + "/" + img); |
---|
| 306 | if (pic.supportsOption(QImageIOHandler::Description)) { |
---|
| 307 | pic.setText("Title", "Solution Graph"); |
---|
[142] | 308 | pic.setText("Software", QCoreApplication::applicationName()); |
---|
[109] | 309 | } |
---|
| 310 | if (format == "png") |
---|
| 311 | pic.setQuality(5); |
---|
| 312 | else if (format == "jpeg") |
---|
| 313 | pic.setQuality(80); |
---|
| 314 | if (!pic.write(i)) { |
---|
| 315 | QApplication::restoreOverrideCursor(); |
---|
| 316 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution graph.\nError: %1").arg(pic.errorString())); |
---|
| 317 | return; |
---|
| 318 | } |
---|
[134] | 319 | #if !defined(NOSVG) |
---|
[109] | 320 | } |
---|
[134] | 321 | #endif // NOSVG |
---|
[108] | 322 | } else { |
---|
[100] | 323 | QTextDocumentWriter dw(selectedFile); |
---|
[108] | 324 | if (!selectedFile.endsWith(".odt",Qt::CaseInsensitive)) |
---|
| 325 | dw.setFormat("plaintext"); |
---|
| 326 | if (!dw.write(solutionText->document())) |
---|
| 327 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(dw.device()->errorString())); |
---|
[100] | 328 | } |
---|
| 329 | QApplication::restoreOverrideCursor(); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | #ifndef QT_NO_PRINTER |
---|
| 333 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
| 334 | { |
---|
| 335 | QPrintPreviewDialog ppd(printer, this); |
---|
| 336 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
| 337 | ppd.exec(); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | void MainWindow::actionFilePrintTriggered() |
---|
| 341 | { |
---|
| 342 | QPrintDialog pd(printer,this); |
---|
| 343 | if (pd.exec() != QDialog::Accepted) |
---|
| 344 | return; |
---|
| 345 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
[117] | 346 | solutionText->print(printer); |
---|
[100] | 347 | QApplication::restoreOverrideCursor(); |
---|
| 348 | } |
---|
| 349 | #endif // QT_NO_PRINTER |
---|
| 350 | |
---|
| 351 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
| 352 | { |
---|
| 353 | SettingsDialog sd(this); |
---|
| 354 | if (sd.exec() != QDialog::Accepted) |
---|
| 355 | return; |
---|
| 356 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
| 357 | if (!solutionText->document()->isEmpty() && sd.colorChanged()) |
---|
| 358 | QMessageBox::information(this, tr("Settings Changed"), tr("You have changed color settings.\nThey will be applied to the next solution output.")); |
---|
| 359 | initDocStyleSheet(); |
---|
| 360 | } |
---|
| 361 | if (sd.translucencyChanged() != 0) |
---|
| 362 | toggleTranclucency(sd.translucencyChanged() == 1); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
| 366 | { |
---|
| 367 | if (checked) { |
---|
| 368 | settings->remove("Language"); |
---|
[142] | 369 | QMessageBox::information(this, tr("Language change"), tr("Language will be autodetected on the next %1 start.").arg(QCoreApplication::applicationName())); |
---|
[100] | 370 | } else |
---|
| 371 | settings->setValue("Language", groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
| 375 | { |
---|
[141] | 376 | #ifndef Q_WS_MAEMO_5 |
---|
[100] | 377 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 378 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 379 | 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) { |
---|
| 380 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 381 | } else |
---|
| 382 | return; |
---|
| 383 | } |
---|
[141] | 384 | #endif |
---|
[100] | 385 | bool untitled = (fileName == tr("Untitled") + ".tspt"); |
---|
| 386 | if (loadLanguage(action->data().toString())) { |
---|
| 387 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 388 | settings->setValue("Language",action->data().toString()); |
---|
| 389 | retranslateUi(); |
---|
| 390 | if (untitled) |
---|
| 391 | setFileName(); |
---|
[141] | 392 | #ifdef Q_WS_WIN32 |
---|
[100] | 393 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
| 394 | toggleStyle(labelVariant, true); |
---|
| 395 | toggleStyle(labelCities, true); |
---|
| 396 | } |
---|
| 397 | #endif |
---|
| 398 | QApplication::restoreOverrideCursor(); |
---|
| 399 | if (!solutionText->document()->isEmpty()) |
---|
| 400 | 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.")); |
---|
| 401 | } |
---|
| 402 | } |
---|
| 403 | |
---|
[115] | 404 | void MainWindow::actionSettingsStyleSystemTriggered(bool checked) |
---|
| 405 | { |
---|
| 406 | if (checked) { |
---|
| 407 | settings->remove("Style"); |
---|
[142] | 408 | QMessageBox::information(this, tr("Style Change"), tr("To apply the default style you need to restart %1.").arg(QCoreApplication::applicationName())); |
---|
[115] | 409 | } else { |
---|
| 410 | settings->setValue("Style", groupSettingsStyleList->checkedAction()->text()); |
---|
| 411 | } |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | void MainWindow::groupSettingsStyleListTriggered(QAction *action) |
---|
| 415 | { |
---|
| 416 | QStyle *s = QStyleFactory::create(action->text()); |
---|
| 417 | if (s != NULL) { |
---|
| 418 | QApplication::setStyle(s); |
---|
| 419 | settings->setValue("Style", action->text()); |
---|
| 420 | actionSettingsStyleSystem->setChecked(false); |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
[118] | 424 | #ifndef HANDHELD |
---|
| 425 | void MainWindow::actionSettingsToolbarsConfigureTriggered() |
---|
| 426 | { |
---|
| 427 | QtToolBarDialog dlg(this); |
---|
| 428 | dlg.setToolBarManager(toolBarManager); |
---|
| 429 | dlg.exec(); |
---|
| 430 | QToolButton *tb = static_cast<QToolButton *>(toolBarMain->widgetForAction(actionFileSave)); |
---|
| 431 | if (tb != NULL) { |
---|
| 432 | tb->setMenu(menuFileSaveAs); |
---|
| 433 | tb->setPopupMode(QToolButton::MenuButtonPopup); |
---|
| 434 | tb->resize(tb->sizeHint()); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | loadToolbarList(); |
---|
| 438 | } |
---|
| 439 | #endif // HANDHELD |
---|
| 440 | |
---|
[100] | 441 | void MainWindow::actionHelpCheck4UpdatesTriggered() |
---|
| 442 | { |
---|
| 443 | if (!hasUpdater()) { |
---|
| 444 | 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.")); |
---|
| 445 | return; |
---|
| 446 | } |
---|
| 447 | |
---|
[139] | 448 | check4Updates(); |
---|
[100] | 449 | } |
---|
| 450 | |
---|
| 451 | void MainWindow::actionHelpAboutTriggered() |
---|
| 452 | { |
---|
[124] | 453 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 454 | |
---|
[100] | 455 | QString title; |
---|
[142] | 456 | title += QString("<b>%1</b><br>").arg(QCoreApplication::applicationName()); |
---|
| 457 | title += QString("%1: <b>%2</b><br>").arg(tr("Version"), QCoreApplication::applicationVersion()); |
---|
[100] | 458 | #ifndef HANDHELD |
---|
[142] | 459 | title += QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b><br>").arg(QDate::currentDate().toString("yyyy"), QCoreApplication::organizationDomain(), QCoreApplication::organizationName()); |
---|
[118] | 460 | #endif // HANDHELD |
---|
[126] | 461 | title += QString("<b><a href=\"http://tspsg.info/\">http://tspsg.info/</a></b>"); |
---|
[100] | 462 | |
---|
| 463 | QString about; |
---|
[133] | 464 | about += QString("%1: <b>%2</b><br>").arg(tr("Target OS (ARCH)"), PLATFROM); |
---|
[100] | 465 | #ifndef STATIC_BUILD |
---|
| 466 | about += QString("%1 (%2):<br>").arg(tr("Qt library"), tr("shared")); |
---|
| 467 | about += QString(" %1: <b>%2</b><br>").arg(tr("Build time"), QT_VERSION_STR); |
---|
| 468 | about += QString(" %1: <b>%2</b><br>").arg(tr("Runtime"), qVersion()); |
---|
| 469 | #else |
---|
| 470 | about += QString("%1: <b>%2</b> (%3)<br>").arg(tr("Qt library"), QT_VERSION_STR, tr("static")); |
---|
| 471 | #endif // STATIC_BUILD |
---|
[133] | 472 | about += tr("Buid <b>%1</b>, built on <b>%2</b> at <b>%3</b> with <b>%4</b> compiler.").arg(BUILD_NUMBER).arg(__DATE__).arg(__TIME__).arg(COMPILER) + "<br>"; |
---|
[100] | 473 | about += QString("%1: <b>%2</b><br>").arg(tr("Algorithm"), CTSPSolver::getVersionId()); |
---|
| 474 | about += "<br>"; |
---|
[124] | 475 | about += tr("This program is free software: you can redistribute it and/or modify<br>\n" |
---|
| 476 | "it under the terms of the GNU General Public License as published by<br>\n" |
---|
| 477 | "the Free Software Foundation, either version 3 of the License, or<br>\n" |
---|
| 478 | "(at your option) any later version.<br>\n" |
---|
| 479 | "<br>\n" |
---|
| 480 | "This program is distributed in the hope that it will be useful,<br>\n" |
---|
| 481 | "but WITHOUT ANY WARRANTY; without even the implied warranty of<br>\n" |
---|
| 482 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>\n" |
---|
| 483 | "GNU General Public License for more details.<br>\n" |
---|
| 484 | "<br>\n" |
---|
| 485 | "You should have received a copy of the GNU General Public License<br>\n" |
---|
| 486 | "along with TSPSG. If not, see <a href=\"http://www.gnu.org/licenses/\">www.gnu.org/licenses/</a>."); |
---|
[100] | 487 | |
---|
[124] | 488 | QString credits; |
---|
[127] | 489 | credits += tr("%1 was created using <b>Qt framework</b> licensed " |
---|
| 490 | "under the terms of the GNU Lesser General Public License,<br>\n" |
---|
[124] | 491 | "see <a href=\"http://qt.nokia.com/\">qt.nokia.com</a><br>\n" |
---|
| 492 | "<br>\n" |
---|
[127] | 493 | "Most icons used in %1 are part of <b>Oxygen Icons</b> project " |
---|
[124] | 494 | "licensed according to the GNU Lesser General Public License,<br>\n" |
---|
| 495 | "see <a href=\"http://www.oxygen-icons.org/\">www.oxygen-icons.org</a><br>\n" |
---|
| 496 | "<br>\n" |
---|
[127] | 497 | "Country flag icons used in %1 are part of the free " |
---|
| 498 | "<b>Flag Icons</b> collection created by <b>IconDrawer</b>,<br>\n" |
---|
[130] | 499 | "see <a href=\"http://www.icondrawer.com/\">www.icondrawer.com</a><br>\n" |
---|
| 500 | "<br>\n" |
---|
| 501 | "%1 comes with the default \"embedded\" font <b>DejaVu LGC Sans " |
---|
| 502 | "Mono</b> from the <b>DejaVu fonts</b> licensed under a Free license</a>,<br>\n" |
---|
[131] | 503 | "see <a href=\"http://dejavu-fonts.org/\">dejavu-fonts.org</a>") |
---|
| 504 | .arg("TSPSG"); |
---|
[124] | 505 | |
---|
| 506 | QFile f(":/files/COPYING"); |
---|
| 507 | f.open(QIODevice::ReadOnly); |
---|
| 508 | |
---|
[142] | 509 | QString translation = QCoreApplication::translate("--------", "AUTHORS %1", "Please, provide translator credits here. %1 will be replaced with VERSION"); |
---|
[124] | 510 | if ((translation != "AUTHORS %1") && (translation.contains("%1"))) { |
---|
[142] | 511 | QString about = QCoreApplication::translate("--------", "VERSION", "Please, provide your translation version here."); |
---|
[124] | 512 | if (about != "VERSION") |
---|
| 513 | translation = translation.arg(about); |
---|
| 514 | } |
---|
| 515 | |
---|
[100] | 516 | QDialog *dlg = new QDialog(this); |
---|
| 517 | QLabel *lblIcon = new QLabel(dlg), |
---|
[124] | 518 | *lblTitle = new QLabel(dlg); |
---|
[100] | 519 | #ifdef HANDHELD |
---|
[142] | 520 | QLabel *lblSubTitle = new QLabel(QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b>").arg(QDate::currentDate().toString("yyyy"), QCoreApplication::organizationDomain(), QCoreApplication::organizationName()), dlg); |
---|
[100] | 521 | #endif // HANDHELD |
---|
[124] | 522 | QTabWidget *tabs = new QTabWidget(dlg); |
---|
[100] | 523 | QTextBrowser *txtAbout = new QTextBrowser(dlg); |
---|
[124] | 524 | QTextBrowser *txtLicense = new QTextBrowser(dlg); |
---|
| 525 | QTextBrowser *txtCredits = new QTextBrowser(dlg); |
---|
[100] | 526 | QVBoxLayout *vb = new QVBoxLayout(); |
---|
| 527 | QHBoxLayout *hb1 = new QHBoxLayout(), |
---|
| 528 | *hb2 = new QHBoxLayout(); |
---|
| 529 | QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg); |
---|
| 530 | |
---|
| 531 | lblTitle->setOpenExternalLinks(true); |
---|
| 532 | lblTitle->setText(title); |
---|
| 533 | lblTitle->setAlignment(Qt::AlignTop); |
---|
| 534 | lblTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
---|
| 535 | #ifndef HANDHELD |
---|
[115] | 536 | lblTitle->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 4px; padding: 1px;}").arg(palette().alternateBase().color().name(), palette().shadow().color().name())); |
---|
[100] | 537 | #endif // HANDHELD |
---|
| 538 | |
---|
| 539 | lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToHeight(lblTitle->sizeHint().height(), Qt::SmoothTransformation)); |
---|
| 540 | lblIcon->setAlignment(Qt::AlignVCenter); |
---|
| 541 | #ifndef HANDHELD |
---|
[115] | 542 | lblIcon->setStyleSheet(QString("QLabel {background-color: white; border-color: %1; border-width: 1px; border-style: solid; border-radius: 4px; padding: 1px;}").arg(palette().windowText().color().name())); |
---|
[100] | 543 | #endif // HANDHELD |
---|
| 544 | |
---|
| 545 | hb1->addWidget(lblIcon); |
---|
| 546 | hb1->addWidget(lblTitle); |
---|
| 547 | |
---|
| 548 | txtAbout->setWordWrapMode(QTextOption::NoWrap); |
---|
| 549 | txtAbout->setOpenExternalLinks(true); |
---|
| 550 | txtAbout->setHtml(about); |
---|
| 551 | txtAbout->moveCursor(QTextCursor::Start); |
---|
[124] | 552 | txtAbout->setFrameShape(QFrame::NoFrame); |
---|
[100] | 553 | |
---|
[124] | 554 | // txtCredits->setWordWrapMode(QTextOption::NoWrap); |
---|
| 555 | txtCredits->setOpenExternalLinks(true); |
---|
| 556 | txtCredits->setHtml(credits); |
---|
| 557 | txtCredits->moveCursor(QTextCursor::Start); |
---|
| 558 | txtCredits->setFrameShape(QFrame::NoFrame); |
---|
[100] | 559 | |
---|
[124] | 560 | txtLicense->setWordWrapMode(QTextOption::NoWrap); |
---|
| 561 | txtLicense->setOpenExternalLinks(true); |
---|
| 562 | txtLicense->setText(f.readAll()); |
---|
| 563 | txtLicense->moveCursor(QTextCursor::Start); |
---|
| 564 | txtLicense->setFrameShape(QFrame::NoFrame); |
---|
[100] | 565 | |
---|
[124] | 566 | bb->button(QDialogButtonBox::Ok)->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
[134] | 567 | bb->button(QDialogButtonBox::Ok)->setIcon(GET_ICON("dialog-ok")); |
---|
[121] | 568 | |
---|
[100] | 569 | hb2->addWidget(bb); |
---|
| 570 | |
---|
[141] | 571 | #ifdef Q_WS_WINCE_WM |
---|
[100] | 572 | vb->setMargin(3); |
---|
[141] | 573 | #endif // Q_WS_WINCE_WM |
---|
[100] | 574 | vb->addLayout(hb1); |
---|
| 575 | #ifdef HANDHELD |
---|
| 576 | vb->addWidget(lblSubTitle); |
---|
| 577 | #endif // HANDHELD |
---|
[124] | 578 | |
---|
| 579 | tabs->addTab(txtAbout, tr("About")); |
---|
| 580 | tabs->addTab(txtLicense, tr("License")); |
---|
| 581 | tabs->addTab(txtCredits, tr("Credits")); |
---|
| 582 | if (translation != "AUTHORS %1") { |
---|
| 583 | QTextBrowser *txtTranslation = new QTextBrowser(dlg); |
---|
| 584 | // txtTranslation->setWordWrapMode(QTextOption::NoWrap); |
---|
| 585 | txtTranslation->setOpenExternalLinks(true); |
---|
| 586 | txtTranslation->setText(translation); |
---|
| 587 | txtTranslation->moveCursor(QTextCursor::Start); |
---|
| 588 | txtTranslation->setFrameShape(QFrame::NoFrame); |
---|
| 589 | |
---|
| 590 | tabs->addTab(txtTranslation, tr("Translation")); |
---|
| 591 | } |
---|
| 592 | #ifndef HANDHELD |
---|
| 593 | tabs->setStyleSheet(QString("QTabWidget::pane {background-color: %1; border-color: %3; border-width: 1px; border-style: solid; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; padding: 1px;} QTabBar::tab {background-color: %2; border-color: %3; border-width: 1px; border-style: solid; border-bottom: none; border-top-left-radius: 4px; border-top-right-radius: 4px; padding: 2px 6px;} QTabBar::tab:selected {background-color: %4;} QTabBar::tab:!selected {margin-top: 1px;}").arg(palette().base().color().name(), palette().button().color().name(), palette().shadow().color().name(), palette().light().color().name())); |
---|
| 594 | #endif // HANDHELD |
---|
| 595 | |
---|
| 596 | vb->addWidget(tabs); |
---|
[100] | 597 | vb->addLayout(hb2); |
---|
| 598 | |
---|
| 599 | dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); |
---|
[142] | 600 | dlg->setWindowTitle(tr("About %1").arg(QCoreApplication::applicationName())); |
---|
[134] | 601 | dlg->setWindowIcon(GET_ICON("help-about")); |
---|
[100] | 602 | dlg->setLayout(vb); |
---|
| 603 | |
---|
| 604 | connect(bb, SIGNAL(accepted()), dlg, SLOT(accept())); |
---|
| 605 | |
---|
[141] | 606 | #ifdef Q_WS_WIN32 |
---|
[100] | 607 | // Adding some eyecandy in Vista and 7 :-) |
---|
| 608 | if (QtWin::isCompositionEnabled()) { |
---|
| 609 | QtWin::enableBlurBehindWindow(dlg, true); |
---|
| 610 | } |
---|
[141] | 611 | #endif // Q_WS_WIN32 |
---|
[100] | 612 | |
---|
| 613 | dlg->resize(450, 350); |
---|
[124] | 614 | QApplication::restoreOverrideCursor(); |
---|
[100] | 615 | |
---|
| 616 | dlg->exec(); |
---|
| 617 | |
---|
| 618 | delete dlg; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | void MainWindow::buttonBackToTaskClicked() |
---|
| 622 | { |
---|
| 623 | tabWidget->setCurrentIndex(0); |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | void MainWindow::buttonRandomClicked() |
---|
| 627 | { |
---|
| 628 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 629 | tspmodel->randomize(); |
---|
| 630 | QApplication::restoreOverrideCursor(); |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | void MainWindow::buttonSolveClicked() |
---|
| 634 | { |
---|
| 635 | TMatrix matrix; |
---|
| 636 | QList<double> row; |
---|
| 637 | int n = spinCities->value(); |
---|
| 638 | bool ok; |
---|
| 639 | for (int r = 0; r < n; r++) { |
---|
| 640 | row.clear(); |
---|
| 641 | for (int c = 0; c < n; c++) { |
---|
| 642 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
| 643 | if (!ok) { |
---|
| 644 | QMessageBox::critical(this, tr("Data error"), tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1)); |
---|
| 645 | return; |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | matrix.append(row); |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | QProgressDialog pd(this); |
---|
| 652 | QProgressBar *pb = new QProgressBar(&pd); |
---|
| 653 | pb->setAlignment(Qt::AlignCenter); |
---|
| 654 | pb->setFormat(tr("%v of %1 parts found").arg(n)); |
---|
| 655 | pd.setBar(pb); |
---|
[124] | 656 | QPushButton *cancel = new QPushButton(&pd); |
---|
[134] | 657 | cancel->setIcon(GET_ICON("dialog-cancel")); |
---|
[142] | 658 | cancel->setText(QCoreApplication::translate("QProgressDialog", "Cancel", "No need to translate this. This translation will be taken from Qt translation files.")); |
---|
[124] | 659 | pd.setCancelButton(cancel); |
---|
[100] | 660 | pd.setMaximum(n); |
---|
| 661 | pd.setAutoReset(false); |
---|
| 662 | pd.setLabelText(tr("Calculating optimal route...")); |
---|
| 663 | pd.setWindowTitle(tr("Solution Progress")); |
---|
| 664 | pd.setWindowModality(Qt::ApplicationModal); |
---|
| 665 | pd.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint); |
---|
| 666 | pd.show(); |
---|
| 667 | |
---|
[141] | 668 | #ifdef Q_WS_WIN32 |
---|
[124] | 669 | HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (LPVOID*)&tl); |
---|
| 670 | if (SUCCEEDED(hr)) { |
---|
| 671 | hr = tl->HrInit(); |
---|
| 672 | if (FAILED(hr)) { |
---|
| 673 | tl->Release(); |
---|
| 674 | tl = NULL; |
---|
| 675 | } else { |
---|
| 676 | // tl->SetProgressState(winId(), TBPF_INDETERMINATE); |
---|
| 677 | tl->SetProgressValue(winId(), 0, n * 2); |
---|
| 678 | } |
---|
| 679 | } |
---|
| 680 | #endif |
---|
| 681 | |
---|
[100] | 682 | CTSPSolver solver; |
---|
[124] | 683 | solver.setCleanupOnCancel(false); |
---|
[100] | 684 | connect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
| 685 | connect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
[141] | 686 | #ifdef Q_WS_WIN32 |
---|
[124] | 687 | if (tl != NULL) |
---|
| 688 | connect(&solver, SIGNAL(routePartFound(int)), SLOT(solverRoutePartFound(int))); |
---|
| 689 | #endif |
---|
[100] | 690 | SStep *root = solver.solve(n, matrix); |
---|
[141] | 691 | #ifdef Q_WS_WIN32 |
---|
[124] | 692 | if (tl != NULL) |
---|
| 693 | disconnect(&solver, SIGNAL(routePartFound(int)), this, SLOT(solverRoutePartFound(int))); |
---|
| 694 | #endif |
---|
[100] | 695 | disconnect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
| 696 | disconnect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
| 697 | if (!root) { |
---|
| 698 | pd.reset(); |
---|
[124] | 699 | if (!solver.wasCanceled()) { |
---|
[141] | 700 | #ifdef Q_WS_WIN32 |
---|
[124] | 701 | if (tl != NULL) { |
---|
| 702 | // tl->SetProgressValue(winId(), n, n * 2); |
---|
| 703 | tl->SetProgressState(winId(), TBPF_ERROR); |
---|
| 704 | } |
---|
| 705 | #endif |
---|
| 706 | QApplication::alert(this); |
---|
[100] | 707 | QMessageBox::warning(this, tr("Solution Result"), tr("Unable to find a solution.\nMaybe, this task has no solution.")); |
---|
[124] | 708 | } |
---|
| 709 | pd.setLabelText(tr("Cleaning up...")); |
---|
| 710 | pd.setMaximum(0); |
---|
| 711 | pd.setCancelButton(NULL); |
---|
| 712 | pd.show(); |
---|
[141] | 713 | #ifdef Q_WS_WIN32 |
---|
[124] | 714 | if (tl != NULL) |
---|
| 715 | tl->SetProgressState(winId(), TBPF_INDETERMINATE); |
---|
| 716 | #endif |
---|
[142] | 717 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[124] | 718 | |
---|
[135] | 719 | #ifndef QT_NO_CONCURRENT |
---|
[124] | 720 | QFuture<void> f = QtConcurrent::run(&solver, &CTSPSolver::cleanup, false); |
---|
| 721 | while (!f.isFinished()) { |
---|
[142] | 722 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[124] | 723 | } |
---|
[135] | 724 | #else |
---|
| 725 | solver.cleanup(true); |
---|
| 726 | #endif |
---|
[124] | 727 | pd.reset(); |
---|
[141] | 728 | #ifdef Q_WS_WIN32 |
---|
[124] | 729 | if (tl != NULL) { |
---|
| 730 | tl->SetProgressState(winId(), TBPF_NOPROGRESS); |
---|
| 731 | tl->Release(); |
---|
| 732 | tl = NULL; |
---|
| 733 | } |
---|
| 734 | #endif |
---|
[100] | 735 | return; |
---|
| 736 | } |
---|
| 737 | pb->setFormat(tr("Generating header")); |
---|
| 738 | pd.setLabelText(tr("Generating solution output...")); |
---|
[107] | 739 | pd.setMaximum(solver.getTotalSteps() + 1); |
---|
[100] | 740 | pd.setValue(0); |
---|
| 741 | |
---|
[141] | 742 | #ifdef Q_WS_WIN32 |
---|
[124] | 743 | if (tl != NULL) |
---|
| 744 | tl->SetProgressValue(winId(), spinCities->value(), spinCities->value() + solver.getTotalSteps() + 1); |
---|
| 745 | #endif |
---|
| 746 | |
---|
[100] | 747 | solutionText->clear(); |
---|
[104] | 748 | solutionText->setDocumentTitle(tr("Solution of Variant #%1 Task").arg(spinVariant->value())); |
---|
| 749 | |
---|
[106] | 750 | QPainter pic; |
---|
[107] | 751 | if (settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) { |
---|
| 752 | pic.begin(&graph); |
---|
[125] | 753 | pic.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); |
---|
[135] | 754 | QFont font = qvariant_cast<QFont>(settings->value("Output/Font", QFont(DEF_FONT_FACE, 9))); |
---|
[127] | 755 | if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { |
---|
| 756 | font.setWeight(QFont::DemiBold); |
---|
| 757 | font.setPointSizeF(font.pointSizeF() * 2); |
---|
| 758 | } |
---|
[125] | 759 | pic.setFont(font); |
---|
[109] | 760 | pic.setBrush(QBrush(QColor(Qt::white))); |
---|
[127] | 761 | if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { |
---|
[125] | 762 | QPen pen = pic.pen(); |
---|
[127] | 763 | pen.setWidth(2); |
---|
| 764 | pic.setPen(pen); |
---|
| 765 | } |
---|
[109] | 766 | pic.setBackgroundMode(Qt::OpaqueMode); |
---|
[107] | 767 | } |
---|
[106] | 768 | |
---|
[104] | 769 | QTextDocument *doc = solutionText->document(); |
---|
| 770 | QTextCursor cur(doc); |
---|
| 771 | |
---|
| 772 | cur.beginEditBlock(); |
---|
| 773 | cur.setBlockFormat(fmt_paragraph); |
---|
[106] | 774 | cur.insertText(tr("Variant #%1 Task").arg(spinVariant->value()), fmt_default); |
---|
[104] | 775 | cur.insertBlock(fmt_paragraph); |
---|
| 776 | cur.insertText(tr("Task:")); |
---|
| 777 | outputMatrix(cur, matrix); |
---|
[121] | 778 | if (settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) { |
---|
| 779 | #ifdef _T_T_L_ |
---|
[122] | 780 | _b_ _i_ _z_ _a_ _r_ _r_ _e_ |
---|
[121] | 781 | #endif |
---|
[107] | 782 | drawNode(pic, 0); |
---|
[121] | 783 | } |
---|
[104] | 784 | cur.insertHtml("<hr>"); |
---|
| 785 | cur.insertBlock(fmt_paragraph); |
---|
[106] | 786 | int imgpos = cur.position(); |
---|
| 787 | cur.insertText(tr("Variant #%1 Solution").arg(spinVariant->value()), fmt_default); |
---|
[104] | 788 | cur.endEditBlock(); |
---|
| 789 | |
---|
[100] | 790 | SStep *step = root; |
---|
[107] | 791 | int c = n = 1; |
---|
[100] | 792 | pb->setFormat(tr("Generating step %v")); |
---|
[107] | 793 | while ((step->next != SStep::NoNextStep) && (c < spinCities->value())) { |
---|
[100] | 794 | if (pd.wasCanceled()) { |
---|
[104] | 795 | pd.setLabelText(tr("Cleaning up...")); |
---|
| 796 | pd.setMaximum(0); |
---|
| 797 | pd.setCancelButton(NULL); |
---|
| 798 | pd.show(); |
---|
[142] | 799 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[141] | 800 | #ifdef Q_WS_WIN32 |
---|
[124] | 801 | if (tl != NULL) |
---|
| 802 | tl->SetProgressState(winId(), TBPF_INDETERMINATE); |
---|
| 803 | #endif |
---|
[135] | 804 | #ifndef QT_NO_CONCURRENT |
---|
[124] | 805 | QFuture<void> f = QtConcurrent::run(&solver, &CTSPSolver::cleanup, false); |
---|
| 806 | while (!f.isFinished()) { |
---|
[142] | 807 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[124] | 808 | } |
---|
[135] | 809 | #else |
---|
| 810 | solver.cleanup(true); |
---|
| 811 | #endif |
---|
[100] | 812 | solutionText->clear(); |
---|
[104] | 813 | toggleSolutionActions(false); |
---|
[141] | 814 | #ifdef Q_WS_WIN32 |
---|
[124] | 815 | if (tl != NULL) { |
---|
| 816 | tl->SetProgressState(winId(), TBPF_NOPROGRESS); |
---|
| 817 | tl->Release(); |
---|
| 818 | tl = NULL; |
---|
| 819 | } |
---|
| 820 | #endif |
---|
[100] | 821 | return; |
---|
| 822 | } |
---|
| 823 | pd.setValue(n); |
---|
[141] | 824 | #ifdef Q_WS_WIN32 |
---|
[124] | 825 | if (tl != NULL) |
---|
| 826 | tl->SetProgressValue(winId(), spinCities->value() + n, spinCities->value() + solver.getTotalSteps() + 1); |
---|
| 827 | #endif |
---|
[100] | 828 | |
---|
[107] | 829 | cur.beginEditBlock(); |
---|
| 830 | cur.insertBlock(fmt_paragraph); |
---|
| 831 | cur.insertText(tr("Step #%1").arg(n)); |
---|
| 832 | 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())))) { |
---|
| 833 | outputMatrix(cur, *step); |
---|
| 834 | } |
---|
| 835 | cur.insertBlock(fmt_paragraph); |
---|
| 836 | 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); |
---|
| 837 | if (!step->alts.empty()) { |
---|
| 838 | SStep::SCandidate cand; |
---|
| 839 | QString alts; |
---|
| 840 | foreach(cand, step->alts) { |
---|
| 841 | if (!alts.isEmpty()) |
---|
| 842 | alts += ", "; |
---|
| 843 | alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); |
---|
[100] | 844 | } |
---|
[107] | 845 | cur.insertBlock(fmt_paragraph); |
---|
| 846 | cur.insertText(tr("%n alternate candidate(s) for branching: %1.", "", step->alts.count()).arg(alts), fmt_altlist); |
---|
[100] | 847 | } |
---|
[107] | 848 | cur.insertBlock(fmt_paragraph); |
---|
| 849 | cur.insertText(" ", fmt_default); |
---|
| 850 | cur.endEditBlock(); |
---|
| 851 | |
---|
| 852 | if (settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) { |
---|
[106] | 853 | if (step->prNode != NULL) |
---|
[107] | 854 | drawNode(pic, n, false, step->prNode); |
---|
[106] | 855 | if (step->plNode != NULL) |
---|
[107] | 856 | drawNode(pic, n, true, step->plNode); |
---|
[106] | 857 | } |
---|
[107] | 858 | n++; |
---|
| 859 | |
---|
| 860 | if (step->next == SStep::RightBranch) { |
---|
| 861 | c++; |
---|
[100] | 862 | step = step->prNode; |
---|
[107] | 863 | } else if (step->next == SStep::LeftBranch) { |
---|
[100] | 864 | step = step->plNode; |
---|
[106] | 865 | } else |
---|
[100] | 866 | break; |
---|
| 867 | } |
---|
| 868 | pb->setFormat(tr("Generating footer")); |
---|
| 869 | pd.setValue(n); |
---|
[141] | 870 | #ifdef Q_WS_WIN32 |
---|
[124] | 871 | if (tl != NULL) |
---|
| 872 | tl->SetProgressValue(winId(), spinCities->value() + n, spinCities->value() + solver.getTotalSteps() + 1); |
---|
| 873 | #endif |
---|
[100] | 874 | |
---|
[104] | 875 | cur.beginEditBlock(); |
---|
| 876 | cur.insertBlock(fmt_paragraph); |
---|
[100] | 877 | if (solver.isOptimal()) |
---|
[104] | 878 | cur.insertText(tr("Optimal path:")); |
---|
[100] | 879 | else |
---|
[104] | 880 | cur.insertText(tr("Resulting path:")); |
---|
| 881 | |
---|
| 882 | cur.insertBlock(fmt_paragraph); |
---|
[110] | 883 | cur.insertText(" " + solver.getSortedPath(tr("City %1"))); |
---|
[104] | 884 | |
---|
| 885 | cur.insertBlock(fmt_paragraph); |
---|
[100] | 886 | if (isInteger(step->price)) |
---|
[104] | 887 | cur.insertHtml("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>"); |
---|
[100] | 888 | else |
---|
[104] | 889 | 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>"); |
---|
[100] | 890 | if (!solver.isOptimal()) { |
---|
[104] | 891 | cur.insertBlock(fmt_paragraph); |
---|
| 892 | cur.insertText(" "); |
---|
| 893 | cur.insertBlock(fmt_paragraph); |
---|
| 894 | 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>"); |
---|
[100] | 895 | } |
---|
[104] | 896 | cur.endEditBlock(); |
---|
[100] | 897 | |
---|
[107] | 898 | if (settings->value("Output/ShowGraph", DEF_SHOW_GRAPH).toBool()) { |
---|
| 899 | pic.end(); |
---|
| 900 | |
---|
[138] | 901 | QImage i(graph.width() + 2, graph.height() + 2, QImage::Format_RGB32); |
---|
[127] | 902 | i.fill(0xFFFFFF); |
---|
[107] | 903 | pic.begin(&i); |
---|
| 904 | pic.drawPicture(1, 1, graph); |
---|
| 905 | pic.end(); |
---|
| 906 | doc->addResource(QTextDocument::ImageResource, QUrl("tspsg://graph.pic"), i); |
---|
[106] | 907 | |
---|
| 908 | QTextImageFormat img; |
---|
[107] | 909 | img.setName("tspsg://graph.pic"); |
---|
[127] | 910 | if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) { |
---|
| 911 | img.setWidth(i.width() / 2); |
---|
| 912 | img.setHeight(i.height() / 2); |
---|
| 913 | } else { |
---|
| 914 | img.setWidth(i.width()); |
---|
| 915 | img.setHeight(i.height()); |
---|
| 916 | } |
---|
[106] | 917 | |
---|
[107] | 918 | cur.setPosition(imgpos); |
---|
| 919 | cur.insertImage(img, QTextFrameFormat::FloatRight); |
---|
| 920 | } |
---|
[106] | 921 | |
---|
[100] | 922 | if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) { |
---|
[104] | 923 | // Scrolling to the end of the text. |
---|
[100] | 924 | solutionText->moveCursor(QTextCursor::End); |
---|
| 925 | } else |
---|
| 926 | solutionText->moveCursor(QTextCursor::Start); |
---|
| 927 | |
---|
| 928 | pd.setLabelText(tr("Cleaning up...")); |
---|
| 929 | pd.setMaximum(0); |
---|
| 930 | pd.setCancelButton(NULL); |
---|
[142] | 931 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[141] | 932 | #ifdef Q_WS_WIN32 |
---|
[124] | 933 | if (tl != NULL) |
---|
| 934 | tl->SetProgressState(winId(), TBPF_INDETERMINATE); |
---|
| 935 | #endif |
---|
[135] | 936 | #ifndef QT_NO_CONCURRENT |
---|
[124] | 937 | QFuture<void> f = QtConcurrent::run(&solver, &CTSPSolver::cleanup, false); |
---|
| 938 | while (!f.isFinished()) { |
---|
[142] | 939 | QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
[124] | 940 | } |
---|
[135] | 941 | #else |
---|
| 942 | solver.cleanup(true); |
---|
| 943 | #endif |
---|
[100] | 944 | toggleSolutionActions(); |
---|
| 945 | tabWidget->setCurrentIndex(1); |
---|
[141] | 946 | #ifdef Q_WS_WIN32 |
---|
[124] | 947 | if (tl != NULL) { |
---|
| 948 | tl->SetProgressState(winId(), TBPF_NOPROGRESS); |
---|
| 949 | tl->Release(); |
---|
| 950 | tl = NULL; |
---|
| 951 | } |
---|
| 952 | #endif |
---|
| 953 | |
---|
| 954 | pd.reset(); |
---|
| 955 | QApplication::alert(this, 3000); |
---|
[100] | 956 | } |
---|
| 957 | |
---|
| 958 | void MainWindow::dataChanged() |
---|
| 959 | { |
---|
| 960 | setWindowModified(true); |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
| 964 | { |
---|
| 965 | setWindowModified(true); |
---|
| 966 | if (settings->value("Autosize", DEF_AUTOSIZE).toBool()) { |
---|
| 967 | for (int k = tl.row(); k <= br.row(); k++) |
---|
| 968 | taskView->resizeRowToContents(k); |
---|
| 969 | for (int k = tl.column(); k <= br.column(); k++) |
---|
| 970 | taskView->resizeColumnToContents(k); |
---|
| 971 | } |
---|
| 972 | } |
---|
| 973 | |
---|
[141] | 974 | #ifdef Q_WS_WINCE_WM |
---|
[100] | 975 | void MainWindow::changeEvent(QEvent *ev) |
---|
| 976 | { |
---|
| 977 | if ((ev->type() == QEvent::ActivationChange) && isActiveWindow()) |
---|
| 978 | desktopResized(0); |
---|
| 979 | |
---|
| 980 | QWidget::changeEvent(ev); |
---|
| 981 | } |
---|
| 982 | |
---|
| 983 | void MainWindow::desktopResized(int screen) |
---|
| 984 | { |
---|
| 985 | if ((screen != 0) || !isActiveWindow()) |
---|
| 986 | return; |
---|
| 987 | |
---|
| 988 | QRect availableGeometry = QApplication::desktop()->availableGeometry(0); |
---|
| 989 | if (currentGeometry != availableGeometry) { |
---|
| 990 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 991 | /*! |
---|
| 992 | * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height. |
---|
| 993 | * If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the |
---|
| 994 | * window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile? |
---|
| 995 | */ |
---|
| 996 | if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) { |
---|
| 997 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
| 998 | } else { |
---|
| 999 | if (windowState() & Qt::WindowMaximized) |
---|
| 1000 | setWindowState(windowState() ^ Qt::WindowMaximized); |
---|
| 1001 | setGeometry(availableGeometry); |
---|
| 1002 | } |
---|
| 1003 | currentGeometry = availableGeometry; |
---|
| 1004 | QApplication::restoreOverrideCursor(); |
---|
| 1005 | } |
---|
| 1006 | } |
---|
[141] | 1007 | #endif // Q_WS_WINCE_WM |
---|
[100] | 1008 | |
---|
| 1009 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 1010 | { |
---|
| 1011 | blockSignals(true); |
---|
| 1012 | spinCities->setValue(nCities); |
---|
| 1013 | blockSignals(false); |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | #ifndef QT_NO_PRINTER |
---|
| 1017 | void MainWindow::printPreview(QPrinter *printer) |
---|
| 1018 | { |
---|
| 1019 | solutionText->print(printer); |
---|
| 1020 | } |
---|
| 1021 | #endif // QT_NO_PRINTER |
---|
| 1022 | |
---|
[141] | 1023 | #ifdef Q_WS_WIN32 |
---|
[124] | 1024 | void MainWindow::solverRoutePartFound(int n) |
---|
| 1025 | { |
---|
[141] | 1026 | #ifdef Q_WS_WIN32 |
---|
[124] | 1027 | tl->SetProgressValue(winId(), n, spinCities->value() * 2); |
---|
| 1028 | #else |
---|
| 1029 | Q_UNUSED(n); |
---|
[141] | 1030 | #endif // Q_WS_WIN32 |
---|
[124] | 1031 | } |
---|
[141] | 1032 | #endif // Q_WS_WIN32 |
---|
[124] | 1033 | |
---|
[100] | 1034 | void MainWindow::spinCitiesValueChanged(int n) |
---|
| 1035 | { |
---|
| 1036 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 1037 | int count = tspmodel->numCities(); |
---|
| 1038 | tspmodel->setNumCities(n); |
---|
| 1039 | if ((n > count) && settings->value("Autosize", DEF_AUTOSIZE).toBool()) |
---|
| 1040 | for (int k = count; k < n; k++) { |
---|
| 1041 | taskView->resizeColumnToContents(k); |
---|
| 1042 | taskView->resizeRowToContents(k); |
---|
| 1043 | } |
---|
| 1044 | QApplication::restoreOverrideCursor(); |
---|
| 1045 | } |
---|
| 1046 | |
---|
[139] | 1047 | void MainWindow::check4Updates(bool silent) |
---|
| 1048 | { |
---|
[141] | 1049 | #ifdef Q_WS_WIN32 |
---|
[139] | 1050 | if (silent) |
---|
| 1051 | QProcess::startDetached("updater/Update.exe -name=\"TSPSG: TSP Solver and Generator\" -check=\"freeupdate\" -silentcheck"); |
---|
| 1052 | else { |
---|
| 1053 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
| 1054 | QProcess::execute("updater/Update.exe -name=\"TSPSG: TSP Solver and Generator\" -check=\"freeupdate\""); |
---|
| 1055 | QApplication::restoreOverrideCursor(); |
---|
| 1056 | } |
---|
[140] | 1057 | #else |
---|
| 1058 | Q_UNUSED(silent) |
---|
[139] | 1059 | #endif |
---|
| 1060 | settings->setValue("Check4Updates/LastAttempt", QDate::currentDate().toString(Qt::ISODate)); |
---|
| 1061 | } |
---|
| 1062 | |
---|
[100] | 1063 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
| 1064 | { |
---|
| 1065 | if (!maybeSave()) { |
---|
| 1066 | ev->ignore(); |
---|
| 1067 | return; |
---|
| 1068 | } |
---|
| 1069 | if (!settings->value("SettingsReset", false).toBool()) { |
---|
| 1070 | settings->setValue("NumCities", spinCities->value()); |
---|
| 1071 | |
---|
| 1072 | // Saving Main Window state |
---|
[118] | 1073 | #ifndef HANDHELD |
---|
[100] | 1074 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
| 1075 | settings->beginGroup("MainWindow"); |
---|
| 1076 | settings->setValue("Geometry", saveGeometry()); |
---|
| 1077 | settings->setValue("State", saveState()); |
---|
[118] | 1078 | settings->setValue("Toolbars", toolBarManager->saveState()); |
---|
[100] | 1079 | settings->endGroup(); |
---|
| 1080 | } |
---|
[135] | 1081 | #else |
---|
| 1082 | settings->setValue("MainWindow/ToolbarVisible", toolBarMain->isVisible()); |
---|
[118] | 1083 | #endif // HANDHELD |
---|
[100] | 1084 | } else { |
---|
| 1085 | settings->remove("SettingsReset"); |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | QMainWindow::closeEvent(ev); |
---|
| 1089 | } |
---|
| 1090 | |
---|
[105] | 1091 | void MainWindow::dragEnterEvent(QDragEnterEvent *ev) |
---|
| 1092 | { |
---|
| 1093 | if (ev->mimeData()->hasUrls() && (ev->mimeData()->urls().count() == 1)) { |
---|
| 1094 | QFileInfo fi(ev->mimeData()->urls().first().toLocalFile()); |
---|
| 1095 | if ((fi.suffix() == "tspt") || (fi.suffix() == "zkt")) |
---|
| 1096 | ev->acceptProposedAction(); |
---|
| 1097 | } |
---|
| 1098 | } |
---|
| 1099 | |
---|
[106] | 1100 | void MainWindow::drawNode(QPainter &pic, int nstep, bool left, SStep *step) |
---|
| 1101 | { |
---|
[127] | 1102 | int r; |
---|
| 1103 | if (settings->value("Output/HQGraph", DEF_HQ_GRAPH).toBool()) |
---|
| 1104 | r = 70; |
---|
| 1105 | else |
---|
| 1106 | r = 35; |
---|
[106] | 1107 | qreal x, y; |
---|
| 1108 | if (step != NULL) |
---|
| 1109 | x = left ? r : r * 3.5; |
---|
| 1110 | else |
---|
| 1111 | x = r * 2.25; |
---|
| 1112 | y = r * (3 * nstep + 1); |
---|
| 1113 | |
---|
[121] | 1114 | #ifdef _T_T_L_ |
---|
| 1115 | if (nstep == -481124) { |
---|
| 1116 | _t_t_l_(pic, r, x); |
---|
| 1117 | return; |
---|
| 1118 | } |
---|
| 1119 | #endif |
---|
| 1120 | |
---|
[106] | 1121 | pic.drawEllipse(QPointF(x, y), r, r); |
---|
| 1122 | |
---|
| 1123 | if (step != NULL) { |
---|
| 1124 | QFont font; |
---|
| 1125 | if (left) { |
---|
| 1126 | font = pic.font(); |
---|
| 1127 | font.setStrikeOut(true); |
---|
| 1128 | pic.setFont(font); |
---|
| 1129 | } |
---|
| 1130 | 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"); |
---|
| 1131 | if (left) { |
---|
| 1132 | font.setStrikeOut(false); |
---|
| 1133 | pic.setFont(font); |
---|
| 1134 | } |
---|
[107] | 1135 | if (step->price != INFINITY) { |
---|
| 1136 | 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())); |
---|
| 1137 | } else { |
---|
| 1138 | pic.drawText(QRectF(x - r, y - r, r * 2, r * 2), Qt::AlignCenter, "\n"INFSTR); |
---|
| 1139 | } |
---|
[106] | 1140 | } else { |
---|
| 1141 | pic.drawText(QRectF(x - r, y - r, r * 2, r * 2), Qt::AlignCenter, tr("Root")); |
---|
| 1142 | } |
---|
| 1143 | |
---|
[107] | 1144 | if (nstep == 1) { |
---|
| 1145 | pic.drawLine(QPointF(x, y - r), QPointF(r * 2.25, y - 2 * r)); |
---|
| 1146 | } else if (nstep > 1) { |
---|
[110] | 1147 | pic.drawLine(QPointF(x, y - r), QPointF((step->pNode->pNode->next == SStep::RightBranch) ? r * 3.5 : r, y - 2 * r)); |
---|
[107] | 1148 | } |
---|
[106] | 1149 | |
---|
| 1150 | } |
---|
| 1151 | |
---|
[105] | 1152 | void MainWindow::dropEvent(QDropEvent *ev) |
---|
| 1153 | { |
---|
| 1154 | if (maybeSave() && tspmodel->loadTask(ev->mimeData()->urls().first().toLocalFile())) { |
---|
| 1155 | setFileName(ev->mimeData()->urls().first().toLocalFile()); |
---|
| 1156 | tabWidget->setCurrentIndex(0); |
---|
| 1157 | setWindowModified(false); |
---|
| 1158 | solutionText->clear(); |
---|
| 1159 | toggleSolutionActions(false); |
---|
| 1160 | |
---|
[107] | 1161 | ev->setDropAction(Qt::CopyAction); |
---|
| 1162 | ev->accept(); |
---|
[105] | 1163 | } |
---|
| 1164 | } |
---|
| 1165 | |
---|
[100] | 1166 | void MainWindow::initDocStyleSheet() |
---|
| 1167 | { |
---|
[135] | 1168 | solutionText->document()->setDefaultFont(qvariant_cast<QFont>(settings->value("Output/Font", QFont(DEF_FONT_FACE, DEF_FONT_SIZE)))); |
---|
[104] | 1169 | |
---|
| 1170 | fmt_paragraph.setTopMargin(0); |
---|
| 1171 | fmt_paragraph.setRightMargin(10); |
---|
| 1172 | fmt_paragraph.setBottomMargin(0); |
---|
| 1173 | fmt_paragraph.setLeftMargin(10); |
---|
| 1174 | |
---|
| 1175 | fmt_table.setTopMargin(5); |
---|
| 1176 | fmt_table.setRightMargin(10); |
---|
| 1177 | fmt_table.setBottomMargin(5); |
---|
| 1178 | fmt_table.setLeftMargin(10); |
---|
| 1179 | fmt_table.setBorder(0); |
---|
| 1180 | fmt_table.setBorderStyle(QTextFrameFormat::BorderStyle_None); |
---|
| 1181 | fmt_table.setCellSpacing(5); |
---|
| 1182 | |
---|
[106] | 1183 | fmt_cell.setAlignment(Qt::AlignHCenter); |
---|
[104] | 1184 | |
---|
[119] | 1185 | settings->beginGroup("Output/Colors"); |
---|
| 1186 | |
---|
[135] | 1187 | QColor color = qvariant_cast<QColor>(settings->value("Text", DEF_TEXT_COLOR)); |
---|
[100] | 1188 | QColor hilight; |
---|
| 1189 | if (color.value() < 192) |
---|
[104] | 1190 | hilight.setHsv(color.hue(), color.saturation(), 127 + qRound(color.value() / 2)); |
---|
[100] | 1191 | else |
---|
[104] | 1192 | hilight.setHsv(color.hue(), color.saturation(), color.value() / 2); |
---|
| 1193 | |
---|
| 1194 | solutionText->document()->setDefaultStyleSheet(QString("* {color: %1;}").arg(color.name())); |
---|
| 1195 | fmt_default.setForeground(QBrush(color)); |
---|
| 1196 | |
---|
[135] | 1197 | fmt_selected.setForeground(QBrush(qvariant_cast<QColor>(settings->value("Selected", DEF_SELECTED_COLOR)))); |
---|
[104] | 1198 | fmt_selected.setFontWeight(QFont::Bold); |
---|
| 1199 | |
---|
[135] | 1200 | fmt_alternate.setForeground(QBrush(qvariant_cast<QColor>(settings->value("Alternate", DEF_ALTERNATE_COLOR)))); |
---|
[104] | 1201 | fmt_alternate.setFontWeight(QFont::Bold); |
---|
| 1202 | fmt_altlist.setForeground(QBrush(hilight)); |
---|
| 1203 | |
---|
[119] | 1204 | settings->endGroup(); |
---|
| 1205 | |
---|
[104] | 1206 | solutionText->setTextColor(color); |
---|
[100] | 1207 | } |
---|
| 1208 | |
---|
| 1209 | void MainWindow::loadLangList() |
---|
| 1210 | { |
---|
[121] | 1211 | QMap<QString, QStringList> langlist; |
---|
| 1212 | QFileInfoList langs; |
---|
| 1213 | QFileInfo lang; |
---|
| 1214 | QString name; |
---|
| 1215 | QStringList language, dirs; |
---|
[100] | 1216 | QTranslator t; |
---|
[121] | 1217 | QDir dir; |
---|
| 1218 | dir.setFilter(QDir::Files); |
---|
| 1219 | dir.setNameFilters(QStringList("tspsg_*.qm")); |
---|
| 1220 | dir.setSorting(QDir::NoSort); |
---|
| 1221 | |
---|
| 1222 | dirs << PATH_L10N << ":/l10n"; |
---|
| 1223 | foreach (QString dirname, dirs) { |
---|
| 1224 | dir.setPath(dirname); |
---|
| 1225 | if (dir.exists()) { |
---|
| 1226 | langs = dir.entryInfoList(); |
---|
| 1227 | for (int k = 0; k < langs.size(); k++) { |
---|
| 1228 | lang = langs.at(k); |
---|
| 1229 | if (lang.completeBaseName().compare("tspsg_en", Qt::CaseInsensitive) && !langlist.contains(lang.completeBaseName().mid(6)) && t.load(lang.completeBaseName(), dirname)) { |
---|
| 1230 | |
---|
| 1231 | language.clear(); |
---|
| 1232 | language.append(lang.completeBaseName().mid(6)); |
---|
| 1233 | language.append(t.translate("--------", "COUNTRY", "Please, provide an ISO 3166-1 alpha-2 country code for this translation language here (eg., UA).").toLower()); |
---|
| 1234 | language.append(t.translate("--------", "LANGNAME", "Please, provide a native name of your translation language here.")); |
---|
| 1235 | language.append(t.translate("MainWindow", "Set application language to %1", "").arg(name)); |
---|
| 1236 | |
---|
| 1237 | langlist.insert(language.at(0), language); |
---|
| 1238 | } |
---|
| 1239 | } |
---|
[100] | 1240 | } |
---|
| 1241 | } |
---|
[121] | 1242 | |
---|
| 1243 | QAction *a; |
---|
| 1244 | foreach (language, langlist) { |
---|
| 1245 | a = menuSettingsLanguage->addAction(language.at(2)); |
---|
| 1246 | a->setStatusTip(language.at(3)); |
---|
[134] | 1247 | #if QT_VERSION >= 0x040600 |
---|
[123] | 1248 | a->setIcon(QIcon::fromTheme(QString("flag-%1").arg(language.at(1)), QIcon(QString(":/images/icons/l10n/flag-%1.png").arg(language.at(1))))); |
---|
[134] | 1249 | #else |
---|
| 1250 | a->setIcon(QIcon(QString(":/images/icons/l10n/flag-%1.png").arg(language.at(1)))); |
---|
| 1251 | #endif |
---|
[121] | 1252 | a->setData(language.at(0)); |
---|
| 1253 | a->setCheckable(true); |
---|
| 1254 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 1255 | if (settings->value("Language", QLocale::system().name()).toString().startsWith(language.at(0))) |
---|
| 1256 | a->setChecked(true); |
---|
| 1257 | } |
---|
[100] | 1258 | } |
---|
| 1259 | |
---|
| 1260 | bool MainWindow::loadLanguage(const QString &lang) |
---|
| 1261 | { |
---|
| 1262 | // i18n |
---|
| 1263 | bool ad = false; |
---|
| 1264 | QString lng = lang; |
---|
| 1265 | if (lng.isEmpty()) { |
---|
[141] | 1266 | ad = settings->value("Language").toString().isEmpty(); |
---|
[100] | 1267 | lng = settings->value("Language", QLocale::system().name()).toString(); |
---|
| 1268 | } |
---|
| 1269 | static QTranslator *qtTranslator; // Qt library translator |
---|
| 1270 | if (qtTranslator) { |
---|
| 1271 | qApp->removeTranslator(qtTranslator); |
---|
| 1272 | delete qtTranslator; |
---|
| 1273 | qtTranslator = NULL; |
---|
| 1274 | } |
---|
| 1275 | static QTranslator *translator; // Application translator |
---|
| 1276 | if (translator) { |
---|
| 1277 | qApp->removeTranslator(translator); |
---|
| 1278 | delete translator; |
---|
| 1279 | translator = NULL; |
---|
| 1280 | } |
---|
| 1281 | |
---|
| 1282 | if (lng == "en") |
---|
| 1283 | return true; |
---|
| 1284 | |
---|
| 1285 | // Trying to load system Qt library translation... |
---|
| 1286 | qtTranslator = new QTranslator(this); |
---|
| 1287 | if (qtTranslator->load("qt_" + lng, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
| 1288 | qApp->installTranslator(qtTranslator); |
---|
| 1289 | else { |
---|
| 1290 | // No luck. Let's try to load a bundled one. |
---|
[121] | 1291 | if (qtTranslator->load("qt_" + lng, PATH_L10N)) { |
---|
| 1292 | // We have a translation in the localization direcotry. |
---|
[100] | 1293 | qApp->installTranslator(qtTranslator); |
---|
[121] | 1294 | } else if (qtTranslator->load("qt_" + lng, ":/l10n")) { |
---|
| 1295 | // We have a translation "built-in" into application resources. |
---|
| 1296 | qApp->installTranslator(qtTranslator); |
---|
| 1297 | } else { |
---|
| 1298 | // Qt library translation unavailable for this language. |
---|
[100] | 1299 | delete qtTranslator; |
---|
| 1300 | qtTranslator = NULL; |
---|
| 1301 | } |
---|
| 1302 | } |
---|
| 1303 | |
---|
| 1304 | // Now let's load application translation. |
---|
| 1305 | translator = new QTranslator(this); |
---|
[121] | 1306 | if (translator->load("tspsg_" + lng, PATH_L10N)) { |
---|
| 1307 | // We have a translation in the localization directory. |
---|
[100] | 1308 | qApp->installTranslator(translator); |
---|
[121] | 1309 | } else if (translator->load("tspsg_" + lng, ":/l10n")) { |
---|
| 1310 | // We have a translation "built-in" into application resources. |
---|
| 1311 | qApp->installTranslator(translator); |
---|
| 1312 | } else { |
---|
[100] | 1313 | delete translator; |
---|
| 1314 | translator = NULL; |
---|
| 1315 | if (!ad) { |
---|
| 1316 | settings->remove("Language"); |
---|
| 1317 | QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor)); |
---|
[121] | 1318 | QMessageBox::warning(isVisible() ? this : NULL, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); |
---|
[100] | 1319 | QApplication::restoreOverrideCursor(); |
---|
| 1320 | } |
---|
| 1321 | return false; |
---|
| 1322 | } |
---|
| 1323 | return true; |
---|
| 1324 | } |
---|
| 1325 | |
---|
[115] | 1326 | void MainWindow::loadStyleList() |
---|
| 1327 | { |
---|
| 1328 | menuSettingsStyle->clear(); |
---|
| 1329 | QStringList styles = QStyleFactory::keys(); |
---|
| 1330 | menuSettingsStyle->insertAction(NULL, actionSettingsStyleSystem); |
---|
| 1331 | actionSettingsStyleSystem->setChecked(!settings->contains("Style")); |
---|
| 1332 | menuSettingsStyle->addSeparator(); |
---|
| 1333 | QAction *a; |
---|
| 1334 | foreach (QString style, styles) { |
---|
| 1335 | a = menuSettingsStyle->addAction(style); |
---|
| 1336 | a->setData(false); |
---|
| 1337 | a->setStatusTip(tr("Set application style to %1").arg(style)); |
---|
| 1338 | a->setCheckable(true); |
---|
| 1339 | a->setActionGroup(groupSettingsStyleList); |
---|
[141] | 1340 | if ((style == settings->value("Stlye").toString()) |
---|
| 1341 | #ifndef Q_WS_MAEMO_5 |
---|
| 1342 | || QString(QApplication::style()->metaObject()->className()).contains(QRegExp(QString("^Q?%1(Style)?$").arg(QRegExp::escape(style)), Qt::CaseInsensitive)) |
---|
| 1343 | #endif |
---|
| 1344 | ) { |
---|
[115] | 1345 | a->setChecked(true); |
---|
| 1346 | } |
---|
| 1347 | } |
---|
| 1348 | } |
---|
| 1349 | |
---|
[118] | 1350 | void MainWindow::loadToolbarList() |
---|
| 1351 | { |
---|
| 1352 | menuSettingsToolbars->clear(); |
---|
| 1353 | #ifndef HANDHELD |
---|
| 1354 | menuSettingsToolbars->insertAction(NULL, actionSettingsToolbarsConfigure); |
---|
| 1355 | menuSettingsToolbars->addSeparator(); |
---|
| 1356 | QList<QToolBar *> list = toolBarManager->toolBars(); |
---|
| 1357 | foreach (QToolBar *t, list) { |
---|
| 1358 | menuSettingsToolbars->insertAction(NULL, t->toggleViewAction()); |
---|
| 1359 | } |
---|
| 1360 | #else // HANDHELD |
---|
| 1361 | menuSettingsToolbars->insertAction(NULL, toolBarMain->toggleViewAction()); |
---|
| 1362 | #endif // HANDHELD |
---|
| 1363 | } |
---|
| 1364 | |
---|
[100] | 1365 | bool MainWindow::maybeSave() |
---|
| 1366 | { |
---|
| 1367 | if (!isWindowModified()) |
---|
| 1368 | return true; |
---|
| 1369 | 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); |
---|
| 1370 | if (res == QMessageBox::Save) |
---|
| 1371 | return actionFileSaveTriggered(); |
---|
| 1372 | else if (res == QMessageBox::Cancel) |
---|
| 1373 | return false; |
---|
| 1374 | else |
---|
| 1375 | return true; |
---|
| 1376 | } |
---|
| 1377 | |
---|
[104] | 1378 | void MainWindow::outputMatrix(QTextCursor &cur, const TMatrix &matrix) |
---|
[100] | 1379 | { |
---|
| 1380 | int n = spinCities->value(); |
---|
[104] | 1381 | QTextTable *table = cur.insertTable(n, n, fmt_table); |
---|
| 1382 | |
---|
[100] | 1383 | for (int r = 0; r < n; r++) { |
---|
| 1384 | for (int c = 0; c < n; c++) { |
---|
[104] | 1385 | cur = table->cellAt(r, c).firstCursorPosition(); |
---|
[106] | 1386 | cur.setBlockFormat(fmt_cell); |
---|
[104] | 1387 | cur.setBlockCharFormat(fmt_default); |
---|
[100] | 1388 | if (matrix.at(r).at(c) == INFINITY) |
---|
[104] | 1389 | cur.insertText(INFSTR); |
---|
[100] | 1390 | else |
---|
[104] | 1391 | 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())); |
---|
[100] | 1392 | } |
---|
[142] | 1393 | QCoreApplication::processEvents(); |
---|
[100] | 1394 | } |
---|
[104] | 1395 | cur.movePosition(QTextCursor::End); |
---|
[100] | 1396 | } |
---|
| 1397 | |
---|
[104] | 1398 | void MainWindow::outputMatrix(QTextCursor &cur, const SStep &step) |
---|
[100] | 1399 | { |
---|
| 1400 | int n = spinCities->value(); |
---|
[104] | 1401 | QTextTable *table = cur.insertTable(n, n, fmt_table); |
---|
| 1402 | |
---|
[100] | 1403 | for (int r = 0; r < n; r++) { |
---|
| 1404 | for (int c = 0; c < n; c++) { |
---|
[104] | 1405 | cur = table->cellAt(r, c).firstCursorPosition(); |
---|
[106] | 1406 | cur.setBlockFormat(fmt_cell); |
---|
[100] | 1407 | if (step.matrix.at(r).at(c) == INFINITY) |
---|
[104] | 1408 | cur.insertText(INFSTR, fmt_default); |
---|
[100] | 1409 | else if ((r == step.candidate.nRow) && (c == step.candidate.nCol)) |
---|
[104] | 1410 | 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); |
---|
[100] | 1411 | else { |
---|
[107] | 1412 | SStep::SCandidate cand; |
---|
[100] | 1413 | cand.nRow = r; |
---|
| 1414 | cand.nCol = c; |
---|
| 1415 | if (step.alts.contains(cand)) |
---|
[104] | 1416 | 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); |
---|
[100] | 1417 | else |
---|
[104] | 1418 | 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); |
---|
[100] | 1419 | } |
---|
| 1420 | } |
---|
[142] | 1421 | QCoreApplication::processEvents(); |
---|
[100] | 1422 | } |
---|
[104] | 1423 | |
---|
| 1424 | cur.movePosition(QTextCursor::End); |
---|
[100] | 1425 | } |
---|
| 1426 | |
---|
| 1427 | void MainWindow::retranslateUi(bool all) |
---|
| 1428 | { |
---|
| 1429 | if (all) |
---|
[135] | 1430 | Ui_MainWindow::retranslateUi(this); |
---|
[100] | 1431 | |
---|
[115] | 1432 | loadStyleList(); |
---|
[118] | 1433 | loadToolbarList(); |
---|
[115] | 1434 | |
---|
[100] | 1435 | #ifndef QT_NO_PRINTER |
---|
[118] | 1436 | actionFilePrintPreview->setText(tr("P&rint Preview...")); |
---|
[100] | 1437 | #ifndef QT_NO_TOOLTIP |
---|
[118] | 1438 | actionFilePrintPreview->setToolTip(tr("Preview solution results")); |
---|
[100] | 1439 | #endif // QT_NO_TOOLTIP |
---|
| 1440 | #ifndef QT_NO_STATUSTIP |
---|
[118] | 1441 | actionFilePrintPreview->setStatusTip(tr("Preview current solution results before printing")); |
---|
[100] | 1442 | #endif // QT_NO_STATUSTIP |
---|
[142] | 1443 | #ifndef QT_NO_STATUSTIP |
---|
| 1444 | actionFileExit->setStatusTip(tr("Exit %1").arg(QCoreApplication::applicationName())); |
---|
| 1445 | #endif // QT_NO_STATUSTIP |
---|
[100] | 1446 | |
---|
[118] | 1447 | actionFilePrint->setText(tr("&Print...")); |
---|
[100] | 1448 | #ifndef QT_NO_TOOLTIP |
---|
[118] | 1449 | actionFilePrint->setToolTip(tr("Print solution")); |
---|
[100] | 1450 | #endif // QT_NO_TOOLTIP |
---|
| 1451 | #ifndef QT_NO_STATUSTIP |
---|
[118] | 1452 | actionFilePrint->setStatusTip(tr("Print current solution results")); |
---|
[100] | 1453 | #endif // QT_NO_STATUSTIP |
---|
[118] | 1454 | actionFilePrint->setShortcut(tr("Ctrl+P")); |
---|
[100] | 1455 | #endif // QT_NO_PRINTER |
---|
[119] | 1456 | |
---|
| 1457 | #ifndef HANDHELD |
---|
| 1458 | actionSettingsToolbarsConfigure->setText(tr("Configure...")); |
---|
| 1459 | #ifndef QT_NO_STATUSTIP |
---|
| 1460 | actionSettingsToolbarsConfigure->setStatusTip(tr("Customize toolbars")); |
---|
| 1461 | #endif // QT_NO_STATUSTIP |
---|
| 1462 | #endif // HANDHELD |
---|
| 1463 | |
---|
[142] | 1464 | #ifndef QT_NO_STATUSTIP |
---|
| 1465 | actionHelpReportBug->setStatusTip(tr("Report about a bug in %1").arg(QCoreApplication::applicationName())); |
---|
| 1466 | #endif // QT_NO_STATUSTIP |
---|
[139] | 1467 | if (actionHelpCheck4Updates != NULL) { |
---|
| 1468 | actionHelpCheck4Updates->setText(tr("Check for &Updates...")); |
---|
[100] | 1469 | #ifndef QT_NO_STATUSTIP |
---|
[142] | 1470 | actionHelpCheck4Updates->setStatusTip(tr("Check for %1 updates").arg(QCoreApplication::applicationName())); |
---|
[100] | 1471 | #endif // QT_NO_STATUSTIP |
---|
[139] | 1472 | } |
---|
[142] | 1473 | #ifndef QT_NO_STATUSTIP |
---|
| 1474 | actionHelpAbout->setStatusTip(tr("About %1").arg(QCoreApplication::applicationName())); |
---|
| 1475 | #endif // QT_NO_STATUSTIP |
---|
[100] | 1476 | } |
---|
| 1477 | |
---|
| 1478 | bool MainWindow::saveTask() { |
---|
| 1479 | QStringList filters(tr("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
| 1480 | filters.append(tr("All Files") + " (*)"); |
---|
| 1481 | QString file; |
---|
[128] | 1482 | if ((fileName == tr("Untitled") + ".tspt") && settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) { |
---|
[133] | 1483 | file = settings->value(OS"/LastUsed/TaskSavePath").toString(); |
---|
[128] | 1484 | if (!file.isEmpty()) |
---|
| 1485 | file.append("/"); |
---|
| 1486 | file.append(fileName); |
---|
| 1487 | } else if (fileName.endsWith(".tspt", Qt::CaseInsensitive)) |
---|
[100] | 1488 | file = fileName; |
---|
| 1489 | else |
---|
[128] | 1490 | file = QFileInfo(fileName).path() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"; |
---|
[100] | 1491 | |
---|
| 1492 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
| 1493 | file = QFileDialog::getSaveFileName(this, tr("Task Save"), file, filters.join(";;"), NULL, opts); |
---|
| 1494 | if (file.isEmpty()) |
---|
| 1495 | return false; |
---|
[128] | 1496 | else if (settings->value("SaveLastUsed", DEF_SAVE_LAST_USED).toBool()) |
---|
[133] | 1497 | settings->setValue(OS"/LastUsed/TaskSavePath", QFileInfo(file).path()); |
---|
[128] | 1498 | |
---|
[100] | 1499 | if (tspmodel->saveTask(file)) { |
---|
| 1500 | setFileName(file); |
---|
| 1501 | setWindowModified(false); |
---|
| 1502 | return true; |
---|
| 1503 | } |
---|
| 1504 | return false; |
---|
| 1505 | } |
---|
| 1506 | |
---|
| 1507 | void MainWindow::setFileName(const QString &fileName) |
---|
| 1508 | { |
---|
| 1509 | this->fileName = fileName; |
---|
[142] | 1510 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(QCoreApplication::applicationName())); |
---|
[100] | 1511 | } |
---|
| 1512 | |
---|
| 1513 | void MainWindow::setupUi() |
---|
| 1514 | { |
---|
[135] | 1515 | Ui_MainWindow::setupUi(this); |
---|
[100] | 1516 | |
---|
[121] | 1517 | // File Menu |
---|
[134] | 1518 | actionFileNew->setIcon(GET_ICON("document-new")); |
---|
| 1519 | actionFileOpen->setIcon(GET_ICON("document-open")); |
---|
| 1520 | actionFileSave->setIcon(GET_ICON("document-save")); |
---|
[135] | 1521 | #ifndef HANDHELD |
---|
[134] | 1522 | menuFileSaveAs->setIcon(GET_ICON("document-save-as")); |
---|
[135] | 1523 | #endif |
---|
[134] | 1524 | actionFileExit->setIcon(GET_ICON("application-exit")); |
---|
[121] | 1525 | // Settings Menu |
---|
[135] | 1526 | #ifndef HANDHELD |
---|
[134] | 1527 | menuSettingsLanguage->setIcon(GET_ICON("preferences-desktop-locale")); |
---|
[135] | 1528 | #if QT_VERSION >= 0x040600 |
---|
| 1529 | actionSettingsLanguageEnglish->setIcon(QIcon::fromTheme("flag-gb", QIcon(":/images/icons/l10n/flag-gb.png"))); |
---|
| 1530 | #else // QT_VERSION >= 0x040600 |
---|
| 1531 | actionSettingsLanguageEnglish->setIcon(QIcon(":/images/icons/l10n/flag-gb.png")); |
---|
| 1532 | #endif // QT_VERSION >= 0x040600 |
---|
[134] | 1533 | menuSettingsStyle->setIcon(GET_ICON("preferences-desktop-theme")); |
---|
[135] | 1534 | #endif // HANDHELD |
---|
[134] | 1535 | actionSettingsPreferences->setIcon(GET_ICON("preferences-system")); |
---|
[121] | 1536 | // Help Menu |
---|
[135] | 1537 | #ifndef HANDHELD |
---|
[134] | 1538 | actionHelpContents->setIcon(GET_ICON("help-contents")); |
---|
| 1539 | actionHelpContextual->setIcon(GET_ICON("help-contextual")); |
---|
[142] | 1540 | actionHelpOnlineSupport->setIcon(GET_ICON("applications-internet")); |
---|
| 1541 | actionHelpReportBug->setIcon(GET_ICON("tools-report-bug")); |
---|
[134] | 1542 | actionHelpAbout->setIcon(GET_ICON("help-about")); |
---|
[138] | 1543 | actionHelpAboutQt->setIcon(QIcon(":/images/icons/"ICON_SIZE"/qtlogo."ICON_FORMAT)); |
---|
[135] | 1544 | #endif |
---|
[121] | 1545 | // Buttons |
---|
[134] | 1546 | buttonRandom->setIcon(GET_ICON("roll")); |
---|
| 1547 | buttonSolve->setIcon(GET_ICON("dialog-ok")); |
---|
| 1548 | buttonSaveSolution->setIcon(GET_ICON("document-save-as")); |
---|
| 1549 | buttonBackToTask->setIcon(GET_ICON("go-previous")); |
---|
[121] | 1550 | |
---|
[134] | 1551 | // action->setIcon(GET_ICON("")); |
---|
[121] | 1552 | |
---|
[100] | 1553 | #if QT_VERSION >= 0x040600 |
---|
| 1554 | setToolButtonStyle(Qt::ToolButtonFollowStyle); |
---|
| 1555 | #endif |
---|
| 1556 | |
---|
| 1557 | #ifndef HANDHELD |
---|
| 1558 | QStatusBar *statusbar = new QStatusBar(this); |
---|
| 1559 | statusbar->setObjectName("statusbar"); |
---|
| 1560 | setStatusBar(statusbar); |
---|
| 1561 | #endif // HANDHELD |
---|
| 1562 | |
---|
[141] | 1563 | #ifdef Q_WS_WINCE_WM |
---|
[100] | 1564 | menuBar()->setDefaultAction(menuFile->menuAction()); |
---|
| 1565 | |
---|
| 1566 | QScrollArea *scrollArea = new QScrollArea(this); |
---|
| 1567 | scrollArea->setFrameShape(QFrame::NoFrame); |
---|
| 1568 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
---|
| 1569 | scrollArea->setWidgetResizable(true); |
---|
| 1570 | scrollArea->setWidget(tabWidget); |
---|
| 1571 | setCentralWidget(scrollArea); |
---|
| 1572 | #else |
---|
| 1573 | setCentralWidget(tabWidget); |
---|
[141] | 1574 | #endif // Q_WS_WINCE_WM |
---|
[100] | 1575 | |
---|
| 1576 | //! \hack HACK: A little hack for toolbar icons to have a sane size. |
---|
[141] | 1577 | #if defined(HANDHELD) && !defined(Q_WS_MAEMO_5) |
---|
[118] | 1578 | toolBarMain->setIconSize(QSize(logicalDpiX() / 4, logicalDpiY() / 4)); |
---|
[114] | 1579 | #endif // HANDHELD |
---|
[118] | 1580 | QToolButton *tb = static_cast<QToolButton *>(toolBarMain->widgetForAction(actionFileSave)); |
---|
| 1581 | if (tb != NULL) { |
---|
| 1582 | tb->setMenu(menuFileSaveAs); |
---|
| 1583 | tb->setPopupMode(QToolButton::MenuButtonPopup); |
---|
| 1584 | } |
---|
[100] | 1585 | |
---|
[119] | 1586 | // solutionText->document()->setDefaultFont(settings->value("Output/Font", QFont(DEF_FONT_FAMILY, DEF_FONT_SIZE)).value<QFont>()); |
---|
[100] | 1587 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
| 1588 | |
---|
| 1589 | #ifndef QT_NO_PRINTER |
---|
| 1590 | actionFilePrintPreview = new QAction(this); |
---|
| 1591 | actionFilePrintPreview->setObjectName("actionFilePrintPreview"); |
---|
| 1592 | actionFilePrintPreview->setEnabled(false); |
---|
[134] | 1593 | actionFilePrintPreview->setIcon(GET_ICON("document-print-preview")); |
---|
[100] | 1594 | |
---|
| 1595 | actionFilePrint = new QAction(this); |
---|
| 1596 | actionFilePrint->setObjectName("actionFilePrint"); |
---|
| 1597 | actionFilePrint->setEnabled(false); |
---|
[134] | 1598 | actionFilePrint->setIcon(GET_ICON("document-print")); |
---|
[100] | 1599 | |
---|
| 1600 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
| 1601 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
| 1602 | menuFile->insertSeparator(actionFileExit); |
---|
| 1603 | |
---|
[118] | 1604 | toolBarMain->insertAction(actionSettingsPreferences, actionFilePrint); |
---|
[100] | 1605 | #endif // QT_NO_PRINTER |
---|
[114] | 1606 | |
---|
[115] | 1607 | groupSettingsLanguageList = new QActionGroup(this); |
---|
[141] | 1608 | #ifdef Q_WS_MAEMO_5 |
---|
| 1609 | groupSettingsLanguageList->addAction(actionSettingsLanguageAutodetect); |
---|
| 1610 | #endif |
---|
[115] | 1611 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 1612 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
| 1613 | loadLangList(); |
---|
| 1614 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language", "").toString().isEmpty()); |
---|
| 1615 | |
---|
| 1616 | actionSettingsStyleSystem->setData(true); |
---|
| 1617 | groupSettingsStyleList = new QActionGroup(this); |
---|
[141] | 1618 | #ifdef Q_WS_MAEMO_5 |
---|
| 1619 | groupSettingsStyleList->addAction(actionSettingsStyleSystem); |
---|
| 1620 | #endif |
---|
[115] | 1621 | |
---|
[118] | 1622 | #ifndef HANDHELD |
---|
| 1623 | actionSettingsToolbarsConfigure = new QAction(this); |
---|
[134] | 1624 | actionSettingsToolbarsConfigure->setIcon(GET_ICON("configure-toolbars")); |
---|
[118] | 1625 | #endif // HANDHELD |
---|
| 1626 | |
---|
[139] | 1627 | if (hasUpdater()) { |
---|
| 1628 | actionHelpCheck4Updates = new QAction(this); |
---|
| 1629 | actionHelpCheck4Updates->setIcon(GET_ICON("system-software-update")); |
---|
| 1630 | actionHelpCheck4Updates->setEnabled(hasUpdater()); |
---|
| 1631 | menuHelp->insertAction(actionHelpAboutQt, actionHelpCheck4Updates); |
---|
| 1632 | menuHelp->insertSeparator(actionHelpAboutQt); |
---|
| 1633 | } else |
---|
| 1634 | actionHelpCheck4Updates = NULL; |
---|
[100] | 1635 | |
---|
| 1636 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
| 1637 | |
---|
[117] | 1638 | #ifndef HANDHELD |
---|
| 1639 | toolBarManager = new QtToolBarManager; |
---|
| 1640 | toolBarManager->setMainWindow(this); |
---|
[118] | 1641 | QString cat = toolBarMain->windowTitle(); |
---|
| 1642 | toolBarManager->addToolBar(toolBarMain, cat); |
---|
[117] | 1643 | #ifndef QT_NO_PRINTER |
---|
| 1644 | toolBarManager->addAction(actionFilePrintPreview, cat); |
---|
| 1645 | #endif // QT_NO_PRINTER |
---|
| 1646 | toolBarManager->addAction(actionHelpContents, cat); |
---|
| 1647 | toolBarManager->addAction(actionHelpContextual, cat); |
---|
| 1648 | // toolBarManager->addAction(action, cat); |
---|
[118] | 1649 | toolBarManager->restoreState(settings->value("MainWindow/Toolbars").toByteArray()); |
---|
[135] | 1650 | #else |
---|
| 1651 | toolBarMain->setVisible(settings->value("MainWindow/ToolbarVisible", true).toBool()); |
---|
[117] | 1652 | #endif // HANDHELD |
---|
[118] | 1653 | |
---|
| 1654 | retranslateUi(false); |
---|
| 1655 | |
---|
[141] | 1656 | #ifdef Q_WS_WIN32 |
---|
[118] | 1657 | // Adding some eyecandy in Vista and 7 :-) |
---|
| 1658 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
| 1659 | toggleTranclucency(true); |
---|
| 1660 | } |
---|
[141] | 1661 | #endif // Q_WS_WIN32 |
---|
[100] | 1662 | } |
---|
| 1663 | |
---|
| 1664 | void MainWindow::toggleSolutionActions(bool enable) |
---|
| 1665 | { |
---|
| 1666 | buttonSaveSolution->setEnabled(enable); |
---|
| 1667 | actionFileSaveAsSolution->setEnabled(enable); |
---|
| 1668 | solutionText->setEnabled(enable); |
---|
| 1669 | #ifndef QT_NO_PRINTER |
---|
| 1670 | actionFilePrint->setEnabled(enable); |
---|
| 1671 | actionFilePrintPreview->setEnabled(enable); |
---|
| 1672 | #endif // QT_NO_PRINTER |
---|
| 1673 | } |
---|
| 1674 | |
---|
| 1675 | void MainWindow::toggleTranclucency(bool enable) |
---|
| 1676 | { |
---|
[141] | 1677 | #ifdef Q_WS_WIN32 |
---|
[100] | 1678 | toggleStyle(labelVariant, enable); |
---|
| 1679 | toggleStyle(labelCities, enable); |
---|
| 1680 | toggleStyle(statusBar(), enable); |
---|
| 1681 | tabWidget->setDocumentMode(enable); |
---|
| 1682 | QtWin::enableBlurBehindWindow(this, enable); |
---|
| 1683 | #else |
---|
| 1684 | Q_UNUSED(enable); |
---|
[141] | 1685 | #endif // Q_WS_WIN32 |
---|
[100] | 1686 | } |
---|
[142] | 1687 | |
---|
| 1688 | void MainWindow::actionHelpOnlineSupportTriggered() |
---|
| 1689 | { |
---|
| 1690 | QDesktopServices::openUrl(QUrl("http://tspsg.info/goto/support")); |
---|
| 1691 | } |
---|
| 1692 | |
---|
| 1693 | void MainWindow::actionHelpReportBugTriggered() |
---|
| 1694 | { |
---|
| 1695 | QDesktopServices::openUrl(QUrl("http://tspsg.info/goto/bugtracker")); |
---|
| 1696 | } |
---|