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 | /*! |
---|
27 | * \brief Class constructor. |
---|
28 | * \param parent Main Window parent widget. |
---|
29 | * |
---|
30 | * Initializes Main Window and creates its layout based on target OS. |
---|
31 | * Loads TSPSG settings and opens a task file if it was specified as a commandline parameter. |
---|
32 | */ |
---|
33 | MainWindow::MainWindow(QWidget *parent) |
---|
34 | : QMainWindow(parent) |
---|
35 | { |
---|
36 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
37 | loadLanguage(); |
---|
38 | setupUi(this); |
---|
39 | #ifndef Q_OS_WINCE |
---|
40 | QStatusBar *statusbar = new QStatusBar(this); |
---|
41 | statusbar->setObjectName("statusbar"); |
---|
42 | setStatusBar(statusbar); |
---|
43 | #endif // Q_OS_WINCE |
---|
44 | initDocStyleSheet(); |
---|
45 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
46 | solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); |
---|
47 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
48 | #ifdef Q_OS_WINCE |
---|
49 | // A little hack for toolbar icons to have sane size. |
---|
50 | int s = qMin(QApplication::desktop()->screenGeometry().width(),QApplication::desktop()->screenGeometry().height()); |
---|
51 | toolBar->setIconSize(QSize(s / 10,s / 10)); |
---|
52 | #endif |
---|
53 | #ifndef QT_NO_PRINTER |
---|
54 | printer = new QPrinter(QPrinter::HighResolution); |
---|
55 | #endif // QT_NO_PRINTER |
---|
56 | groupSettingsLanguageList = new QActionGroup(this); |
---|
57 | actionSettingsLanguageEnglish->setData("en"); |
---|
58 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
59 | loadLangList(); |
---|
60 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
61 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
62 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
63 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
64 | connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered())); |
---|
65 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
66 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
67 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
68 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
69 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
70 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
71 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
72 | #ifndef QT_NO_PRINTER |
---|
73 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
74 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
75 | menuFile->insertSeparator(actionFileExit); |
---|
76 | toolBar->insertAction(actionSettingsPreferences,actionFilePrint); |
---|
77 | connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered())); |
---|
78 | connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered())); |
---|
79 | #endif // QT_NO_PRINTER |
---|
80 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
81 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
82 | connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked())); |
---|
83 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
84 | setCentralWidget(tabWidget); |
---|
85 | |
---|
86 | if (settings->value("SavePos", false).toBool()) { |
---|
87 | // Loading of saved window state |
---|
88 | settings->beginGroup("MainWindow"); |
---|
89 | #ifndef Q_OS_WINCE |
---|
90 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
91 | #endif // Q_OS_WINCE |
---|
92 | restoreState(settings->value("State").toByteArray()); |
---|
93 | settings->endGroup(); |
---|
94 | #ifndef Q_OS_WINCE |
---|
95 | } else { |
---|
96 | // Centering main window |
---|
97 | QRect rect = geometry(); |
---|
98 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
99 | setGeometry(rect); |
---|
100 | #endif // Q_OS_WINCE |
---|
101 | } |
---|
102 | |
---|
103 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
104 | tspmodel = new CTSPModel(this); |
---|
105 | taskView->setModel(tspmodel); |
---|
106 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
107 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
108 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
109 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
110 | setFileName(QCoreApplication::arguments().at(1)); |
---|
111 | else { |
---|
112 | setFileName(); |
---|
113 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
114 | spinCitiesValueChanged(spinCities->value()); |
---|
115 | } |
---|
116 | setWindowModified(false); |
---|
117 | } |
---|
118 | |
---|
119 | /* Privates **********************************************************/ |
---|
120 | |
---|
121 | void MainWindow::actionFileNewTriggered() |
---|
122 | { |
---|
123 | if (!maybeSave()) |
---|
124 | return; |
---|
125 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
126 | tspmodel->clear(); |
---|
127 | setFileName(); |
---|
128 | setWindowModified(false); |
---|
129 | tabWidget->setCurrentIndex(0); |
---|
130 | solutionText->clear(); |
---|
131 | enableSolutionActions(false); |
---|
132 | QApplication::restoreOverrideCursor(); |
---|
133 | } |
---|
134 | |
---|
135 | void MainWindow::actionFileOpenTriggered() |
---|
136 | { |
---|
137 | if (!maybeSave()) |
---|
138 | return; |
---|
139 | QFileDialog od(this); |
---|
140 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
141 | od.setFileMode(QFileDialog::ExistingFile); |
---|
142 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
143 | filters.append(trUtf8("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
144 | filters.append(trUtf8("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
145 | filters.append(trUtf8("All Files") + " (*)"); |
---|
146 | od.setNameFilters(filters); |
---|
147 | if (od.exec() != QDialog::Accepted) |
---|
148 | return; |
---|
149 | QStringList files = od.selectedFiles(); |
---|
150 | if (files.empty()) |
---|
151 | return; |
---|
152 | if (!tspmodel->loadTask(files.first())) |
---|
153 | return; |
---|
154 | setFileName(files.first()); |
---|
155 | tabWidget->setCurrentIndex(0); |
---|
156 | setWindowModified(false); |
---|
157 | solutionText->clear(); |
---|
158 | enableSolutionActions(false); |
---|
159 | } |
---|
160 | |
---|
161 | void MainWindow::actionFileSaveTriggered() |
---|
162 | { |
---|
163 | if ((fileName == trUtf8("Untitled") + ".tspt") || (!fileName.endsWith(".tspt",Qt::CaseInsensitive))) |
---|
164 | saveTask(); |
---|
165 | else |
---|
166 | if (tspmodel->saveTask(fileName)) |
---|
167 | setWindowModified(false); |
---|
168 | } |
---|
169 | |
---|
170 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
171 | { |
---|
172 | saveTask(); |
---|
173 | } |
---|
174 | |
---|
175 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
176 | { |
---|
177 | static QString selectedFile; |
---|
178 | if (selectedFile.isEmpty()) |
---|
179 | #ifndef QT_NO_PRINTER |
---|
180 | selectedFile = "solution.pdf"; |
---|
181 | #else |
---|
182 | selectedFile = "solution.html"; |
---|
183 | #endif // QT_NO_PRINTER |
---|
184 | QFileDialog sd(this); |
---|
185 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
186 | QStringList filters; |
---|
187 | #ifndef QT_NO_PRINTER |
---|
188 | filters.append(trUtf8("PDF Files") + "(*.pdf)"); |
---|
189 | #endif |
---|
190 | filters.append(trUtf8("HTML Files") + " (*.html *.htm)"); |
---|
191 | #if QT_VERSION >= 0x040500 |
---|
192 | filters.append(trUtf8("OpenDocument Files") + " (*.odt)"); |
---|
193 | #endif // QT_VERSION >= 0x040500 |
---|
194 | filters.append(trUtf8("All Files") + " (*)"); |
---|
195 | sd.setNameFilters(filters); |
---|
196 | sd.selectFile(selectedFile); |
---|
197 | if (sd.exec() != QDialog::Accepted) |
---|
198 | return; |
---|
199 | QStringList files = sd.selectedFiles(); |
---|
200 | if (files.empty()) |
---|
201 | return; |
---|
202 | selectedFile = files.first(); |
---|
203 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
204 | #ifndef QT_NO_PRINTER |
---|
205 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
206 | QPrinter printer(QPrinter::HighResolution); |
---|
207 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
208 | printer.setOutputFileName(selectedFile); |
---|
209 | solutionText->document()->print(&printer); |
---|
210 | QApplication::restoreOverrideCursor(); |
---|
211 | return; |
---|
212 | } |
---|
213 | #endif |
---|
214 | #if QT_VERSION >= 0x040500 |
---|
215 | QTextDocumentWriter dw(selectedFile); |
---|
216 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
217 | dw.setFormat("plaintext"); |
---|
218 | dw.write(solutionText->document()); |
---|
219 | #else |
---|
220 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
221 | QFile file(selectedFile); |
---|
222 | if (!file.open(QFile::WriteOnly)) { |
---|
223 | QApplication::restoreOverrideCursor(); |
---|
224 | return; |
---|
225 | } |
---|
226 | QTextStream ts(&file); |
---|
227 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
228 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
229 | file.close(); |
---|
230 | #endif // QT_VERSION >= 0x040500 |
---|
231 | QApplication::restoreOverrideCursor(); |
---|
232 | } |
---|
233 | |
---|
234 | #ifndef QT_NO_PRINTER |
---|
235 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
236 | { |
---|
237 | QPrintPreviewDialog ppd(printer, this); |
---|
238 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
239 | ppd.exec(); |
---|
240 | } |
---|
241 | |
---|
242 | void MainWindow::actionFilePrintTriggered() |
---|
243 | { |
---|
244 | QPrintDialog pd(printer,this); |
---|
245 | #if QT_VERSION >= 0x040500 |
---|
246 | // No such methods in Qt < 4.5 |
---|
247 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
248 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
249 | #endif |
---|
250 | if (pd.exec() != QDialog::Accepted) |
---|
251 | return; |
---|
252 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
253 | solutionText->document()->print(printer); |
---|
254 | QApplication::restoreOverrideCursor(); |
---|
255 | } |
---|
256 | #endif // QT_NO_PRINTER |
---|
257 | |
---|
258 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
259 | { |
---|
260 | SettingsDialog sd(this); |
---|
261 | if (sd.exec() != QDialog::Accepted) |
---|
262 | return; |
---|
263 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
264 | initDocStyleSheet(); |
---|
265 | 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)) { |
---|
266 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
267 | solutionText->clear(); |
---|
268 | solutionText->setHtml(output.join("")); |
---|
269 | QApplication::restoreOverrideCursor(); |
---|
270 | } |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
275 | { |
---|
276 | if (checked) { |
---|
277 | settings->remove("Language"); |
---|
278 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
279 | } else |
---|
280 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
281 | } |
---|
282 | |
---|
283 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
284 | { |
---|
285 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
286 | // We have language autodetection. It needs to be disabled to change language. |
---|
287 | 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) { |
---|
288 | actionSettingsLanguageAutodetect->trigger(); |
---|
289 | } else |
---|
290 | return; |
---|
291 | } |
---|
292 | bool untitled = (fileName == trUtf8("Untitled") + ".tspt"); |
---|
293 | if (loadLanguage(action->data().toString())) { |
---|
294 | settings->setValue("Language",action->data().toString()); |
---|
295 | retranslateUi(this); |
---|
296 | if (untitled) |
---|
297 | setFileName(); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | void MainWindow::actionHelpAboutTriggered() |
---|
302 | { |
---|
303 | //! \todo TODO: Normal about window :-) |
---|
304 | QString about = QString::fromUtf8("<b>TSPSG: TSP Solver and Generator</b><br>"); |
---|
305 | about += QString::fromUtf8(" Version: <b>"BUILD_VERSION"</b><br>"); |
---|
306 | about += QString::fromUtf8(" Copyright: <b>© 2007-%1 Lёppa</b><br>").arg(QDate::currentDate().toString("yyyy")); |
---|
307 | about += QString::fromUtf8(" <b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b><br>"); |
---|
308 | about += "<br>"; |
---|
309 | about += QString::fromUtf8("Target OS: <b>%1</b><br>").arg(OS); |
---|
310 | about += "Qt library:<br>"; |
---|
311 | about += QString::fromUtf8(" Build time: <b>%1</b><br>").arg(QT_VERSION_STR); |
---|
312 | about += QString::fromUtf8(" Runtime: <b>%1</b><br>").arg(qVersion()); |
---|
313 | about += QString::fromUtf8("Built on <b>%1</b> at <b>%2</b><br>").arg(__DATE__).arg(__TIME__); |
---|
314 | about += QString::fromUtf8("Id: <b>"VERSIONID"</b><br>"); |
---|
315 | about += QString::fromUtf8("Algorithm: <b>%1</b><br>").arg(CTSPSolver::getVersionId()); |
---|
316 | about += "<br>"; |
---|
317 | about += "TSPSG is free software: you can redistribute it and/or modify it<br>" |
---|
318 | "under the terms of the GNU General Public License as published<br>" |
---|
319 | "by the Free Software Foundation, either version 3 of the License,<br>" |
---|
320 | "or (at your option) any later version.<br>" |
---|
321 | "<br>" |
---|
322 | "TSPSG is distributed in the hope that it will be useful, but<br>" |
---|
323 | "WITHOUT ANY WARRANTY; without even the implied warranty of<br>" |
---|
324 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>" |
---|
325 | "GNU General Public License for more details.<br>" |
---|
326 | "<br>" |
---|
327 | "You should have received a copy of the GNU General Public License<br>" |
---|
328 | "along with TSPSG. If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>."; |
---|
329 | |
---|
330 | QDialog *dlg = new QDialog(this); |
---|
331 | QLabel *lblIcon = new QLabel(dlg); |
---|
332 | QTextBrowser *txtAbout = new QTextBrowser(dlg); |
---|
333 | QVBoxLayout *vb1 = new QVBoxLayout(), |
---|
334 | *vb2 = new QVBoxLayout(); |
---|
335 | QHBoxLayout *hb = new QHBoxLayout(); |
---|
336 | QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg); |
---|
337 | |
---|
338 | lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(64, Qt::SmoothTransformation)); |
---|
339 | |
---|
340 | vb1->addWidget(lblIcon); |
---|
341 | vb1->addStretch(); |
---|
342 | |
---|
343 | // txtAbout->setTextInteractionFlags(txtAbout->textInteractionFlags() ^ Qt::TextEditable); |
---|
344 | txtAbout->setWordWrapMode(QTextOption::NoWrap); |
---|
345 | txtAbout->setOpenExternalLinks(true); |
---|
346 | txtAbout->setHtml(about); |
---|
347 | txtAbout->moveCursor(QTextCursor::Start); |
---|
348 | |
---|
349 | hb->addLayout(vb1); |
---|
350 | hb->addWidget(txtAbout); |
---|
351 | |
---|
352 | vb2->addLayout(hb); |
---|
353 | vb2->addWidget(bb); |
---|
354 | |
---|
355 | dlg->setWindowTitle(trUtf8("About TSPSG")); |
---|
356 | dlg->setLayout(vb2); |
---|
357 | |
---|
358 | connect(bb, SIGNAL(accepted()), dlg, SLOT(accept())); |
---|
359 | |
---|
360 | dlg->resize(475, 350); |
---|
361 | dlg->exec(); |
---|
362 | |
---|
363 | delete dlg; |
---|
364 | } |
---|
365 | |
---|
366 | void MainWindow::buttonBackToTaskClicked() |
---|
367 | { |
---|
368 | tabWidget->setCurrentIndex(0); |
---|
369 | } |
---|
370 | |
---|
371 | void MainWindow::buttonRandomClicked() |
---|
372 | { |
---|
373 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
374 | tspmodel->randomize(); |
---|
375 | QApplication::restoreOverrideCursor(); |
---|
376 | } |
---|
377 | |
---|
378 | void MainWindow::buttonSolveClicked() |
---|
379 | { |
---|
380 | TMatrix matrix; |
---|
381 | QList<double> row; |
---|
382 | int n = spinCities->value(); |
---|
383 | bool ok; |
---|
384 | for (int r = 0; r < n; r++) { |
---|
385 | row.clear(); |
---|
386 | for (int c = 0; c < n; c++) { |
---|
387 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
388 | if (!ok) { |
---|
389 | 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(); |
---|
390 | return; |
---|
391 | } |
---|
392 | } |
---|
393 | matrix.append(row); |
---|
394 | } |
---|
395 | CTSPSolver solver; |
---|
396 | SStep *root = solver.solve(n,matrix,this); |
---|
397 | if (!root) |
---|
398 | return; |
---|
399 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
400 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
401 | output.clear(); |
---|
402 | output.append("<p>" + trUtf8("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
403 | output.append("<p>" + trUtf8("Task:") + "</p>"); |
---|
404 | outputMatrix(matrix,output); |
---|
405 | output.append("<hr>"); |
---|
406 | output.append("<p>" + trUtf8("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
407 | SStep *step = root; |
---|
408 | n = 1; |
---|
409 | while (n <= spinCities->value()) { |
---|
410 | if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) { |
---|
411 | if (n != spinCities->value()) { |
---|
412 | output.append("<p>" + trUtf8("Step #%1").arg(n++) + "</p>"); |
---|
413 | outputMatrix(*step, output); |
---|
414 | output.append("<p>" + trUtf8("Selected candidate for branching: %1.").arg(trUtf8("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>"); |
---|
415 | if (!step->alts.empty()) { |
---|
416 | SCandidate cand; |
---|
417 | QString alts; |
---|
418 | foreach(cand, step->alts) { |
---|
419 | if (!alts.isEmpty()) |
---|
420 | alts += ", "; |
---|
421 | alts += trUtf8("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); |
---|
422 | } |
---|
423 | output.append("<p class=\"hasalts\">" + trUtf8("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>"); |
---|
424 | } |
---|
425 | output.append("<p> </p>"); |
---|
426 | } |
---|
427 | } |
---|
428 | if (step->prNode->prNode != NULL) |
---|
429 | step = step->prNode; |
---|
430 | else if (step->plNode->prNode != NULL) |
---|
431 | step = step->plNode; |
---|
432 | else |
---|
433 | break; |
---|
434 | } |
---|
435 | if (solver.isOptimal()) |
---|
436 | output.append("<p>" + trUtf8("Optimal path:") + "</p>"); |
---|
437 | else |
---|
438 | output.append("<p>" + trUtf8("Resulting path:") + "</p>"); |
---|
439 | output.append("<p> " + solver.getSortedPath() + "</p>"); |
---|
440 | output.append("<p>" + trUtf8("The price is <b>%1</b> units.").arg(step->price) + "</p>"); |
---|
441 | if (!solver.isOptimal()) { |
---|
442 | output.append("<p> </p>"); |
---|
443 | output.append("<p>" + trUtf8("<b>WARNING!!!</b><br>This result is a record, but it may not be optimal.<br>Iterations need to be continued to check whether this result is optimal or get an optimal one.") + "</p>"); |
---|
444 | } |
---|
445 | output.append("<p></p>"); |
---|
446 | solutionText->setHtml(output.join("")); |
---|
447 | solutionText->setDocumentTitle(trUtf8("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
448 | |
---|
449 | // Scrolling to the end of text. |
---|
450 | solutionText->moveCursor(QTextCursor::End); |
---|
451 | |
---|
452 | enableSolutionActions(); |
---|
453 | tabWidget->setCurrentIndex(1); |
---|
454 | QApplication::restoreOverrideCursor(); |
---|
455 | } |
---|
456 | |
---|
457 | void MainWindow::dataChanged() |
---|
458 | { |
---|
459 | setWindowModified(true); |
---|
460 | } |
---|
461 | |
---|
462 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
463 | { |
---|
464 | setWindowModified(true); |
---|
465 | if (settings->value("Autosize",true).toBool()) { |
---|
466 | for (int k = tl.row(); k <= br.row(); k++) |
---|
467 | taskView->resizeRowToContents(k); |
---|
468 | for (int k = tl.column(); k <= br.column(); k++) |
---|
469 | taskView->resizeColumnToContents(k); |
---|
470 | } |
---|
471 | } |
---|
472 | |
---|
473 | void MainWindow::numCitiesChanged(int nCities) |
---|
474 | { |
---|
475 | blockSignals(true); |
---|
476 | spinCities->setValue(nCities); |
---|
477 | blockSignals(false); |
---|
478 | } |
---|
479 | |
---|
480 | #ifndef QT_NO_PRINTER |
---|
481 | void MainWindow::printPreview(QPrinter *printer) |
---|
482 | { |
---|
483 | solutionText->print(printer); |
---|
484 | } |
---|
485 | #endif // QT_NO_PRINTER |
---|
486 | |
---|
487 | void MainWindow::spinCitiesValueChanged(int n) |
---|
488 | { |
---|
489 | int count = tspmodel->numCities(); |
---|
490 | tspmodel->setNumCities(n); |
---|
491 | if ((n > count) && settings->value("Autosize",true).toBool()) |
---|
492 | for (int k = count; k < n; k++) { |
---|
493 | taskView->resizeColumnToContents(k); |
---|
494 | taskView->resizeRowToContents(k); |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
499 | { |
---|
500 | if (!maybeSave()) { |
---|
501 | ev->ignore(); |
---|
502 | return; |
---|
503 | } |
---|
504 | settings->setValue("NumCities", spinCities->value()); |
---|
505 | |
---|
506 | // Saving Main Window state |
---|
507 | if (settings->value("SavePos", false).toBool()) { |
---|
508 | settings->beginGroup("MainWindow"); |
---|
509 | #ifndef Q_OS_WINCE |
---|
510 | settings->setValue("Geometry", saveGeometry()); |
---|
511 | #endif // Q_OS_WINCE |
---|
512 | settings->setValue("State", saveState()); |
---|
513 | settings->endGroup(); |
---|
514 | } |
---|
515 | |
---|
516 | QMainWindow::closeEvent(ev); |
---|
517 | } |
---|
518 | |
---|
519 | void MainWindow::enableSolutionActions(bool enable) |
---|
520 | { |
---|
521 | buttonSaveSolution->setEnabled(enable); |
---|
522 | actionFileSaveAsSolution->setEnabled(enable); |
---|
523 | solutionText->setEnabled(enable); |
---|
524 | if (!enable) |
---|
525 | output.clear(); |
---|
526 | #ifndef QT_NO_PRINTER |
---|
527 | actionFilePrint->setEnabled(enable); |
---|
528 | actionFilePrintPreview->setEnabled(enable); |
---|
529 | #endif // QT_NO_PRINTER |
---|
530 | } |
---|
531 | |
---|
532 | void MainWindow::initDocStyleSheet() |
---|
533 | { |
---|
534 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
535 | QColor hilight; |
---|
536 | if (color.value() < 192) |
---|
537 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
538 | else |
---|
539 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
540 | 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;}"); |
---|
541 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
542 | } |
---|
543 | |
---|
544 | void MainWindow::loadLangList() |
---|
545 | { |
---|
546 | QSettings langinfo(PATH_I18N"/languages.ini",QSettings::IniFormat); |
---|
547 | #if QT_VERSION >= 0x040500 |
---|
548 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
549 | langinfo.setIniCodec("UTF-8"); |
---|
550 | #endif |
---|
551 | QDir dir(PATH_I18N,"*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
552 | if (!dir.exists()) |
---|
553 | return; |
---|
554 | QFileInfoList langs = dir.entryInfoList(); |
---|
555 | if (langs.size() <= 0) |
---|
556 | return; |
---|
557 | QAction *a; |
---|
558 | for (int k = 0; k < langs.size(); k++) { |
---|
559 | QFileInfo lang = langs.at(k); |
---|
560 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
561 | #if QT_VERSION >= 0x040500 |
---|
562 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
563 | #else |
---|
564 | // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings |
---|
565 | // reads .ini file as ASCII and there is no way to set file encoding. |
---|
566 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString()); |
---|
567 | #endif |
---|
568 | a->setData(lang.completeBaseName()); |
---|
569 | a->setCheckable(true); |
---|
570 | a->setActionGroup(groupSettingsLanguageList); |
---|
571 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
572 | a->setChecked(true); |
---|
573 | } |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | bool MainWindow::loadLanguage(const QString &lang) |
---|
578 | { |
---|
579 | // i18n |
---|
580 | bool ad = false; |
---|
581 | QString lng = lang; |
---|
582 | if (lng.isEmpty()) { |
---|
583 | ad = settings->value("Language","").toString().isEmpty(); |
---|
584 | lng = settings->value("Language",QLocale::system().name()).toString(); |
---|
585 | } |
---|
586 | static QTranslator *qtTranslator; // Qt library translator |
---|
587 | if (qtTranslator) { |
---|
588 | qApp->removeTranslator(qtTranslator); |
---|
589 | delete qtTranslator; |
---|
590 | qtTranslator = NULL; |
---|
591 | } |
---|
592 | static QTranslator *translator; // Application translator |
---|
593 | if (translator) { |
---|
594 | qApp->removeTranslator(translator); |
---|
595 | delete translator; |
---|
596 | } |
---|
597 | translator = new QTranslator(); |
---|
598 | if ((lng.compare("en") != 0) && !lng.startsWith("en_")) { |
---|
599 | // Trying to load system Qt library translation... |
---|
600 | qtTranslator = new QTranslator(); |
---|
601 | if (qtTranslator->load("qt_" + lng,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
602 | qApp->installTranslator(qtTranslator); |
---|
603 | else { |
---|
604 | // No luck. Let's try to load a bundled one. |
---|
605 | if (qtTranslator->load("qt_" + lng,PATH_I18N)) |
---|
606 | qApp->installTranslator(qtTranslator); |
---|
607 | else { |
---|
608 | // Qt library translation unavailable |
---|
609 | delete qtTranslator; |
---|
610 | qtTranslator = NULL; |
---|
611 | } |
---|
612 | } |
---|
613 | } |
---|
614 | // Now let's load application translation. |
---|
615 | if (translator->load(lng,PATH_I18N)) |
---|
616 | qApp->installTranslator(translator); |
---|
617 | else { |
---|
618 | delete translator; |
---|
619 | translator = NULL; |
---|
620 | if ((lng.compare("en") != 0) && !lng.startsWith("en_")) { |
---|
621 | if (!ad) |
---|
622 | QMessageBox(QMessageBox::Warning,trUtf8("Language Change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); |
---|
623 | return false; |
---|
624 | } |
---|
625 | } |
---|
626 | return true; |
---|
627 | } |
---|
628 | |
---|
629 | bool MainWindow::maybeSave() |
---|
630 | { |
---|
631 | if (!isWindowModified()) |
---|
632 | return true; |
---|
633 | 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(); |
---|
634 | if (res == QMessageBox::Save) |
---|
635 | return saveTask(); |
---|
636 | else if (res == QMessageBox::Cancel) |
---|
637 | return false; |
---|
638 | else |
---|
639 | return true; |
---|
640 | } |
---|
641 | |
---|
642 | void MainWindow::outputMatrix(const TMatrix &matrix, QStringList &output) |
---|
643 | { |
---|
644 | int n = spinCities->value(); |
---|
645 | QString line=""; |
---|
646 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
647 | for (int r = 0; r < n; r++) { |
---|
648 | line = "<tr>"; |
---|
649 | for (int c = 0; c < n; c++) { |
---|
650 | if (matrix.at(r).at(c) == INFINITY) |
---|
651 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
652 | else |
---|
653 | line += "<td align=\"center\">" + QVariant(matrix.at(r).at(c)).toString() + "</td>"; |
---|
654 | } |
---|
655 | line += "</tr>"; |
---|
656 | output.append(line); |
---|
657 | } |
---|
658 | output.append("</table>"); |
---|
659 | } |
---|
660 | |
---|
661 | void MainWindow::outputMatrix(const SStep &step, QStringList &output) |
---|
662 | { |
---|
663 | int n = spinCities->value(); |
---|
664 | QString line=""; |
---|
665 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
666 | for (int r = 0; r < n; r++) { |
---|
667 | line = "<tr>"; |
---|
668 | for (int c = 0; c < n; c++) { |
---|
669 | if (step.matrix.at(r).at(c) == INFINITY) |
---|
670 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
671 | else if ((r == step.candidate.nRow) && (c == step.candidate.nCol)) |
---|
672 | line += "<td align=\"center\" class=\"selected\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
673 | else { |
---|
674 | SCandidate cand; |
---|
675 | cand.nRow = r; |
---|
676 | cand.nCol = c; |
---|
677 | if (step.alts.contains(cand)) |
---|
678 | line += "<td align=\"center\" class=\"alternate\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
679 | else |
---|
680 | line += "<td align=\"center\">" + QVariant(step.matrix.at(r).at(c)).toString() + "</td>"; |
---|
681 | } |
---|
682 | } |
---|
683 | line += "</tr>"; |
---|
684 | output.append(line); |
---|
685 | } |
---|
686 | output.append("</table>"); |
---|
687 | } |
---|
688 | |
---|
689 | bool MainWindow::saveTask() { |
---|
690 | QFileDialog sd(this); |
---|
691 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
692 | QStringList filters(trUtf8("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
693 | filters.append(trUtf8("All Files") + " (*)"); |
---|
694 | sd.setNameFilters(filters); |
---|
695 | sd.setDefaultSuffix("tspt"); |
---|
696 | if (fileName.endsWith(".tspt",Qt::CaseInsensitive)) |
---|
697 | sd.selectFile(fileName); |
---|
698 | else |
---|
699 | sd.selectFile(QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"); |
---|
700 | if (sd.exec() != QDialog::Accepted) |
---|
701 | return false; |
---|
702 | QStringList files = sd.selectedFiles(); |
---|
703 | if (files.empty()) |
---|
704 | return false; |
---|
705 | if (tspmodel->saveTask(files.first())) { |
---|
706 | setFileName(files.first()); |
---|
707 | setWindowModified(false); |
---|
708 | return true; |
---|
709 | } |
---|
710 | return false; |
---|
711 | } |
---|
712 | |
---|
713 | void MainWindow::setFileName(const QString &fileName) |
---|
714 | { |
---|
715 | this->fileName = fileName; |
---|
716 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(trUtf8("Travelling Salesman Problem"))); |
---|
717 | } |
---|