source: tspsg/src/mainwindow.cpp @ 1fbf016a09

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

+ Added an ability to disable showing of the solution steps' matrices in solution output.

  • Replaced Ok and Cancel buttons in SettingsDialog? with QButtonBox. Now the buttons conform the interface guidelines of the target platform.
  • Increased the allowed maximum number of cities to 50. Solution steps' matrices aren't shown by default if the number of cities in task is more than 15.
  • For WinCE the size of the toolbar buttons is now determined based on the device DPI instead of its resolution.
  • Open and save file dialogs are now using native dialogs where supported.
  • Reworked About dialog a little bit.
  • Property mode set to 100644
File size: 26.3 KB
Line 
1/*
2 *  TSPSG: TSP Solver and Generator
3 *  Copyright (C) 2007-2009 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");
37        loadLanguage();
38        setupUi(this);
39#ifndef Q_OS_WINCE
40QStatusBar *statusbar = new QStatusBar(this);
41        statusbar->setObjectName("statusbar");
42        setStatusBar(statusbar);
43#endif // Q_OS_WINCE
44        initDocStyleSheet();
45        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
46        solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>());
47        solutionText->setWordWrapMode(QTextOption::WordWrap);
48#ifdef Q_OS_WINCE
49        // A little hack for toolbar icons to have a sane size.
50        toolBar->setIconSize(QSize(logicalDpiX() / 4, logicalDpiY() / 4));
51#endif
52#ifndef QT_NO_PRINTER
53        printer = new QPrinter(QPrinter::HighResolution);
54#endif // QT_NO_PRINTER
55        groupSettingsLanguageList = new QActionGroup(this);
56        actionSettingsLanguageEnglish->setData("en");
57        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
58        loadLangList();
59        spinCities->setMaximum(MAX_NUM_CITIES);
60        actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty());
61        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
62        connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered()));
63        connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered()));
64        connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered()));
65        connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered()));
66        connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered()));
67        connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool)));
68        connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *)));
69        connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt()));
70        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered()));
71#ifndef QT_NO_PRINTER
72        menuFile->insertAction(actionFileExit,actionFilePrintPreview);
73        menuFile->insertAction(actionFileExit,actionFilePrint);
74        menuFile->insertSeparator(actionFileExit);
75        toolBar->insertAction(actionSettingsPreferences,actionFilePrint);
76        connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered()));
77        connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered()));
78#endif // QT_NO_PRINTER
79        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked()));
80        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked()));
81        connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked()));
82        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
83        setCentralWidget(tabWidget);
84
85        if (settings->value("SavePos", false).toBool()) {
86                // Loading of saved window state
87                settings->beginGroup("MainWindow");
88#ifndef Q_OS_WINCE
89                restoreGeometry(settings->value("Geometry").toByteArray());
90#endif // Q_OS_WINCE
91                restoreState(settings->value("State").toByteArray());
92                settings->endGroup();
93#ifndef Q_OS_WINCE
94        } else {
95                // Centering main window
96QRect rect = geometry();
97                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
98                setGeometry(rect);
99#endif // Q_OS_WINCE
100        }
101
102        qsrand(QDateTime().currentDateTime().toTime_t());
103        tspmodel = new CTSPModel(this);
104        taskView->setModel(tspmodel);
105        connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int)));
106        connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &)));
107        connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged()));
108        if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1))))
109                setFileName(QCoreApplication::arguments().at(1));
110        else {
111                setFileName();
112                spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt());
113                spinCitiesValueChanged(spinCities->value());
114        }
115        setWindowModified(false);
116}
117
118/* Privates **********************************************************/
119
120void MainWindow::actionFileNewTriggered()
121{
122        if (!maybeSave())
123                return;
124        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
125        tspmodel->clear();
126        setFileName();
127        setWindowModified(false);
128        tabWidget->setCurrentIndex(0);
129        solutionText->clear();
130        toggleSolutionActions(false);
131        QApplication::restoreOverrideCursor();
132}
133
134void MainWindow::actionFileOpenTriggered()
135{
136        if (!maybeSave())
137                return;
138
139QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)");
140        filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)");
141        filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)");
142        filters.append(trUtf8("All Files") + " (*)");
143
144QString file = QFileDialog::getOpenFileName(this, trUtf8("Task Load"), QString(), filters.join(";;"));
145        if (file.isEmpty() || !QFileInfo(file).isFile())
146                return;
147        if (!tspmodel->loadTask(file))
148                return;
149        setFileName(file);
150        tabWidget->setCurrentIndex(0);
151        setWindowModified(false);
152        solutionText->clear();
153        toggleSolutionActions(false);
154}
155
156void MainWindow::actionFileSaveTriggered()
157{
158        if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive)))
159                saveTask();
160        else
161                if (tspmodel->saveTask(fileName))
162                        setWindowModified(false);
163}
164
165void MainWindow::actionFileSaveAsTaskTriggered()
166{
167        saveTask();
168}
169
170void MainWindow::actionFileSaveAsSolutionTriggered()
171{
172static QString selectedFile;
173        if (selectedFile.isEmpty()) {
174                if (fileName == trUtf8("Untitled") + ".tspt") {
175#ifndef QT_NO_PRINTER
176                        selectedFile = "solution.pdf";
177#else
178                        selectedFile = "solution.html";
179#endif // QT_NO_PRINTER
180                } else {
181#ifndef QT_NO_PRINTER
182                        selectedFile = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".pdf";
183#else
184                        selectedFile = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".html";
185#endif // QT_NO_PRINTER
186                }
187        }
188
189QStringList filters;
190#ifndef QT_NO_PRINTER
191        filters.append(trUtf8("PDF Files") + " (*.pdf)");
192#endif
193        filters.append(trUtf8("HTML Files") + " (*.html *.htm)");
194#if QT_VERSION >= 0x040500
195        filters.append(trUtf8("OpenDocument Files") + " (*.odt)");
196#endif // QT_VERSION >= 0x040500
197        filters.append(trUtf8("All Files") + " (*)");
198
199QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"));
200        if (file.isEmpty())
201                return;
202        selectedFile = file;
203        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
204#ifndef QT_NO_PRINTER
205        if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) {
206QPrinter printer(QPrinter::HighResolution);
207                printer.setOutputFormat(QPrinter::PdfFormat);
208                printer.setOutputFileName(selectedFile);
209                solutionText->document()->print(&printer);
210                QApplication::restoreOverrideCursor();
211                return;
212        }
213#endif
214#if QT_VERSION >= 0x040500
215QTextDocumentWriter dw(selectedFile);
216        if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive)))
217                dw.setFormat("plaintext");
218        dw.write(solutionText->document());
219#else
220        // Qt < 4.5 has no QTextDocumentWriter class
221QFile file(selectedFile);
222        if (!file.open(QFile::WriteOnly)) {
223                QApplication::restoreOverrideCursor();
224                return;
225        }
226QTextStream ts(&file);
227        ts.setCodec(QTextCodec::codecForName("UTF-8"));
228        ts << solutionText->document()->toHtml("UTF-8");
229        file.close();
230#endif // QT_VERSION >= 0x040500
231        QApplication::restoreOverrideCursor();
232}
233
234#ifndef QT_NO_PRINTER
235void MainWindow::actionFilePrintPreviewTriggered()
236{
237QPrintPreviewDialog ppd(printer, this);
238    connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *)));
239    ppd.exec();
240}
241
242void MainWindow::actionFilePrintTriggered()
243{
244QPrintDialog pd(printer,this);
245#if QT_VERSION >= 0x040500
246        // No such methods in Qt < 4.5
247        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
248        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
249#endif
250        if (pd.exec() != QDialog::Accepted)
251                return;
252        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
253        solutionText->document()->print(printer);
254        QApplication::restoreOverrideCursor();
255}
256#endif // QT_NO_PRINTER
257
258void MainWindow::actionSettingsPreferencesTriggered()
259{
260SettingsDialog sd(this);
261        if (sd.exec() != QDialog::Accepted)
262                return;
263        if (sd.colorChanged() || sd.fontChanged()) {
264                initDocStyleSheet();
265                if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,trUtf8("Settings Changed"),trUtf8("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) {
266                        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
267                        solutionText->clear();
268                        solutionText->setHtml(output.join(""));
269                        QApplication::restoreOverrideCursor();
270                }
271        }
272}
273
274void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked)
275{
276        if (checked) {
277                settings->remove("Language");
278                QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec();
279        } else
280                settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString());
281}
282
283void MainWindow::groupSettingsLanguageListTriggered(QAction *action)
284{
285        if (actionSettingsLanguageAutodetect->isChecked()) {
286                // We have language autodetection. It needs to be disabled to change language.
287                if (QMessageBox(QMessageBox::Question,trUtf8("Language change"),trUtf8("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) {
288                        actionSettingsLanguageAutodetect->trigger();
289                } else
290                        return;
291        }
292bool untitled = (fileName == trUtf8("Untitled") + ".tspt");
293        if (loadLanguage(action->data().toString())) {
294                settings->setValue("Language",action->data().toString());
295                retranslateUi(this);
296                if (untitled)
297                        setFileName();
298        }
299}
300
301void MainWindow::actionHelpAboutTriggered()
302{
303//! \todo TODO: Normal about window :-)
304QString title;
305#ifdef Q_OS_WINCE
306        title += QString::fromUtf8("<b>TSPSG<br>TSP Solver and Generator</b><br>");
307#else
308        title += QString::fromUtf8("<b>TSPSG: TSP Solver and Generator</b><br>");
309#endif // Q_OS_WINCE
310        title += QString::fromUtf8("Version: <b>"BUILD_VERSION"</b><br>");
311        title += QString::fromUtf8("<b>&copy; 2007-%1 Lёppa</b><br>").arg(QDate::currentDate().toString("yyyy"));
312        title += QString::fromUtf8("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sf.net/</a></b><br>");
313QString about;
314        about += QString::fromUtf8("Target OS: <b>%1</b><br>").arg(OS);
315#ifndef STATIC_BUILD
316        about += "Qt library (shared):<br>";
317        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Build time: <b>%1</b><br>").arg(QT_VERSION_STR);
318        about += QString::fromUtf8("&nbsp;&nbsp;&nbsp;&nbsp;Runtime: <b>%1</b><br>").arg(qVersion());
319#else
320        about += QString::fromUtf8("Qt library: <b>%1</b> (static)<br>").arg(QT_VERSION_STR);
321#endif // STATIC_BUILD
322        about += QString::fromUtf8("Built on <b>%1</b> at <b>%2</b><br>").arg(__DATE__).arg(__TIME__);
323        about += "<br>";
324        about += QString::fromUtf8("Id: <b>"VERSIONID"</b><br>");
325        about += QString::fromUtf8("Algorithm: <b>%1</b><br>").arg(CTSPSolver::getVersionId());
326        about += "<br>";
327        about += "TSPSG is free software: you can redistribute it and/or modify it<br>"
328                "under the terms of the GNU General Public License as published<br>"
329                "by the Free Software Foundation, either version 3 of the License,<br>"
330                "or (at your option) any later version.<br>"
331                "<br>"
332                "TSPSG is distributed in the hope that it will be useful, but<br>"
333                "WITHOUT ANY WARRANTY; without even the implied warranty of<br>"
334                "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>"
335                "GNU General Public License for more details.<br>"
336                "<br>"
337                "You should have received a copy of the GNU General Public License<br>"
338                "along with TSPSG.  If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>.";
339
340QDialog *dlg = new QDialog(this);
341QLabel *lblIcon = new QLabel(dlg),
342        *lblTitle = new QLabel(dlg);
343QTextBrowser *txtAbout = new QTextBrowser(dlg);
344QVBoxLayout *vb = new QVBoxLayout();
345QHBoxLayout *hb = new QHBoxLayout();
346QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg);
347
348        lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(logicalDpiX() * 2 / 3, Qt::SmoothTransformation));
349        lblIcon->setAlignment(Qt::AlignTop);
350        lblTitle->setText(title);
351
352        hb->addWidget(lblIcon);
353        hb->addWidget(lblTitle);
354        hb->addStretch();
355
356//      txtAbout->setTextInteractionFlags(txtAbout->textInteractionFlags() ^ Qt::TextEditable);
357        txtAbout->setWordWrapMode(QTextOption::NoWrap);
358        txtAbout->setOpenExternalLinks(true);
359        txtAbout->setHtml(about);
360        txtAbout->moveCursor(QTextCursor::Start);
361
362        vb->addLayout(hb);
363        vb->addWidget(txtAbout);
364        vb->addWidget(bb);
365
366        dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
367        dlg->setWindowTitle(trUtf8("About TSPSG"));
368        dlg->setLayout(vb);
369
370        connect(bb, SIGNAL(accepted()), dlg, SLOT(accept()));
371
372        dlg->resize(410, 300);
373        dlg->exec();
374
375        delete dlg;
376}
377
378void MainWindow::buttonBackToTaskClicked()
379{
380        tabWidget->setCurrentIndex(0);
381}
382
383void MainWindow::buttonRandomClicked()
384{
385        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
386        tspmodel->randomize();
387        QApplication::restoreOverrideCursor();
388}
389
390void MainWindow::buttonSolveClicked()
391{
392TMatrix matrix;
393QList<double> row;
394int n = spinCities->value();
395bool ok;
396        for (int r = 0; r < n; r++) {
397                row.clear();
398                for (int c = 0; c < n; c++) {
399                        row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok));
400                        if (!ok) {
401                                QMessageBox(QMessageBox::Critical,trUtf8("Data error"),trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
402                                return;
403                        }
404                }
405                matrix.append(row);
406        }
407CTSPSolver solver;
408SStep *root = solver.solve(n,matrix,this);
409        if (!root)
410                return;
411        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
412QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
413        output.clear();
414        output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>");
415        output.append("<p>" + trUtf8("Task:") + "</p>");
416        outputMatrix(matrix, output);
417        output.append("<hr>");
418        output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>");
419SStep *step = root;
420        n = 1;
421        while (n <= spinCities->value()) {
422                if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) {
423                        if (n != spinCities->value()) {
424                                output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>");
425                                if (settings->value("Output/ShowMatrix", DEF_SHOW_MATRIX).toBool() && settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() && (spinCities->value() <= settings->value("Output/ShowMatrixCitiesLimit", DEF_SHOW_MATRIX_CITY_LIMIT).toInt())) {
426                                        outputMatrix(*step, output);
427                                }
428                                output.append("<p>" + trUtf8("Selected candidate for branching: %1.").arg(trUtf8("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>");
429                                if (!step->alts.empty()) {
430SCandidate cand;
431QString alts;
432                                        foreach(cand, step->alts) {
433                                                if (!alts.isEmpty())
434                                                        alts += ", ";
435                                                alts += trUtf8("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1);
436                                        }
437                                        output.append("<p class=\"hasalts\">" + trUtf8("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>");
438                                }
439                                output.append("<p>&nbsp;</p>");
440                        }
441                }
442                if (step->prNode->prNode != NULL)
443                        step = step->prNode;
444                else if (step->plNode->prNode != NULL)
445                        step = step->plNode;
446                else
447                        break;
448        }
449        if (solver.isOptimal())
450                output.append("<p>" + trUtf8("Optimal path:") + "</p>");
451        else
452                output.append("<p>" + trUtf8("Resulting path:") + "</p>");
453        output.append("<p>&nbsp;&nbsp;" + solver.getSortedPath() + "</p>");
454        output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>");
455        if (!solver.isOptimal()) {
456                output.append("<p>&nbsp;</p>");
457                output.append("<p>" + trUtf8("<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>");
458        }
459        output.append("<p></p>");
460
461        solutionText->setHtml(output.join(""));
462        solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()));
463
464        // Scrolling to the end of text.
465        solutionText->moveCursor(QTextCursor::End);
466
467        toggleSolutionActions();
468        tabWidget->setCurrentIndex(1);
469        QApplication::restoreOverrideCursor();
470}
471
472void MainWindow::dataChanged()
473{
474        setWindowModified(true);
475}
476
477void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br)
478{
479        setWindowModified(true);
480        if (settings->value("Autosize",true).toBool()) {
481                for (int k = tl.row(); k <= br.row(); k++)
482                        taskView->resizeRowToContents(k);
483                for (int k = tl.column(); k <= br.column(); k++)
484                        taskView->resizeColumnToContents(k);
485        }
486}
487
488void MainWindow::numCitiesChanged(int nCities)
489{
490        blockSignals(true);
491        spinCities->setValue(nCities);
492        blockSignals(false);
493}
494
495#ifndef QT_NO_PRINTER
496void MainWindow::printPreview(QPrinter *printer)
497{
498        solutionText->print(printer);
499}
500#endif // QT_NO_PRINTER
501
502void MainWindow::spinCitiesValueChanged(int n)
503{
504int count = tspmodel->numCities();
505        tspmodel->setNumCities(n);
506        if ((n > count) && settings->value("Autosize",true).toBool())
507                for (int k = count; k < n; k++) {
508                        taskView->resizeColumnToContents(k);
509                        taskView->resizeRowToContents(k);
510                }
511}
512
513void MainWindow::closeEvent(QCloseEvent *ev)
514{
515        if (!maybeSave()) {
516                ev->ignore();
517                return;
518        }
519        settings->setValue("NumCities", spinCities->value());
520
521        // Saving Main Window state
522        if (settings->value("SavePos", false).toBool()) {
523                settings->beginGroup("MainWindow");
524#ifndef Q_OS_WINCE
525                settings->setValue("Geometry", saveGeometry());
526#endif // Q_OS_WINCE
527                settings->setValue("State", saveState());
528                settings->endGroup();
529        }
530
531        QMainWindow::closeEvent(ev);
532}
533
534void MainWindow::initDocStyleSheet()
535{
536QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
537QColor hilight;
538        if (color.value() < 192)
539                hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2));
540        else
541                hilight.setHsv(color.hue(),color.saturation(),color.value() / 2);
542        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;}");
543        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
544}
545
546void MainWindow::loadLangList()
547{
548QSettings langinfo(PATH_I18N"/languages.ini",QSettings::IniFormat);
549#if QT_VERSION >= 0x040500
550        // In Qt < 4.5 QSettings doesn't have method setIniCodec.
551        langinfo.setIniCodec("UTF-8");
552#endif
553QDir dir(PATH_I18N,"*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files);
554        if (!dir.exists())
555                return;
556QFileInfoList langs = dir.entryInfoList();
557        if (langs.size() <= 0)
558                return;
559QAction *a;
560        for (int k = 0; k < langs.size(); k++) {
561                QFileInfo lang = langs.at(k);
562                if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) {
563#if QT_VERSION >= 0x040500
564                        a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString());
565#else
566                        // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings
567                        // reads .ini file as ASCII and there is no way to set file encoding.
568                        a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString());
569#endif
570                        a->setData(lang.completeBaseName());
571                        a->setCheckable(true);
572                        a->setActionGroup(groupSettingsLanguageList);
573                        if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName()))
574                                a->setChecked(true);
575                }
576        }
577}
578
579bool MainWindow::loadLanguage(const QString &lang)
580{
581// i18n
582bool ad = false;
583QString lng = lang;
584        if (lng.isEmpty()) {
585                ad = settings->value("Language","").toString().isEmpty();
586                lng = settings->value("Language",QLocale::system().name()).toString();
587        }
588static QTranslator *qtTranslator; // Qt library translator
589        if (qtTranslator) {
590                qApp->removeTranslator(qtTranslator);
591                delete qtTranslator;
592                qtTranslator = NULL;
593        }
594static QTranslator *translator; // Application translator
595        if (translator) {
596                qApp->removeTranslator(translator);
597                delete translator;
598        }
599        translator = new QTranslator();
600        if ((lng.compare("en") != 0) && !lng.startsWith("en_")) {
601                // Trying to load system Qt library translation...
602                qtTranslator = new QTranslator();
603                if (qtTranslator->load("qt_" + lng,QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
604                        qApp->installTranslator(qtTranslator);
605                else {
606                        // No luck. Let's try to load a bundled one.
607                        if (qtTranslator->load("qt_" + lng,PATH_I18N))
608                                qApp->installTranslator(qtTranslator);
609                        else {
610                                // Qt library translation unavailable
611                                delete qtTranslator;
612                                qtTranslator = NULL;
613                        }
614                }
615        }
616        // Now let's load application translation.
617        if (translator->load(lng,PATH_I18N))
618                qApp->installTranslator(translator);
619        else {
620                delete translator;
621                translator = NULL;
622                if ((lng.compare("en") != 0) && !lng.startsWith("en_")) {
623                        if (!ad)
624                                QMessageBox(QMessageBox::Warning,trUtf8("Language Change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec();
625                        return false;
626                }
627        }
628        return true;
629}
630
631bool MainWindow::maybeSave()
632{
633        if (!isWindowModified())
634                return true;
635int res = QMessageBox(QMessageBox::Warning,trUtf8("Unsaved Changes"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec();
636        if (res == QMessageBox::Save)
637                return saveTask();
638        else if (res == QMessageBox::Cancel)
639                return false;
640        else
641                return true;
642}
643
644void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output)
645{
646int n = spinCities->value();
647QString line="";
648        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
649        for (int r = 0; r < n; r++) {
650                line = "<tr>";
651                for (int c = 0; c < n; c++) {
652                        if (matrix.at(r).at(c) == INFINITY)
653                                line += "<td align=\"center\">"INFSTR"</td>";
654                        else
655                                line += "<td align=\"center\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>";
656                }
657                line += "</tr>";
658                output.append(line);
659        }
660        output.append("</table>");
661}
662
663void MainWindow::outputMatrix(const SStep &step, QStringList &output)
664{
665int n = spinCities->value();
666QString line="";
667        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
668        for (int r = 0; r < n; r++) {
669                line = "<tr>";
670                for (int c = 0; c < n; c++) {
671                        if (step.matrix.at(r).at(c) == INFINITY)
672                                line += "<td align=\"center\">"INFSTR"</td>";
673                        else if ((r == step.candidate.nRow) && (c == step.candidate.nCol))
674                                line += "<td align=\"center\" class=\"selected\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
675                        else {
676SCandidate cand;
677                                cand.nRow = r;
678                                cand.nCol = c;
679                                if (step.alts.contains(cand))
680                                        line += "<td align=\"center\" class=\"alternate\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
681                                else
682                                        line += "<td align=\"center\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>";
683                        }
684                }
685                line += "</tr>";
686                output.append(line);
687        }
688        output.append("</table>");
689}
690
691bool MainWindow::saveTask() {
692QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)");
693        filters.append(trUtf8("All Files") + " (*)");
694QString file;
695        if (fileName.endsWith(".tspt", Qt::CaseInsensitive))
696                file = fileName;
697        else
698                file = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt";
699
700        file = QFileDialog::getSaveFileName(this, trUtf8("Task Save"), file, filters.join(";;"));
701        if (file.isEmpty())
702                return false;
703        if (tspmodel->saveTask(file)) {
704                setFileName(file);
705                setWindowModified(false);
706                return true;
707        }
708        return false;
709}
710
711void MainWindow::setFileName(const QString &fileName)
712{
713        this->fileName = fileName;
714        setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem")));
715}
716
717void MainWindow::toggleSolutionActions(bool enable)
718{
719        buttonSaveSolution->setEnabled(enable);
720        actionFileSaveAsSolution->setEnabled(enable);
721        solutionText->setEnabled(enable);
722        if (!enable)
723                output.clear();
724#ifndef QT_NO_PRINTER
725        actionFilePrint->setEnabled(enable);
726        actionFilePrintPreview->setEnabled(enable);
727#endif // QT_NO_PRINTER
728}
Note: See TracBrowser for help on using the repository browser.