source: tspsg/src/mainwindow.cpp @ b26dc16dcf

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

Some refactoring for the things to come...

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