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