source: tspsg/src/mainwindow.cpp @ 468fa8e7e5

appveyor
Last change on this file since 468fa8e7e5 was 468fa8e7e5, checked in by leppa, 2 years ago

Fix compilation errors when C++11 is enabled

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