source: tspsg/src/mainwindow.cpp @ ca9cb6dfa5

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

Use native BlackBerry? file pickers.

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