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
RevLine 
[1]1/*
[6]2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com>
[1]4 *
[6]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 $
[4]7 *
[6]8 *  This file is part of TSPSG.
[1]9 *
[6]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.
[1]14 *
[6]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.
[1]19 *
[6]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/>.
[1]22 */
23
24#include <QtGui>
25#include "mainwindow.h"
26
[12]27// TODO: Saving window state on close
[2]28
[1]29MainWindow::MainWindow(QWidget *parent)
[6]30        : QMainWindow(parent), randMin(1), randMax(10)
[1]31{
32        setupUi(this);
33        connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings()));
[6]34        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve()));
35        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random()));
[4]36        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int)));
[2]37        // Centering MainWindow
38        // TODO: Loading of saved window state
39QRect rect = geometry();
[15]40        rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center());
[2]41        setGeometry(rect);
[11]42        qsrand(QDateTime().currentDateTime().toTime_t());
[15]43        tspmodel = new CTSPModel();
44        tspmodel->randMin = randMin;
45        tspmodel->randMax = randMax;
46        tspmodel->setNumCities(spinCities->value());
47        taskView->setModel(tspmodel);
[6]48}
49
[4]50void MainWindow::CitiesNumberChanged(int n)
51{
[15]52        tspmodel->setNumCities(n);
[1]53}
54
55void MainWindow::ChangeSettings()
56{
57SettingsDialog sd(this);
[6]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        }
[1]64}
[6]65
66void MainWindow::Random()
67{
[15]68        tspmodel->randomize();
[6]69}
70
71void MainWindow::Solve()
72{
[13]73        // TODO: Task solving goes here :-)
74tMatrix matrix;
75double *row;
[15]76int n = spinCities->value();
[13]77bool ok;
[15]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;
[13]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();
[12]93        // tabWidget->setCurrentIndex(1);
[6]94}
95
Note: See TracBrowser for help on using the repository browser.