source: tspsg/src/mainwindow.cpp @ ac4cb71650

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

English language should always be in language menu.

  • Property mode set to 100644
File size: 9.9 KB
RevLine 
[5515c2c2a7]1/*
[bb994a7ff8]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 <QtGui>
[5354a01311]25#ifndef Q_OS_WINCE
26        #include <QPrintDialog>
27#endif // Q_OS_WINCE
[5515c2c2a7]28#include "mainwindow.h"
29
30MainWindow::MainWindow(QWidget *parent)
[aecdf994f9]31        : QMainWindow(parent)
[5515c2c2a7]32{
[665d32434f]33        settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg");
[899d1b8e15]34        loadLanguage();
35        setupUi(this);
36#ifndef Q_OS_WINCE
37        printer = new QPrinter();
38#endif // Q_OS_WINCE
39        groupSettingsLanguageList = new QActionGroup(this);
[ac4cb71650]40        actionSettingsLanguageEnglish->setData("en");
41        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
[899d1b8e15]42        loadLangList();
[665d32434f]43        spinCities->setValue(settings->value("NumCities",5).toInt());
[899d1b8e15]44        actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty());
45        connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered()));
46        connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered()));
47        connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool)));
48        connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *)));
49        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered()));
[5354a01311]50#ifndef Q_OS_WINCE
[899d1b8e15]51        connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered()));
[5354a01311]52#endif // Q_OS_WINCE
[899d1b8e15]53        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked()));
54        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked()));
55        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int)));
[5354a01311]56QRect rect = geometry();
57#ifdef Q_OS_WINCE
58        // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget.
59        rect.setSize(QApplication::desktop()->availableGeometry().size());
60        rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height()));
61        tabWidget->resize(rect.width(),rect.height() - toolBar->size().height());
62#else
[665d32434f]63        if (settings->value("SavePos",false).toBool()) {
[aecdf994f9]64                // Loading of saved window state
[665d32434f]65                settings->beginGroup("MainWindow");
66                resize(settings->value("Size",size()).toSize());
67                move(settings->value("Position",pos()).toPoint());
68                if (settings->value("Maximized",false).toBool())
[aecdf994f9]69                        setWindowState(windowState() | Qt::WindowMaximized);
[665d32434f]70                settings->endGroup();
[aecdf994f9]71        } else {
72                // Centering main window
73                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
74                setGeometry(rect);
75        }
[5354a01311]76#endif // Q_OS_WINCE
[fc9f661ded]77        qsrand(QDateTime().currentDateTime().toTime_t());
[2bc8e278b7]78        tspmodel = new CTSPModel();
79        tspmodel->setNumCities(spinCities->value());
80        taskView->setModel(tspmodel);
[aecdf994f9]81#ifdef Q_OS_WINCE
82        taskView->resizeColumnsToContents();
83        taskView->resizeRowsToContents();
84#endif // Q_OS_WINCE
[003e4193be]85}
[052d1b9331]86
[ac4cb71650]87bool MainWindow::loadLanguage(QString lang)
[899d1b8e15]88{
89// i18n
[ac4cb71650]90bool ad = false;
91        if (lang.isEmpty()) {
92                ad = settings->value("Language","").toString().isEmpty();
93                lang = settings->value("Language",QLocale::system().name()).toString();
94        }
[899d1b8e15]95static QTranslator *qtTranslator;
96        if (qtTranslator) {
97                qApp->removeTranslator(qtTranslator);
98                delete qtTranslator;
99                qtTranslator = NULL;
100        }
101        qtTranslator = new QTranslator();
[ac4cb71650]102static QTranslator *translator;
103        if (translator) {
104                qApp->removeTranslator(translator);
105                delete translator;
106        }
107        translator = new QTranslator();
[899d1b8e15]108        if (lang.compare("en") && !lang.startsWith("en_")) {
109                // Trying to load system Qt library translation...
110                if (qtTranslator->load("qt_" + lang,QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
111                        qApp->installTranslator(qtTranslator);
112                else
113                        // No luck. Let's try to load bundled one.
114                        if (qtTranslator->load("qt_" + lang,"i18n"))
115                                qApp->installTranslator(qtTranslator);
116                        else {
117                                delete qtTranslator;
118                                qtTranslator = NULL;
119                        }
[ac4cb71650]120                // Now let's load application translation.
[899d1b8e15]121                if (translator->load(lang,"i18n"))
122                        qApp->installTranslator(translator);
123                else {
124                        if (!ad)
125                                QMessageBox(QMessageBox::Warning,trUtf8("Language change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec();
126                        delete translator;
127                        translator = NULL;
128                        return false;
129                }
130        }
131        return true;
132}
133
134void MainWindow::spinCitiesValueChanged(int n)
[003e4193be]135{
[aecdf994f9]136#ifdef Q_OS_WINCE
137int count = tspmodel->numCities();
138#endif // Q_OS_WINCE
[2bc8e278b7]139        tspmodel->setNumCities(n);
[aecdf994f9]140#ifdef Q_OS_WINCE
141        if (n > count)
142                for (int k = count; k < n; k++) {
143                        taskView->resizeColumnToContents(k);
144                        taskView->resizeRowToContents(k);
145                }
146#endif // Q_OS_WINCE
[5515c2c2a7]147}
148
[899d1b8e15]149
150void MainWindow::actionFileNewTriggered()
151{
152        tspmodel->clear();
153}
154
155void MainWindow::actionSettingsPreferencesTriggered()
[5515c2c2a7]156{
157SettingsDialog sd(this);
[aecdf994f9]158        sd.exec();
[5515c2c2a7]159}
[bb994a7ff8]160
[5354a01311]161#ifndef Q_OS_WINCE
[899d1b8e15]162void MainWindow::actionFilePrintSetupTriggered()
[5354a01311]163{
[899d1b8e15]164QPrintDialog pd(printer,this);
165        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
166        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
[5354a01311]167        pd.exec();
168}
169#endif // Q_OS_WINCE
170
[899d1b8e15]171void MainWindow::buttonRandomClicked()
[bb994a7ff8]172{
[2bc8e278b7]173        tspmodel->randomize();
[aecdf994f9]174#ifdef Q_OS_WINCE
175        taskView->resizeColumnsToContents();
176        taskView->resizeRowsToContents();
177#endif // Q_OS_WINCE
[bb994a7ff8]178}
179
[899d1b8e15]180void MainWindow::buttonSolveClicked()
[bb994a7ff8]181{
182        // TODO: Task solving goes here :-)
[e664262f7d]183tMatrix matrix;
184double *row;
[2bc8e278b7]185int n = spinCities->value();
[e664262f7d]186bool ok;
[2bc8e278b7]187        for (int r = 0; r < n; r++) {
188                row = new double[n];
189                for (int c = 0; c < n; c++) {
190                        row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);
191                        if (!ok) {
[5587b87fac]192                                QMessageBox(QMessageBox::Critical,trUtf8("Data error"),QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
[2bc8e278b7]193                                return;
[e664262f7d]194                        }
195                }
196                matrix.append(row);
197        }
198CTSPSolver solver;
199sStep *root = solver.solve(spinCities->value(),matrix);
200        if (!root)
[5587b87fac]201                QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec();
[e664262f7d]202        // tabWidget->setCurrentIndex(1);
[bb994a7ff8]203}
[aecdf994f9]204
[899d1b8e15]205void MainWindow::actionHelpAboutTriggered()
[aecdf994f9]206{
207        // TODO: Normal about window :-)
[ac4cb71650]208QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n");
209about += QString::fromUtf8("    Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy"));
210        about += "Qt library versions:\n";
211        about += QString::fromUtf8("    Compile time: %1\n").arg(QT_VERSION_STR);
212        about += QString::fromUtf8("    Runtime: %1\n").arg(qVersion());
213        about += "\n";
214        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]215        QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec();
[aecdf994f9]216}
217
[899d1b8e15]218void MainWindow::loadLangList()
219{
220QSettings langinfo("i18n/languages.ini",QSettings::IniFormat);
221        langinfo.setIniCodec("UTF-8");
222QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files);
223        if (!dir.exists())
224                return;
225QFileInfoList langs = dir.entryInfoList();
226        if (langs.size() <= 0)
227                return;
228QAction *a;
229        for (int k = 0; k < langs.size(); k++) {
230                QFileInfo lang = langs.at(k);
[ac4cb71650]231                if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) {
[899d1b8e15]232                        a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString());
233                        a->setData(lang.completeBaseName());
234                        a->setCheckable(true);
235                        a->setActionGroup(groupSettingsLanguageList);
236                        if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName()))
237                                a->setChecked(true);
238                }
239        }
240}
241
242void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked)
243{
244        if (checked) {
245                settings->remove("Language");
246                QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec();
247        } else
248                settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString());
249}
250
251void MainWindow::groupSettingsLanguageListTriggered(QAction *action)
252{
253        if (actionSettingsLanguageAutodetect->isChecked()) {
254                // We have language autodetection. It needs to be disabled to change language.
255                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) {
256                        actionSettingsLanguageAutodetect->trigger();
257                } else
258                        return;
259        }
[ac4cb71650]260        if (loadLanguage(action->data().toString())) {
[899d1b8e15]261                settings->setValue("Language",action->data().toString());
262                retranslateUi(this);
263        }
264}
265
[aecdf994f9]266void MainWindow::closeEvent(QCloseEvent *event)
267{
[665d32434f]268        settings->setValue("NumCities",spinCities->value());
[0621172ec1]269#ifndef Q_OS_WINCE
270        // Saving windows state
[665d32434f]271        if (settings->value("SavePos",false).toBool()) {
272                settings->beginGroup("MainWindow");
273                settings->setValue("Maximized",isMaximized());
[aecdf994f9]274                if (!isMaximized()) {
[665d32434f]275                        settings->setValue("Size",size());
276                        settings->setValue("Position",pos());
[aecdf994f9]277                }
[665d32434f]278                settings->endGroup();
[aecdf994f9]279        }
[0621172ec1]280#endif // Q_OS_WINCE
[aecdf994f9]281        QMainWindow::closeEvent(event);
282}
Note: See TracBrowser for help on using the repository browser.