source: tspsg-svn/trunk/src/mainwindow.cpp @ 22

Last change on this file since 22 was 21, checked in by laleppa, 15 years ago

+ Settings are now saved and restored.
+ Font color selection in settings.
+ Primitive about dialog.
+ Automatic resizing of cells to its contents in wince.

  • No "Save window position" checkbox in wince.
  • Property svn:keywords set to Id URL
File size: 5.5 KB
Line 
1/*
2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name>
4 *
5 *  $Id: mainwindow.cpp 21 2009-06-21 23:37:12Z laleppa $
6 *  $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/mainwindow.cpp $
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 "defines.h"
29#include "mainwindow.h"
30
31MainWindow::MainWindow(QWidget *parent)
32        : QMainWindow(parent)
33{
34        setupUi(this);
35QSettings settings(INI_FILE,QSettings::IniFormat);
36        spinCities->setValue(settings.value("NumCities",5).toInt());
37        connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings()));
38        connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout()));
39#ifndef Q_OS_WINCE
40        connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup()));
41#endif // Q_OS_WINCE
42        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve()));
43        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random()));
44        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int)));
45QRect rect = geometry();
46#ifdef Q_OS_WINCE
47        // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget.
48        rect.setSize(QApplication::desktop()->availableGeometry().size());
49        rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height()));
50        tabWidget->resize(rect.width(),rect.height() - toolBar->size().height());
51#else
52        if (settings.value("SavePos",false).toBool()) {
53                // Loading of saved window state
54                settings.beginGroup("MainWindow");
55                resize(settings.value("Size",size()).toSize());
56                move(settings.value("Position",pos()).toPoint());
57                if (settings.value("Maximized",false).toBool())
58                        setWindowState(windowState() | Qt::WindowMaximized);
59                settings.endGroup();
60        } else {
61                // Centering main window
62                rect.moveCenter(QApplication::desktop()->availableGeometry(this).center());
63                setGeometry(rect);
64        }
65#endif // Q_OS_WINCE
66        qsrand(QDateTime().currentDateTime().toTime_t());
67        tspmodel = new CTSPModel();
68        tspmodel->setNumCities(spinCities->value());
69        taskView->setModel(tspmodel);
70#ifdef Q_OS_WINCE
71        taskView->resizeColumnsToContents();
72        taskView->resizeRowsToContents();
73#endif // Q_OS_WINCE
74}
75
76void MainWindow::CitiesNumberChanged(int n)
77{
78#ifdef Q_OS_WINCE
79int count = tspmodel->numCities();
80#endif // Q_OS_WINCE
81        tspmodel->setNumCities(n);
82#ifdef Q_OS_WINCE
83        if (n > count)
84                for (int k = count; k < n; k++) {
85                        taskView->resizeColumnToContents(k);
86                        taskView->resizeRowToContents(k);
87                }
88#endif // Q_OS_WINCE
89}
90
91void MainWindow::ChangeSettings()
92{
93SettingsDialog sd(this);
94        sd.exec();
95}
96
97#ifndef Q_OS_WINCE
98void MainWindow::PrintSetup()
99{
100QPrintDialog pd;
101        pd.exec();
102}
103#endif // Q_OS_WINCE
104
105void MainWindow::Random()
106{
107        tspmodel->randomize();
108#ifdef Q_OS_WINCE
109        taskView->resizeColumnsToContents();
110        taskView->resizeRowsToContents();
111#endif // Q_OS_WINCE
112}
113
114void MainWindow::Solve()
115{
116        // TODO: Task solving goes here :-)
117tMatrix matrix;
118double *row;
119int n = spinCities->value();
120bool ok;
121        for (int r = 0; r < n; r++) {
122                row = new double[n];
123                for (int c = 0; c < n; c++) {
124                        row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);
125                        if (!ok) {
126                                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
127                                return;
128                        }
129                }
130                matrix.append(row);
131        }
132CTSPSolver solver;
133sStep *root = solver.solve(spinCities->value(),matrix);
134        if (!root)
135                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec();
136        // tabWidget->setCurrentIndex(1);
137}
138
139void MainWindow::showAbout()
140{
141        // TODO: Normal about window :-)
142QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n\
143    Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n\
144Qt library versions:\n\
145    Compile time: %2\n\
146    Runtime: %3\n\
147\n\
148TSPSG 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());
149        QMessageBox(QMessageBox::Information,"About",about).exec();
150}
151
152#ifndef Q_OS_WINCE
153void MainWindow::closeEvent(QCloseEvent *event)
154{
155        // Saving windows state
156QSettings settings(INI_FILE,QSettings::IniFormat);
157        settings.setValue("NumCities",spinCities->value());
158        if (settings.value("SavePos",false).toBool()) {
159                settings.beginGroup("MainWindow");
160                settings.setValue("Maximized",isMaximized());
161                if (!isMaximized()) {
162                        settings.setValue("Size",size());
163                        settings.setValue("Position",pos());
164                }
165                settings.endGroup();
166        }
167        QMainWindow::closeEvent(event);
168}
169#endif // Q_OS_WINCE
Note: See TracBrowser for help on using the repository browser.