source: tspsg/src/mainwindow.cpp @ ca9cb6dfa5

0.1.4.170-beta2-bb10
Last change on this file since ca9cb6dfa5 was 2512bd5a48, checked in by Oleksii Serdiuk, 11 years ago

Use native BlackBerry? file pickers.

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