source: tspsg-svn/trunk/src/mainwindow.cpp @ 158

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