source: tspsg/src/mainwindow.cpp @ 5f49b20d9d

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

Closes #16: Default text color in Symbian is the same as background color.

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