1 | /* |
---|
2 | * TSPSG: TSP Solver and Generator |
---|
3 | * Copyright (C) 2007-2010 Lёppa <contacts[at]oleksii[dot]name> |
---|
4 | * |
---|
5 | * $Id: mainwindow.cpp 99 2010-03-22 20:45:16Z 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 "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", this); |
---|
37 | |
---|
38 | loadLanguage(); |
---|
39 | setupUi(); |
---|
40 | |
---|
41 | initDocStyleSheet(); |
---|
42 | |
---|
43 | #ifndef QT_NO_PRINTER |
---|
44 | printer = new QPrinter(QPrinter::HighResolution); |
---|
45 | #endif // QT_NO_PRINTER |
---|
46 | |
---|
47 | #ifdef Q_OS_WINCE |
---|
48 | currentGeometry = QApplication::desktop()->availableGeometry(0); |
---|
49 | // We need to react to SIP show/hide and resize the window appropriately |
---|
50 | connect(QApplication::desktop(), SIGNAL(workAreaResized(int)), SLOT(desktopResized(int))); |
---|
51 | #endif // Q_OS_WINCE |
---|
52 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
53 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
54 | connect(actionFileSave,SIGNAL(triggered()),this,SLOT(actionFileSaveTriggered())); |
---|
55 | connect(actionFileSaveAsTask,SIGNAL(triggered()),this,SLOT(actionFileSaveAsTaskTriggered())); |
---|
56 | connect(actionFileSaveAsSolution,SIGNAL(triggered()),this,SLOT(actionFileSaveAsSolutionTriggered())); |
---|
57 | #ifndef QT_NO_PRINTER |
---|
58 | connect(actionFilePrintPreview,SIGNAL(triggered()),this,SLOT(actionFilePrintPreviewTriggered())); |
---|
59 | connect(actionFilePrint,SIGNAL(triggered()),this,SLOT(actionFilePrintTriggered())); |
---|
60 | #endif // QT_NO_PRINTER |
---|
61 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
62 | #ifdef Q_OS_WIN32 |
---|
63 | connect(actionHelpCheck4Updates, SIGNAL(triggered()), SLOT(actionHelpCheck4UpdatesTriggered())); |
---|
64 | #endif // Q_OS_WIN32 |
---|
65 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
66 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
67 | connect(actionHelpAboutQt,SIGNAL(triggered()),qApp,SLOT(aboutQt())); |
---|
68 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
69 | |
---|
70 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
71 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
72 | connect(buttonBackToTask,SIGNAL(clicked()),this,SLOT(buttonBackToTaskClicked())); |
---|
73 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
74 | |
---|
75 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
76 | // Centering main window |
---|
77 | QRect rect = geometry(); |
---|
78 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
79 | setGeometry(rect); |
---|
80 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
81 | // Loading of saved window state |
---|
82 | settings->beginGroup("MainWindow"); |
---|
83 | restoreGeometry(settings->value("Geometry").toByteArray()); |
---|
84 | restoreState(settings->value("State").toByteArray()); |
---|
85 | settings->endGroup(); |
---|
86 | } |
---|
87 | #else |
---|
88 | setWindowState(Qt::WindowMaximized); |
---|
89 | #endif // Q_OS_WINCE |
---|
90 | |
---|
91 | tspmodel = new CTSPModel(this); |
---|
92 | taskView->setModel(tspmodel); |
---|
93 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
94 | connect(tspmodel,SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),this,SLOT(dataChanged(const QModelIndex &, const QModelIndex &))); |
---|
95 | connect(tspmodel,SIGNAL(layoutChanged()),this,SLOT(dataChanged())); |
---|
96 | if ((QCoreApplication::arguments().count() > 1) && (tspmodel->loadTask(QCoreApplication::arguments().at(1)))) |
---|
97 | setFileName(QCoreApplication::arguments().at(1)); |
---|
98 | else { |
---|
99 | setFileName(); |
---|
100 | spinCities->setValue(settings->value("NumCities",DEF_NUM_CITIES).toInt()); |
---|
101 | spinCitiesValueChanged(spinCities->value()); |
---|
102 | } |
---|
103 | setWindowModified(false); |
---|
104 | } |
---|
105 | |
---|
106 | MainWindow::~MainWindow() |
---|
107 | { |
---|
108 | #ifndef QT_NO_PRINTER |
---|
109 | delete printer; |
---|
110 | #endif |
---|
111 | } |
---|
112 | |
---|
113 | /* Privates **********************************************************/ |
---|
114 | |
---|
115 | void MainWindow::actionFileNewTriggered() |
---|
116 | { |
---|
117 | if (!maybeSave()) |
---|
118 | return; |
---|
119 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
120 | tspmodel->clear(); |
---|
121 | setFileName(); |
---|
122 | setWindowModified(false); |
---|
123 | tabWidget->setCurrentIndex(0); |
---|
124 | solutionText->clear(); |
---|
125 | toggleSolutionActions(false); |
---|
126 | QApplication::restoreOverrideCursor(); |
---|
127 | } |
---|
128 | |
---|
129 | void MainWindow::actionFileOpenTriggered() |
---|
130 | { |
---|
131 | if (!maybeSave()) |
---|
132 | return; |
---|
133 | |
---|
134 | QStringList filters(tr("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
135 | filters.append(tr("%1 Task Files").arg("TSPSG") + " (*.tspt)"); |
---|
136 | filters.append(tr("%1 Task Files").arg("ZKomModRd") + " (*.zkt)"); |
---|
137 | filters.append(tr("All Files") + " (*)"); |
---|
138 | |
---|
139 | QString file = QFileInfo(fileName).canonicalPath(); |
---|
140 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
141 | file = QFileDialog::getOpenFileName(this, tr("Task Load"), file, filters.join(";;"), NULL, opts); |
---|
142 | if (file.isEmpty() || !QFileInfo(file).isFile()) |
---|
143 | return; |
---|
144 | if (!tspmodel->loadTask(file)) |
---|
145 | return; |
---|
146 | setFileName(file); |
---|
147 | tabWidget->setCurrentIndex(0); |
---|
148 | setWindowModified(false); |
---|
149 | solutionText->clear(); |
---|
150 | toggleSolutionActions(false); |
---|
151 | } |
---|
152 | |
---|
153 | bool MainWindow::actionFileSaveTriggered() |
---|
154 | { |
---|
155 | if ((fileName == tr("Untitled") + ".tspt") || (!fileName.endsWith(".tspt", Qt::CaseInsensitive))) |
---|
156 | return saveTask(); |
---|
157 | else |
---|
158 | if (tspmodel->saveTask(fileName)) { |
---|
159 | setWindowModified(false); |
---|
160 | return true; |
---|
161 | } else |
---|
162 | return false; |
---|
163 | } |
---|
164 | |
---|
165 | void MainWindow::actionFileSaveAsTaskTriggered() |
---|
166 | { |
---|
167 | saveTask(); |
---|
168 | } |
---|
169 | |
---|
170 | void MainWindow::actionFileSaveAsSolutionTriggered() |
---|
171 | { |
---|
172 | static QString selectedFile; |
---|
173 | if (selectedFile.isEmpty()) |
---|
174 | selectedFile = QFileInfo(fileName).canonicalPath(); |
---|
175 | else |
---|
176 | selectedFile = QFileInfo(selectedFile).canonicalPath(); |
---|
177 | if (!selectedFile.isEmpty()) |
---|
178 | selectedFile += "/"; |
---|
179 | if (fileName == tr("Untitled") + ".tspt") { |
---|
180 | #ifndef QT_NO_PRINTER |
---|
181 | selectedFile += "solution.pdf"; |
---|
182 | #else |
---|
183 | selectedFile += "solution.html"; |
---|
184 | #endif // QT_NO_PRINTER |
---|
185 | } else { |
---|
186 | #ifndef QT_NO_PRINTER |
---|
187 | selectedFile += QFileInfo(fileName).completeBaseName() + ".pdf"; |
---|
188 | #else |
---|
189 | selectedFile += QFileInfo(fileName).completeBaseName() + ".html"; |
---|
190 | #endif // QT_NO_PRINTER |
---|
191 | } |
---|
192 | |
---|
193 | QStringList filters; |
---|
194 | #ifndef QT_NO_PRINTER |
---|
195 | filters.append(tr("PDF Files") + " (*.pdf)"); |
---|
196 | #endif |
---|
197 | filters.append(tr("HTML Files") + " (*.html *.htm)"); |
---|
198 | #if QT_VERSION >= 0x040500 |
---|
199 | filters.append(tr("OpenDocument Files") + " (*.odt)"); |
---|
200 | #endif // QT_VERSION >= 0x040500 |
---|
201 | filters.append(tr("All Files") + " (*)"); |
---|
202 | |
---|
203 | QFileDialog::Options opts(settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog); |
---|
204 | QString file = QFileDialog::getSaveFileName(this, QString(), selectedFile, filters.join(";;"), NULL, opts); |
---|
205 | if (file.isEmpty()) |
---|
206 | return; |
---|
207 | selectedFile = file; |
---|
208 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
209 | #ifndef QT_NO_PRINTER |
---|
210 | if (selectedFile.endsWith(".pdf",Qt::CaseInsensitive)) { |
---|
211 | QPrinter printer(QPrinter::HighResolution); |
---|
212 | printer.setOutputFormat(QPrinter::PdfFormat); |
---|
213 | printer.setOutputFileName(selectedFile); |
---|
214 | solutionText->document()->print(&printer); |
---|
215 | QApplication::restoreOverrideCursor(); |
---|
216 | return; |
---|
217 | } |
---|
218 | #endif |
---|
219 | #if QT_VERSION >= 0x040500 |
---|
220 | QTextDocumentWriter dw(selectedFile); |
---|
221 | if (!(selectedFile.endsWith(".htm",Qt::CaseInsensitive) || selectedFile.endsWith(".html",Qt::CaseInsensitive) || selectedFile.endsWith(".odt",Qt::CaseInsensitive) || selectedFile.endsWith(".txt",Qt::CaseInsensitive))) |
---|
222 | dw.setFormat("plaintext"); |
---|
223 | if (!dw.write(solutionText->document())) |
---|
224 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(dw.device()->errorString())); |
---|
225 | #else // QT_VERSION >= 0x040500 |
---|
226 | // Qt < 4.5 has no QTextDocumentWriter class |
---|
227 | QFile file(selectedFile); |
---|
228 | if (!file.open(QFile::WriteOnly)) { |
---|
229 | QApplication::restoreOverrideCursor(); |
---|
230 | QMessageBox::critical(this, tr("Solution Save"), tr("Unable to save the solution.\nError: %1").arg(file->errorString())); |
---|
231 | return; |
---|
232 | } |
---|
233 | QTextStream ts(&file); |
---|
234 | ts.setCodec(QTextCodec::codecForName("UTF-8")); |
---|
235 | ts << solutionText->document()->toHtml("UTF-8"); |
---|
236 | file.close(); |
---|
237 | #endif // QT_VERSION >= 0x040500 |
---|
238 | QApplication::restoreOverrideCursor(); |
---|
239 | } |
---|
240 | |
---|
241 | #ifndef QT_NO_PRINTER |
---|
242 | void MainWindow::actionFilePrintPreviewTriggered() |
---|
243 | { |
---|
244 | QPrintPreviewDialog ppd(printer, this); |
---|
245 | connect(&ppd,SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *))); |
---|
246 | ppd.exec(); |
---|
247 | } |
---|
248 | |
---|
249 | void MainWindow::actionFilePrintTriggered() |
---|
250 | { |
---|
251 | QPrintDialog pd(printer,this); |
---|
252 | #if QT_VERSION >= 0x040500 |
---|
253 | // No such methods in Qt < 4.5 |
---|
254 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
255 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
256 | #endif |
---|
257 | if (pd.exec() != QDialog::Accepted) |
---|
258 | return; |
---|
259 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
260 | solutionText->document()->print(printer); |
---|
261 | QApplication::restoreOverrideCursor(); |
---|
262 | } |
---|
263 | #endif // QT_NO_PRINTER |
---|
264 | |
---|
265 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
266 | { |
---|
267 | SettingsDialog sd(this); |
---|
268 | if (sd.exec() != QDialog::Accepted) |
---|
269 | return; |
---|
270 | if (sd.colorChanged() || sd.fontChanged()) { |
---|
271 | initDocStyleSheet(); |
---|
272 | if (!solutionText->document()->isEmpty() && sd.colorChanged()) |
---|
273 | QMessageBox::information(this, tr("Settings Changed"), tr("You have changed color settings.\nThey will be applied to the next solution output.")); |
---|
274 | } |
---|
275 | if (sd.translucencyChanged() != 0) |
---|
276 | toggleTranclucency(sd.translucencyChanged() == 1); |
---|
277 | } |
---|
278 | |
---|
279 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
280 | { |
---|
281 | if (checked) { |
---|
282 | settings->remove("Language"); |
---|
283 | QMessageBox::information(this, tr("Language change"), tr("Language will be autodetected on next application start.")); |
---|
284 | } else |
---|
285 | settings->setValue("Language", groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
286 | } |
---|
287 | |
---|
288 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
289 | { |
---|
290 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
291 | // We have language autodetection. It needs to be disabled to change language. |
---|
292 | if (QMessageBox::question(this, tr("Language change"), tr("You have language autodetection turned on.\nIt needs to be off.\nDo you wish to turn it off?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { |
---|
293 | actionSettingsLanguageAutodetect->trigger(); |
---|
294 | } else |
---|
295 | return; |
---|
296 | } |
---|
297 | bool untitled = (fileName == tr("Untitled") + ".tspt"); |
---|
298 | if (loadLanguage(action->data().toString())) { |
---|
299 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
300 | settings->setValue("Language",action->data().toString()); |
---|
301 | retranslateUi(); |
---|
302 | if (untitled) |
---|
303 | setFileName(); |
---|
304 | #ifdef Q_OS_WIN32 |
---|
305 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
306 | toggleStyle(labelVariant, true); |
---|
307 | toggleStyle(labelCities, true); |
---|
308 | } |
---|
309 | #endif |
---|
310 | QApplication::restoreOverrideCursor(); |
---|
311 | QMessageBox::information(this, tr("Settings Changed"), tr("You have changed the application language.\nTo get current solution output in the new language\nyou need to re-run the solution process.")); |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | #ifdef Q_OS_WIN32 |
---|
316 | void MainWindow::actionHelpCheck4UpdatesTriggered() |
---|
317 | { |
---|
318 | if (!hasUpdater()) { |
---|
319 | QMessageBox::warning(this, tr("Unsupported Feature"), tr("Sorry, but this feature is not supported on your platform\nor support for this feature was not installed.")); |
---|
320 | return; |
---|
321 | } |
---|
322 | |
---|
323 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
324 | QProcess::execute("updater/Update.exe -name=\"TSPSG: TSP Solver and Generator\" -check=\"freeupdate\""); |
---|
325 | QApplication::restoreOverrideCursor(); |
---|
326 | } |
---|
327 | #endif // Q_OS_WIN32 |
---|
328 | |
---|
329 | void MainWindow::actionHelpAboutTriggered() |
---|
330 | { |
---|
331 | QString title; |
---|
332 | #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
---|
333 | title += QString("<b>TSPSG<br>TSP Solver and Generator</b><br>"); |
---|
334 | #else |
---|
335 | title += QString("<b>TSPSG: TSP Solver and Generator</b><br>"); |
---|
336 | #endif // Q_OS_WINCE || Q_OS_SYMBIAN |
---|
337 | title += QString("%1: <b>%2</b><br>").arg(tr("Version"), QApplication::applicationVersion()); |
---|
338 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
339 | title += QString("<b>© 2007-%1 <a href=\"http://%2/\">%3</a></b><br>").arg(QDate::currentDate().toString("yyyy"), QApplication::organizationDomain(), QApplication::organizationName()); |
---|
340 | title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sourceforge.net/</a></b>"); |
---|
341 | #else |
---|
342 | title += QString("<b><a href=\"http://tspsg.sourceforge.net/\">http://tspsg.sf.net/</a></b>"); |
---|
343 | #endif // Q_OS_WINCE && Q_OS_SYMBIAN |
---|
344 | |
---|
345 | QString about; |
---|
346 | about += QString("%1: <b>%2</b><br>").arg(tr("Target OS (ARCH)"), OS); |
---|
347 | #ifndef STATIC_BUILD |
---|
348 | about += QString("%1 (%2):<br>").arg(tr("Qt library"), tr("shared")); |
---|
349 | about += QString(" %1: <b>%2</b><br>").arg(tr("Build time"), QT_VERSION_STR); |
---|
350 | about += QString(" %1: <b>%2</b><br>").arg(tr("Runtime"), qVersion()); |
---|
351 | #else |
---|
352 | about += QString("%1: <b>%2</b> (%3)<br>").arg(tr("Qt library"), QT_VERSION_STR, tr("static")); |
---|
353 | #endif // STATIC_BUILD |
---|
354 | about += tr("Buid <b>%1</b>, built on <b>%2</b> at <b>%3</b>").arg(BUILD_NUMBER).arg(__DATE__).arg(__TIME__) + "<br>"; |
---|
355 | about += QString("%1: <b>%2</b><br>").arg(tr("Algorithm"), CTSPSolver::getVersionId()); |
---|
356 | about += "<br>"; |
---|
357 | about += tr("TSPSG is free software: you can redistribute it and/or modify it<br>" |
---|
358 | "under the terms of the GNU General Public License as published<br>" |
---|
359 | "by the Free Software Foundation, either version 3 of the License,<br>" |
---|
360 | "or (at your option) any later version.<br>" |
---|
361 | "<br>" |
---|
362 | "TSPSG is distributed in the hope that it will be useful, but<br>" |
---|
363 | "WITHOUT ANY WARRANTY; without even the implied warranty of<br>" |
---|
364 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>" |
---|
365 | "GNU General Public License for more details.<br>" |
---|
366 | "<br>" |
---|
367 | "You should have received a copy of the GNU General Public License<br>" |
---|
368 | "along with TSPSG. If not, see <a href=\"http://www.gnu.org/licenses/\">http://www.gnu.org/licenses/</a>."); |
---|
369 | |
---|
370 | QDialog *dlg = new QDialog(this); |
---|
371 | QLabel *lblIcon = new QLabel(dlg), |
---|
372 | *lblTitle = new QLabel(dlg), |
---|
373 | *lblTranslated = new QLabel(dlg); |
---|
374 | #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
---|
375 | QLabel *lblSubTitle = new QLabel(QString("<b>© 2007-%1 Oleksii \"Lёppa\" Serdiuk</b>").arg(QDate::currentDate().toString("yyyy")), dlg); |
---|
376 | #endif // Q_OS_WINCE || Q_OS_SYMBIAN |
---|
377 | QTextBrowser *txtAbout = new QTextBrowser(dlg); |
---|
378 | QVBoxLayout *vb = new QVBoxLayout(); |
---|
379 | QHBoxLayout *hb1 = new QHBoxLayout(), |
---|
380 | *hb2 = new QHBoxLayout(); |
---|
381 | QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, dlg); |
---|
382 | |
---|
383 | lblIcon->setPixmap(QPixmap(":/images/tspsg.png").scaledToWidth(logicalDpiX() * 2 / 3, Qt::SmoothTransformation)); |
---|
384 | lblIcon->setAlignment(Qt::AlignTop); |
---|
385 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
386 | lblIcon->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().windowText().color().name())); |
---|
387 | #endif |
---|
388 | |
---|
389 | lblTitle->setOpenExternalLinks(true); |
---|
390 | lblTitle->setText(title); |
---|
391 | lblTitle->setAlignment(Qt::AlignTop); |
---|
392 | lblTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); |
---|
393 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
394 | lblTitle->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name())); |
---|
395 | #endif |
---|
396 | |
---|
397 | hb1->addWidget(lblIcon); |
---|
398 | hb1->addWidget(lblTitle); |
---|
399 | |
---|
400 | txtAbout->setWordWrapMode(QTextOption::NoWrap); |
---|
401 | txtAbout->setOpenExternalLinks(true); |
---|
402 | txtAbout->setHtml(about); |
---|
403 | txtAbout->moveCursor(QTextCursor::Start); |
---|
404 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
405 | txtAbout->setStyleSheet(QString("QTextBrowser {border-color: %1; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().shadow().color().name())); |
---|
406 | #endif |
---|
407 | |
---|
408 | bb->button(QDialogButtonBox::Ok)->setCursor(QCursor(Qt::PointingHandCursor)); |
---|
409 | |
---|
410 | lblTranslated->setText(QApplication::translate("--------", "TRANSLATION", "Please, provide translator credits here.")); |
---|
411 | if (lblTranslated->text() == "TRANSLATION") |
---|
412 | lblTranslated->hide(); |
---|
413 | else { |
---|
414 | lblTranslated->setOpenExternalLinks(true); |
---|
415 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
416 | lblTranslated->setStyleSheet(QString("QLabel {background-color: %1; border-color: %2; border-width: 1px; border-style: solid; border-radius: 3px;}").arg(palette().window().color().name(), palette().shadow().color().name())); |
---|
417 | #endif |
---|
418 | hb2->addWidget(lblTranslated); |
---|
419 | } |
---|
420 | |
---|
421 | hb2->addWidget(bb); |
---|
422 | |
---|
423 | #if defined(Q_OS_WINCE) |
---|
424 | vb->setMargin(3); |
---|
425 | #endif |
---|
426 | vb->addLayout(hb1); |
---|
427 | #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
---|
428 | vb->addWidget(lblSubTitle); |
---|
429 | #endif // Q_OS_WINCE || Q_OS_SYMBIAN |
---|
430 | vb->addWidget(txtAbout); |
---|
431 | vb->addLayout(hb2); |
---|
432 | |
---|
433 | dlg->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); |
---|
434 | dlg->setWindowTitle(tr("About TSPSG")); |
---|
435 | dlg->setLayout(vb); |
---|
436 | |
---|
437 | connect(bb, SIGNAL(accepted()), dlg, SLOT(accept())); |
---|
438 | |
---|
439 | #ifdef Q_OS_WIN32 |
---|
440 | // Adding some eyecandy in Vista and 7 :-) |
---|
441 | if (QtWin::isCompositionEnabled()) { |
---|
442 | QtWin::enableBlurBehindWindow(dlg, true); |
---|
443 | } |
---|
444 | #endif // Q_OS_WIN32 |
---|
445 | |
---|
446 | dlg->resize(450, 350); |
---|
447 | |
---|
448 | dlg->exec(); |
---|
449 | |
---|
450 | delete dlg; |
---|
451 | } |
---|
452 | |
---|
453 | void MainWindow::buttonBackToTaskClicked() |
---|
454 | { |
---|
455 | tabWidget->setCurrentIndex(0); |
---|
456 | } |
---|
457 | |
---|
458 | void MainWindow::buttonRandomClicked() |
---|
459 | { |
---|
460 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
461 | tspmodel->randomize(); |
---|
462 | QApplication::restoreOverrideCursor(); |
---|
463 | } |
---|
464 | |
---|
465 | void MainWindow::buttonSolveClicked() |
---|
466 | { |
---|
467 | TMatrix matrix; |
---|
468 | QList<double> row; |
---|
469 | int n = spinCities->value(); |
---|
470 | bool ok; |
---|
471 | for (int r = 0; r < n; r++) { |
---|
472 | row.clear(); |
---|
473 | for (int c = 0; c < n; c++) { |
---|
474 | row.append(tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok)); |
---|
475 | if (!ok) { |
---|
476 | QMessageBox::critical(this, tr("Data error"), tr("Error in cell [Row %1; Column %2]: Invalid data format.").arg(r + 1).arg(c + 1)); |
---|
477 | return; |
---|
478 | } |
---|
479 | } |
---|
480 | matrix.append(row); |
---|
481 | } |
---|
482 | |
---|
483 | QProgressDialog pd(this); |
---|
484 | QProgressBar *pb = new QProgressBar(&pd); |
---|
485 | pb->setAlignment(Qt::AlignCenter); |
---|
486 | pb->setFormat(tr("%v of %1 parts found").arg(n)); |
---|
487 | pd.setBar(pb); |
---|
488 | pd.setMaximum(n * 2 + 3); |
---|
489 | pd.setMinimumDuration(1000); |
---|
490 | pd.setLabelText(tr("Calculating optimal route...")); |
---|
491 | pd.setWindowTitle(tr("Solution Progress")); |
---|
492 | pd.setWindowModality(Qt::ApplicationModal); |
---|
493 | pd.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint); |
---|
494 | pd.setValue(0); |
---|
495 | |
---|
496 | CTSPSolver solver; |
---|
497 | connect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
498 | connect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
499 | SStep *root = solver.solve(n, matrix); |
---|
500 | disconnect(&solver, SIGNAL(routePartFound(int)), &pd, SLOT(setValue(int))); |
---|
501 | disconnect(&pd, SIGNAL(canceled()), &solver, SLOT(cancel())); |
---|
502 | if (!root) { |
---|
503 | pd.reset(); |
---|
504 | if (!solver.wasCanceled()) |
---|
505 | QMessageBox::warning(this, tr("Solution Result"), tr("Unable to find a solution.\nMaybe, this task has no solution.")); |
---|
506 | return; |
---|
507 | } |
---|
508 | pb->setFormat("%p%"); |
---|
509 | pd.setLabelText(tr("Generating solution output...")); |
---|
510 | pd.setValue(n + 1); |
---|
511 | |
---|
512 | solutionText->clear(); |
---|
513 | pd.setValue(n + 2); |
---|
514 | |
---|
515 | solutionText->setDocumentTitle(tr("Solution of Variant #%1 task").arg(spinVariant->value())); |
---|
516 | solutionText->append("<p>" + tr("Variant #%1").arg(spinVariant->value()) + "</p>"); |
---|
517 | solutionText->append("<p>" + tr("Task:") + "</p>"); |
---|
518 | solutionText->append(outputMatrix(matrix)); |
---|
519 | solutionText->append("<hr><p>" + tr("Solution of Variant #%1 task").arg(spinVariant->value()) + "</p>"); |
---|
520 | SStep *step = root; |
---|
521 | n = 1; |
---|
522 | while (n <= spinCities->value()) { |
---|
523 | if (pd.wasCanceled()) { |
---|
524 | solutionText->clear(); |
---|
525 | return; |
---|
526 | } |
---|
527 | pd.setValue(spinCities->value() + 2 + n); |
---|
528 | |
---|
529 | if (step->prNode->prNode != NULL || ((step->prNode->prNode == NULL) && (step->plNode->prNode == NULL))) { |
---|
530 | if (n != spinCities->value()) { |
---|
531 | solutionText->append("<p>" + tr("Step #%1").arg(n++) + "</p>"); |
---|
532 | if (settings->value("Output/ShowMatrix", DEF_SHOW_MATRIX).toBool() && (!settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() || (settings->value("Output/UseShowMatrixLimit", DEF_USE_SHOW_MATRIX_LIMIT).toBool() && (spinCities->value() <= settings->value("Output/ShowMatrixLimit", DEF_SHOW_MATRIX_LIMIT).toInt())))) { |
---|
533 | solutionText->append(outputMatrix(*step)); |
---|
534 | } |
---|
535 | solutionText->append("<p>" + tr("Selected candidate for branching: %1.").arg(tr("(%1;%2)").arg(step->candidate.nRow + 1).arg(step->candidate.nCol + 1)) + "</p>"); |
---|
536 | if (!step->alts.empty()) { |
---|
537 | SCandidate cand; |
---|
538 | QString alts; |
---|
539 | foreach(cand, step->alts) { |
---|
540 | if (!alts.isEmpty()) |
---|
541 | alts += ", "; |
---|
542 | alts += tr("(%1;%2)").arg(cand.nRow + 1).arg(cand.nCol + 1); |
---|
543 | } |
---|
544 | solutionText->append("<p class=\"hasalts\">" + tr("%n alternate candidate(s) for branching: %1.","",step->alts.count()).arg(alts) + "</p>"); |
---|
545 | } |
---|
546 | solutionText->append("<p> </p>"); |
---|
547 | } |
---|
548 | } |
---|
549 | if (step->prNode->prNode != NULL) |
---|
550 | step = step->prNode; |
---|
551 | else if (step->plNode->prNode != NULL) |
---|
552 | step = step->plNode; |
---|
553 | else |
---|
554 | break; |
---|
555 | } |
---|
556 | pd.setValue(spinCities->value() + 2 + n); |
---|
557 | |
---|
558 | if (solver.isOptimal()) |
---|
559 | solutionText->append("<p>" + tr("Optimal path:") + "</p>"); |
---|
560 | else |
---|
561 | solutionText->append("<p>" + tr("Resulting path:") + "</p>"); |
---|
562 | solutionText->append("<p> " + solver.getSortedPath() + "</p>"); |
---|
563 | if (isInteger(step->price)) |
---|
564 | solutionText->append("<p>" + tr("The price is <b>%n</b> unit(s).", "", qRound(step->price)) + "</p>"); |
---|
565 | else |
---|
566 | solutionText->append("<p>" + tr("The price is <b>%1</b> units.").arg(step->price, 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()) + "</p>"); |
---|
567 | if (!solver.isOptimal()) { |
---|
568 | solutionText->append("<p> </p>"); |
---|
569 | solutionText->append("<p>" + tr("<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>"); |
---|
570 | } |
---|
571 | |
---|
572 | if (settings->value("Output/ScrollToEnd", DEF_SCROLL_TO_END).toBool()) { |
---|
573 | // Scrolling to the end of text. |
---|
574 | solutionText->moveCursor(QTextCursor::End); |
---|
575 | } else |
---|
576 | solutionText->moveCursor(QTextCursor::Start); |
---|
577 | |
---|
578 | pd.setLabelText(tr("Cleaning up...")); |
---|
579 | pd.setMaximum(0); |
---|
580 | pd.setCancelButton(NULL); |
---|
581 | pd.adjustSize(); |
---|
582 | QApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
---|
583 | solver.cleanup(true); |
---|
584 | toggleSolutionActions(); |
---|
585 | tabWidget->setCurrentIndex(1); |
---|
586 | } |
---|
587 | |
---|
588 | void MainWindow::dataChanged() |
---|
589 | { |
---|
590 | setWindowModified(true); |
---|
591 | } |
---|
592 | |
---|
593 | void MainWindow::dataChanged(const QModelIndex &tl, const QModelIndex &br) |
---|
594 | { |
---|
595 | setWindowModified(true); |
---|
596 | if (settings->value("Autosize", DEF_AUTOSIZE).toBool()) { |
---|
597 | for (int k = tl.row(); k <= br.row(); k++) |
---|
598 | taskView->resizeRowToContents(k); |
---|
599 | for (int k = tl.column(); k <= br.column(); k++) |
---|
600 | taskView->resizeColumnToContents(k); |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | #ifdef Q_OS_WINCE |
---|
605 | void MainWindow::changeEvent(QEvent *ev) |
---|
606 | { |
---|
607 | if ((ev->type() == QEvent::ActivationChange) && isActiveWindow()) |
---|
608 | desktopResized(0); |
---|
609 | |
---|
610 | QWidget::changeEvent(ev); |
---|
611 | } |
---|
612 | |
---|
613 | void MainWindow::desktopResized(int screen) |
---|
614 | { |
---|
615 | if ((screen != 0) || !isActiveWindow()) |
---|
616 | return; |
---|
617 | |
---|
618 | QRect availableGeometry = QApplication::desktop()->availableGeometry(0); |
---|
619 | if (currentGeometry != availableGeometry) { |
---|
620 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
621 | /*! |
---|
622 | * \hack HACK: This hack checks whether \link QDesktopWidget::availableGeometry() availableGeometry()\endlink's \c top + \c hegiht = \link QDesktopWidget::screenGeometry() screenGeometry()\endlink's \c height. |
---|
623 | * If \c true, the window gets maximized. If we used \c setGeometry() in this case, the bottom of the |
---|
624 | * window would end up being behind the soft buttons. Is this a bug in Qt or Windows Mobile? |
---|
625 | */ |
---|
626 | if ((availableGeometry.top() + availableGeometry.height()) == QApplication::desktop()->screenGeometry().height()) { |
---|
627 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
628 | } else { |
---|
629 | if (windowState() & Qt::WindowMaximized) |
---|
630 | setWindowState(windowState() ^ Qt::WindowMaximized); |
---|
631 | setGeometry(availableGeometry); |
---|
632 | } |
---|
633 | currentGeometry = availableGeometry; |
---|
634 | QApplication::restoreOverrideCursor(); |
---|
635 | } |
---|
636 | } |
---|
637 | #endif // Q_OS_WINCE |
---|
638 | |
---|
639 | void MainWindow::numCitiesChanged(int nCities) |
---|
640 | { |
---|
641 | blockSignals(true); |
---|
642 | spinCities->setValue(nCities); |
---|
643 | blockSignals(false); |
---|
644 | } |
---|
645 | |
---|
646 | #ifndef QT_NO_PRINTER |
---|
647 | void MainWindow::printPreview(QPrinter *printer) |
---|
648 | { |
---|
649 | solutionText->print(printer); |
---|
650 | } |
---|
651 | #endif // QT_NO_PRINTER |
---|
652 | |
---|
653 | void MainWindow::spinCitiesValueChanged(int n) |
---|
654 | { |
---|
655 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
---|
656 | int count = tspmodel->numCities(); |
---|
657 | tspmodel->setNumCities(n); |
---|
658 | if ((n > count) && settings->value("Autosize", DEF_AUTOSIZE).toBool()) |
---|
659 | for (int k = count; k < n; k++) { |
---|
660 | taskView->resizeColumnToContents(k); |
---|
661 | taskView->resizeRowToContents(k); |
---|
662 | } |
---|
663 | QApplication::restoreOverrideCursor(); |
---|
664 | } |
---|
665 | |
---|
666 | void MainWindow::closeEvent(QCloseEvent *ev) |
---|
667 | { |
---|
668 | if (!maybeSave()) { |
---|
669 | ev->ignore(); |
---|
670 | return; |
---|
671 | } |
---|
672 | if (!settings->value("SettingsReset", false).toBool()) { |
---|
673 | settings->setValue("NumCities", spinCities->value()); |
---|
674 | |
---|
675 | // Saving Main Window state |
---|
676 | if (settings->value("SavePos", DEF_SAVEPOS).toBool()) { |
---|
677 | settings->beginGroup("MainWindow"); |
---|
678 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
679 | settings->setValue("Geometry", saveGeometry()); |
---|
680 | #endif // Q_OS_WINCE |
---|
681 | settings->setValue("State", saveState()); |
---|
682 | settings->endGroup(); |
---|
683 | } |
---|
684 | } else { |
---|
685 | settings->remove("SettingsReset"); |
---|
686 | } |
---|
687 | |
---|
688 | QMainWindow::closeEvent(ev); |
---|
689 | } |
---|
690 | |
---|
691 | bool MainWindow::hasUpdater() const |
---|
692 | { |
---|
693 | #ifdef Q_OS_WIN32 |
---|
694 | return QFile::exists("updater/Update.exe"); |
---|
695 | #else // Q_OS_WIN32 |
---|
696 | return false; |
---|
697 | #endif // Q_OS_WIN32 |
---|
698 | } |
---|
699 | |
---|
700 | void MainWindow::initDocStyleSheet() |
---|
701 | { |
---|
702 | QColor color = settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>(); |
---|
703 | QColor hilight; |
---|
704 | if (color.value() < 192) |
---|
705 | hilight.setHsv(color.hue(),color.saturation(),127 + qRound(color.value() / 2)); |
---|
706 | else |
---|
707 | hilight.setHsv(color.hue(),color.saturation(),color.value() / 2); |
---|
708 | 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;}"); |
---|
709 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
710 | } |
---|
711 | |
---|
712 | void MainWindow::loadLangList() |
---|
713 | { |
---|
714 | QDir dir(PATH_L10N, "tspsg_*.qm", QDir::Name | QDir::IgnoreCase, QDir::Files); |
---|
715 | if (!dir.exists()) |
---|
716 | return; |
---|
717 | QFileInfoList langs = dir.entryInfoList(); |
---|
718 | if (langs.size() <= 0) |
---|
719 | return; |
---|
720 | QAction *a; |
---|
721 | QTranslator t; |
---|
722 | QString name; |
---|
723 | for (int k = 0; k < langs.size(); k++) { |
---|
724 | QFileInfo lang = langs.at(k); |
---|
725 | if (lang.completeBaseName().compare("tspsg_en", Qt::CaseInsensitive) && t.load(lang.completeBaseName(), PATH_L10N)) { |
---|
726 | name = t.translate("--------", "LANGNAME", "Please, provide a native name of your translation language here."); |
---|
727 | a = menuSettingsLanguage->addAction(name); |
---|
728 | a->setStatusTip(QString("Set application language to %1").arg(name)); |
---|
729 | a->setData(lang.completeBaseName().mid(6)); |
---|
730 | a->setCheckable(true); |
---|
731 | a->setActionGroup(groupSettingsLanguageList); |
---|
732 | if (settings->value("Language", QLocale::system().name()).toString().startsWith(lang.completeBaseName().mid(6))) |
---|
733 | a->setChecked(true); |
---|
734 | } |
---|
735 | } |
---|
736 | } |
---|
737 | |
---|
738 | bool MainWindow::loadLanguage(const QString &lang) |
---|
739 | { |
---|
740 | // i18n |
---|
741 | bool ad = false; |
---|
742 | QString lng = lang; |
---|
743 | if (lng.isEmpty()) { |
---|
744 | ad = settings->value("Language", "").toString().isEmpty(); |
---|
745 | lng = settings->value("Language", QLocale::system().name()).toString(); |
---|
746 | } |
---|
747 | static QTranslator *qtTranslator; // Qt library translator |
---|
748 | if (qtTranslator) { |
---|
749 | qApp->removeTranslator(qtTranslator); |
---|
750 | delete qtTranslator; |
---|
751 | qtTranslator = NULL; |
---|
752 | } |
---|
753 | static QTranslator *translator; // Application translator |
---|
754 | if (translator) { |
---|
755 | qApp->removeTranslator(translator); |
---|
756 | delete translator; |
---|
757 | translator = NULL; |
---|
758 | } |
---|
759 | |
---|
760 | if (lng == "en") |
---|
761 | return true; |
---|
762 | |
---|
763 | // Trying to load system Qt library translation... |
---|
764 | qtTranslator = new QTranslator(this); |
---|
765 | if (qtTranslator->load("qt_" + lng, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
766 | qApp->installTranslator(qtTranslator); |
---|
767 | else { |
---|
768 | // No luck. Let's try to load a bundled one. |
---|
769 | if (qtTranslator->load("qt_" + lng, PATH_L10N)) |
---|
770 | qApp->installTranslator(qtTranslator); |
---|
771 | else { |
---|
772 | // Qt library translation unavailable |
---|
773 | delete qtTranslator; |
---|
774 | qtTranslator = NULL; |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | // Now let's load application translation. |
---|
779 | translator = new QTranslator(this); |
---|
780 | if (translator->load("tspsg_" + lng, PATH_L10N)) |
---|
781 | qApp->installTranslator(translator); |
---|
782 | else { |
---|
783 | delete translator; |
---|
784 | translator = NULL; |
---|
785 | if (!ad) { |
---|
786 | settings->remove("Language"); |
---|
787 | QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor)); |
---|
788 | if (isVisible()) |
---|
789 | QMessageBox::warning(this, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); |
---|
790 | else |
---|
791 | QMessageBox::warning(NULL, tr("Language Change"), tr("Unable to load the translation language.\nFalling back to autodetection.")); |
---|
792 | QApplication::restoreOverrideCursor(); |
---|
793 | } |
---|
794 | return false; |
---|
795 | } |
---|
796 | return true; |
---|
797 | } |
---|
798 | |
---|
799 | bool MainWindow::maybeSave() |
---|
800 | { |
---|
801 | if (!isWindowModified()) |
---|
802 | return true; |
---|
803 | int res = QMessageBox::warning(this, tr("Unsaved Changes"), tr("Would you like to save changes in the current task?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); |
---|
804 | if (res == QMessageBox::Save) |
---|
805 | return actionFileSaveTriggered(); |
---|
806 | else if (res == QMessageBox::Cancel) |
---|
807 | return false; |
---|
808 | else |
---|
809 | return true; |
---|
810 | } |
---|
811 | |
---|
812 | QString MainWindow::outputMatrix(const TMatrix &matrix) const |
---|
813 | { |
---|
814 | int n = spinCities->value(); |
---|
815 | QString output(""), line; |
---|
816 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
817 | for (int r = 0; r < n; r++) { |
---|
818 | line = "<tr>"; |
---|
819 | for (int c = 0; c < n; c++) { |
---|
820 | if (matrix.at(r).at(c) == INFINITY) |
---|
821 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
822 | else |
---|
823 | line += isInteger(matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
824 | } |
---|
825 | line += "</tr>"; |
---|
826 | output.append(line); |
---|
827 | } |
---|
828 | output.append("</table>"); |
---|
829 | return output; |
---|
830 | } |
---|
831 | |
---|
832 | QString MainWindow::outputMatrix(const SStep &step) const |
---|
833 | { |
---|
834 | int n = spinCities->value(); |
---|
835 | QString output(""), line; |
---|
836 | output.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"); |
---|
837 | for (int r = 0; r < n; r++) { |
---|
838 | line = "<tr>"; |
---|
839 | for (int c = 0; c < n; c++) { |
---|
840 | if (step.matrix.at(r).at(c) == INFINITY) |
---|
841 | line += "<td align=\"center\">"INFSTR"</td>"; |
---|
842 | else if ((r == step.candidate.nRow) && (c == step.candidate.nCol)) |
---|
843 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"selected\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
844 | else { |
---|
845 | SCandidate cand; |
---|
846 | cand.nRow = r; |
---|
847 | cand.nCol = c; |
---|
848 | if (step.alts.contains(cand)) |
---|
849 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\" class=\"alternate\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
850 | else |
---|
851 | line += isInteger(step.matrix.at(r).at(c)) ? QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c)) : QString("<td align=\"center\">%1</td>").arg(step.matrix.at(r).at(c), 0, 'f', settings->value("Task/FractionalAccuracy", DEF_FRACTIONAL_ACCURACY).toInt()); |
---|
852 | } |
---|
853 | } |
---|
854 | line += "</tr>"; |
---|
855 | output.append(line); |
---|
856 | } |
---|
857 | output.append("</table>"); |
---|
858 | return output; |
---|
859 | } |
---|
860 | |
---|
861 | void MainWindow::retranslateUi(bool all) |
---|
862 | { |
---|
863 | if (all) |
---|
864 | Ui::MainWindow::retranslateUi(this); |
---|
865 | |
---|
866 | actionSettingsLanguageEnglish->setStatusTip(tr("Set application language to %1").arg("English")); |
---|
867 | |
---|
868 | #ifndef QT_NO_PRINTER |
---|
869 | actionFilePrintPreview->setText(QApplication::translate("MainWindow", "P&rint Preview...", 0, QApplication::UnicodeUTF8)); |
---|
870 | #ifndef QT_NO_TOOLTIP |
---|
871 | actionFilePrintPreview->setToolTip(QApplication::translate("MainWindow", "Preview solution results", 0, QApplication::UnicodeUTF8)); |
---|
872 | #endif // QT_NO_TOOLTIP |
---|
873 | #ifndef QT_NO_STATUSTIP |
---|
874 | actionFilePrintPreview->setStatusTip(QApplication::translate("MainWindow", "Preview current solution results before printing", 0, QApplication::UnicodeUTF8)); |
---|
875 | #endif // QT_NO_STATUSTIP |
---|
876 | |
---|
877 | actionFilePrint->setText(QApplication::translate("MainWindow", "&Print...", 0, QApplication::UnicodeUTF8)); |
---|
878 | #ifndef QT_NO_TOOLTIP |
---|
879 | actionFilePrint->setToolTip(QApplication::translate("MainWindow", "Print solution", 0, QApplication::UnicodeUTF8)); |
---|
880 | #endif // QT_NO_TOOLTIP |
---|
881 | #ifndef QT_NO_STATUSTIP |
---|
882 | actionFilePrint->setStatusTip(QApplication::translate("MainWindow", "Print current solution results", 0, QApplication::UnicodeUTF8)); |
---|
883 | #endif // QT_NO_STATUSTIP |
---|
884 | actionFilePrint->setShortcut(QApplication::translate("MainWindow", "Ctrl+P", 0, QApplication::UnicodeUTF8)); |
---|
885 | #endif // QT_NO_PRINTER |
---|
886 | #ifdef Q_OS_WIN32 |
---|
887 | actionHelpCheck4Updates->setText(tr("Check for &Updates...")); |
---|
888 | #ifndef QT_NO_TOOLTIP |
---|
889 | actionHelpCheck4Updates->setToolTip(tr("Check for %1 updates").arg(QApplication::applicationName())); |
---|
890 | #endif // QT_NO_TOOLTIP |
---|
891 | #ifndef QT_NO_STATUSTIP |
---|
892 | actionHelpCheck4Updates->setStatusTip(tr("Check for %1 updates").arg(QApplication::applicationName())); |
---|
893 | #endif // QT_NO_STATUSTIP |
---|
894 | #endif // Q_OS_WIN32 |
---|
895 | } |
---|
896 | |
---|
897 | bool MainWindow::saveTask() { |
---|
898 | QStringList filters(tr("%1 Task File").arg("TSPSG") + " (*.tspt)"); |
---|
899 | filters.append(tr("All Files") + " (*)"); |
---|
900 | QString file; |
---|
901 | if (fileName.endsWith(".tspt", Qt::CaseInsensitive)) |
---|
902 | file = fileName; |
---|
903 | else |
---|
904 | file = QFileInfo(fileName).canonicalPath() + "/" + QFileInfo(fileName).completeBaseName() + ".tspt"; |
---|
905 | |
---|
906 | QFileDialog::Options opts = settings->value("UseNativeDialogs", DEF_USE_NATIVE_DIALOGS).toBool() ? QFileDialog::Options() : QFileDialog::DontUseNativeDialog; |
---|
907 | file = QFileDialog::getSaveFileName(this, tr("Task Save"), file, filters.join(";;"), NULL, opts); |
---|
908 | |
---|
909 | if (file.isEmpty()) |
---|
910 | return false; |
---|
911 | if (tspmodel->saveTask(file)) { |
---|
912 | setFileName(file); |
---|
913 | setWindowModified(false); |
---|
914 | return true; |
---|
915 | } |
---|
916 | return false; |
---|
917 | } |
---|
918 | |
---|
919 | void MainWindow::setFileName(const QString &fileName) |
---|
920 | { |
---|
921 | this->fileName = fileName; |
---|
922 | setWindowTitle(QString("%1[*] - %2").arg(QFileInfo(fileName).completeBaseName()).arg(tr("Travelling Salesman Problem"))); |
---|
923 | } |
---|
924 | |
---|
925 | void MainWindow::setupUi() |
---|
926 | { |
---|
927 | Ui::MainWindow::setupUi(this); |
---|
928 | |
---|
929 | #if QT_VERSION >= 0x040600 |
---|
930 | setToolButtonStyle(Qt::ToolButtonFollowStyle); |
---|
931 | #endif |
---|
932 | |
---|
933 | #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
---|
934 | QStatusBar *statusbar = new QStatusBar(this); |
---|
935 | statusbar->setObjectName("statusbar"); |
---|
936 | setStatusBar(statusbar); |
---|
937 | #endif // Q_OS_WINCE |
---|
938 | |
---|
939 | #ifdef Q_OS_WINCE |
---|
940 | menuBar()->setDefaultAction(menuFile->menuAction()); |
---|
941 | |
---|
942 | QScrollArea *scrollArea = new QScrollArea(this); |
---|
943 | scrollArea->setFrameShape(QFrame::NoFrame); |
---|
944 | scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
---|
945 | scrollArea->setWidgetResizable(true); |
---|
946 | scrollArea->setWidget(tabWidget); |
---|
947 | setCentralWidget(scrollArea); |
---|
948 | #else |
---|
949 | setCentralWidget(tabWidget); |
---|
950 | #endif // Q_OS_WINCE |
---|
951 | |
---|
952 | //! \hack HACK: A little hack for toolbar icons to have a sane size. |
---|
953 | #ifdef Q_OS_WINCE |
---|
954 | toolBar->setIconSize(QSize(logicalDpiX() / 4, logicalDpiY() / 4)); |
---|
955 | #elif defined(Q_OS_SYMBIAN) |
---|
956 | toolBar->setIconSize(QSize(logicalDpiX() / 5, logicalDpiY() / 5)); |
---|
957 | #endif // Q_OS_WINCE |
---|
958 | |
---|
959 | solutionText->document()->setDefaultFont(settings->value("Output/Font",QFont(DEF_FONT_FAMILY,DEF_FONT_SIZE)).value<QFont>()); |
---|
960 | solutionText->setTextColor(settings->value("Output/Color",DEF_FONT_COLOR).value<QColor>()); |
---|
961 | solutionText->setWordWrapMode(QTextOption::WordWrap); |
---|
962 | |
---|
963 | #ifndef QT_NO_PRINTER |
---|
964 | actionFilePrintPreview = new QAction(this); |
---|
965 | actionFilePrintPreview->setObjectName("actionFilePrintPreview"); |
---|
966 | actionFilePrintPreview->setEnabled(false); |
---|
967 | actionFilePrintPreview->setIcon(QIcon(":/images/icons/document_preview.png")); |
---|
968 | |
---|
969 | actionFilePrint = new QAction(this); |
---|
970 | actionFilePrint->setObjectName("actionFilePrint"); |
---|
971 | actionFilePrint->setEnabled(false); |
---|
972 | actionFilePrint->setIcon(QIcon(":/images/icons/fileprint.png")); |
---|
973 | |
---|
974 | menuFile->insertAction(actionFileExit,actionFilePrintPreview); |
---|
975 | menuFile->insertAction(actionFileExit,actionFilePrint); |
---|
976 | menuFile->insertSeparator(actionFileExit); |
---|
977 | |
---|
978 | toolBar->insertAction(actionSettingsPreferences,actionFilePrint); |
---|
979 | #endif // QT_NO_PRINTER |
---|
980 | #ifdef Q_OS_WIN32 |
---|
981 | actionHelpCheck4Updates = new QAction(this); |
---|
982 | actionHelpCheck4Updates->setEnabled(hasUpdater()); |
---|
983 | menuHelp->insertAction(actionHelpAboutQt, actionHelpCheck4Updates); |
---|
984 | menuHelp->insertSeparator(actionHelpAboutQt); |
---|
985 | #endif // Q_OS_WIN32 |
---|
986 | |
---|
987 | groupSettingsLanguageList = new QActionGroup(this); |
---|
988 | actionSettingsLanguageEnglish->setData("en"); |
---|
989 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
990 | loadLangList(); |
---|
991 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language", "").toString().isEmpty()); |
---|
992 | |
---|
993 | spinCities->setMaximum(MAX_NUM_CITIES); |
---|
994 | |
---|
995 | retranslateUi(false); |
---|
996 | |
---|
997 | #ifdef Q_OS_WIN32 |
---|
998 | // Adding some eyecandy in Vista and 7 :-) |
---|
999 | if (QtWin::isCompositionEnabled() && settings->value("UseTranslucency", DEF_USE_TRANSLUCENCY).toBool()) { |
---|
1000 | toggleTranclucency(true); |
---|
1001 | } |
---|
1002 | #endif // Q_OS_WIN32 |
---|
1003 | } |
---|
1004 | |
---|
1005 | void MainWindow::toggleSolutionActions(bool enable) |
---|
1006 | { |
---|
1007 | buttonSaveSolution->setEnabled(enable); |
---|
1008 | actionFileSaveAsSolution->setEnabled(enable); |
---|
1009 | solutionText->setEnabled(enable); |
---|
1010 | #ifndef QT_NO_PRINTER |
---|
1011 | actionFilePrint->setEnabled(enable); |
---|
1012 | actionFilePrintPreview->setEnabled(enable); |
---|
1013 | #endif // QT_NO_PRINTER |
---|
1014 | } |
---|
1015 | |
---|
1016 | void MainWindow::toggleTranclucency(bool enable) |
---|
1017 | { |
---|
1018 | #ifdef Q_OS_WIN32 |
---|
1019 | toggleStyle(labelVariant, enable); |
---|
1020 | toggleStyle(labelCities, enable); |
---|
1021 | toggleStyle(statusBar(), enable); |
---|
1022 | tabWidget->setDocumentMode(enable); |
---|
1023 | QtWin::enableBlurBehindWindow(this, enable); |
---|
1024 | #else |
---|
1025 | Q_UNUSED(enable); |
---|
1026 | #endif // Q_OS_WIN32 |
---|
1027 | } |
---|