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
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 <QtGui>
25#ifndef Q_OS_WINCE
26        #include <QPrintDialog>
27#endif // Q_OS_WINCE
28#include "mainwindow.h"
29
30MainWindow::MainWindow(QWidget *parent)
31        : QMainWindow(parent)
32{
33        settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg");
34        loadLanguage();
35        setupUi(this);
36#ifndef Q_OS_WINCE
37        printer = new QPrinter();
38#endif // Q_OS_WINCE
39        groupSettingsLanguageList = new QActionGroup(this);
40        actionSettingsLanguageEnglish->setData("en");
41        actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList);
42        loadLangList();
43        spinCities->setValue(settings->value("NumCities",5).toInt());
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()));
50#ifndef Q_OS_WINCE
51        connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered()));
52#endif // Q_OS_WINCE
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)));
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
63        if (settings->value("SavePos",false).toBool()) {
64                // Loading of saved window state
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())
69                        setWindowState(windowState() | Qt::WindowMaximized);
70                settings->endGroup();
71        } else {
72                // Centering main window
73                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
74                setGeometry(rect);
75        }
76#endif // Q_OS_WINCE
77        qsrand(QDateTime().currentDateTime().toTime_t());
78        tspmodel = new CTSPModel();
79        tspmodel->setNumCities(spinCities->value());
80        taskView->setModel(tspmodel);
81#ifdef Q_OS_WINCE
82        taskView->resizeColumnsToContents();
83        taskView->resizeRowsToContents();
84#endif // Q_OS_WINCE
85}
86
87bool MainWindow::loadLanguage(QString lang)
88{
89// i18n
90bool ad = false;
91        if (lang.isEmpty()) {
92                ad = settings->value("Language","").toString().isEmpty();
93                lang = settings->value("Language",QLocale::system().name()).toString();
94        }
95static QTranslator *qtTranslator;
96        if (qtTranslator) {
97                qApp->removeTranslator(qtTranslator);
98                delete qtTranslator;
99                qtTranslator = NULL;
100        }
101        qtTranslator = new QTranslator();
102static QTranslator *translator;
103        if (translator) {
104                qApp->removeTranslator(translator);
105                delete translator;
106        }
107        translator = new QTranslator();
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                        }
120                // Now let's load application translation.
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)
135{
136#ifdef Q_OS_WINCE
137int count = tspmodel->numCities();
138#endif // Q_OS_WINCE
139        tspmodel->setNumCities(n);
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
147}
148
149
150void MainWindow::actionFileNewTriggered()
151{
152        tspmodel->clear();
153}
154
155void MainWindow::actionSettingsPreferencesTriggered()
156{
157SettingsDialog sd(this);
158        sd.exec();
159}
160
161#ifndef Q_OS_WINCE
162void MainWindow::actionFilePrintSetupTriggered()
163{
164QPrintDialog pd(printer,this);
165        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
166        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
167        pd.exec();
168}
169#endif // Q_OS_WINCE
170
171void MainWindow::buttonRandomClicked()
172{
173        tspmodel->randomize();
174#ifdef Q_OS_WINCE
175        taskView->resizeColumnsToContents();
176        taskView->resizeRowsToContents();
177#endif // Q_OS_WINCE
178}
179
180void MainWindow::buttonSolveClicked()
181{
182        // TODO: Task solving goes here :-)
183tMatrix matrix;
184double *row;
185int n = spinCities->value();
186bool ok;
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) {
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();
193                                return;
194                        }
195                }
196                matrix.append(row);
197        }
198CTSPSolver solver;
199sStep *root = solver.solve(spinCities->value(),matrix);
200        if (!root)
201                QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec();
202        // tabWidget->setCurrentIndex(1);
203}
204
205void MainWindow::actionHelpAboutTriggered()
206{
207        // TODO: Normal about window :-)
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.";
215        QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec();
216}
217
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);
231                if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) {
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        }
260        if (loadLanguage(action->data().toString())) {
261                settings->setValue("Language",action->data().toString());
262                retranslateUi(this);
263        }
264}
265
266void MainWindow::closeEvent(QCloseEvent *event)
267{
268        settings->setValue("NumCities",spinCities->value());
269#ifndef Q_OS_WINCE
270        // Saving windows state
271        if (settings->value("SavePos",false).toBool()) {
272                settings->beginGroup("MainWindow");
273                settings->setValue("Maximized",isMaximized());
274                if (!isMaximized()) {
275                        settings->setValue("Size",size());
276                        settings->setValue("Position",pos());
277                }
278                settings->endGroup();
279        }
280#endif // Q_OS_WINCE
281        QMainWindow::closeEvent(event);
282}
Note: See TracBrowser for help on using the repository browser.