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