source: tspsg/src/mainwindow.cpp @ c039ebbe82

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

Use "/accounts/1000/shared" folder as default in open/save dialogs.

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