1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com> |
---|
4 | * |
---|
5 | * $Id: mainwindow.cpp 11 2007-10-19 02:22:33Z 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 | PrepareTable(); |
---|
44 | } |
---|
45 | |
---|
46 | int MainWindow::rand(int min, int max) |
---|
47 | { |
---|
48 | return min + (int)(((float)qrand() / RAND_MAX) * max); |
---|
49 | } |
---|
50 | |
---|
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 { |
---|
64 | item = new QTableWidgetItem(QVariant(rand(randMin,randMax)).toString()); |
---|
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 | } |
---|
72 | } |
---|
73 | tableTask->resizeRowsToContents(); |
---|
74 | tableTask->resizeColumnsToContents(); |
---|
75 | } |
---|
76 | |
---|
77 | void MainWindow::CitiesNumberChanged(int n) |
---|
78 | { |
---|
79 | bool 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) { |
---|
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); |
---|
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()); |
---|
98 | QFont 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 | |
---|
111 | void MainWindow::ChangeSettings() |
---|
112 | { |
---|
113 | SettingsDialog 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 | |
---|
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) |
---|
127 | tableTask->item(x,y)->setText(QVariant(rand(randMin, randMax)).toString()); |
---|
128 | } |
---|
129 | |
---|
130 | void MainWindow::Solve() |
---|
131 | { |
---|
132 | tabWidget->setCurrentIndex(1); |
---|
133 | // TODO: Task solving goes here :-) |
---|
134 | } |
---|
135 | |
---|