[1] | 1 | /* |
---|
[6] | 2 | * TSPSG - TSP Solver and Generator |
---|
[17] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[1] | 4 | * |
---|
[6] | 5 | * $Id: mainwindow.cpp 27 2009-06-23 17:56:29Z laleppa $ |
---|
| 6 | * $URL: https://tspsg.svn.sourceforge.net/svnroot/tspsg/trunk/src/mainwindow.cpp $ |
---|
[4] | 7 | * |
---|
[6] | 8 | * This file is part of TSPSG. |
---|
[1] | 9 | * |
---|
[6] | 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. |
---|
[1] | 14 | * |
---|
[6] | 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. |
---|
[1] | 19 | * |
---|
[6] | 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/>. |
---|
[1] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include <QtGui> |
---|
[17] | 25 | #ifndef Q_OS_WINCE |
---|
| 26 | #include <QPrintDialog> |
---|
| 27 | #endif // Q_OS_WINCE |
---|
[1] | 28 | #include "mainwindow.h" |
---|
| 29 | |
---|
| 30 | MainWindow::MainWindow(QWidget *parent) |
---|
[21] | 31 | : QMainWindow(parent) |
---|
[1] | 32 | { |
---|
| 33 | setupUi(this); |
---|
[23] | 34 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
| 35 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
[1] | 36 | connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); |
---|
[21] | 37 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout())); |
---|
[17] | 38 | #ifndef Q_OS_WINCE |
---|
| 39 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); |
---|
| 40 | #endif // Q_OS_WINCE |
---|
[6] | 41 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); |
---|
| 42 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); |
---|
[4] | 43 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); |
---|
[17] | 44 | QRect rect = geometry(); |
---|
| 45 | #ifdef Q_OS_WINCE |
---|
| 46 | // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget. |
---|
| 47 | rect.setSize(QApplication::desktop()->availableGeometry().size()); |
---|
| 48 | rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height())); |
---|
| 49 | tabWidget->resize(rect.width(),rect.height() - toolBar->size().height()); |
---|
| 50 | #else |
---|
[23] | 51 | if (settings->value("SavePos",false).toBool()) { |
---|
[21] | 52 | // Loading of saved window state |
---|
[23] | 53 | settings->beginGroup("MainWindow"); |
---|
| 54 | resize(settings->value("Size",size()).toSize()); |
---|
| 55 | move(settings->value("Position",pos()).toPoint()); |
---|
| 56 | if (settings->value("Maximized",false).toBool()) |
---|
[21] | 57 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
[23] | 58 | settings->endGroup(); |
---|
[21] | 59 | } else { |
---|
| 60 | // Centering main window |
---|
| 61 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 62 | setGeometry(rect); |
---|
| 63 | } |
---|
[17] | 64 | #endif // Q_OS_WINCE |
---|
[11] | 65 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[15] | 66 | tspmodel = new CTSPModel(); |
---|
| 67 | tspmodel->setNumCities(spinCities->value()); |
---|
| 68 | taskView->setModel(tspmodel); |
---|
[21] | 69 | #ifdef Q_OS_WINCE |
---|
| 70 | taskView->resizeColumnsToContents(); |
---|
| 71 | taskView->resizeRowsToContents(); |
---|
| 72 | #endif // Q_OS_WINCE |
---|
[6] | 73 | } |
---|
| 74 | |
---|
[4] | 75 | void MainWindow::CitiesNumberChanged(int n) |
---|
| 76 | { |
---|
[21] | 77 | #ifdef Q_OS_WINCE |
---|
| 78 | int count = tspmodel->numCities(); |
---|
| 79 | #endif // Q_OS_WINCE |
---|
[15] | 80 | tspmodel->setNumCities(n); |
---|
[21] | 81 | #ifdef Q_OS_WINCE |
---|
| 82 | if (n > count) |
---|
| 83 | for (int k = count; k < n; k++) { |
---|
| 84 | taskView->resizeColumnToContents(k); |
---|
| 85 | taskView->resizeRowToContents(k); |
---|
| 86 | } |
---|
| 87 | #endif // Q_OS_WINCE |
---|
[1] | 88 | } |
---|
| 89 | |
---|
| 90 | void MainWindow::ChangeSettings() |
---|
| 91 | { |
---|
| 92 | SettingsDialog sd(this); |
---|
[21] | 93 | sd.exec(); |
---|
[1] | 94 | } |
---|
[6] | 95 | |
---|
[17] | 96 | #ifndef Q_OS_WINCE |
---|
| 97 | void MainWindow::PrintSetup() |
---|
| 98 | { |
---|
| 99 | QPrintDialog pd; |
---|
| 100 | pd.exec(); |
---|
| 101 | } |
---|
| 102 | #endif // Q_OS_WINCE |
---|
| 103 | |
---|
[6] | 104 | void MainWindow::Random() |
---|
| 105 | { |
---|
[15] | 106 | tspmodel->randomize(); |
---|
[21] | 107 | #ifdef Q_OS_WINCE |
---|
| 108 | taskView->resizeColumnsToContents(); |
---|
| 109 | taskView->resizeRowsToContents(); |
---|
| 110 | #endif // Q_OS_WINCE |
---|
[6] | 111 | } |
---|
| 112 | |
---|
| 113 | void MainWindow::Solve() |
---|
| 114 | { |
---|
[13] | 115 | // TODO: Task solving goes here :-) |
---|
| 116 | tMatrix matrix; |
---|
| 117 | double *row; |
---|
[15] | 118 | int n = spinCities->value(); |
---|
[13] | 119 | bool ok; |
---|
[15] | 120 | for (int r = 0; r < n; r++) { |
---|
| 121 | row = new double[n]; |
---|
| 122 | for (int c = 0; c < n; c++) { |
---|
| 123 | row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); |
---|
| 124 | if (!ok) { |
---|
[27] | 125 | QMessageBox(QMessageBox::Critical,trUtf8("Data error"),QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); |
---|
[15] | 126 | return; |
---|
[13] | 127 | } |
---|
| 128 | } |
---|
| 129 | matrix.append(row); |
---|
| 130 | } |
---|
| 131 | CTSPSolver solver; |
---|
| 132 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
| 133 | if (!root) |
---|
[27] | 134 | QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec(); |
---|
[12] | 135 | // tabWidget->setCurrentIndex(1); |
---|
[6] | 136 | } |
---|
[21] | 137 | |
---|
| 138 | void MainWindow::showAbout() |
---|
| 139 | { |
---|
| 140 | // TODO: Normal about window :-) |
---|
| 141 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n\ |
---|
| 142 | Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n\ |
---|
| 143 | Qt library versions:\n\ |
---|
| 144 | Compile time: %2\n\ |
---|
| 145 | Runtime: %3\n\ |
---|
| 146 | \n\ |
---|
| 147 | TSPSG is licensed under the terms of the GNU General Public License. You should have received a copy of the GNU General Public License along with TSPSG.").arg(QDate().toString("%Y"),QT_VERSION_STR,qVersion()); |
---|
[27] | 148 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
[21] | 149 | } |
---|
| 150 | |
---|
| 151 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
| 152 | { |
---|
[26] | 153 | settings->setValue("NumCities",spinCities->value()); |
---|
| 154 | #ifndef Q_OS_WINCE |
---|
[21] | 155 | // Saving windows state |
---|
[23] | 156 | if (settings->value("SavePos",false).toBool()) { |
---|
| 157 | settings->beginGroup("MainWindow"); |
---|
| 158 | settings->setValue("Maximized",isMaximized()); |
---|
[21] | 159 | if (!isMaximized()) { |
---|
[23] | 160 | settings->setValue("Size",size()); |
---|
| 161 | settings->setValue("Position",pos()); |
---|
[21] | 162 | } |
---|
[23] | 163 | settings->endGroup(); |
---|
[21] | 164 | } |
---|
[26] | 165 | #endif // Q_OS_WINCE |
---|
[21] | 166 | QMainWindow::closeEvent(event); |
---|
| 167 | } |
---|