source: tspsg/src/mainwindow.cpp @ 628500a5d6

0.1.4.170-beta2-bb10
Last change on this file since 628500a5d6 was 628500a5d6, checked in by Oleksii Serdiuk, 11 years ago

UI fixes:

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