source: tspsg/src/mainwindow.cpp @ aaf2113307

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

+ Implemented File/Save? action.
+ Added "Save Solution" and "Back to Task" buttons to Solution tab for better usability.

  • Increased maximum number of cities to 20. Solving for 18-20 cities is already takes much time, so I thought it doesn't make sense to increase more.
  • Columns and rows are now resized to contents on all platforms.
  • Property mode set to 100644
File size: 20.0 KB
RevLine 
[ec54b4490b]1/*
[430bd7f7e9]2 *  TSPSG: TSP Solver and Generator
[5354a01311]3 *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
[003e4193be]4 *
[bb994a7ff8]5 *  $Id$
6 *  $URL$
[5515c2c2a7]7 *
[bb994a7ff8]8 *  This file is part of TSPSG.
[5515c2c2a7]9 *
[bb994a7ff8]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.
[5515c2c2a7]14 *
[bb994a7ff8]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.
[5515c2c2a7]19 *
[bb994a7ff8]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/>.
[5515c2c2a7]22 */
23
24#include "mainwindow.h"
25
26MainWindow::MainWindow(QWidget *parent)
[aecdf994f9]27        : QMainWindow(parent)
[5515c2c2a7]28{
[665d32434f]29        settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg");
[899d1b8e15]30        loadLanguage();
31        setupUi(this);
[430bd7f7e9]32        initDocStyleSheet();
33        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
34        solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>());
35        solutionText->setWordWrapMode(QTextOption::WordWrap);
[134a9158bd]36#ifdef Q_OS_WINCE
37        // A little hack for toolbar icons to have sane size.
38int s = qMin(QApplication::desktop()->screenGeometry().width(),QApplication::desktop()->screenGeometry().height());
39        toolBar->setIconSize(QSize(s / 10,s / 10));
40#endif
[899d1b8e15]41#ifndef Q_OS_WINCE
42        printer = new QPrinter();
43#endif // Q_OS_WINCE
44        groupSettingsLanguageList = new QActionGroup(this);
[ac4cb71650]45        actionSettingsLanguageEnglish->setData("en");
46        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
[899d1b8e15]47        loadLangList();
[aaf2113307]48        spinCities->setMaximum(MAX_NUM_CITIES);
[899d1b8e15]49        actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty());
50        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
[993d5af6f6]51        connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered()));
[aaf2113307]52        connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered()));
[430bd7f7e9]53        connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered()));
54        connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered()));
[899d1b8e15]55        connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered()));
56        connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool)));
57        connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *)));
[690f6939a7]58        connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt()));
[899d1b8e15]59        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered()));
[5354a01311]60#ifndef Q_OS_WINCE
[899d1b8e15]61        connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered()));
[5354a01311]62#endif // Q_OS_WINCE
[899d1b8e15]63        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked()));
64        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked()));
[aaf2113307]65        connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked()));
[899d1b8e15]66        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
[134a9158bd]67        setCentralWidget(tabWidget);
[aaf2113307]68QRect rect = geometry();
[430bd7f7e9]69#ifndef Q_OS_WINCE
[665d32434f]70        if (settings->value("SavePos",false).toBool()) {
[aecdf994f9]71                // Loading of saved window state
[665d32434f]72                settings->beginGroup("MainWindow");
73                resize(settings->value("Size",size()).toSize());
74                move(settings->value("Position",pos()).toPoint());
75                if (settings->value("Maximized",false).toBool())
[aecdf994f9]76                        setWindowState(windowState() | Qt::WindowMaximized);
[665d32434f]77                settings->endGroup();
[aecdf994f9]78        } else {
79                // Centering main window
80                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
81                setGeometry(rect);
82        }
[5354a01311]83#endif // Q_OS_WINCE
[fc9f661ded]84        qsrand(QDateTime().currentDateTime().toTime_t());
[2bc8e278b7]85        tspmodel = new CTSPModel();
[993d5af6f6]86        connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int)));
[690f6939a7]87        connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged()));
88        connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged()));
[aaf2113307]89        if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1))))
[b424a7e320]90                setFileName(QCoreApplication::arguments().at(1));
[aaf2113307]91        else {
[b424a7e320]92                setFileName();
[aaf2113307]93                spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt());
94        }
95        taskView->setModel(tspmodel);
96        setWindowModified(false);
[aecdf994f9]97        taskView->resizeColumnsToContents();
98        taskView->resizeRowsToContents();
[003e4193be]99}
[052d1b9331]100
[430bd7f7e9]101void MainWindow::enableSolutionActions(bool enable)
102{
[aaf2113307]103        buttonSaveSolution->setEnabled(enable);
[430bd7f7e9]104        actionFileSaveAsSolution->setEnabled(enable);
105        solutionText->setEnabled(enable);
106        if (!enable)
107                output.clear();
108}
109
[ac4cb71650]110bool MainWindow::loadLanguage(QString lang)
[899d1b8e15]111{
112// i18n
[ac4cb71650]113bool ad = false;
114        if (lang.isEmpty()) {
115                ad = settings->value("Language","").toString().isEmpty();
116                lang = settings->value("Language",QLocale::system().name()).toString();
117        }
[899d1b8e15]118static QTranslator *qtTranslator;
119        if (qtTranslator) {
120                qApp->removeTranslator(qtTranslator);
121                delete qtTranslator;
122                qtTranslator = NULL;
123        }
124        qtTranslator = new QTranslator();
[ac4cb71650]125static QTranslator *translator;
126        if (translator) {
127                qApp->removeTranslator(translator);
128                delete translator;
129        }
130        translator = new QTranslator();
[899d1b8e15]131        if (lang.compare("en") && !lang.startsWith("en_")) {
132                // Trying to load system Qt library translation...
133                if (qtTranslator->load("qt_" + lang,QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
134                        qApp->installTranslator(qtTranslator);
135                else
136                        // No luck. Let's try to load bundled one.
137                        if (qtTranslator->load("qt_" + lang,"i18n"))
138                                qApp->installTranslator(qtTranslator);
139                        else {
140                                delete qtTranslator;
141                                qtTranslator = NULL;
142                        }
[ac4cb71650]143                // Now let's load application translation.
[899d1b8e15]144                if (translator->load(lang,"i18n"))
145                        qApp->installTranslator(translator);
146                else {
147                        if (!ad)
148                                QMessageBox(QMessageBox::Warning,trUtf8("Language change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec();
149                        delete translator;
150                        translator = NULL;
151                        return false;
152                }
153        }
154        return true;
155}
156
[430bd7f7e9]157void MainWindow::initDocStyleSheet()
158{
159QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
160QColor hilight;
161        if (color.value() < 192)
162                hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2));
163        else
164                hilight.setHsv(color.hue(),color.saturation(),color.value() / 2);
165        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;}");
166        solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>());
167}
168
[b424a7e320]169void MainWindow::setFileName(QString fileName)
170{
171        this->fileName = fileName;
172        setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem")));
173}
174
[899d1b8e15]175void MainWindow::spinCitiesValueChanged(int n)
[003e4193be]176{
[aecdf994f9]177int count = tspmodel->numCities();
[2bc8e278b7]178        tspmodel->setNumCities(n);
[aecdf994f9]179        if (n > count)
180                for (int k = count; k < n; k++) {
181                        taskView->resizeColumnToContents(k);
182                        taskView->resizeRowToContents(k);
183                }
[5515c2c2a7]184}
185
[b424a7e320]186bool MainWindow::maybeSave()
187{
188        if (!isWindowModified())
189                return true;
190int 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();
191        if (res == QMessageBox::Save)
192                return saveTask();
193        else if (res == QMessageBox::Cancel)
194                return false;
195        else
196                return true;
197}
[899d1b8e15]198
199void MainWindow::actionFileNewTriggered()
200{
[b424a7e320]201        if (!maybeSave())
202                return;
[899d1b8e15]203        tspmodel->clear();
[b424a7e320]204        taskView->resizeColumnsToContents();
205        taskView->resizeRowsToContents();
206        setFileName();
[690f6939a7]207        setWindowModified(false);
[430bd7f7e9]208        tabWidget->setCurrentIndex(0);
209        solutionText->clear();
210        enableSolutionActions(false);
[899d1b8e15]211}
212
[993d5af6f6]213void MainWindow::actionFileOpenTriggered()
214{
[b424a7e320]215        if (!maybeSave())
216                return;
[993d5af6f6]217QFileDialog od(this);
218        od.setAcceptMode(QFileDialog::AcceptOpen);
219        od.setFileMode(QFileDialog::ExistingFile);
220QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)");
[b424a7e320]221        filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)");
222        filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)");
[993d5af6f6]223        filters.append(trUtf8("All Files") + " (*)");
224        od.setNameFilters(filters);
225        if (od.exec() != QDialog::Accepted)
226                return;
227QStringList files = od.selectedFiles();
[b424a7e320]228        if (files.empty())
229                return;
230        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
231        if (!tspmodel->loadTask(files.first())) {
232                QApplication::restoreOverrideCursor();
[993d5af6f6]233                return;
[b424a7e320]234        }
235        setFileName(files.first());
236        taskView->resizeColumnsToContents();
237        taskView->resizeRowsToContents();
238        tabWidget->setCurrentIndex(0);
[690f6939a7]239        setWindowModified(false);
[430bd7f7e9]240        solutionText->clear();
241        enableSolutionActions(false);
[b424a7e320]242        QApplication::restoreOverrideCursor();
[993d5af6f6]243}
244
[aaf2113307]245void MainWindow::actionFileSaveTriggered()
246{
247        if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive)))
248                saveTask();
249        else {
250                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
251                if (tspmodel->saveTask(fileName))
252                        setWindowModified(false);
253                QApplication::restoreOverrideCursor();
254        }
255}
256
[430bd7f7e9]257void MainWindow::actionFileSaveAsTaskTriggered()
[993d5af6f6]258{
[690f6939a7]259        saveTask();
260}
261
[430bd7f7e9]262void MainWindow::actionFileSaveAsSolutionTriggered()
263{
264static QString selectedFile;
265        if (selectedFile.isEmpty())
266                selectedFile = "solution.html";
267QFileDialog sd(this);
268        sd.setAcceptMode(QFileDialog::AcceptSave);
269QStringList filters(trUtf8("HTML Files") + " (*.html *.htm)");
[ec54b4490b]270#if QT_VERSION >= 0x040500
[430bd7f7e9]271        filters.append(trUtf8("OpenDocument Files") + " (*.odt)");
[ec54b4490b]272#endif // QT_VERSION >= 0x040500
[430bd7f7e9]273        filters.append(trUtf8("All Files") + " (*)");
274        sd.setNameFilters(filters);
275        sd.selectFile(selectedFile);
276        if (sd.exec() != QDialog::Accepted)
277                return;
278QStringList files = sd.selectedFiles();
279        if (files.empty())
280                return;
281        selectedFile = files.first();
[ec54b4490b]282#if QT_VERSION >= 0x040500
[430bd7f7e9]283QTextDocumentWriter dw(selectedFile);
284        if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive)))
285                dw.setFormat("plaintext");
286        dw.write(solutionText->document());
[ec54b4490b]287#else
288        // Qt < 4.5 has no QTextDocumentWriter class
289QFile file(selectedFile);
290        if (!file.open(QFile::WriteOnly))
291                return;
292QTextStream ts(&file);
293        ts.setCodec(QTextCodec::codecForName("UTF-8"));
294        ts << solutionText->document()->toHtml("UTF-8");
295#endif // QT_VERSION >= 0x040500
[430bd7f7e9]296}
297
[690f6939a7]298bool MainWindow::saveTask() {
[993d5af6f6]299QFileDialog sd(this);
300        sd.setAcceptMode(QFileDialog::AcceptSave);
[b424a7e320]301QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)");
[993d5af6f6]302        filters.append(trUtf8("All Files") + " (*)");
303        sd.setNameFilters(filters);
304        sd.setDefaultSuffix("tspt");
[b424a7e320]305        if (fileName.endsWith(".tspt",Qt::CaseInsensitive))
306                sd.selectFile(fileName);
307        else
308                sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt");
[993d5af6f6]309        if (sd.exec() != QDialog::Accepted)
[690f6939a7]310                return false;
[993d5af6f6]311QStringList files = sd.selectedFiles();
[b424a7e320]312        if (files.empty())
[690f6939a7]313                return false;
[b424a7e320]314        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
[690f6939a7]315        if (tspmodel->saveTask(files.first())) {
[b424a7e320]316                setFileName(files.first());
[690f6939a7]317                setWindowModified(false);
[b424a7e320]318                QApplication::restoreOverrideCursor();
[690f6939a7]319                return true;
[b424a7e320]320        }
321        QApplication::restoreOverrideCursor();
322        return false;
[993d5af6f6]323}
324
[899d1b8e15]325void MainWindow::actionSettingsPreferencesTriggered()
[5515c2c2a7]326{
327SettingsDialog sd(this);
[430bd7f7e9]328        if (sd.exec() != QDialog::Accepted)
329                return;
330        if (sd.colorChanged() || sd.fontChanged()) {
331                initDocStyleSheet();
332                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)) {
333                        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
334                        solutionText->clear();
335                        solutionText->setHtml(output.join(""));
336                        QApplication::restoreOverrideCursor();
337                }
338        }
[5515c2c2a7]339}
[bb994a7ff8]340
[5354a01311]341#ifndef Q_OS_WINCE
[899d1b8e15]342void MainWindow::actionFilePrintSetupTriggered()
[5354a01311]343{
[899d1b8e15]344QPrintDialog pd(printer,this);
[140912822f]345#if QT_VERSION >= 0x040500
346        // No such methods in Qt < 4.5
[899d1b8e15]347        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
348        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
[140912822f]349#endif
[5354a01311]350        pd.exec();
351}
352#endif // Q_OS_WINCE
353
[899d1b8e15]354void MainWindow::buttonRandomClicked()
[bb994a7ff8]355{
[2bc8e278b7]356        tspmodel->randomize();
[690f6939a7]357        setWindowModified(true);
[aecdf994f9]358        taskView->resizeColumnsToContents();
359        taskView->resizeRowsToContents();
[aaf2113307]360}
361
362void MainWindow::buttonBackToTaskClicked()
363{
364        tabWidget->setCurrentIndex(0);
[bb994a7ff8]365}
366
[430bd7f7e9]367void MainWindow::outputMatrix(tMatrix matrix, QStringList &output, int nRow, int nCol)
368{
369int n = spinCities->value();
370QString line="";
371        output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
372        for (int r = 0; r < n; r++) {
373                line = "<tr>";
374                for (int c = 0; c < n; c++) {
375                        if (matrix[r][c] == INFINITY)
376                                line += "<td align=\"center\">"INFSTR"</td>";
377                        else if ((r == nRow) && (c == nCol))
378                                line += "<td align=\"center\" class=\"selected\">" + QVariant(matrix[r][c]).toString() + "</td>";
379                        else
380                                line += "<td align=\"center\">" + QVariant(matrix[r][c]).toString() + "</td>";
381                }
382                line += "</tr>";
383                output.append(line);
384        }
385        output.append("</table>");
386}
387
[899d1b8e15]388void MainWindow::buttonSolveClicked()
[bb994a7ff8]389{
[e664262f7d]390tMatrix matrix;
[430bd7f7e9]391QList<double> row;
[2bc8e278b7]392int n = spinCities->value();
[e664262f7d]393bool ok;
[2bc8e278b7]394        for (int r = 0; r < n; r++) {
[430bd7f7e9]395                row.clear();
[2bc8e278b7]396                for (int c = 0; c < n; c++) {
[430bd7f7e9]397                        row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok));
[2bc8e278b7]398                        if (!ok) {
[b424a7e320]399                                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();
[2bc8e278b7]400                                return;
[e664262f7d]401                        }
402                }
403                matrix.append(row);
404        }
405CTSPSolver solver;
[aaf2113307]406sStep *root = solver.solve(n,matrix,this);
[e664262f7d]407        if (!root)
[430bd7f7e9]408                return;
409        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
410QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>();
411        output.clear();
412        output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>");
413        output.append("<p>" + trUtf8("Task:") + "</p>");
414        outputMatrix(matrix,output);
415        output.append("<hr>");
416        output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>");
417sStep *step = root;
418        n = 1;
419QString path = "";
420        while (n <= spinCities->value()) {
421                if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) {
422                        if (n != spinCities->value()) {
423                                output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>");
424                                outputMatrix(step->matrix,output,step->candidate.nRow,step->candidate.nCol);
425                                if (step->alts)
426                                        output.append("<p class=\"hasalts\">" + trUtf8("This step has alternate candidates for branching.") + "</p>");
427                                output.append("<p>&nbsp;</p>");
428                        }
429                        path += QString(" (%1,%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1);
430                }
431                if (step->prNode->prNode != NULL)
432                        step = step->prNode;
433                else if (step->plNode->prNode != NULL)
434                        step = step->plNode;
435                else
436                        break;
437        }
438        output.append("<p>" + trUtf8("Optimal path:") + "</p>");
439        output.append("<p>&nbsp;&nbsp;" + path + "</p>");
440        output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>");
441        solutionText->setHtml(output.join(""));
442        solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()));
443        enableSolutionActions();
444        tabWidget->setCurrentIndex(1);
445        QApplication::restoreOverrideCursor();
[bb994a7ff8]446}
[aecdf994f9]447
[899d1b8e15]448void MainWindow::actionHelpAboutTriggered()
[aecdf994f9]449{
450        // TODO: Normal about window :-)
[ac4cb71650]451QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n");
[ec54b4490b]452        about += QString::fromUtf8("    Version: "BUILD_VERSION"\n");
[430bd7f7e9]453        about += QString::fromUtf8("    Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy"));
[690f6939a7]454        about += QString::fromUtf8("Target OS: %1\n").arg(OS);
455        about += "Qt library:\n";
[ac4cb71650]456        about += QString::fromUtf8("    Compile time: %1\n").arg(QT_VERSION_STR);
457        about += QString::fromUtf8("    Runtime: %1\n").arg(qVersion());
[b5c9bcb585]458        about += QString::fromUtf8("Built on %1 at %2\n").arg(__DATE__).arg(__TIME__);
[ac4cb71650]459        about += "\n";
460        about += "TSPSG is licensed under the terms of the GNU General Public License. You should have received a copy of the GNU General Public License along with TSPSG.";
[5587b87fac]461        QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec();
[aecdf994f9]462}
463
[899d1b8e15]464void MainWindow::loadLangList()
465{
466QSettings langinfo("i18n/languages.ini",QSettings::IniFormat);
[140912822f]467#if QT_VERSION >= 0x040500
468        // In Qt < 4.5 QSettings doesn't have method setIniCodec.
[899d1b8e15]469        langinfo.setIniCodec("UTF-8");
[140912822f]470#endif
[899d1b8e15]471QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files);
472        if (!dir.exists())
473                return;
474QFileInfoList langs = dir.entryInfoList();
475        if (langs.size() <= 0)
476                return;
477QAction *a;
478        for (int k = 0; k < langs.size(); k++) {
479                QFileInfo lang = langs.at(k);
[ac4cb71650]480                if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) {
[140912822f]481#if QT_VERSION >= 0x040500
[899d1b8e15]482                        a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString());
[140912822f]483#else
484                        // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings
485                        // reads .ini file as ASCII and there is no way to set file encoding.
486                        a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString());
487#endif
[899d1b8e15]488                        a->setData(lang.completeBaseName());
489                        a->setCheckable(true);
490                        a->setActionGroup(groupSettingsLanguageList);
491                        if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName()))
492                                a->setChecked(true);
493                }
494        }
495}
496
497void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked)
498{
499        if (checked) {
500                settings->remove("Language");
501                QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec();
502        } else
503                settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString());
504}
505
506void MainWindow::groupSettingsLanguageListTriggered(QAction *action)
507{
508        if (actionSettingsLanguageAutodetect->isChecked()) {
509                // We have language autodetection. It needs to be disabled to change language.
510                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) {
511                        actionSettingsLanguageAutodetect->trigger();
512                } else
513                        return;
514        }
[b424a7e320]515bool untitled = (fileName == trUtf8("Untitled") + ".tspt");
[ac4cb71650]516        if (loadLanguage(action->data().toString())) {
[899d1b8e15]517                settings->setValue("Language",action->data().toString());
518                retranslateUi(this);
[b424a7e320]519                if (untitled)
520                        setFileName();
[899d1b8e15]521        }
522}
523
[aecdf994f9]524void MainWindow::closeEvent(QCloseEvent *event)
525{
[b424a7e320]526        if (!maybeSave()) {
527                event->ignore();
528                return;
[690f6939a7]529        }
[665d32434f]530        settings->setValue("NumCities",spinCities->value());
[0621172ec1]531#ifndef Q_OS_WINCE
532        // Saving windows state
[665d32434f]533        if (settings->value("SavePos",false).toBool()) {
534                settings->beginGroup("MainWindow");
535                settings->setValue("Maximized",isMaximized());
[aecdf994f9]536                if (!isMaximized()) {
[665d32434f]537                        settings->setValue("Size",size());
538                        settings->setValue("Position",pos());
[aecdf994f9]539                }
[665d32434f]540                settings->endGroup();
[aecdf994f9]541        }
[0621172ec1]542#endif // Q_OS_WINCE
[aecdf994f9]543        QMainWindow::closeEvent(event);
544}
[993d5af6f6]545
[690f6939a7]546void MainWindow::dataChanged()
547{
548        setWindowModified(true);
549}
550
[993d5af6f6]551void MainWindow::numCitiesChanged(int nCities)
552{
[aaf2113307]553        blockSignals(true);
[993d5af6f6]554        spinCities->setValue(nCities);
[aaf2113307]555        blockSignals(false);
[993d5af6f6]556}
Note: See TracBrowser for help on using the repository browser.