| 1 | /* |
---|
| 2 | * TSPSG - TSP Solver and Generator |
---|
| 3 | * Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com> |
---|
| 4 | * |
---|
| 5 | * $Id: mainwindow.cpp 6 2007-10-14 16:23:11Z 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 | PrepareTable(); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | void MainWindow::PrepareTable() |
---|
| 46 | { |
---|
| 47 | QTableWidgetItem *item; |
---|
| 48 | for (int y = 0; y < spinCities->value(); y++) { |
---|
| 49 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); |
---|
| 50 | tableTask->setVerticalHeaderItem(y,item); |
---|
| 51 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(y + 1).toString()); |
---|
| 52 | tableTask->setHorizontalHeaderItem(y,item); |
---|
| 53 | for (int x = 0; x < spinCities->value(); x++) { |
---|
| 54 | if (y == x) { |
---|
| 55 | item = new QTableWidgetItem("..."); |
---|
| 56 | item->setFlags(item->flags() ^ Qt::ItemIsEditable); |
---|
| 57 | } else { |
---|
| 58 | item = new QTableWidgetItem(QVariant(randMin + qrand() * randMax / RAND_MAX).toString()); |
---|
| 59 | QFont font = item->font(); |
---|
| 60 | font.setBold(true); |
---|
| 61 | item->setFont(font); |
---|
| 62 | } |
---|
| 63 | item->setTextAlignment(Qt::AlignCenter); |
---|
| 64 | tableTask->setItem(x,y,item); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | tableTask->resizeRowsToContents(); |
---|
| 68 | tableTask->resizeColumnsToContents(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void MainWindow::CitiesNumberChanged(int n) |
---|
| 72 | { |
---|
| 73 | bool increased = tableTask->rowCount() < n ? true : false; |
---|
| 74 | tableTask->setRowCount(n); |
---|
| 75 | tableTask->setColumnCount(n); |
---|
| 76 | for (int k = 0; k < n; k++) |
---|
| 77 | // tableTask->setColumnWidth(k,tableTask->viewport()->width() / n); |
---|
| 78 | // If number of cities increased we need to reset headers and set |
---|
| 79 | // appropriate sizes for new column and row. |
---|
| 80 | if (increased) { |
---|
| 81 | QTableWidgetItem *item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); |
---|
| 82 | tableTask->setVerticalHeaderItem(n - 1,item); |
---|
| 83 | item = new QTableWidgetItem(trUtf8("Город ") + QVariant(n).toString()); |
---|
| 84 | tableTask->setHorizontalHeaderItem(n - 1,item); |
---|
| 85 | tableTask->resizeRowToContents(n - 1); |
---|
| 86 | tableTask->resizeColumnToContents(n - 1); |
---|
| 87 | item = new QTableWidgetItem("..."); |
---|
| 88 | item->setFlags(item->flags() ^ Qt::ItemIsEditable); |
---|
| 89 | tableTask->setItem(spinCities->value() - 1,spinCities->value() - 1,item); |
---|
| 90 | for (int k = 0; k < spinCities->value() - 1; k++) { |
---|
| 91 | item = new QTableWidgetItem(QVariant(randMin + qrand() * randMax / RAND_MAX).toString()); |
---|
| 92 | QFont font = item->font(); |
---|
| 93 | font.setBold(true); |
---|
| 94 | item->setFont(font); |
---|
| 95 | tableTask->setItem(k,spinCities->value() - 1,item); |
---|
| 96 | item = new QTableWidgetItem(QVariant(randMin + qrand() * randMax / RAND_MAX).toString()); |
---|
| 97 | font = item->font(); |
---|
| 98 | font.setBold(true); |
---|
| 99 | item->setFont(font); |
---|
| 100 | tableTask->setItem(spinCities->value() - 1,k,item); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void MainWindow::ChangeSettings() |
---|
| 106 | { |
---|
| 107 | SettingsDialog sd(this); |
---|
| 108 | sd.spinRandMin->setValue(randMin); |
---|
| 109 | sd.spinRandMax->setValue(randMax); |
---|
| 110 | if (sd.exec() == QDialog::Accepted) { |
---|
| 111 | randMin = sd.spinRandMin->value(); |
---|
| 112 | randMax = sd.spinRandMax->value(); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void MainWindow::Random() |
---|
| 117 | { |
---|
| 118 | for (int y = 0; y < spinCities->value(); y++) |
---|
| 119 | for (int x = 0; x < spinCities->value(); x++) |
---|
| 120 | if (x != y) |
---|
| 121 | tableTask->item(x,y)->setText(QVariant(randMin + qrand() * randMax / RAND_MAX).toString()); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void MainWindow::Solve() |
---|
| 125 | { |
---|
| 126 | tabWidget->setCurrentIndex(1); |
---|
| 127 | // TODO: Task solving goes here :-) |
---|
| 128 | } |
---|
| 129 | |
---|