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