[1] | 1 | /* |
---|
[6] | 2 | * TSPSG - TSP Solver and Generator |
---|
[17] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[1] | 4 | * |
---|
[6] | 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 $ |
---|
[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> |
---|
[17] | 25 | #ifndef Q_OS_WINCE |
---|
| 26 | #include <QPrintDialog> |
---|
| 27 | #endif // Q_OS_WINCE |
---|
[1] | 28 | #include "mainwindow.h" |
---|
| 29 | |
---|
[12] | 30 | // TODO: Saving window state on close |
---|
[2] | 31 | |
---|
[1] | 32 | MainWindow::MainWindow(QWidget *parent) |
---|
[6] | 33 | : QMainWindow(parent), randMin(1), randMax(10) |
---|
[1] | 34 | { |
---|
| 35 | setupUi(this); |
---|
| 36 | connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); |
---|
[17] | 37 | #ifndef Q_OS_WINCE |
---|
| 38 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); |
---|
| 39 | #endif // Q_OS_WINCE |
---|
[6] | 40 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); |
---|
| 41 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); |
---|
[4] | 42 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); |
---|
[17] | 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 |
---|
[2] | 50 | // Centering MainWindow |
---|
| 51 | // TODO: Loading of saved window state |
---|
[17] | 52 | rect.moveCenter(QApplication::desktop()->availableGeometry().center()); |
---|
| 53 | #endif // Q_OS_WINCE |
---|
[2] | 54 | setGeometry(rect); |
---|
[11] | 55 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[15] | 56 | tspmodel = new CTSPModel(); |
---|
| 57 | tspmodel->randMin = randMin; |
---|
| 58 | tspmodel->randMax = randMax; |
---|
| 59 | tspmodel->setNumCities(spinCities->value()); |
---|
| 60 | taskView->setModel(tspmodel); |
---|
[6] | 61 | } |
---|
| 62 | |
---|
[4] | 63 | void MainWindow::CitiesNumberChanged(int n) |
---|
| 64 | { |
---|
[15] | 65 | tspmodel->setNumCities(n); |
---|
[1] | 66 | } |
---|
| 67 | |
---|
| 68 | void MainWindow::ChangeSettings() |
---|
| 69 | { |
---|
| 70 | SettingsDialog sd(this); |
---|
[6] | 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 | } |
---|
[1] | 77 | } |
---|
[6] | 78 | |
---|
[17] | 79 | #ifndef Q_OS_WINCE |
---|
| 80 | void MainWindow::PrintSetup() |
---|
| 81 | { |
---|
| 82 | QPrintDialog pd; |
---|
| 83 | pd.exec(); |
---|
| 84 | } |
---|
| 85 | #endif // Q_OS_WINCE |
---|
| 86 | |
---|
[6] | 87 | void MainWindow::Random() |
---|
| 88 | { |
---|
[15] | 89 | tspmodel->randomize(); |
---|
[6] | 90 | } |
---|
| 91 | |
---|
| 92 | void MainWindow::Solve() |
---|
| 93 | { |
---|
[13] | 94 | // TODO: Task solving goes here :-) |
---|
| 95 | tMatrix matrix; |
---|
| 96 | double *row; |
---|
[15] | 97 | int n = spinCities->value(); |
---|
[13] | 98 | bool ok; |
---|
[15] | 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; |
---|
[13] | 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(); |
---|
[12] | 114 | // tabWidget->setCurrentIndex(1); |
---|
[6] | 115 | } |
---|