[5515c2c2a7] | 1 | /* |
---|
[bb994a7ff8] | 2 | * TSPSG - TSP Solver and Generator |
---|
[5354a01311] | 3 | * Copyright (C) 2007-2009 Lёppa <contacts[at]oleksii[dot]name> |
---|
[003e4193be] | 4 | * |
---|
[bb994a7ff8] | 5 | * $Id$ |
---|
| 6 | * $URL$ |
---|
[5515c2c2a7] | 7 | * |
---|
[bb994a7ff8] | 8 | * This file is part of TSPSG. |
---|
[5515c2c2a7] | 9 | * |
---|
[bb994a7ff8] | 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. |
---|
[5515c2c2a7] | 14 | * |
---|
[bb994a7ff8] | 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. |
---|
[5515c2c2a7] | 19 | * |
---|
[bb994a7ff8] | 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/>. |
---|
[5515c2c2a7] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include "mainwindow.h" |
---|
| 25 | |
---|
| 26 | MainWindow::MainWindow(QWidget *parent) |
---|
[aecdf994f9] | 27 | : QMainWindow(parent) |
---|
[5515c2c2a7] | 28 | { |
---|
[665d32434f] | 29 | settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); |
---|
[899d1b8e15] | 30 | loadLanguage(); |
---|
| 31 | setupUi(this); |
---|
| 32 | #ifndef Q_OS_WINCE |
---|
| 33 | printer = new QPrinter(); |
---|
| 34 | #endif // Q_OS_WINCE |
---|
| 35 | groupSettingsLanguageList = new QActionGroup(this); |
---|
[ac4cb71650] | 36 | actionSettingsLanguageEnglish->setData("en"); |
---|
| 37 | actionSettingsLanguageEnglish->setActionGroup(groupSettingsLanguageList); |
---|
[899d1b8e15] | 38 | loadLangList(); |
---|
[665d32434f] | 39 | spinCities->setValue(settings->value("NumCities",5).toInt()); |
---|
[899d1b8e15] | 40 | actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); |
---|
| 41 | connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); |
---|
[993d5af6f6] | 42 | connect(actionFileOpen,SIGNAL(triggered()),this,SLOT(actionFileOpenTriggered())); |
---|
| 43 | connect(actionFileSaveTask,SIGNAL(triggered()),this,SLOT(actionFileSaveTaskTriggered())); |
---|
[899d1b8e15] | 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())); |
---|
[5354a01311] | 48 | #ifndef Q_OS_WINCE |
---|
[899d1b8e15] | 49 | connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered())); |
---|
[5354a01311] | 50 | #endif // Q_OS_WINCE |
---|
[899d1b8e15] | 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))); |
---|
[5354a01311] | 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 |
---|
[665d32434f] | 61 | if (settings->value("SavePos",false).toBool()) { |
---|
[aecdf994f9] | 62 | // Loading of saved window state |
---|
[665d32434f] | 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()) |
---|
[aecdf994f9] | 67 | setWindowState(windowState() | Qt::WindowMaximized); |
---|
[665d32434f] | 68 | settings->endGroup(); |
---|
[aecdf994f9] | 69 | } else { |
---|
| 70 | // Centering main window |
---|
| 71 | rect.moveCenter(QApplication::desktop()->availableGeometry(this).center()); |
---|
| 72 | setGeometry(rect); |
---|
| 73 | } |
---|
[5354a01311] | 74 | #endif // Q_OS_WINCE |
---|
[fc9f661ded] | 75 | qsrand(QDateTime().currentDateTime().toTime_t()); |
---|
[2bc8e278b7] | 76 | tspmodel = new CTSPModel(); |
---|
| 77 | tspmodel->setNumCities(spinCities->value()); |
---|
| 78 | taskView->setModel(tspmodel); |
---|
[993d5af6f6] | 79 | connect(tspmodel,SIGNAL(numCitiesChanged(int)),this,SLOT(numCitiesChanged(int))); |
---|
[aecdf994f9] | 80 | #ifdef Q_OS_WINCE |
---|
| 81 | taskView->resizeColumnsToContents(); |
---|
| 82 | taskView->resizeRowsToContents(); |
---|
| 83 | #endif // Q_OS_WINCE |
---|
[003e4193be] | 84 | } |
---|
[052d1b9331] | 85 | |
---|
[ac4cb71650] | 86 | bool MainWindow::loadLanguage(QString lang) |
---|
[899d1b8e15] | 87 | { |
---|
| 88 | // i18n |
---|
[ac4cb71650] | 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 | } |
---|
[899d1b8e15] | 94 | static QTranslator *qtTranslator; |
---|
| 95 | if (qtTranslator) { |
---|
| 96 | qApp->removeTranslator(qtTranslator); |
---|
| 97 | delete qtTranslator; |
---|
| 98 | qtTranslator = NULL; |
---|
| 99 | } |
---|
| 100 | qtTranslator = new QTranslator(); |
---|
[ac4cb71650] | 101 | static QTranslator *translator; |
---|
| 102 | if (translator) { |
---|
| 103 | qApp->removeTranslator(translator); |
---|
| 104 | delete translator; |
---|
| 105 | } |
---|
| 106 | translator = new QTranslator(); |
---|
[899d1b8e15] | 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 | } |
---|
[ac4cb71650] | 119 | // Now let's load application translation. |
---|
[899d1b8e15] | 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) |
---|
[003e4193be] | 134 | { |
---|
[aecdf994f9] | 135 | #ifdef Q_OS_WINCE |
---|
| 136 | int count = tspmodel->numCities(); |
---|
| 137 | #endif // Q_OS_WINCE |
---|
[2bc8e278b7] | 138 | tspmodel->setNumCities(n); |
---|
[aecdf994f9] | 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 |
---|
[5515c2c2a7] | 146 | } |
---|
| 147 | |
---|
[899d1b8e15] | 148 | |
---|
| 149 | void MainWindow::actionFileNewTriggered() |
---|
| 150 | { |
---|
| 151 | tspmodel->clear(); |
---|
| 152 | } |
---|
| 153 | |
---|
[993d5af6f6] | 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 | |
---|
[899d1b8e15] | 188 | void MainWindow::actionSettingsPreferencesTriggered() |
---|
[5515c2c2a7] | 189 | { |
---|
| 190 | SettingsDialog sd(this); |
---|
[aecdf994f9] | 191 | sd.exec(); |
---|
[5515c2c2a7] | 192 | } |
---|
[bb994a7ff8] | 193 | |
---|
[5354a01311] | 194 | #ifndef Q_OS_WINCE |
---|
[899d1b8e15] | 195 | void MainWindow::actionFilePrintSetupTriggered() |
---|
[5354a01311] | 196 | { |
---|
[899d1b8e15] | 197 | QPrintDialog pd(printer,this); |
---|
[140912822f] | 198 | #if QT_VERSION >= 0x040500 |
---|
| 199 | // No such methods in Qt < 4.5 |
---|
[899d1b8e15] | 200 | pd.setOption(QAbstractPrintDialog::PrintSelection,false); |
---|
| 201 | pd.setOption(QAbstractPrintDialog::PrintPageRange,false); |
---|
[140912822f] | 202 | #endif |
---|
[5354a01311] | 203 | pd.exec(); |
---|
| 204 | } |
---|
| 205 | #endif // Q_OS_WINCE |
---|
| 206 | |
---|
[899d1b8e15] | 207 | void MainWindow::buttonRandomClicked() |
---|
[bb994a7ff8] | 208 | { |
---|
[2bc8e278b7] | 209 | tspmodel->randomize(); |
---|
[aecdf994f9] | 210 | #ifdef Q_OS_WINCE |
---|
| 211 | taskView->resizeColumnsToContents(); |
---|
| 212 | taskView->resizeRowsToContents(); |
---|
| 213 | #endif // Q_OS_WINCE |
---|
[bb994a7ff8] | 214 | } |
---|
| 215 | |
---|
[899d1b8e15] | 216 | void MainWindow::buttonSolveClicked() |
---|
[bb994a7ff8] | 217 | { |
---|
| 218 | // TODO: Task solving goes here :-) |
---|
[e664262f7d] | 219 | tMatrix matrix; |
---|
| 220 | double *row; |
---|
[2bc8e278b7] | 221 | int n = spinCities->value(); |
---|
[e664262f7d] | 222 | bool ok; |
---|
[2bc8e278b7] | 223 | for (int r = 0; r < n; r++) { |
---|
| 224 | row = new double[n]; |
---|
| 225 | for (int c = 0; c < n; c++) { |
---|
| 226 | row[c] = tspmodel->index(r,c).data(Qt::UserRole).toDouble(&ok); |
---|
| 227 | if (!ok) { |
---|
[5587b87fac] | 228 | 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(); |
---|
[2bc8e278b7] | 229 | return; |
---|
[e664262f7d] | 230 | } |
---|
| 231 | } |
---|
| 232 | matrix.append(row); |
---|
| 233 | } |
---|
| 234 | CTSPSolver solver; |
---|
| 235 | sStep *root = solver.solve(spinCities->value(),matrix); |
---|
| 236 | if (!root) |
---|
[5587b87fac] | 237 | QMessageBox(QMessageBox::Critical,trUtf8("Solution error"),trUtf8("There was an error while solving the task."),QMessageBox::Ok,this).exec(); |
---|
[e664262f7d] | 238 | // tabWidget->setCurrentIndex(1); |
---|
[bb994a7ff8] | 239 | } |
---|
[aecdf994f9] | 240 | |
---|
[899d1b8e15] | 241 | void MainWindow::actionHelpAboutTriggered() |
---|
[aecdf994f9] | 242 | { |
---|
| 243 | // TODO: Normal about window :-) |
---|
[ac4cb71650] | 244 | QString about = QString::fromUtf8("TSPSG - TSP Solver and Generator\n"); |
---|
| 245 | about += QString::fromUtf8(" Copyright (C) 2007-%1 Lёppa <contacts[at]oleksii[dot]name>\n").arg(QDate::currentDate().toString("yyyy")); |
---|
| 246 | about += "Qt library versions:\n"; |
---|
[993d5af6f6] | 247 | about += QString::fromUtf8(" OS: %1\n").arg(OS); |
---|
[ac4cb71650] | 248 | about += QString::fromUtf8(" Compile time: %1\n").arg(QT_VERSION_STR); |
---|
| 249 | about += QString::fromUtf8(" Runtime: %1\n").arg(qVersion()); |
---|
| 250 | about += "\n"; |
---|
| 251 | 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."; |
---|
[5587b87fac] | 252 | QMessageBox(QMessageBox::Information,"About",about,QMessageBox::Ok,this).exec(); |
---|
[aecdf994f9] | 253 | } |
---|
| 254 | |
---|
[899d1b8e15] | 255 | void MainWindow::loadLangList() |
---|
| 256 | { |
---|
| 257 | QSettings langinfo("i18n/languages.ini",QSettings::IniFormat); |
---|
[140912822f] | 258 | #if QT_VERSION >= 0x040500 |
---|
| 259 | // In Qt < 4.5 QSettings doesn't have method setIniCodec. |
---|
[899d1b8e15] | 260 | langinfo.setIniCodec("UTF-8"); |
---|
[140912822f] | 261 | #endif |
---|
[899d1b8e15] | 262 | QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); |
---|
| 263 | if (!dir.exists()) |
---|
| 264 | return; |
---|
| 265 | QFileInfoList langs = dir.entryInfoList(); |
---|
| 266 | if (langs.size() <= 0) |
---|
| 267 | return; |
---|
| 268 | QAction *a; |
---|
| 269 | for (int k = 0; k < langs.size(); k++) { |
---|
| 270 | QFileInfo lang = langs.at(k); |
---|
[ac4cb71650] | 271 | if (!lang.completeBaseName().startsWith("qt_") && lang.completeBaseName().compare("en")) { |
---|
[140912822f] | 272 | #if QT_VERSION >= 0x040500 |
---|
[899d1b8e15] | 273 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); |
---|
[140912822f] | 274 | #else |
---|
| 275 | // We use Name if Qt < 4.5 because NativeName is in UTF-8, QSettings |
---|
| 276 | // reads .ini file as ASCII and there is no way to set file encoding. |
---|
| 277 | a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/Name",lang.completeBaseName()).toString()); |
---|
| 278 | #endif |
---|
[899d1b8e15] | 279 | a->setData(lang.completeBaseName()); |
---|
| 280 | a->setCheckable(true); |
---|
| 281 | a->setActionGroup(groupSettingsLanguageList); |
---|
| 282 | if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) |
---|
| 283 | a->setChecked(true); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) |
---|
| 289 | { |
---|
| 290 | if (checked) { |
---|
| 291 | settings->remove("Language"); |
---|
| 292 | QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); |
---|
| 293 | } else |
---|
| 294 | settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void MainWindow::groupSettingsLanguageListTriggered(QAction *action) |
---|
| 298 | { |
---|
| 299 | if (actionSettingsLanguageAutodetect->isChecked()) { |
---|
| 300 | // We have language autodetection. It needs to be disabled to change language. |
---|
| 301 | 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) { |
---|
| 302 | actionSettingsLanguageAutodetect->trigger(); |
---|
| 303 | } else |
---|
| 304 | return; |
---|
| 305 | } |
---|
[ac4cb71650] | 306 | if (loadLanguage(action->data().toString())) { |
---|
[899d1b8e15] | 307 | settings->setValue("Language",action->data().toString()); |
---|
| 308 | retranslateUi(this); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
[aecdf994f9] | 312 | void MainWindow::closeEvent(QCloseEvent *event) |
---|
| 313 | { |
---|
[665d32434f] | 314 | settings->setValue("NumCities",spinCities->value()); |
---|
[0621172ec1] | 315 | #ifndef Q_OS_WINCE |
---|
| 316 | // Saving windows state |
---|
[665d32434f] | 317 | if (settings->value("SavePos",false).toBool()) { |
---|
| 318 | settings->beginGroup("MainWindow"); |
---|
| 319 | settings->setValue("Maximized",isMaximized()); |
---|
[aecdf994f9] | 320 | if (!isMaximized()) { |
---|
[665d32434f] | 321 | settings->setValue("Size",size()); |
---|
| 322 | settings->setValue("Position",pos()); |
---|
[aecdf994f9] | 323 | } |
---|
[665d32434f] | 324 | settings->endGroup(); |
---|
[aecdf994f9] | 325 | } |
---|
[0621172ec1] | 326 | #endif // Q_OS_WINCE |
---|
[aecdf994f9] | 327 | QMainWindow::closeEvent(event); |
---|
| 328 | } |
---|
[993d5af6f6] | 329 | |
---|
| 330 | void MainWindow::numCitiesChanged(int nCities) |
---|
| 331 | { |
---|
| 332 | spinCities->setValue(nCities); |
---|
| 333 | } |
---|