[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 13 2007-10-21 13:07:21Z 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] | 29 | MainWindow::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 |
---|
| 39 | QRect rect = geometry(); |
---|
| 40 | rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center()); |
---|
| 41 | setGeometry(rect); |
---|
[11] | 42 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[6] | 43 | PrepareTable(); |
---|
| 44 | } |
---|
| 45 | |
---|
[11] | 46 | int MainWindow::rand(int min, int max) |
---|
| 47 | { |
---|
| 48 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
| 49 | } |
---|
| 50 | |
---|
[6] | 51 | void MainWindow::PrepareTable() |
---|
| 52 | { |
---|
| 53 | QTableWidgetItem *item; |
---|
| 54 | for (int y = 0; y < spinCities->value(); y++) { |
---|
| 55 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); |
---|
| 56 | tableTask->setVerticalHeaderItem(y,item); |
---|
| 57 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); |
---|
| 58 | tableTask->setHorizontalHeaderItem(y,item); |
---|
| 59 | for (int x = 0; x < spinCities->value(); x++) { |
---|
| 60 | if (y == x) { |
---|
| 61 | item = new QTableWidgetItem("..."); |
---|
| 62 | item->setFlags(item->flags() ^ Qt::ItemIsEditable); |
---|
| 63 | } else { |
---|
[11] | 64 | item = new QTableWidgetItem(QVariant(rand(randMin,randMax)).toString()); |
---|
[6] | 65 | QFont font = item->font(); |
---|
| 66 | font.setBold(true); |
---|
| 67 | item->setFont(font); |
---|
| 68 | } |
---|
| 69 | item->setTextAlignment(Qt::AlignCenter); |
---|
| 70 | tableTask->setItem(x,y,item); |
---|
| 71 | } |
---|
[4] | 72 | } |
---|
| 73 | tableTask->resizeRowsToContents(); |
---|
| 74 | tableTask->resizeColumnsToContents(); |
---|
| 75 | } |
---|
[2] | 76 | |
---|
[4] | 77 | void MainWindow::CitiesNumberChanged(int n) |
---|
| 78 | { |
---|
| 79 | bool increased = tableTask->rowCount() < n ? true : false; |
---|
| 80 | tableTask->setRowCount(n); |
---|
[6] | 81 | tableTask->setColumnCount(n); |
---|
| 82 | for (int k = 0; k < n; k++) |
---|
| 83 | // tableTask->setColumnWidth(k,tableTask->viewport()->width() / n); |
---|
| 84 | // If number of cities increased we need to reset headers and set |
---|
[12] | 85 | // appropriate sizes for new column and row |
---|
[6] | 86 | if (increased) { |
---|
| 87 | QTableWidgetItem *item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); |
---|
| 88 | tableTask->setVerticalHeaderItem(n - 1,item); |
---|
| 89 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); |
---|
| 90 | tableTask->setHorizontalHeaderItem(n - 1,item); |
---|
[4] | 91 | tableTask->resizeRowToContents(n - 1); |
---|
[6] | 92 | tableTask->resizeColumnToContents(n - 1); |
---|
| 93 | item = new QTableWidgetItem("..."); |
---|
| 94 | item->setFlags(item->flags() ^ Qt::ItemIsEditable); |
---|
| 95 | tableTask->setItem(spinCities->value() - 1,spinCities->value() - 1,item); |
---|
| 96 | for (int k = 0; k < spinCities->value() - 1; k++) { |
---|
[11] | 97 | item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString()); |
---|
[6] | 98 | QFont font = item->font(); |
---|
| 99 | font.setBold(true); |
---|
| 100 | item->setFont(font); |
---|
| 101 | tableTask->setItem(k,spinCities->value() - 1,item); |
---|
[11] | 102 | item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString()); |
---|
[6] | 103 | font = item->font(); |
---|
| 104 | font.setBold(true); |
---|
| 105 | item->setFont(font); |
---|
| 106 | tableTask->setItem(spinCities->value() - 1,k,item); |
---|
| 107 | } |
---|
| 108 | } |
---|
[1] | 109 | } |
---|
| 110 | |
---|
| 111 | void MainWindow::ChangeSettings() |
---|
| 112 | { |
---|
| 113 | SettingsDialog sd(this); |
---|
[6] | 114 | sd.spinRandMin->setValue(randMin); |
---|
| 115 | sd.spinRandMax->setValue(randMax); |
---|
| 116 | if (sd.exec() == QDialog::Accepted) { |
---|
| 117 | randMin = sd.spinRandMin->value(); |
---|
| 118 | randMax = sd.spinRandMax->value(); |
---|
| 119 | } |
---|
[1] | 120 | } |
---|
[6] | 121 | |
---|
| 122 | void MainWindow::Random() |
---|
| 123 | { |
---|
| 124 | for (int y = 0; y < spinCities->value(); y++) |
---|
| 125 | for (int x = 0; x < spinCities->value(); x++) |
---|
| 126 | if (x != y) |
---|
[11] | 127 | tableTask->item(x,y)->setText(QVariant(rand(randMin, randMax)).toString()); |
---|
[6] | 128 | } |
---|
| 129 | |
---|
| 130 | void MainWindow::Solve() |
---|
| 131 | { |
---|
[13] | 132 | // TODO: Task solving goes here :-) |
---|
| 133 | tMatrix matrix; |
---|
| 134 | double *row; |
---|
| 135 | bool ok; |
---|
| 136 | for (int x = 0; x < spinCities->value(); x++) { |
---|
| 137 | row = new double[spinCities->value()]; |
---|
| 138 | for (int y = 0; y < spinCities->value(); y++) { |
---|
| 139 | if (x == y) |
---|
| 140 | row[y] = infinity; |
---|
| 141 | else { |
---|
| 142 | row[y] = tableTask->item(x,y)->text().toDouble(&ok); |
---|
| 143 | if (!ok) { |
---|
| 144 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(x + 1).arg(y + 1),QMessageBox::Ok,this).exec(); |
---|
| 145 | return; |
---|
| 146 | } else if (row[y] < 0) { |
---|
| 147 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Значение не может быть меньше нуля.")).arg(x + 1).arg(y + 1),QMessageBox::Ok,this).exec(); |
---|
| 148 | return; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | matrix.append(row); |
---|
| 153 | } |
---|
| 154 | CTSPSolver solver; |
---|
| 155 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
| 156 | if (!root) |
---|
| 157 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec(); |
---|
[12] | 158 | // tabWidget->setCurrentIndex(1); |
---|
[6] | 159 | } |
---|
| 160 | |
---|