source: tspsg/src/mainwindow.cpp @ 20e8115cee

0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since 20e8115cee was 20e8115cee, checked in by Oleksii Serdiuk, 13 years ago

+ Added the ability to setup page-related options for printing (closed ticket #5).

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