source: tspsg/src/mainwindow.cpp @ 1241232377

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

[BB10] Created BlackBerry? 10 packaging rules and fixed build errors.

Cherry-picks from:
0edec5a2aec568f1d6d370bc5f4826f379890207
bd4553fa91c90a175e2d2709c2823b2faf5d0818
ca9cb6dfa53108993c75186063a5aad525669c25

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