source: tspsg/src/mainwindow.cpp @ 2bc8e278b7

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

Finished converting to Qt's model/view architecture

  • Property mode set to 100644
File size: 3.0 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
[67e53c96d7]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();
[2bc8e278b7]40        rect.moveCenter(QApplication::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()).center());
[052d1b9331]41        setGeometry(rect);
[fc9f661ded]42        qsrand(QDateTime().currentDateTime().toTime_t());
[2bc8e278b7]43        tspmodel = new CTSPModel();
44        tspmodel->randMin = randMin;
45        tspmodel->randMax = randMax;
46        tspmodel->setNumCities(spinCities->value());
47        taskView->setModel(tspmodel);
[003e4193be]48}
[052d1b9331]49
[003e4193be]50void MainWindow::CitiesNumberChanged(int n)
51{
[2bc8e278b7]52        tspmodel->setNumCities(n);
[5515c2c2a7]53}
54
55void MainWindow::ChangeSettings()
56{
57SettingsDialog sd(this);
[bb994a7ff8]58        sd.spinRandMin->setValue(randMin);
59        sd.spinRandMax->setValue(randMax);
60        if (sd.exec() == QDialog::Accepted) {
61                randMin = sd.spinRandMin->value();
62                randMax = sd.spinRandMax->value();
63        }
[5515c2c2a7]64}
[bb994a7ff8]65
66void MainWindow::Random()
67{
[2bc8e278b7]68        tspmodel->randomize();
[bb994a7ff8]69}
70
71void MainWindow::Solve()
72{
73        // TODO: Task solving goes here :-)
[e664262f7d]74tMatrix matrix;
75double *row;
[2bc8e278b7]76int n = spinCities->value();
[e664262f7d]77bool ok;
[2bc8e278b7]78        for (int r = 0; r < n; r++) {
79                row = new double[n];
80                for (int c = 0; c < n; c++) {
81                        row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok);
82                        if (!ok) {
83                                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec();
84                                return;
[e664262f7d]85                        }
86                }
87                matrix.append(row);
88        }
89CTSPSolver solver;
90sStep *root = solver.solve(spinCities->value(),matrix);
91        if (!root)
92                QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec();
93        // tabWidget->setCurrentIndex(1);
[bb994a7ff8]94}
95
Note: See TracBrowser for help on using the repository browser.