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 "mainwindow.h" |
---|
25 | |
---|
26 | MainWindow::MainWindow(QWidget *parent) |
---|
27 | : QMainWindow(parent) |
---|
28 | { |
---|
29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
30 | loadLanguage(); |
---|
31 | setupUi(this); |
---|
32 | initDocStyleSheet(); |
---|
33 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
34 | solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); |
---|
35 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
36 | #ifdef Q_OS_WINCE |
---|
37 | // A little hack for toolbar icons to have sane size. |
---|
38 | int s = qMin(QApplication::desktop()->screenGeometry().width(),QApplication::desktop()->screenGeometry().height()); |
---|
39 | toolBar->setIconSize(QSize(s / 10,s / 10)); |
---|
40 | #endif |
---|
41 | #ifndef Q_OS_WINCE |
---|
42 | printer = new QPrinter(); |
---|
43 | #endif // Q_OS_WINCE |
---|
44 | groupSettingsLanguageList = new QActionGroup(this); |
---|
45 | actionSettingsLanguageEnglish->setData("en"); |
---|
46 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
47 | loadLangList(); |
---|
48 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
49 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
50 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
51 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
52 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
53 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
54 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
55 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
56 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
57 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
58 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
59 | #ifndef Q_OS_WINCE |
---|
60 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered())); |
---|
61 | #endif // Q_OS_WINCE |
---|
62 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
63 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
64 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
65 | QRect rect = geometry(); |
---|
66 | setCentralWidget(tabWidget); |
---|
67 | #ifndef Q_OS_WINCE |
---|
68 | if (settings->value("SavePos",false).toBool()) { |
---|
69 | // Loading of saved window state |
---|
70 | settings->beginGroup("MainWindow"); |
---|
71 | resize(settings->value("Size",size()).toSize()); |
---|
72 | move(settings->value("Position",pos()).toPoint()); |
---|
73 | if (settings->value("Maximized",false).toBool()) |
---|
74 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
75 | settings->endGroup(); |
---|
76 | } else { |
---|
77 | // Centering main window |
---|
78 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
79 | setGeometry(rect); |
---|
80 | } |
---|
81 | #endif // Q_OS_WINCE |
---|
82 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
83 | tspmodel = new CTSPModel(); |
---|
84 | tspmodel->setNumCities(spinCities->value()); |
---|
85 | taskView->setModel(tspmodel); |
---|
86 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
87 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged())); |
---|
88 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
89 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) { |
---|
90 | setFileName(QCoreApplication::arguments().at(1)); |
---|
91 | setWindowModified(false); |
---|
92 | } else |
---|
93 | setFileName(); |
---|
94 | #ifdef Q_OS_WINCE |
---|
95 | taskView->resizeColumnsToContents(); |
---|
96 | taskView->resizeRowsToContents(); |
---|
97 | #endif // Q_OS_WINCE |
---|
98 | } |
---|
99 | |
---|
100 | void MainWindow::enableSolutionActions(bool enable) |
---|
101 | { |
---|
102 | actionFileSaveAsSolution->setEnabled(enable); |
---|
103 | solutionText->setEnabled(enable); |
---|
104 | if (!enable) |
---|
105 | output.clear(); |
---|
106 | } |
---|
107 | |
---|
108 | bool MainWindow::loadLanguage(QString lang) |
---|
109 | { |
---|
110 | // i18n |
---|
111 | bool ad = false; |
---|
112 | if (lang.isEmpty()) { |
---|
113 | ad = settings->value("Language","").toString().isEmpty(); |
---|
114 | lang = settings->value("Language",QLocale::system().name()).toString(); |
---|
115 | } |
---|
116 | static QTranslator *qtTranslator; |
---|
117 | if (qtTranslator) { |
---|
118 | qApp->removeTranslator(qtTranslator); |
---|
119 | delete qtTranslator; |
---|
120 | qtTranslator = NULL; |
---|
121 | } |
---|
122 | qtTranslator = new QTranslator(); |
---|
123 | static QTranslator *translator; |
---|
124 | if (translator) { |
---|
125 | qApp->removeTranslator(translator); |
---|
126 | delete translator; |
---|
127 | } |
---|
128 | translator = new QTranslator(); |
---|
129 | if (lang.compare("en") && !lang.startsWith("en_")) { |
---|
130 | // Trying to load system Qt library translation... |
---|
131 | if (qtTranslator->load("qt_" + lang,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
132 | qApp->installTranslator(qtTranslator); |
---|
133 | else |
---|
134 | // No luck. Let's try to load bundled one. |
---|
135 | if (qtTranslator->load("qt_" + lang,"i18n")) |
---|
136 | qApp->installTranslator(qtTranslator); |
---|
137 | else { |
---|
138 | delete qtTranslator; |
---|
139 | qtTranslator = NULL; |
---|
140 | } |
---|
141 | // Now let's load application translation. |
---|
142 | if (translator->load(lang,"i18n")) |
---|
143 | qApp->installTranslator(translator); |
---|
144 | else { |
---|
145 | if (!ad) |
---|
146 | QMessageBox(QMessageBox::Warning,trUtf8("Language change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); |
---|
147 | delete translator; |
---|
148 | translator = NULL; |
---|
149 | return false; |
---|
150 | } |
---|
151 | } |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | void MainWindow::initDocStyleSheet() |
---|
156 | { |
---|
157 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
158 | QColor hilight; |
---|
159 | if (color.value() < 192) |
---|
160 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
161 | else |
---|
162 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
163 | solutionText->document()->setDefaultStyleSheet("* {color: " + color.name() +";} p {margin: 0px 10px;} table {margin: 5px;} td {padding: 1px 5px;} .hasalts {color: " + hilight.name() + ";} .selected {color: #A00000; font-weight: bold;} .alternate {color: #008000; font-weight: bold;}"); |
---|
164 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
165 | } |
---|
166 | |
---|
167 | void MainWindow::setFileName(QString fileName) |
---|
168 | { |
---|
169 | this->fileName = fileName; |
---|
170 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem"))); |
---|
171 | } |
---|
172 | |
---|
173 | void MainWindow::spinCitiesValueChanged(int n) |
---|
174 | { |
---|
175 | #ifdef Q_OS_WINCE |
---|
176 | int count = tspmodel->numCities(); |
---|
177 | #endif // Q_OS_WINCE |
---|
178 | tspmodel->setNumCities(n); |
---|
179 | #ifdef Q_OS_WINCE |
---|
180 | if (n > count) |
---|
181 | for (int k = count; k < n; k++) { |
---|
182 | taskView->resizeColumnToContents(k); |
---|
183 | taskView->resizeRowToContents(k); |
---|
184 | } |
---|
185 | #endif // Q_OS_WINCE |
---|
186 | } |
---|
187 | |
---|
188 | bool MainWindow::maybeSave() |
---|
189 | { |
---|
190 | if (!isWindowModified()) |
---|
191 | return true; |
---|
192 | int res = QMessageBox(QMessageBox::Warning,trUtf8("Unsaved Changes"),trUtf8("Would you like to save changes in current task?"),QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,this).exec(); |
---|
193 | if (res == QMessageBox::Save) |
---|
194 | return saveTask(); |
---|
195 | else if (res == QMessageBox::Cancel) |
---|
196 | return false; |
---|
197 | else |
---|
198 | return true; |
---|
199 | } |
---|
200 | |
---|
201 | void MainWindow::actionFileNewTriggered() |
---|
202 | { |
---|
203 | if (!maybeSave()) |
---|
204 | return; |
---|
205 | tspmodel->clear(); |
---|
206 | #ifdef Q_OS_WINCE |
---|
207 | taskView->resizeColumnsToContents(); |
---|
208 | taskView->resizeRowsToContents(); |
---|
209 | #endif |
---|
210 | setFileName(); |
---|
211 | setWindowModified(false); |
---|
212 | tabWidget->setCurrentIndex(0); |
---|
213 | solutionText->clear(); |
---|
214 | enableSolutionActions(false); |
---|
215 | } |
---|
216 | |
---|
217 | void MainWindow::actionFileOpenTriggered() |
---|
218 | { |
---|
219 | if (!maybeSave()) |
---|
220 | return; |
---|
221 | QFileDialog od(this); |
---|
222 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
223 | od.setFileMode(QFileDialog::ExistingFile); |
---|
224 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
225 | filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
226 | filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
227 | filters.append(trUtf8("All Files") + " (*)"); |
---|
228 | od.setNameFilters(filters); |
---|
229 | if (od.exec() != QDialog::Accepted) |
---|
230 | return; |
---|
231 | QStringList files = od.selectedFiles(); |
---|
232 | if (files.empty()) |
---|
233 | return; |
---|
234 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
235 | if (!tspmodel->loadTask(files.first())) { |
---|
236 | QApplication::restoreOverrideCursor(); |
---|
237 | return; |
---|
238 | } |
---|
239 | setFileName(files.first()); |
---|
240 | #ifdef Q_OS_WINCE |
---|
241 | taskView->resizeColumnsToContents(); |
---|
242 | taskView->resizeRowsToContents(); |
---|
243 | #endif |
---|
244 | tabWidget->setCurrentIndex(0); |
---|
245 | setWindowModified(false); |
---|
246 | solutionText->clear(); |
---|
247 | enableSolutionActions(false); |
---|
248 | QApplication::restoreOverrideCursor(); |
---|
249 | } |
---|
250 | |
---|
251 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
252 | { |
---|
253 | saveTask(); |
---|
254 | } |
---|
255 | |
---|
256 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
257 | { |
---|
258 | static QString selectedFile; |
---|
259 | if (selectedFile.isEmpty()) |
---|
260 | selectedFile = "solution.html"; |
---|
261 | QFileDialog sd(this); |
---|
262 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
263 | QStringList filters(trUtf8("HTML Files") + " (*.html *.htm)"); |
---|
264 | #if QT_VERSION >= 0x040500 |
---|
265 | filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); |
---|
266 | #endif // QT_VERSION >= 0x040500 |
---|
267 | filters.append(trUtf8("All Files") + " (*)"); |
---|
268 | sd.setNameFilters(filters); |
---|
269 | sd.selectFile(selectedFile); |
---|
270 | if (sd.exec() != QDialog::Accepted) |
---|
271 | return; |
---|
272 | QStringList files = sd.selectedFiles(); |
---|
273 | if (files.empty()) |
---|
274 | return; |
---|
275 | selectedFile = files.first(); |
---|
276 | #if QT_VERSION >= 0x040500 |
---|
277 | QTextDocumentWriter dw(selectedFile); |
---|
278 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
279 | dw.setFormat("plaintext"); |
---|
280 | dw.write(solutionText->document()); |
---|
281 | #else |
---|
282 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
283 | QFile file(selectedFile); |
---|
284 | if (!file.open(QFile::WriteOnly)) |
---|
285 | return; |
---|
286 | QTextStream ts(&file); |
---|
287 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
288 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
289 | #endif // QT_VERSION >= 0x040500 |
---|
290 | } |
---|
291 | |
---|
292 | bool MainWindow::saveTask() { |
---|
293 | QFileDialog sd(this); |
---|
294 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
295 | QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
296 | filters.append(trUtf8("All Files") + " (*)"); |
---|
297 | sd.setNameFilters(filters); |
---|
298 | sd.setDefaultSuffix("tspt"); |
---|
299 | if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) |
---|
300 | sd.selectFile(fileName); |
---|
301 | else |
---|
302 | sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); |
---|
303 | if (sd.exec() != QDialog::Accepted) |
---|
304 | return false; |
---|
305 | QStringList files = sd.selectedFiles(); |
---|
306 | if (files.empty()) |
---|
307 | return false; |
---|
308 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
309 | if (tspmodel->saveTask(files.first())) { |
---|
310 | setFileName(files.first()); |
---|
311 | setWindowModified(false); |
---|
312 | QApplication::restoreOverrideCursor(); |
---|
313 | return true; |
---|
314 | } |
---|
315 | QApplication::restoreOverrideCursor(); |
---|
316 | return false; |
---|
317 | } |
---|
318 | |
---|
319 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
320 | { |
---|
321 | SettingsDialog sd(this); |
---|
322 | if (sd.exec() != QDialog::Accepted) |
---|
323 | return; |
---|
324 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
325 | initDocStyleSheet(); |
---|
326 | if (!output.isEmpty() && sd.colorChanged() && (QMessageBox(QMessageBox::Question,trUtf8("Settings Changed"),trUtf8("You have changed color settings.\nDo you wish to apply them to current solution text?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes)) { |
---|
327 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
328 | solutionText->clear(); |
---|
329 | solutionText->setHtml(output.join("")); |
---|
330 | QApplication::restoreOverrideCursor(); |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | #ifndef Q_OS_WINCE |
---|
336 | void MainWindow::actionFilePrintSetupTriggered() |
---|
337 | { |
---|
338 | QPrintDialog pd(printer,this); |
---|
339 | #if QT_VERSION >= 0x040500 |
---|
340 | // No such methods in Qt < 4.5 |
---|
341 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
342 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
343 | #endif |
---|
344 | pd.exec(); |
---|
345 | } |
---|
346 | #endif // Q_OS_WINCE |
---|
347 | |
---|
348 | void MainWindow::buttonRandomClicked() |
---|
349 | { |
---|
350 | tspmodel->randomize(); |
---|
351 | setWindowModified(true); |
---|
352 | #ifdef Q_OS_WINCE |
---|
353 | taskView->resizeColumnsToContents(); |
---|
354 | taskView->resizeRowsToContents(); |
---|
355 | #endif // Q_OS_WINCE |
---|
356 | } |
---|
357 | |
---|
358 | void MainWindow::outputMatrix(tMatrix matrix, QStringList &output, int nRow, int nCol) |
---|
359 | { |
---|
360 | int n = spinCities->value(); |
---|
361 | QString line=""; |
---|
362 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
363 | for (int r = 0; r < n; r++) { |
---|
364 | line = "<tr>"; |
---|
365 | for (int c = 0; c < n; c++) { |
---|
366 | if (matrix[r][c] == INFINITY) |
---|
367 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
368 | else if ((r == nRow) && (c == nCol)) |
---|
369 | line += "<td align=\"center\" class=\"selected\">" + QVariant(matrix[r][c]).toString() + "</td>"; |
---|
370 | else |
---|
371 | line += "<td align=\"center\">" + QVariant(matrix[r][c]).toString() + "</td>"; |
---|
372 | } |
---|
373 | line += "</tr>"; |
---|
374 | output.append(line); |
---|
375 | } |
---|
376 | output.append("</table>"); |
---|
377 | } |
---|
378 | |
---|
379 | void MainWindow::buttonSolveClicked() |
---|
380 | { |
---|
381 | tMatrix matrix; |
---|
382 | QList<double> row; |
---|
383 | int n = spinCities->value(); |
---|
384 | bool ok; |
---|
385 | for (int r = 0; r < n; r++) { |
---|
386 | row.clear(); |
---|
387 | for (int c = 0; c < n; c++) { |
---|
388 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
389 | if (!ok) { |
---|
390 | QMessageBox(QMessageBox::Critical,trUtf8("Data error"),trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); |
---|
391 | return; |
---|
392 | } |
---|
393 | } |
---|
394 | matrix.append(row); |
---|
395 | } |
---|
396 | CTSPSolver solver; |
---|
397 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
398 | if (!root) |
---|
399 | return; |
---|
400 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
401 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
402 | output.clear(); |
---|
403 | output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
404 | output.append("<p>" + trUtf8("Task:") + "</p>"); |
---|
405 | outputMatrix(matrix,output); |
---|
406 | output.append("<hr>"); |
---|
407 | output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
408 | sStep *step = root; |
---|
409 | n = 1; |
---|
410 | QString path = ""; |
---|
411 | while (n <= spinCities->value()) { |
---|
412 | if (step->prNode->prNode != NULL || (step->prNode->prNode == NULL && step->plNode->prNode == NULL)) { |
---|
413 | if (n != spinCities->value()) { |
---|
414 | output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>"); |
---|
415 | outputMatrix(step->matrix,output,step->candidate.nRow,step->candidate.nCol); |
---|
416 | if (step->alts) |
---|
417 | output.append("<p class=\"hasalts\">" + trUtf8("This step has alternate candidates for branching.") + "</p>"); |
---|
418 | output.append("<p> </p>"); |
---|
419 | } |
---|
420 | path += QString(" (%1,%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1); |
---|
421 | } |
---|
422 | if (step->prNode->prNode != NULL) |
---|
423 | step = step->prNode; |
---|
424 | else if (step->plNode->prNode != NULL) |
---|
425 | step = step->plNode; |
---|
426 | else |
---|
427 | break; |
---|
428 | } |
---|
429 | output.append("<p>" + trUtf8("Optimal path:") + "</p>"); |
---|
430 | output.append("<p> " + path + "</p>"); |
---|
431 | output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); |
---|
432 | solutionText->setHtml(output.join("")); |
---|
433 | solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
434 | enableSolutionActions(); |
---|
435 | tabWidget->setCurrentIndex(1); |
---|
436 | QApplication::restoreOverrideCursor(); |
---|
437 | } |
---|
438 | |
---|
439 | void MainWindow::actionHelpAboutTriggered() |
---|
440 | { |
---|
441 | // TODO: Normal about window :-) |
---|
442 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n"); |
---|
443 | about += QString::fromUtf8(" Version: "BUILD_VERSION"\n"); |
---|
444 | about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); |
---|
445 | about += QString::fromUtf8("Target OS: %1\n").arg(OS); |
---|
446 | about += "Qt library:\n"; |
---|
447 | about += QString::fromUtf8(" Compile time: %1\n").arg(QT_VERSION_STR); |
---|
448 | about += QString::fromUtf8(" Runtime: %1\n").arg(qVersion()); |
---|
449 | about += QString::fromUtf8("Built on %1 at %2\n").arg(__DATE__).arg(__TIME__); |
---|
450 | about += "\n"; |
---|
451 | about += "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."; |
---|
452 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
453 | } |
---|
454 | |
---|
455 | void MainWindow::loadLangList() |
---|
456 | { |
---|
457 | QSettings langinfo("i18n/languages.ini",QSettings::IniFormat); |
---|
458 | #if QT_VERSION >= 0x040500 |
---|
459 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
460 | langinfo.setIniCodec("UTF-8"); |
---|
461 | #endif |
---|
462 | QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
463 | if (!dir.exists()) |
---|
464 | return; |
---|
465 | QFileInfoList langs = dir.entryInfoList(); |
---|
466 | if (langs.size() <= 0) |
---|
467 | return; |
---|
468 | QAction *a; |
---|
469 | for (int k = 0; k < langs.size(); k++) { |
---|
470 | QFileInfo lang = langs.at(k); |
---|
471 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
472 | #if QT_VERSION >= 0x040500 |
---|
473 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
474 | #else |
---|
475 | // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings |
---|
476 | // reads .ini file as ASCII and there is no way to set file encoding. |
---|
477 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString()); |
---|
478 | #endif |
---|
479 | a->setData(lang.completeBaseName()); |
---|
480 | a->setCheckable(true); |
---|
481 | a->setActionGroup(groupSettingsLanguageList); |
---|
482 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
483 | a->setChecked(true); |
---|
484 | } |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
489 | { |
---|
490 | if (checked) { |
---|
491 | settings->remove("Language"); |
---|
492 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
493 | } else |
---|
494 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
495 | } |
---|
496 | |
---|
497 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
498 | { |
---|
499 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
500 | // We have language autodetection. It needs to be disabled to change language. |
---|
501 | if (QMessageBox(QMessageBox::Question,trUtf8("Language change"),trUtf8("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"),QMessageBox::Yes | QMessageBox::No,this).exec() == QMessageBox::Yes) { |
---|
502 | actionSettingsLanguageAutodetect->trigger(); |
---|
503 | } else |
---|
504 | return; |
---|
505 | } |
---|
506 | bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); |
---|
507 | if (loadLanguage(action->data().toString())) { |
---|
508 | settings->setValue("Language",action->data().toString()); |
---|
509 | retranslateUi(this); |
---|
510 | if (untitled) |
---|
511 | setFileName(); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
516 | { |
---|
517 | if (!maybeSave()) { |
---|
518 | event->ignore(); |
---|
519 | return; |
---|
520 | } |
---|
521 | settings->setValue("NumCities",spinCities->value()); |
---|
522 | #ifndef Q_OS_WINCE |
---|
523 | // Saving windows state |
---|
524 | if (settings->value("SavePos",false).toBool()) { |
---|
525 | settings->beginGroup("MainWindow"); |
---|
526 | settings->setValue("Maximized",isMaximized()); |
---|
527 | if (!isMaximized()) { |
---|
528 | settings->setValue("Size",size()); |
---|
529 | settings->setValue("Position",pos()); |
---|
530 | } |
---|
531 | settings->endGroup(); |
---|
532 | } |
---|
533 | #endif // Q_OS_WINCE |
---|
534 | QMainWindow::closeEvent(event); |
---|
535 | } |
---|
536 | |
---|
537 | void MainWindow::dataChanged() |
---|
538 | { |
---|
539 | setWindowModified(true); |
---|
540 | } |
---|
541 | |
---|
542 | void MainWindow::numCitiesChanged(int nCities) |
---|
543 | { |
---|
544 | spinCities->setValue(nCities); |
---|
545 | } |
---|