1 | /* |
---|
2 | * TSPSG - TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * $URL$ |
---|
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 "defines.h" |
---|
29 | #include "mainwindow.h" |
---|
30 | |
---|
31 | MainWindow::MainWindow(QWidget *parent) |
---|
32 | : QMainWindow(parent) |
---|
33 | { |
---|
34 | setupUi(this); |
---|
35 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
36 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
37 | connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); |
---|
38 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout())); |
---|
39 | #ifndef Q_OS_WINCE |
---|
40 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); |
---|
41 | #endif // Q_OS_WINCE |
---|
42 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); |
---|
43 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); |
---|
44 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); |
---|
45 | QRect rect = geometry(); |
---|
46 | #ifdef Q_OS_WINCE |
---|
47 | // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget. |
---|
48 | rect.setSize(QApplication::desktop()->availableGeometry().size()); |
---|
49 | rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height())); |
---|
50 | tabWidget->resize(rect.width(),rect.height() - toolBar->size().height()); |
---|
51 | #else |
---|
52 | if (settings->value("SavePos",false).toBool()) { |
---|
53 | // Loading of saved window state |
---|
54 | settings->beginGroup("MainWindow"); |
---|
55 | resize(settings->value("Size",size()).toSize()); |
---|
56 | move(settings->value("Position",pos()).toPoint()); |
---|
57 | if (settings->value("Maximized",false).toBool()) |
---|
58 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
59 | settings->endGroup(); |
---|
60 | } else { |
---|
61 | // Centering main window |
---|
62 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
63 | setGeometry(rect); |
---|
64 | } |
---|
65 | #endif // Q_OS_WINCE |
---|
66 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
67 | tspmodel = new CTSPModel(); |
---|
68 | tspmodel->setNumCities(spinCities->value()); |
---|
69 | taskView->setModel(tspmodel); |
---|
70 | #ifdef Q_OS_WINCE |
---|
71 | taskView->resizeColumnsToContents(); |
---|
72 | taskView->resizeRowsToContents(); |
---|
73 | #endif // Q_OS_WINCE |
---|
74 | } |
---|
75 | |
---|
76 | void MainWindow::CitiesNumberChanged(int n) |
---|
77 | { |
---|
78 | #ifdef Q_OS_WINCE |
---|
79 | int count = tspmodel->numCities(); |
---|
80 | #endif // Q_OS_WINCE |
---|
81 | tspmodel->setNumCities(n); |
---|
82 | #ifdef Q_OS_WINCE |
---|
83 | if (n > count) |
---|
84 | for (int k = count; k < n; k++) { |
---|
85 | taskView->resizeColumnToContents(k); |
---|
86 | taskView->resizeRowToContents(k); |
---|
87 | } |
---|
88 | #endif // Q_OS_WINCE |
---|
89 | } |
---|
90 | |
---|
91 | void MainWindow::ChangeSettings() |
---|
92 | { |
---|
93 | SettingsDialog sd(this); |
---|
94 | sd.exec(); |
---|
95 | } |
---|
96 | |
---|
97 | #ifndef Q_OS_WINCE |
---|
98 | void MainWindow::PrintSetup() |
---|
99 | { |
---|
100 | QPrintDialog pd; |
---|
101 | pd.exec(); |
---|
102 | } |
---|
103 | #endif // Q_OS_WINCE |
---|
104 | |
---|
105 | void MainWindow::Random() |
---|
106 | { |
---|
107 | tspmodel->randomize(); |
---|
108 | #ifdef Q_OS_WINCE |
---|
109 | taskView->resizeColumnsToContents(); |
---|
110 | taskView->resizeRowsToContents(); |
---|
111 | #endif // Q_OS_WINCE |
---|
112 | } |
---|
113 | |
---|
114 | void MainWindow::Solve() |
---|
115 | { |
---|
116 | // TODO: Task solving goes here :-) |
---|
117 | tMatrix matrix; |
---|
118 | double *row; |
---|
119 | int n = spinCities->value(); |
---|
120 | bool ok; |
---|
121 | for (int r = 0; r < n; r++) { |
---|
122 | row = new double[n]; |
---|
123 | for (int c = 0; c < n; c++) { |
---|
124 | row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); |
---|
125 | if (!ok) { |
---|
126 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка в данных"),QString(trUtf8("Ошибка в ячейке [Строка %1; Колонка %2]: Неверный формат данных.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); |
---|
127 | return; |
---|
128 | } |
---|
129 | } |
---|
130 | matrix.append(row); |
---|
131 | } |
---|
132 | CTSPSolver solver; |
---|
133 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
134 | if (!root) |
---|
135 | QMessageBox(QMessageBox::Critical,trUtf8("Ошибка при решении"),trUtf8("Во время решения задачи возникла ошибка"),QMessageBox::Ok,this).exec(); |
---|
136 | // tabWidget->setCurrentIndex(1); |
---|
137 | } |
---|
138 | |
---|
139 | void MainWindow::showAbout() |
---|
140 | { |
---|
141 | // TODO: Normal about window :-) |
---|
142 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n\ |
---|
143 | Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n\ |
---|
144 | Qt library versions:\n\ |
---|
145 | Compile time: %2\n\ |
---|
146 | Runtime: %3\n\ |
---|
147 | \n\ |
---|
148 | 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()); |
---|
149 | QMessageBox(QMessageBox::Information,"About",about).exec(); |
---|
150 | } |
---|
151 | |
---|
152 | #ifndef Q_OS_WINCE |
---|
153 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
154 | { |
---|
155 | // Saving windows state |
---|
156 | settings->setValue("NumCities",spinCities->value()); |
---|
157 | if (settings->value("SavePos",false).toBool()) { |
---|
158 | settings->beginGroup("MainWindow"); |
---|
159 | settings->setValue("Maximized",isMaximized()); |
---|
160 | if (!isMaximized()) { |
---|
161 | settings->setValue("Size",size()); |
---|
162 | settings->setValue("Position",pos()); |
---|
163 | } |
---|
164 | settings->endGroup(); |
---|
165 | } |
---|
166 | QMainWindow::closeEvent(event); |
---|
167 | } |
---|
168 | #endif // Q_OS_WINCE |
---|