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