source: tspsg/src/mainwindow.cpp @ e2abfd326f

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since e2abfd326f was e2abfd326f, checked in by Oleksii Serdiuk, 14 years ago

+ Translatable About dialog.

  • Improved installation rules in .pro file.
  • Disabled SVG and JPEG support. They aren't needed at this point.
  • Fixed a bug when labels were hardly visible under Windows Vista/7 whith translucency on and a dark background behind the application.
  • Fixed a bug when Main Window area was blank if the application was compiled using Qt < 4.6.0.
  • Property mode set to 100644
File size: 35.4 KB
Line 
1/*
2 *  TSPSG: TSP Solver and Generator
3 *  Copyright (C) 2007-2010 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/*!
27 * \brief Class constructor.
28 * \param parent Main Window parent widget.
29 *
30 *  Initializes Main Window and creates its layout based on target OS.
31 *  Loads TSPSG settings and opens a task file if it was specified as a commandline parameter.
32 */
33MainWindow::MainWindow(QWidget *parent)
34        : QMainWindow(parent)
35{
36        settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TSPSG", "tspsg", this);
37
38        loadLanguage();
39        setupUi();
40
41        initDocStyleSheet();
42
43#ifndef QT_NO_PRINTER
44        printer = new QPrinter(QPrinter::HighResolution);
45#endif // QT_NO_PRINTER
46
47#ifdef Q_OS_WINCE
48        currentGeometry = QApplication::desktop()->availableGeometry(0);
49        // We need to react to SIP show/hide and resize the window appropriately
50        connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int)));
51#endif // Q_OS_WINCE
52        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
53        connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered()));
54        connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered()));
55        connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered()));
56        connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered()));
57#ifndef QT_NO_PRINTER
58        connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered()));
59        connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered()));
60#endif // QT_NO_PRINTER
61        connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered()));
62        connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool)));
63        connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *)));
64        connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt()));
65        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered()));
66
67        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked()));
68        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked()));
69        connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked()));
70        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
71
72#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
73        // Centering main window
74QRect rect = geometry();
75        rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
76        setGeometry(rect);
77        if (settings->value("SavePos", DEF_SAVEPOS).toBool()) {
78                // Loading of saved window state
79                settings->beginGroup("MainWindow");
80                restoreGeometry(settings->value("Geometry").toByteArray());
81                restoreState(settings->value("State").toByteArray());
82                settings->endGroup();
83        }
84#else
85        setWindowState(Qt::WindowMaximized);
86#endif // Q_OS_WINCE
87
88        tspmodel = new CTSPModel(this);
89        taskView->setModel(tspmodel);
90        connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int)));
91        connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &)));
92        connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged()));
93        if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1))))
94                setFileName(QCoreApplication::arguments().at(1));
95        else {
96                setFileName();
97                spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt());
98                spinCitiesValueChanged(spinCities->value());
99        }
100        setWindowModified(false);
101}
102
103MainWindow::~MainWindow()
104{
105#ifndef QT_NO_PRINTER
106        delete printer;
107#endif
108}
109
110/* Privates **********************************************************/
111
112void MainWindow::actionFileNewTriggered()
113{
114        if (!maybeSave())
115                return;
116        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
117        tspmodel->clear();
118        setFileName();
119        setWindowModified(false);
120        tabWidget->setCurrentIndex(0);
121        solutionText->clear();
122        toggleSolutionActions(false);
123        QApplication::restoreOverrideCursor();
124}
125
126void MainWindow::actionFileOpenTriggered()
127{
128        if (!maybeSave())
129                return;
130
131QStringList filters(tr("All Supported Formats") + " (*.tspt *.zkt)");
132        filters.append(tr("%1 Task Files").arg("TSPSG") + " (*.tspt)");
133        filters.append(tr("%1 Task Files").arg("ZKomModRd") + " (*.zkt)");
134        filters.append(tr("All Files") + " (*)");
135
136QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog;
137QString file = QFileDialog::getOpenFileName(this, tr("Task Load"), QString(), filters.join(";;"), NULL, opts);
138        if (file.isEmpty() || !QFileInfo(file).isFile())
139                return;
140        if (!tspmodel->loadTask(file))
141                return;
142        setFileName(file);
143        tabWidget->setCurrentIndex(0);
144        setWindowModified(false);
145        solutionText->clear();
146        toggleSolutionActions(false);
147}
148
149void MainWindow::actionFileSaveTriggered()
150{
151        qDebug() << tr("Untitled");
152        if ((fileName == tr("Untitled") + ".tspt") || (!fileName.endsWith(".tspt", Qt::CaseInsensitive)))
153                saveTask();
154        else
155                if (tspmodel->saveTask(fileName))
156                        setWindowModified(false);
157}
158
159void MainWindow::actionFileSaveAsTaskTriggered()
160{
161        saveTask();
162}
163
164void MainWindow::actionFileSaveAsSolutionTriggered()
165{
166static QString selectedFile;
167        if (selectedFile.isEmpty()) {
168                if (fileName == tr("Untitled") + ".tspt") {
169#ifndef QT_NO_PRINTER
170                        selectedFile = "solution.pdf";
171#else
172                        selectedFile = "solution.html";
173#endif // QT_NO_PRINTER
174                } else {
175#ifndef QT_NO_PRINTER
176                        selectedFile = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".pdf";
177#else
178                        selectedFile = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".html";
179#endif // QT_NO_PRINTER
180                }
181        }
182
183QStringList filters;
184#ifndef QT_NO_PRINTER
185        filters.append(tr("PDF Files") + " (*.pdf)");
186#endif
187        filters.append(tr("HTML Files") + " (*.html *.htm)");
188#if QT_VERSION >= 0x040500
189        filters.append(tr("OpenDocument Files") + " (*.odt)");
190#endif // QT_VERSION >= 0x040500
191        filters.append(tr("All Files") + " (*)");
192
193QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog;
194QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"), NULL, opts);
195        if (file.isEmpty())
196                return;
197        selectedFile = file;
198        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
199#ifndef QT_NO_PRINTER
200        if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) {
201QPrinter printer(QPrinter::HighResolution);
202                printer.setOutputFormat(QPrinter::PdfFormat);
203                printer.setOutputFileName(selectedFile);
204                solutionText->document()->print(&printer);
205                QApplication::restoreOverrideCursor();
206                return;
207        }
208#endif
209#if QT_VERSION >= 0x040500
210QTextDocumentWriter dw(selectedFile);
211        if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive)))
212                dw.setFormat("plaintext");
213        dw.write(solutionText->document());
214#else
215        // Qt < 4.5 has no QTextDocumentWriter class
216QFile file(selectedFile);
217        if (!file.open(QFile::WriteOnly)) {
218                QApplication::restoreOverrideCursor();
219                return;
220        }
221QTextStream ts(&file);
222        ts.setCodec(QTextCodec::codecForName("UTF-8"));
223        ts << solutionText->document()->toHtml("UTF-8");
224        file.close();
225#endif // QT_VERSION >= 0x040500
226        QApplication::restoreOverrideCursor();
227}
228
229#ifndef QT_NO_PRINTER
230void MainWindow::actionFilePrintPreviewTriggered()
231{
232QPrintPreviewDialog ppd(printer, this);
233        connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *)));
234        ppd.exec();
235}
236
237void MainWindow::actionFilePrintTriggered()
238{
239QPrintDialog pd(printer,this);
240#if QT_VERSION >= 0x040500
241        // No such methods in Qt < 4.5
242        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
243        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
244#endif
245        if (pd.exec() != QDialog::Accepted)
246                return;
247        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
248        solutionText->document()->print(printer);
249        QApplication::restoreOverrideCursor();
250}
251#endif // QT_NO_PRINTER
252
253void MainWindow::actionSettingsPreferencesTriggered()
254{
255SettingsDialog sd(this);
256        if (sd.exec() != QDialog::Accepted)
257                return;
258        if (sd.colorChanged() || sd.fontChanged()) {
259                initDocStyleSheet();
260                if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,tr("Settings Changed"),tr("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) {
261                        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
262                        solutionText->clear();
263                        solutionText->setHtml(output.join(""));
264                        QApplication::restoreOverrideCursor();
265                }
266        }
267        if (sd.translucencyChanged() != 0) {
268                toggleTranclucency(sd.translucencyChanged() == 1);
269        }
270}
271
272void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked)
273{
274        if (checked) {
275                settings->remove("Language");
276                QMessageBox::information(this, tr("Language change"), tr("Language will be autodetected on next application start."));
277        } else
278                settings->setValue("Language", groupSettingsLanguageList->checkedAction()->data().toString());
279}
280
281void MainWindow::groupSettingsLanguageListTriggered(QAction *action)
282{
283        if (actionSettingsLanguageAutodetect->isChecked()) {
284                // We have language autodetection. It needs to be disabled to change language.
285                if (QMessageBox(QMessageBox::Question,tr("Language change"),tr("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes) {
286                        actionSettingsLanguageAutodetect->trigger();
287                } else
288                        return;
289        }
290bool untitled = (fileName == tr("Untitled") + ".tspt");
291        if (loadLanguage(action->data().toString())) {
292                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
293                settings->setValue("Language",action->data().toString());
294                retranslateUi();
295                if (untitled)
296                        setFileName();
297#ifdef Q_OS_WIN32
298                if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool())  {
299                        toggleStyle(labelVariant, true);
300                        toggleStyle(labelCities, true);
301                }
302#endif
303                QApplication::restoreOverrideCursor();
304        }
305}
306
307void MainWindow::actionHelpAboutTriggered()
308{
309//! \todo TODO: Normal about window :-)
310QString title;
311#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
312        title += QString("<b>TSPSG<br>TSP Solver and Generator</b><br>");
313#else
314        title += QString("<b>TSPSG: TSP Solver and Generator</b><br>");
315#endif // Q_OS_WINCE || Q_OS_SYMBIAN
316        title += QString("%1: <b>%2</b><br>").arg(tr("Version"), BUILD_VERSION);
317#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
318        title += QString("<b>&copy; 2007-%1 Oleksii \"Lёppa\" Serdiuk</b><br>").arg(QDate::currentDate().toString("yyyy"));
319        title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b>");
320#else
321        title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sf.net/</a></b>");
322#endif // Q_OS_WINCE && Q_OS_SYMBIAN
323
324QString about;
325        about += QString("%1: <b>%2</b><br>").arg(tr("Target OS (ARCH)"), OS);
326#ifndef STATIC_BUILD
327        about += QString("%1 (%2):<br>").arg(tr("Qt library"), tr("shared"));
328        about += QString("&nbsp;&nbsp;&nbsp;&nbsp;%1: <b>%2</b><br>").arg(tr("Build time"), QT_VERSION_STR);
329        about += QString("&nbsp;&nbsp;&nbsp;&nbsp;%1: <b>%2</b><br>").arg(tr("Runtime"), qVersion());
330#else
331        about += QString("%1: <b>%2</b> (%3)<br>").arg(tr("Qt library"), QT_VERSION_STR, tr("static"));
332#endif // STATIC_BUILD
333        about += tr("Buid <b>%1</b>, built on <b>%2</b> at <b>%3</b>").arg(BUILD_NUMBER).arg(__DATE__).arg(__TIME__) + "<br>";
334        about += QString("%1: <b>%2</b><br>").arg(tr("Algorithm"), CTSPSolver::getVersionId());
335        about += "<br>";
336        about += tr("TSPSG is free software: you can redistribute it and/or modify it<br>"
337                "under the terms of the GNU General Public License as published<br>"
338                "by the Free Software Foundation, either version 3 of the License,<br>"
339                "or (at your option) any later version.<br>"
340                "<br>"
341                "TSPSG is distributed in the hope that it will be useful, but<br>"
342                "WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
343                "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
344                "GNU General Public License for more details.<br>"
345                "<br>"
346                "You should have received a copy of the GNU General Public License<br>"
347                "along with TSPSG.  If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>.");
348
349QDialog *dlg = new QDialog(this);
350QLabel *lblIcon = new QLabel(dlg),
351        *lblTitle = new QLabel(dlg),
352        *lblTranslated = new QLabel(dlg);
353#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
354QLabel *lblSubTitle = new QLabel(QString("<b>&copy; 2007-%1 Oleksii \"Lёppa\" Serdiuk</b>").arg(QDate::currentDate().toString("yyyy")), dlg);
355#endif // Q_OS_WINCE || Q_OS_SYMBIAN
356QTextBrowser *txtAbout = new QTextBrowser(dlg);
357QVBoxLayout *vb = new QVBoxLayout();
358QHBoxLayout *hb1 = new QHBoxLayout(),
359        *hb2 = new QHBoxLayout();
360QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg);
361
362        lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(logicalDpiX() * 2 / 3, Qt::SmoothTransformation));
363        lblIcon->setAlignment(Qt::AlignTop);
364#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
365        lblIcon->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().windowText().color().name()));
366#endif
367
368        lblTitle->setOpenExternalLinks(true);
369        lblTitle->setText(title);
370        lblTitle->setAlignment(Qt::AlignTop);
371        lblTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
372#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
373        lblTitle->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name()));
374#endif
375
376        hb1->addWidget(lblIcon);
377        hb1->addWidget(lblTitle);
378
379        txtAbout->setWordWrapMode(QTextOption::NoWrap);
380        txtAbout->setOpenExternalLinks(true);
381        txtAbout->setHtml(about);
382        txtAbout->moveCursor(QTextCursor::Start);
383#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
384        txtAbout->setStyleSheet(QString("QTextBrowser {border-color: %1; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().shadow().color().name()));
385#endif
386
387        bb->button(QDialogButtonBox::Ok)->setCursor(QCursor(Qt::PointingHandCursor));
388
389        lblTranslated->setText(QApplication::translate("--------", "TRANSLATION", "Please, provide translator credits here."));
390        if (lblTranslated->text() == "TRANSLATION")
391                lblTranslated->hide();
392        else {
393                lblTranslated->setOpenExternalLinks(true);
394#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
395                lblTranslated->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name()));
396#endif
397                hb2->addWidget(lblTranslated);
398        }
399
400        hb2->addWidget(bb);
401
402#if defined(Q_OS_WINCE)
403        vb->setMargin(3);
404#endif
405        vb->addLayout(hb1);
406#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
407        vb->addWidget(lblSubTitle);
408#endif // Q_OS_WINCE || Q_OS_SYMBIAN
409        vb->addWidget(txtAbout);
410        vb->addLayout(hb2);
411
412        dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint);
413        dlg->setWindowTitle(tr("About TSPSG"));
414        dlg->setLayout(vb);
415
416        connect(bb, SIGNAL(accepted()), dlg, SLOT(accept()));
417
418#ifdef Q_OS_WIN32
419        // Adding some eyecandy in Vista and 7 :-)
420        if (QtWin::isCompositionEnabled())  {
421                QtWin::enableBlurBehindWindow(dlg, true);
422        }
423#endif // Q_OS_WIN32
424
425        dlg->resize(450, 350);
426
427        dlg->exec();
428
429        delete dlg;
430}
431
432void MainWindow::buttonBackToTaskClicked()
433{
434        tabWidget->setCurrentIndex(0);
435}
436
437void MainWindow::buttonRandomClicked()
438{
439        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
440        tspmodel->randomize();
441        QApplication::restoreOverrideCursor();
442}
443
444void MainWindow::buttonSolveClicked()
445{
446TMatrix matrix;
447QList<double> row;
448int n = spinCities->value();
449bool ok;
450        for (int r = 0; r < n; r++) {
451                row.clear();
452                for (int c = 0; c < n; c++) {
453                        row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok));
454                        if (!ok) {
455                                QMessageBox(QMessageBox::Critical,tr("Data error"),tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
456                                return;
457                        }
458                }
459                matrix.append(row);
460        }
461CTSPSolver solver;
462SStep *root = solver.solve(n,matrix,this);
463        if (!root)
464                return;
465        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
466QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
467        output.clear();
468        output.append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>");
469        output.append("<p>" + tr("Task:") + "</p>");
470        outputMatrix(matrix, output);
471        output.append("<hr>");
472        output.append("<p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>");
473SStep *step = root;
474        n = 1;
475        while (n <= spinCities->value()) {
476                if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) {
477                        if (n != spinCities->value()) {
478                                output.append("<p>" + tr("Step #%1").arg(n++) + "</p>");
479                                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())))) {
480                                        outputMatrix(*step, output);
481                                }
482                                output.append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>");
483                                if (!step->alts.empty()) {
484SCandidate cand;
485QString alts;
486                                        foreach(cand, step->alts) {
487                                                if (!alts.isEmpty())
488                                                        alts += ", ";
489                                                alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1);
490                                        }
491                                        output.append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>");
492                                }
493                                output.append("<p>&nbsp;</p>");
494                        }
495                }
496                if (step->prNode->prNode != NULL)
497                        step = step->prNode;
498                else if (step->plNode->prNode != NULL)
499                        step = step->plNode;
500                else
501                        break;
502        }
503        if (solver.isOptimal())
504                output.append("<p>" + tr("Optimal path:") + "</p>");
505        else
506                output.append("<p>" + tr("Resulting path:") + "</p>");
507        output.append("<p>&nbsp;&nbsp;" + solver.getSortedPath() + "</p>");
508        if (isInteger(step->price))
509                output.append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>");
510        else
511                output.append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>");
512        if (!solver.isOptimal()) {
513                output.append("<p>&nbsp;</p>");
514                output.append("<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>");
515        }
516        output.append("<p></p>");
517
518        solutionText->setHtml(output.join(""));
519        solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value()));
520
521        if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) {
522                // Scrolling to the end of text.
523                solutionText->moveCursor(QTextCursor::End);
524        }
525
526        toggleSolutionActions();
527        tabWidget->setCurrentIndex(1);
528        QApplication::restoreOverrideCursor();
529}
530
531void MainWindow::dataChanged()
532{
533        setWindowModified(true);
534}
535
536void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br)
537{
538        setWindowModified(true);
539        if (settings->value("Autosize", DEF_AUTOSIZE).toBool()) {
540                for (int k = tl.row(); k <= br.row(); k++)
541                        taskView->resizeRowToContents(k);
542                for (int k = tl.column(); k <= br.column(); k++)
543                        taskView->resizeColumnToContents(k);
544        }
545}
546
547#ifdef Q_OS_WINCE
548void MainWindow::changeEvent(QEvent *ev)
549{
550        if ((ev->type() == QEvent::ActivationChange) && isActiveWindow())
551                desktopResized(0);
552
553        QWidget::changeEvent(ev);
554}
555
556void MainWindow::desktopResized(int screen)
557{
558        if ((screen != 0) || !isActiveWindow())
559                return;
560
561QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
562        if (currentGeometry != availableGeometry) {
563                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
564                /*!
565                 * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height.
566                 *  If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the
567                 *  window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile?
568                 */
569                if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) {
570                        setWindowState(windowState() | Qt::WindowMaximized);
571                } else {
572                        if (windowState() & Qt::WindowMaximized)
573                                setWindowState(windowState() ^ Qt::WindowMaximized);
574                        setGeometry(availableGeometry);
575                }
576                currentGeometry = availableGeometry;
577                QApplication::restoreOverrideCursor();
578        }
579}
580#endif // Q_OS_WINCE
581
582void MainWindow::numCitiesChanged(int nCities)
583{
584        blockSignals(true);
585        spinCities->setValue(nCities);
586        blockSignals(false);
587}
588
589#ifndef QT_NO_PRINTER
590void MainWindow::printPreview(QPrinter *printer)
591{
592        solutionText->print(printer);
593}
594#endif // QT_NO_PRINTER
595
596void MainWindow::spinCitiesValueChanged(int n)
597{
598        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
599int count = tspmodel->numCities();
600        tspmodel->setNumCities(n);
601        if ((n > count) && settings->value("Autosize", DEF_AUTOSIZE).toBool())
602                for (int k = count; k < n; k++) {
603                        taskView->resizeColumnToContents(k);
604                        taskView->resizeRowToContents(k);
605                }
606        QApplication::restoreOverrideCursor();
607}
608
609void MainWindow::closeEvent(QCloseEvent *ev)
610{
611        if (!maybeSave()) {
612                ev->ignore();
613                return;
614        }
615        if (!settings->value("SettingsReset", false).toBool()) {
616                settings->setValue("NumCities", spinCities->value());
617
618                // Saving Main Window state
619                if (settings->value("SavePos", DEF_SAVEPOS).toBool()) {
620                        settings->beginGroup("MainWindow");
621#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
622                        settings->setValue("Geometry", saveGeometry());
623#endif // Q_OS_WINCE
624                        settings->setValue("State", saveState());
625                        settings->endGroup();
626                }
627        } else {
628                settings->remove("SettingsReset");
629        }
630
631        QMainWindow::closeEvent(ev);
632}
633
634void MainWindow::initDocStyleSheet()
635{
636QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
637QColor hilight;
638        if (color.value() < 192)
639                hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2));
640        else
641                hilight.setHsv(color.hue(),color.saturation(),color.value() / 2);
642        solutionText->document()->setDefaultStyleSheet("* {color: " + color.name() +";} p {margin: 0px 10px;} table {margin: 5px;} td {padding: 1px 5px;} .hasalts {color: " + hilight.name() + ";} .selected {color: #A00000; font-weight: bold;} .alternate {color: #008000; font-weight: bold;}");
643        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
644}
645
646void MainWindow::loadLangList()
647{
648QDir dir(PATH_L10N, "tspsg_*.qm", QDir::Name | QDir::IgnoreCase, QDir::Files);
649        if (!dir.exists())
650                return;
651QFileInfoList langs = dir.entryInfoList();
652        if (langs.size() <= 0)
653                return;
654QAction *a;
655QTranslator t;
656QString name;
657        for (int k = 0; k < langs.size(); k++) {
658                QFileInfo lang = langs.at(k);
659                if (lang.completeBaseName().compare("tspsg_en", Qt::CaseInsensitive) && t.load(lang.completeBaseName(), PATH_L10N)) {
660                        name = t.translate("--------", "LANGNAME", "Please, provide a native name of your translation language here.");
661                        a = menuSettingsLanguage->addAction(name);
662                        a->setStatusTip(QString("Set application language to %1").arg(name));
663                        a->setData(lang.completeBaseName().mid(6));
664                        a->setCheckable(true);
665                        a->setActionGroup(groupSettingsLanguageList);
666                        if (settings->value("Language", QLocale::system().name()).toString().startsWith(lang.completeBaseName().mid(6)))
667                                a->setChecked(true);
668                }
669        }
670}
671
672bool MainWindow::loadLanguage(const QString &lang)
673{
674// i18n
675bool ad = false;
676QString lng = lang;
677        if (lng.isEmpty()) {
678                ad = settings->value("Language", "").toString().isEmpty();
679                lng = settings->value("Language", QLocale::system().name()).toString();
680        }
681static QTranslator *qtTranslator; // Qt library translator
682        if (qtTranslator) {
683                qApp->removeTranslator(qtTranslator);
684                delete qtTranslator;
685                qtTranslator = NULL;
686        }
687static QTranslator *translator; // Application translator
688        if (translator) {
689                qApp->removeTranslator(translator);
690                delete translator;
691                translator = NULL;
692        }
693
694        if (lng == "en")
695                return true;
696
697        // Trying to load system Qt library translation...
698        qtTranslator = new QTranslator(this);
699        if (qtTranslator->load("qt_" + lng, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
700                qApp->installTranslator(qtTranslator);
701        else {
702                // No luck. Let's try to load a bundled one.
703                if (qtTranslator->load("qt_" + lng, PATH_L10N))
704                        qApp->installTranslator(qtTranslator);
705                else {
706                        // Qt library translation unavailable
707                        delete qtTranslator;
708                        qtTranslator = NULL;
709                }
710        }
711
712        // Now let's load application translation.
713        translator = new QTranslator(this);
714        if (translator->load("tspsg_" + lng, PATH_L10N))
715                qApp->installTranslator(translator);
716        else {
717                delete translator;
718                translator = NULL;
719                if (!ad) {
720                        settings->remove("Language");
721                        if (QApplication::overrideCursor() != 0)
722                                QApplication::restoreOverrideCursor();
723                        if (isVisible())
724                                QMessageBox::warning(this, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection."));
725                        else
726                                QMessageBox::warning(NULL, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection."));
727                }
728                return false;
729        }
730        return true;
731}
732
733bool MainWindow::maybeSave()
734{
735        if (!isWindowModified())
736                return true;
737int res = QMessageBox(QMessageBox::Warning,tr("Unsaved Changes"),tr("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec();
738        if (res == QMessageBox::Save)
739                return saveTask();
740        else if (res == QMessageBox::Cancel)
741                return false;
742        else
743                return true;
744}
745
746void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output)
747{
748int n = spinCities->value();
749QString line="";
750        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
751        for (int r = 0; r < n; r++) {
752                line = "<tr>";
753                for (int c = 0; c < n; c++) {
754                        if (matrix.at(r).at(c) == INFINITY)
755                                line += "<td align=\"center\">"INFSTR"</td>";
756                        else
757                                line += isInteger(matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
758                }
759                line += "</tr>";
760                output.append(line);
761        }
762        output.append("</table>");
763}
764
765void MainWindow::outputMatrix(const SStep &step, QStringList &output)
766{
767int n = spinCities->value();
768QString line="";
769        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
770        for (int r = 0; r < n; r++) {
771                line = "<tr>";
772                for (int c = 0; c < n; c++) {
773                        if (step.matrix.at(r).at(c) == INFINITY)
774                                line += "<td align=\"center\">"INFSTR"</td>";
775                        else if ((r == step.candidate.nRow) && (c == step.candidate.nCol))
776                                line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
777                        else {
778SCandidate cand;
779                                cand.nRow = r;
780                                cand.nCol = c;
781                                if (step.alts.contains(cand))
782                                        line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
783                                else
784                                        line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt());
785                        }
786                }
787                line += "</tr>";
788                output.append(line);
789        }
790        output.append("</table>");
791}
792
793void MainWindow::retranslateUi(bool all)
794{
795        if (all)
796                Ui::MainWindow::retranslateUi(this);
797
798        actionSettingsLanguageEnglish->setStatusTip(tr("Set application language to %1").arg("English"));
799
800#ifndef QT_NO_PRINTER
801        actionFilePrintPreview->setText(QApplication::translate("MainWindow", "P&rint Preview...", 0, QApplication::UnicodeUTF8));
802#ifndef QT_NO_TOOLTIP
803        actionFilePrintPreview->setToolTip(QApplication::translate("MainWindow", "Preview solution results", 0, QApplication::UnicodeUTF8));
804#endif // QT_NO_TOOLTIP
805#ifndef QT_NO_STATUSTIP
806        actionFilePrintPreview->setStatusTip(QApplication::translate("MainWindow", "Preview current solution results before printing", 0, QApplication::UnicodeUTF8));
807#endif // QT_NO_STATUSTIP
808
809        actionFilePrint->setText(QApplication::translate("MainWindow", "&Print...", 0, QApplication::UnicodeUTF8));
810#ifndef QT_NO_TOOLTIP
811        actionFilePrint->setToolTip(QApplication::translate("MainWindow", "Print solution", 0, QApplication::UnicodeUTF8));
812#endif // QT_NO_TOOLTIP
813#ifndef QT_NO_STATUSTIP
814        actionFilePrint->setStatusTip(QApplication::translate("MainWindow", "Print current solution results", 0, QApplication::UnicodeUTF8));
815#endif // QT_NO_STATUSTIP
816        actionFilePrint->setShortcut(QApplication::translate("MainWindow", "Ctrl+P", 0, QApplication::UnicodeUTF8));
817#endif // QT_NO_PRINTER
818}
819
820bool MainWindow::saveTask() {
821QStringList filters(tr("%1 Task File").arg("TSPSG") + " (*.tspt)");
822        filters.append(tr("All Files") + " (*)");
823QString file;
824        if (fileName.endsWith(".tspt", Qt::CaseInsensitive))
825                file = fileName;
826        else
827                file = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt";
828
829QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog;
830        file = QFileDialog::getSaveFileName(this, tr("Task Save"), file, filters.join(";;"), NULL, opts);
831
832        if (file.isEmpty())
833                return false;
834        if (tspmodel->saveTask(file)) {
835                setFileName(file);
836                setWindowModified(false);
837                return true;
838        }
839        return false;
840}
841
842void MainWindow::setFileName(const QString &fileName)
843{
844        this->fileName = fileName;
845        setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(tr("Travelling Salesman Problem")));
846}
847
848void MainWindow::setupUi()
849{
850        Ui::MainWindow::setupUi(this);
851
852#if QT_VERSION >= 0x040600
853        setToolButtonStyle(Qt::ToolButtonFollowStyle);
854#endif
855
856#if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
857QStatusBar *statusbar = new QStatusBar(this);
858        statusbar->setObjectName("statusbar");
859        setStatusBar(statusbar);
860#endif // Q_OS_WINCE
861
862#ifdef Q_OS_WINCE
863        menuBar()->setDefaultAction(menuFile->menuAction());
864
865QScrollArea *scrollArea = new QScrollArea(this);
866        scrollArea->setFrameShape(QFrame::NoFrame);
867        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
868        scrollArea->setWidgetResizable(true);
869        scrollArea->setWidget(tabWidget);
870        setCentralWidget(scrollArea);
871#else
872        setCentralWidget(tabWidget);
873#endif // Q_OS_WINCE
874
875        //! \hack HACK: A little hack for toolbar icons to have a sane size.
876#ifdef Q_OS_WINCE
877        toolBar->setIconSize(QSize(logicalDpiX() / 4, logicalDpiY() / 4));
878#elif defined(Q_OS_SYMBIAN)
879        toolBar->setIconSize(QSize(logicalDpiX() / 5, logicalDpiY() / 5));
880#endif // Q_OS_WINCE
881
882        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
883        solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>());
884        solutionText->setWordWrapMode(QTextOption::WordWrap);
885
886#ifndef QT_NO_PRINTER
887        actionFilePrintPreview = new QAction(this);
888        actionFilePrintPreview->setObjectName("actionFilePrintPreview");
889        actionFilePrintPreview->setEnabled(false);
890        actionFilePrintPreview->setIcon(QIcon(":/images/icons/document_preview.png"));
891
892        actionFilePrint = new QAction(this);
893        actionFilePrint->setObjectName("actionFilePrint");
894        actionFilePrint->setEnabled(false);
895        actionFilePrint->setIcon(QIcon(":/images/icons/fileprint.png"));
896
897        menuFile->insertAction(actionFileExit,actionFilePrintPreview);
898        menuFile->insertAction(actionFileExit,actionFilePrint);
899        menuFile->insertSeparator(actionFileExit);
900
901        toolBar->insertAction(actionSettingsPreferences,actionFilePrint);
902#endif // QT_NO_PRINTER
903
904        groupSettingsLanguageList = new QActionGroup(this);
905        actionSettingsLanguageEnglish->setData("en");
906        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
907        loadLangList();
908        actionSettingsLanguageAutodetect->setChecked(settings->value("Language", "").toString().isEmpty());
909
910        spinCities->setMaximum(MAX_NUM_CITIES);
911
912        retranslateUi(false);
913
914#ifdef Q_OS_WIN32
915        // Adding some eyecandy in Vista and 7 :-)
916        if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool())  {
917                toggleTranclucency(true);
918        }
919#endif // Q_OS_WIN32
920}
921
922void MainWindow::toggleSolutionActions(bool enable)
923{
924        buttonSaveSolution->setEnabled(enable);
925        actionFileSaveAsSolution->setEnabled(enable);
926        solutionText->setEnabled(enable);
927        if (!enable)
928                output.clear();
929#ifndef QT_NO_PRINTER
930        actionFilePrint->setEnabled(enable);
931        actionFilePrintPreview->setEnabled(enable);
932#endif // QT_NO_PRINTER
933}
934
935void MainWindow::toggleTranclucency(bool enable)
936{
937#ifdef Q_OS_WIN32
938        toggleStyle(labelVariant, enable);
939        toggleStyle(labelCities, enable);
940        toggleStyle(statusBar(), enable);
941        tabWidget->setDocumentMode(enable);
942        QtWin::enableBlurBehindWindow(this, enable);
943#else
944        Q_UNUSED(enable);
945#endif // Q_OS_WIN32
946}
Note: See TracBrowser for help on using the repository browser.