source: tspsg/src/mainwindow.cpp @ f44855d99e

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

Fixed wrong signal sender in mainwindow.ce.ui.

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