source: tspsg/src/mainwindow.cpp @ fc9f661ded

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

Fixed random number generation algorithm (didn't work at all in linux).

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[5515c2c2a7]1/*
[bb994a7ff8]2 *  TSPSG - TSP Solver and Generator
3 *  Copyright (C) 2007 Lёppa <lacontacts[at]gmail[dot]com>
[003e4193be]4 *
[bb994a7ff8]5 *  $Id$
6 *  $URL$
[5515c2c2a7]7 *
[bb994a7ff8]8 *  This file is part of TSPSG.
[5515c2c2a7]9 *
[bb994a7ff8]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.
[5515c2c2a7]14 *
[bb994a7ff8]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.
[5515c2c2a7]19 *
[bb994a7ff8]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/>.
[5515c2c2a7]22 */
23
24#include <QtGui>
25#include "mainwindow.h"
26
[fc9f661ded]27// TODO: Saving window state on close.
[052d1b9331]28
[5515c2c2a7]29MainWindow::MainWindow(QWidget *parent)
[bb994a7ff8]30        : QMainWindow(parent), randMin(1), randMax(10)
[5515c2c2a7]31{
32        setupUi(this);
33        connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings()));
[bb994a7ff8]34        connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve()));
35        connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random()));
[003e4193be]36        connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int)));
[052d1b9331]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);
[fc9f661ded]42        qsrand(QDateTime().currentDateTime().toTime_t());
[bb994a7ff8]43        PrepareTable();
44}
45
[fc9f661ded]46int MainWindow::rand(int min, int max)
47{
48        return min + (int)(((float)qrand() / RAND_MAX) * max);
49}
50
[bb994a7ff8]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 {
[fc9f661ded]64                                item = new QTableWidgetItem(QVariant(rand(randMin,randMax)).toString());
[bb994a7ff8]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                }
[003e4193be]72        }
73        tableTask->resizeRowsToContents();
74        tableTask->resizeColumnsToContents();
75}
[052d1b9331]76
[003e4193be]77void MainWindow::CitiesNumberChanged(int n)
78{
79bool increased = tableTask->rowCount() < n ? true : false;
80        tableTask->setRowCount(n);
81        tableTask->setColumnCount(n);
[bb994a7ff8]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++) {
[fc9f661ded]97                        item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString());
[bb994a7ff8]98QFont font = item->font();
99                        font.setBold(true);
100                        item->setFont(font);
101                        tableTask->setItem(k,spinCities->value() - 1,item);
[fc9f661ded]102                        item = new QTableWidgetItem(QVariant(rand(randMin, randMax)).toString());
[bb994a7ff8]103                        font = item->font();
104                        font.setBold(true);
105                        item->setFont(font);
106                        tableTask->setItem(spinCities->value() - 1,k,item);
107                }
108        }
[5515c2c2a7]109}
110
111void MainWindow::ChangeSettings()
112{
113SettingsDialog sd(this);
[bb994a7ff8]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        }
[5515c2c2a7]120}
[bb994a7ff8]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)
[fc9f661ded]127                                tableTask->item(x,y)->setText(QVariant(rand(randMin, randMax)).toString());
[bb994a7ff8]128}
129
130void MainWindow::Solve()
131{
132        tabWidget->setCurrentIndex(1);
133        // TODO: Task solving goes here :-)
134}
135
Note: See TracBrowser for help on using the repository browser.