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 | |
---|
29 | MainWindow::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 |
---|
39 | QRect 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 | |
---|
50 | void MainWindow::CitiesNumberChanged(int n) |
---|
51 | { |
---|
52 | tspmodel->setNumCities(n); |
---|
53 | } |
---|
54 | |
---|
55 | void MainWindow::ChangeSettings() |
---|
56 | { |
---|
57 | SettingsDialog 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 | |
---|
66 | void MainWindow::Random() |
---|
67 | { |
---|
68 | tspmodel->randomize(); |
---|
69 | } |
---|
70 | |
---|
71 | void MainWindow::Solve() |
---|
72 | { |
---|
73 | // TODO: Task solving goes here :-) |
---|
74 | tMatrix matrix; |
---|
75 | double *row; |
---|
76 | int n = spinCities->value(); |
---|
77 | bool 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 | } |
---|
89 | CTSPSolver solver; |
---|
90 | sStep *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 | |
---|