source: tspsg/src/mainwindow.cpp @ b26dc16dcf

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

Some refactoring for the things to come...

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