[1] | 1 | /* |
---|
[6] | 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 31 2009-06-29 23:02:31Z 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 | |
---|
| 26 | MainWindow::MainWindow(QWidget *parent) |
---|
[21] | 27 | : QMainWindow(parent) |
---|
[1] | 28 | { |
---|
[29] | 29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
| 30 | loadLanguage(); |
---|
[1] | 31 | setupUi(this); |
---|
[29] | 32 | #ifndef Q_OS_WINCE |
---|
| 33 | printer = new QPrinter(); |
---|
| 34 | #endif // Q_OS_WINCE |
---|
| 35 | groupSettingsLanguageList = new QActionGroup(this); |
---|
[30] | 36 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 37 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
[29] | 38 | loadLangList(); |
---|
[23] | 39 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
[29] | 40 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
| 41 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
[31] | 42 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
| 43 | connect(actionFileSaveTask,SIGNAL(triggered()),this,SLOT(actionFileSaveTaskTriggered())); |
---|
[29] | 44 | connect(actionSettingsPreferences,SIGNAL(triggered()),this,SLOT(actionSettingsPreferencesTriggered())); |
---|
| 45 | connect(actionSettingsLanguageAutodetect,SIGNAL(triggered(bool)),this,SLOT(actionSettingsLanguageAutodetectTriggered(bool))); |
---|
| 46 | connect(groupSettingsLanguageList,SIGNAL(triggered(QAction *)),this,SLOT(groupSettingsLanguageListTriggered(QAction *))); |
---|
| 47 | connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(actionHelpAboutTriggered())); |
---|
[17] | 48 | #ifndef Q_OS_WINCE |
---|
[29] | 49 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered())); |
---|
[17] | 50 | #endif // Q_OS_WINCE |
---|
[29] | 51 | connect(buttonSolve,SIGNAL(clicked()),this,SLOT(buttonSolveClicked())); |
---|
| 52 | connect(buttonRandom,SIGNAL(clicked()),this,SLOT(buttonRandomClicked())); |
---|
| 53 | connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(spinCitiesValueChanged(int))); |
---|
[17] | 54 | QRect rect = geometry(); |
---|
| 55 | #ifdef Q_OS_WINCE |
---|
| 56 | // HACK: Fix for all tabWidget elements becoming "unclickable" if making it central widget. |
---|
| 57 | rect.setSize(QApplication::desktop()->availableGeometry().size()); |
---|
| 58 | rect.setHeight(rect.height() - (QApplication::desktop()->screenGeometry().height() - QApplication::desktop()->availableGeometry().height())); |
---|
| 59 | tabWidget->resize(rect.width(),rect.height() - toolBar->size().height()); |
---|
| 60 | #else |
---|
[23] | 61 | if (settings->value("SavePos",false).toBool()) { |
---|
[21] | 62 | // Loading of saved window state |
---|
[23] | 63 | settings->beginGroup("MainWindow"); |
---|
| 64 | resize(settings->value("Size",size()).toSize()); |
---|
| 65 | move(settings->value("Position",pos()).toPoint()); |
---|
| 66 | if (settings->value("Maximized",false).toBool()) |
---|
[21] | 67 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
[23] | 68 | settings->endGroup(); |
---|
[21] | 69 | } else { |
---|
| 70 | // Centering main window |
---|
| 71 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 72 | setGeometry(rect); |
---|
| 73 | } |
---|
[17] | 74 | #endif // Q_OS_WINCE |
---|
[11] | 75 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[15] | 76 | tspmodel = new CTSPModel(); |
---|
| 77 | tspmodel->setNumCities(spinCities->value()); |
---|
| 78 | taskView->setModel(tspmodel); |
---|
[31] | 79 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
[21] | 80 | #ifdef Q_OS_WINCE |
---|
| 81 | taskView->resizeColumnsToContents(); |
---|
| 82 | taskView->resizeRowsToContents(); |
---|
| 83 | #endif // Q_OS_WINCE |
---|
[6] | 84 | } |
---|
| 85 | |
---|
[30] | 86 | bool MainWindow::loadLanguage(QString lang) |
---|
[4] | 87 | { |
---|
[29] | 88 | // i18n |
---|
[30] | 89 | bool ad = false; |
---|
| 90 | if (lang.isEmpty()) { |
---|
| 91 | ad = settings->value("Language","").toString().isEmpty(); |
---|
| 92 | lang = settings->value("Language",QLocale::system().name()).toString(); |
---|
| 93 | } |
---|
[29] | 94 | static QTranslator *qtTranslator; |
---|
| 95 | if (qtTranslator) { |
---|
| 96 | qApp->removeTranslator(qtTranslator); |
---|
| 97 | delete qtTranslator; |
---|
| 98 | qtTranslator = NULL; |
---|
| 99 | } |
---|
| 100 | qtTranslator = new QTranslator(); |
---|
[30] | 101 | static QTranslator *translator; |
---|
| 102 | if (translator) { |
---|
| 103 | qApp->removeTranslator(translator); |
---|
| 104 | delete translator; |
---|
| 105 | } |
---|
| 106 | translator = new QTranslator(); |
---|
[29] | 107 | if (lang.compare("en") && !lang.startsWith("en_")) { |
---|
| 108 | // Trying to load system Qt library translation... |
---|
| 109 | if (qtTranslator->load("qt_" + lang,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) |
---|
| 110 | qApp->installTranslator(qtTranslator); |
---|
| 111 | else |
---|
| 112 | // No luck. Let's try to load bundled one. |
---|
| 113 | if (qtTranslator->load("qt_" + lang,"i18n")) |
---|
| 114 | qApp->installTranslator(qtTranslator); |
---|
| 115 | else { |
---|
| 116 | delete qtTranslator; |
---|
| 117 | qtTranslator = NULL; |
---|
| 118 | } |
---|
[30] | 119 | // Now let's load application translation. |
---|
[29] | 120 | if (translator->load(lang,"i18n")) |
---|
| 121 | qApp->installTranslator(translator); |
---|
| 122 | else { |
---|
| 123 | if (!ad) |
---|
| 124 | QMessageBox(QMessageBox::Warning,trUtf8("Language change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); |
---|
| 125 | delete translator; |
---|
| 126 | translator = NULL; |
---|
| 127 | return false; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | return true; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void MainWindow::spinCitiesValueChanged(int n) |
---|
| 134 | { |
---|
[21] | 135 | #ifdef Q_OS_WINCE |
---|
| 136 | int count = tspmodel->numCities(); |
---|
| 137 | #endif // Q_OS_WINCE |
---|
[15] | 138 | tspmodel->setNumCities(n); |
---|
[21] | 139 | #ifdef Q_OS_WINCE |
---|
| 140 | if (n > count) |
---|
| 141 | for (int k = count; k < n; k++) { |
---|
| 142 | taskView->resizeColumnToContents(k); |
---|
| 143 | taskView->resizeRowToContents(k); |
---|
| 144 | } |
---|
| 145 | #endif // Q_OS_WINCE |
---|
[1] | 146 | } |
---|
| 147 | |
---|
[29] | 148 | |
---|
| 149 | void MainWindow::actionFileNewTriggered() |
---|
[1] | 150 | { |
---|
[29] | 151 | tspmodel->clear(); |
---|
| 152 | } |
---|
| 153 | |
---|
[31] | 154 | void MainWindow::actionFileOpenTriggered() |
---|
| 155 | { |
---|
| 156 | QFileDialog od(this); |
---|
| 157 | od.setAcceptMode(QFileDialog::AcceptOpen); |
---|
| 158 | od.setFileMode(QFileDialog::ExistingFile); |
---|
| 159 | QStringList filters(trUtf8("All Supported Formats") + " (*.tspt *.zkt)"); |
---|
| 160 | filters.append(QString(trUtf8("%1 Task Files")).arg("TSPSG") + " (*.tspt)"); |
---|
| 161 | filters.append(QString(trUtf8("%1 Task Files")).arg("ZKomModRd") + " (*.zkt)"); |
---|
| 162 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 163 | od.setNameFilters(filters); |
---|
| 164 | if (od.exec() != QDialog::Accepted) |
---|
| 165 | return; |
---|
| 166 | QStringList files = od.selectedFiles(); |
---|
| 167 | if (files.size() < 1) |
---|
| 168 | return; |
---|
| 169 | tspmodel->loadTask(files.first()); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void MainWindow::actionFileSaveTaskTriggered() |
---|
| 173 | { |
---|
| 174 | QFileDialog sd(this); |
---|
| 175 | sd.setAcceptMode(QFileDialog::AcceptSave); |
---|
| 176 | QStringList filters(QString(trUtf8("%1 Task File")).arg("TSPSG") + " (*.tspt)"); |
---|
| 177 | filters.append(trUtf8("All Files") + " (*)"); |
---|
| 178 | sd.setNameFilters(filters); |
---|
| 179 | sd.setDefaultSuffix("tspt"); |
---|
| 180 | if (sd.exec() != QDialog::Accepted) |
---|
| 181 | return; |
---|
| 182 | QStringList files = sd.selectedFiles(); |
---|
| 183 | if (files.size() < 1) |
---|
| 184 | return; |
---|
| 185 | tspmodel->saveTask(files.first()); |
---|
| 186 | } |
---|
| 187 | |
---|
[29] | 188 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
| 189 | { |
---|
[1] | 190 | SettingsDialog sd(this); |
---|
[21] | 191 | sd.exec(); |
---|
[1] | 192 | } |
---|
[6] | 193 | |
---|
[17] | 194 | #ifndef Q_OS_WINCE |
---|
[29] | 195 | void MainWindow::actionFilePrintSetupTriggered() |
---|
[17] | 196 | { |
---|
[29] | 197 | QPrintDialog pd(printer,this); |
---|
| 198 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 199 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
[17] | 200 | pd.exec(); |
---|
| 201 | } |
---|
| 202 | #endif // Q_OS_WINCE |
---|
| 203 | |
---|
[29] | 204 | void MainWindow::buttonRandomClicked() |
---|
[6] | 205 | { |
---|
[15] | 206 | tspmodel->randomize(); |
---|
[21] | 207 | #ifdef Q_OS_WINCE |
---|
| 208 | taskView->resizeColumnsToContents(); |
---|
| 209 | taskView->resizeRowsToContents(); |
---|
| 210 | #endif // Q_OS_WINCE |
---|
[6] | 211 | } |
---|
| 212 | |
---|
[29] | 213 | void MainWindow::buttonSolveClicked() |
---|
[6] | 214 | { |
---|
[13] | 215 | // TODO: Task solving goes here :-) |
---|
| 216 | tMatrix matrix; |
---|
| 217 | double *row; |
---|
[15] | 218 | int n = spinCities->value(); |
---|
[13] | 219 | bool ok; |
---|
[15] | 220 | for (int r = 0; r < n; r++) { |
---|
| 221 | row = new double[n]; |
---|
| 222 | for (int c = 0; c < n; c++) { |
---|
| 223 | row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); |
---|
| 224 | if (!ok) { |
---|
[27] | 225 | QMessageBox(QMessageBox::Critical,trUtf8("Data error"),QString(trUtf8("Error in cell [Row %1; Column %2]: Invalid data format.")).arg(r + 1).arg(c + 1),QMessageBox::Ok,this).exec(); |
---|
[15] | 226 | return; |
---|
[13] | 227 | } |
---|
| 228 | } |
---|
| 229 | matrix.append(row); |
---|
| 230 | } |
---|
| 231 | CTSPSolver solver; |
---|
| 232 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
| 233 | if (!root) |
---|
[27] | 234 | QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec(); |
---|
[12] | 235 | // tabWidget->setCurrentIndex(1); |
---|
[6] | 236 | } |
---|
[21] | 237 | |
---|
[29] | 238 | void MainWindow::actionHelpAboutTriggered() |
---|
[21] | 239 | { |
---|
| 240 | // TODO: Normal about window :-) |
---|
[30] | 241 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n"); |
---|
| 242 | about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); |
---|
| 243 | about += "Qt library versions:\n"; |
---|
[31] | 244 | about += QString::fromUtf8(" OS: %1\n").arg(OS); |
---|
[30] | 245 | about += QString::fromUtf8(" Compile time: %1\n").arg(QT_VERSION_STR); |
---|
| 246 | about += QString::fromUtf8(" Runtime: %1\n").arg(qVersion()); |
---|
| 247 | about += "\n"; |
---|
| 248 | about += "TSPSG is licensed under the terms of the GNU General Public License. You should have received a copy of the GNU General Public License along with TSPSG."; |
---|
[27] | 249 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
[21] | 250 | } |
---|
| 251 | |
---|
[29] | 252 | void MainWindow::loadLangList() |
---|
| 253 | { |
---|
| 254 | QSettings langinfo("i18n/languages.ini",QSettings::IniFormat); |
---|
| 255 | langinfo.setIniCodec("UTF-8"); |
---|
| 256 | QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
| 257 | if (!dir.exists()) |
---|
| 258 | return; |
---|
| 259 | QFileInfoList langs = dir.entryInfoList(); |
---|
| 260 | if (langs.size() <= 0) |
---|
| 261 | return; |
---|
| 262 | QAction *a; |
---|
| 263 | for (int k = 0; k < langs.size(); k++) { |
---|
| 264 | QFileInfo lang = langs.at(k); |
---|
[30] | 265 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
[29] | 266 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
| 267 | a->setData(lang.completeBaseName()); |
---|
| 268 | a->setCheckable(true); |
---|
| 269 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 270 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
| 271 | a->setChecked(true); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
| 277 | { |
---|
| 278 | if (checked) { |
---|
| 279 | settings->remove("Language"); |
---|
| 280 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
| 281 | } else |
---|
| 282 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
| 286 | { |
---|
| 287 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 288 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 289 | 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) { |
---|
| 290 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 291 | } else |
---|
| 292 | return; |
---|
| 293 | } |
---|
[30] | 294 | if (loadLanguage(action->data().toString())) { |
---|
[29] | 295 | settings->setValue("Language",action->data().toString()); |
---|
| 296 | retranslateUi(this); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
[21] | 300 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
| 301 | { |
---|
[26] | 302 | settings->setValue("NumCities",spinCities->value()); |
---|
| 303 | #ifndef Q_OS_WINCE |
---|
[21] | 304 | // Saving windows state |
---|
[23] | 305 | if (settings->value("SavePos",false).toBool()) { |
---|
| 306 | settings->beginGroup("MainWindow"); |
---|
| 307 | settings->setValue("Maximized",isMaximized()); |
---|
[21] | 308 | if (!isMaximized()) { |
---|
[23] | 309 | settings->setValue("Size",size()); |
---|
| 310 | settings->setValue("Position",pos()); |
---|
[21] | 311 | } |
---|
[23] | 312 | settings->endGroup(); |
---|
[21] | 313 | } |
---|
[26] | 314 | #endif // Q_OS_WINCE |
---|
[21] | 315 | QMainWindow::closeEvent(event); |
---|
| 316 | } |
---|
[31] | 317 | |
---|
| 318 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 319 | { |
---|
| 320 | spinCities->setValue(nCities); |
---|
| 321 | } |
---|