1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
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 $ |
---|
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 | #ifndef Q_OS_WINCE |
---|
26 | #include <QPrintDialog> |
---|
27 | #endif // Q_OS_WINCE |
---|
28 | #include "mainwindow.h" |
---|
29 | |
---|
30 | MainWindow::MainWindow(QWidget *parent) |
---|
31 | : QMainWindow(parent) |
---|
32 | { |
---|
33 | setupUi(this); |
---|
34 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
35 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
36 | connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); |
---|
37 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout())); |
---|
38 | #ifndef Q_OS_WINCE |
---|
39 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); |
---|
40 | #endif // Q_OS_WINCE |
---|
41 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); |
---|
42 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); |
---|
43 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); |
---|
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 |
---|
51 | if (settings->value("SavePos",false).toBool()) { |
---|
52 | // Loading of saved window state |
---|
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()) |
---|
57 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
58 | settings->endGroup(); |
---|
59 | } else { |
---|
60 | // Centering main window |
---|
61 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
62 | setGeometry(rect); |
---|
63 | } |
---|
64 | #endif // Q_OS_WINCE |
---|
65 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
66 | tspmodel = new CTSPModel(); |
---|
67 | tspmodel->setNumCities(spinCities->value()); |
---|
68 | taskView->setModel(tspmodel); |
---|
69 | #ifdef Q_OS_WINCE |
---|
70 | taskView->resizeColumnsToContents(); |
---|
71 | taskView->resizeRowsToContents(); |
---|
72 | #endif // Q_OS_WINCE |
---|
73 | } |
---|
74 | |
---|
75 | void MainWindow::CitiesNumberChanged(int n) |
---|
76 | { |
---|
77 | #ifdef Q_OS_WINCE |
---|
78 | int count = tspmodel->numCities(); |
---|
79 | #endif // Q_OS_WINCE |
---|
80 | tspmodel->setNumCities(n); |
---|
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 |
---|
88 | } |
---|
89 | |
---|
90 | void MainWindow::ChangeSettings() |
---|
91 | { |
---|
92 | SettingsDialog sd(this); |
---|
93 | sd.exec(); |
---|
94 | } |
---|
95 | |
---|
96 | #ifndef Q_OS_WINCE |
---|
97 | void MainWindow::PrintSetup() |
---|
98 | { |
---|
99 | QPrintDialog pd; |
---|
100 | pd.exec(); |
---|
101 | } |
---|
102 | #endif // Q_OS_WINCE |
---|
103 | |
---|
104 | void MainWindow::Random() |
---|
105 | { |
---|
106 | tspmodel->randomize(); |
---|
107 | #ifdef Q_OS_WINCE |
---|
108 | taskView->resizeColumnsToContents(); |
---|
109 | taskView->resizeRowsToContents(); |
---|
110 | #endif // Q_OS_WINCE |
---|
111 | } |
---|
112 | |
---|
113 | void MainWindow::Solve() |
---|
114 | { |
---|
115 | // TODO: Task solving goes here :-) |
---|
116 | tMatrix matrix; |
---|
117 | double *row; |
---|
118 | int n = spinCities->value(); |
---|
119 | bool ok; |
---|
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) { |
---|
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(); |
---|
126 | return; |
---|
127 | } |
---|
128 | } |
---|
129 | matrix.append(row); |
---|
130 | } |
---|
131 | CTSPSolver solver; |
---|
132 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
133 | if (!root) |
---|
134 | QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec(); |
---|
135 | // tabWidget->setCurrentIndex(1); |
---|
136 | } |
---|
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()); |
---|
148 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
149 | } |
---|
150 | |
---|
151 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
152 | { |
---|
153 | settings->setValue("NumCities",spinCities->value()); |
---|
154 | #ifndef Q_OS_WINCE |
---|
155 | // Saving windows state |
---|
156 | if (settings->value("SavePos",false).toBool()) { |
---|
157 | settings->beginGroup("MainWindow"); |
---|
158 | settings->setValue("Maximized",isMaximized()); |
---|
159 | if (!isMaximized()) { |
---|
160 | settings->setValue("Size",size()); |
---|
161 | settings->setValue("Position",pos()); |
---|
162 | } |
---|
163 | settings->endGroup(); |
---|
164 | } |
---|
165 | #endif // Q_OS_WINCE |
---|
166 | QMainWindow::closeEvent(event); |
---|
167 | } |
---|