source: tspsg/src/mainwindow.cpp @ 9adbc413c7

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 9adbc413c7 was 9adbc413c7, checked in by Oleksii Serdiuk, 14 years ago

Some small tweaks...

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