source: tspsg/src/mainwindow.cpp @ 5587b87fac

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

+ Language is automatically loaded at application startup (based on user's locale name).
+ Full Ukrainian and Russian translation.

  • English is now default language.
  • Property mode set to 100644
File size: 5.4 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{
33        setupUi(this);
[665d32434f]34        settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg");
35        spinCities->setValue(settings->value("NumCities",5).toInt());
[5515c2c2a7]36        connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings()));
[aecdf994f9]37        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
[5354a01311]38#ifndef Q_OS_WINCE
39        connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup()));
40#endif // Q_OS_WINCE
[bb994a7ff8]41        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve()));
42        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random()));
[003e4193be]43        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int)));
[5354a01311]44QRect rect = geometry();
45#ifdef Q_OS_WINCE
46        // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget.
47        rect.setSize(QApplication::desktop()->availableGeometry().size());
48        rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height()));
49        tabWidget->resize(rect.width(),rect.height() - toolBar->size().height());
50#else
[665d32434f]51        if (settings->value("SavePos",false).toBool()) {
[aecdf994f9]52                // Loading of saved window state
[665d32434f]53                settings->beginGroup("MainWindow");
54                resize(settings->value("Size",size()).toSize());
55                move(settings->value("Position",pos()).toPoint());
56                if (settings->value("Maximized",false).toBool())
[aecdf994f9]57                        setWindowState(windowState() | Qt::WindowMaximized);
[665d32434f]58                settings->endGroup();
[aecdf994f9]59        } else {
60                // Centering main window
61                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
62                setGeometry(rect);
63        }
[5354a01311]64#endif // Q_OS_WINCE
[fc9f661ded]65        qsrand(QDateTime().currentDateTime().toTime_t());
[2bc8e278b7]66        tspmodel = new CTSPModel();
67        tspmodel->setNumCities(spinCities->value());
68        taskView->setModel(tspmodel);
[aecdf994f9]69#ifdef Q_OS_WINCE
70        taskView->resizeColumnsToContents();
71        taskView->resizeRowsToContents();
72#endif // Q_OS_WINCE
[003e4193be]73}
[052d1b9331]74
[003e4193be]75void MainWindow::CitiesNumberChanged(int n)
76{
[aecdf994f9]77#ifdef Q_OS_WINCE
78int count = tspmodel->numCities();
79#endif // Q_OS_WINCE
[2bc8e278b7]80        tspmodel->setNumCities(n);
[aecdf994f9]81#ifdef Q_OS_WINCE
82        if (n > count)
83                for (int k = count; k < n; k++) {
84                        taskView->resizeColumnToContents(k);
85                        taskView->resizeRowToContents(k);
86                }
87#endif // Q_OS_WINCE
[5515c2c2a7]88}
89
90void MainWindow::ChangeSettings()
91{
92SettingsDialog sd(this);
[aecdf994f9]93        sd.exec();
[5515c2c2a7]94}
[bb994a7ff8]95
[5354a01311]96#ifndef Q_OS_WINCE
97void MainWindow::PrintSetup()
98{
99QPrintDialog pd;
100        pd.exec();
101}
102#endif // Q_OS_WINCE
103
[bb994a7ff8]104void MainWindow::Random()
105{
[2bc8e278b7]106        tspmodel->randomize();
[aecdf994f9]107#ifdef Q_OS_WINCE
108        taskView->resizeColumnsToContents();
109        taskView->resizeRowsToContents();
110#endif // Q_OS_WINCE
[bb994a7ff8]111}
112
113void MainWindow::Solve()
114{
115        // TODO: Task solving goes here :-)
[e664262f7d]116tMatrix matrix;
117double *row;
[2bc8e278b7]118int n = spinCities->value();
[e664262f7d]119bool ok;
[2bc8e278b7]120        for (int r = 0; r < n; r++) {
121                row = new double[n];
122                for (int c = 0; c < n; c++) {
123                        row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);
124                        if (!ok) {
[5587b87fac]125                                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]126                                return;
[e664262f7d]127                        }
128                }
129                matrix.append(row);
130        }
131CTSPSolver solver;
132sStep *root = solver.solve(spinCities->value(),matrix);
133        if (!root)
[5587b87fac]134                QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec();
[e664262f7d]135        // tabWidget->setCurrentIndex(1);
[bb994a7ff8]136}
[aecdf994f9]137
138void MainWindow::showAbout()
139{
140        // TODO: Normal about window :-)
141QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n\
142    Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n\
143Qt library versions:\n\
144    Compile time: %2\n\
145    Runtime: %3\n\
146\n\
147TSPSG 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.").arg(QDate().toString("%Y"),QT_VERSION_STR,qVersion());
[5587b87fac]148        QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec();
[aecdf994f9]149}
150
151void MainWindow::closeEvent(QCloseEvent *event)
152{
[665d32434f]153        settings->setValue("NumCities",spinCities->value());
[0621172ec1]154#ifndef Q_OS_WINCE
155        // Saving windows state
[665d32434f]156        if (settings->value("SavePos",false).toBool()) {
157                settings->beginGroup("MainWindow");
158                settings->setValue("Maximized",isMaximized());
[aecdf994f9]159                if (!isMaximized()) {
[665d32434f]160                        settings->setValue("Size",size());
161                        settings->setValue("Position",pos());
[aecdf994f9]162                }
[665d32434f]163                settings->endGroup();
[aecdf994f9]164        }
[0621172ec1]165#endif // Q_OS_WINCE
[aecdf994f9]166        QMainWindow::closeEvent(event);
167}
Note: See TracBrowser for help on using the repository browser.