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

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

Finished converting to Qt's model/view architecture

  • Property svn:keywords set to Id URL
File size: 3.0 KB
Line 
1/*
2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com>
4 *
5 *  $Id: mainwindow.cpp 15 2007-11-05 00:32:40Z 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#include "mainwindow.h"
26
27// TODO: Saving window state on close
28
29MainWindow::MainWindow(QWidget *parent)
30        : QMainWindow(parent), randMin(1), randMax(10)
31{
32        setupUi(this);
33        connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings()));
34        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve()));
35        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random()));
36        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int)));
37        // Centering MainWindow
38        // TODO: Loading of saved window state
39QRect rect = geometry();
40        rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center());
41        setGeometry(rect);
42        qsrand(QDateTime().currentDateTime().toTime_t());
43        tspmodel = new CTSPModel();
44        tspmodel->randMin = randMin;
45        tspmodel->randMax = randMax;
46        tspmodel->setNumCities(spinCities->value());
47        taskView->setModel(tspmodel);
48}
49
50void MainWindow::CitiesNumberChanged(int n)
51{
52        tspmodel->setNumCities(n);
53}
54
55void MainWindow::ChangeSettings()
56{
57SettingsDialog sd(this);
58        sd.spinRandMin->setValue(randMin);
59        sd.spinRandMax->setValue(randMax);
60        if (sd.exec() == QDialog::Accepted) {
61                randMin = sd.spinRandMin->value();
62                randMax = sd.spinRandMax->value();
63        }
64}
65
66void MainWindow::Random()
67{
68        tspmodel->randomize();
69}
70
71void MainWindow::Solve()
72{
73        // TODO: Task solving goes here :-)
74tMatrix matrix;
75double *row;
76int n = spinCities->value();
77bool ok;
78        for (int r = 0; r < n; r++) {
79                row = new double[n];
80                for (int c = 0; c < n; c++) {
81                        row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);
82                        if (!ok) {
83                                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
84                                return;
85                        }
86                }
87                matrix.append(row);
88        }
89CTSPSolver solver;
90sStep *root = solver.solve(spinCities->value(),matrix);
91        if (!root)
92                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec();
93        // tabWidget->setCurrentIndex(1);
94}
95
Note: See TracBrowser for help on using the repository browser.