1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: mainwindow.cpp 17 2009-06-15 15:10:25Z 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 "mainwindow.h" |
---|
29 | |
---|
30 | // TODO: Saving window state on close |
---|
31 | |
---|
32 | MainWindow::MainWindow(QWidget *parent) |
---|
33 | : QMainWindow(parent), randMin(1), randMax(10) |
---|
34 | { |
---|
35 | setupUi(this); |
---|
36 | connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); |
---|
37 | #ifndef Q_OS_WINCE |
---|
38 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); |
---|
39 | #endif // Q_OS_WINCE |
---|
40 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); |
---|
41 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); |
---|
42 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); |
---|
43 | QRect rect = geometry(); |
---|
44 | #ifdef Q_OS_WINCE |
---|
45 | // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget. |
---|
46 | rect.setSize(QApplication::desktop()->availableGeometry().size()); |
---|
47 | rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height())); |
---|
48 | tabWidget->resize(rect.width(),rect.height() - toolBar->size().height()); |
---|
49 | #else |
---|
50 | // Centering MainWindow |
---|
51 | // TODO: Loading of saved window state |
---|
52 | rect.moveCenter(QApplication::desktop()->availableGeometry().center()); |
---|
53 | #endif // Q_OS_WINCE |
---|
54 | setGeometry(rect); |
---|
55 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
56 | tspmodel = new CTSPModel(); |
---|
57 | tspmodel->randMin = randMin; |
---|
58 | tspmodel->randMax = randMax; |
---|
59 | tspmodel->setNumCities(spinCities->value()); |
---|
60 | taskView->setModel(tspmodel); |
---|
61 | } |
---|
62 | |
---|
63 | void MainWindow::CitiesNumberChanged(int n) |
---|
64 | { |
---|
65 | tspmodel->setNumCities(n); |
---|
66 | } |
---|
67 | |
---|
68 | void MainWindow::ChangeSettings() |
---|
69 | { |
---|
70 | SettingsDialog sd(this); |
---|
71 | sd.spinRandMin->setValue(randMin); |
---|
72 | sd.spinRandMax->setValue(randMax); |
---|
73 | if (sd.exec() == QDialog::Accepted) { |
---|
74 | randMin = sd.spinRandMin->value(); |
---|
75 | randMax = sd.spinRandMax->value(); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | #ifndef Q_OS_WINCE |
---|
80 | void MainWindow::PrintSetup() |
---|
81 | { |
---|
82 | QPrintDialog pd; |
---|
83 | pd.exec(); |
---|
84 | } |
---|
85 | #endif // Q_OS_WINCE |
---|
86 | |
---|
87 | void MainWindow::Random() |
---|
88 | { |
---|
89 | tspmodel->randomize(); |
---|
90 | } |
---|
91 | |
---|
92 | void MainWindow::Solve() |
---|
93 | { |
---|
94 | // TODO: Task solving goes here :-) |
---|
95 | tMatrix matrix; |
---|
96 | double *row; |
---|
97 | int n = spinCities->value(); |
---|
98 | bool ok; |
---|
99 | for (int r = 0; r < n; r++) { |
---|
100 | row = new double[n]; |
---|
101 | for (int c = 0; c < n; c++) { |
---|
102 | row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); |
---|
103 | if (!ok) { |
---|
104 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); |
---|
105 | return; |
---|
106 | } |
---|
107 | } |
---|
108 | matrix.append(row); |
---|
109 | } |
---|
110 | CTSPSolver solver; |
---|
111 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
112 | if (!root) |
---|
113 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec(); |
---|
114 | // tabWidget->setCurrentIndex(1); |
---|
115 | } |
---|