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