source: tspsg/src/mainwindow.cpp @ 07e43cf61a

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

Improved compilation time by about 30%.

By reducing number of includes to only required minimum.

NOTE: Compilation time comparison was done by measuring compilation time
while using only one thread (i.e., "make -j1").

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