source: tspsg/src/mainwindow.cpp @ e664262f7d

0.1.3.145-beta1-symbian0.1.4.170-beta2-bb10appveyorimgbotreadme
Last change on this file since e664262f7d was e664262f7d, checked in by Oleksii Serdiuk, 17 years ago

Writing solving algorithm...

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com>
4 *
5 *  $Id$
6 *  $URL$
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
29MainWindow::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
39QRect rect = geometry();
40    rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center());
41        setGeometry(rect);
42        qsrand(QDateTime().currentDateTime().toTime_t());
43        PrepareTable();
44}
45
46int MainWindow::rand(int min, int max)
47{
48        return min + (int)(((float)qrand() / RAND_MAX) * max);
49}
50
51void MainWindow::PrepareTable()
52{
53QTableWidgetItem *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 {
64                                item = new QTableWidgetItem(QVariant(rand(randMin,randMax)).toString());
65QFont font = item->font();
66                                font.setBold(true);
67                                item->setFont(font);
68                        }
69                        item->setTextAlignment(Qt::AlignCenter);
70                        tableTask->setItem(x,y,item);
71                }
72        }
73        tableTask->resizeRowsToContents();
74        tableTask->resizeColumnsToContents();
75}
76
77void MainWindow::CitiesNumberChanged(int n)
78{
79bool increased = tableTask->rowCount() < n ? true : false;
80        tableTask->setRowCount(n);
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
85        // appropriate sizes for new column and row
86        if (increased) {
87QTableWidgetItem *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);
91                tableTask->resizeRowToContents(n - 1);
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++) {
97                        item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString());
98QFont font = item->font();
99                        font.setBold(true);
100                        item->setFont(font);
101                        tableTask->setItem(k,spinCities->value() - 1,item);
102                        item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString());
103                        font = item->font();
104                        font.setBold(true);
105                        item->setFont(font);
106                        tableTask->setItem(spinCities->value() - 1,k,item);
107                }
108        }
109}
110
111void MainWindow::ChangeSettings()
112{
113SettingsDialog sd(this);
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        }
120}
121
122void 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)
127                                tableTask->item(x,y)->setText(QVariant(rand(randMin, randMax)).toString());
128}
129
130void MainWindow::Solve()
131{
132        // TODO: Task solving goes here :-)
133tMatrix matrix;
134double *row;
135bool 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        }
154CTSPSolver solver;
155sStep *root = solver.solve(spinCities->value(),matrix);
156        if (!root)
157                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec();
158        // tabWidget->setCurrentIndex(1);
159}
160
Note: See TracBrowser for help on using the repository browser.