source: tspsg/src/mainwindow.cpp @ 07e43cf61a

appveyorimgbotreadme
Last change on this file since 07e43cf61a was 07e43cf61a, checked in by Oleksii Serdiuk, 12 years ago

Improved compilation time by about 30%.

By reducing number of includes to only required minimum.

NOTE: Compilation time comparison was done by measuring compilation time
while using only one thread (i.e., "make -j1").

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