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