Changeset 899d1b8e15 in tspsg for src
- Timestamp:
- Jun 24, 2009, 6:54:10 PM (16 years ago)
- Branches:
- 0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
- Children:
- ac4cb71650
- Parents:
- 78282ec778
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main.cpp
r78282ec778 r899d1b8e15 33 33 app.setOrganizationDomain("www.leppsville.com"); 34 34 app.setApplicationName("TSPSG"); 35 // i18n 36 // TODO: Language selection in application. 37 // TODO: Saving and restoring selected language. 38 QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); 39 QString locale = QLocale::languageToString(QLocale::system().language()); 40 QTranslator translator; 41 // Getting current locale language name and trying to load it, if it's not English 42 if (locale.compare("English") && translator.load(locale,"i18n")) 43 app.installTranslator(&translator); 35 QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); 44 36 MainWindow mainwindow; 45 37 mainwindow.show(); -
src/mainwindow.cpp
r78282ec778 r899d1b8e15 31 31 : QMainWindow(parent) 32 32 { 33 settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); 34 loadLanguage(); 33 35 setupUi(this); 34 settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg"); 36 #ifndef Q_OS_WINCE 37 printer = new QPrinter(); 38 #endif // Q_OS_WINCE 39 groupSettingsLanguageList = new QActionGroup(this); 40 loadLangList(); 35 41 spinCities->setValue(settings->value("NumCities",5).toInt()); 36 connect(actionSettingsSettings,SIGNAL(triggered()),this,SLOT(ChangeSettings())); 37 connect(actionHelpAbout,SIGNAL(triggered()),this,SLOT(showAbout())); 38 #ifndef Q_OS_WINCE 39 connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(PrintSetup())); 40 #endif // Q_OS_WINCE 41 connect(buttonSolve,SIGNAL(clicked()),this,SLOT(Solve())); 42 connect(buttonRandom,SIGNAL(clicked()),this,SLOT(Random())); 43 connect(spinCities,SIGNAL(valueChanged(int)),this,SLOT(CitiesNumberChanged(int))); 42 actionSettingsLanguageAutodetect->setChecked(settings->value("Language","").toString().isEmpty()); 43 connect(actionFileNew,SIGNAL(triggered()),this,SLOT(actionFileNewTriggered())); 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())); 48 #ifndef Q_OS_WINCE 49 connect(actionFilePrintSetup,SIGNAL(triggered()),this,SLOT(actionFilePrintSetupTriggered())); 50 #endif // Q_OS_WINCE 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))); 44 54 QRect rect = geometry(); 45 55 #ifdef Q_OS_WINCE … … 73 83 } 74 84 75 void MainWindow::CitiesNumberChanged(int n) 85 bool MainWindow::loadLanguage() 86 { 87 // i18n 88 bool ad = settings->value("Language","").toString().isEmpty(); 89 QString lang = settings->value("Language",QLocale::system().name()).toString(); 90 static QTranslator *qtTranslator; 91 if (qtTranslator) { 92 qApp->removeTranslator(qtTranslator); 93 delete qtTranslator; 94 qtTranslator = NULL; 95 } 96 qtTranslator = new QTranslator(); 97 if (lang.compare("en") && !lang.startsWith("en_")) { 98 // Trying to load system Qt library translation... 99 if (qtTranslator->load("qt_" + lang,QLibraryInfo::location(QLibraryInfo::TranslationsPath))) 100 qApp->installTranslator(qtTranslator); 101 else 102 // No luck. Let's try to load bundled one. 103 if (qtTranslator->load("qt_" + lang,"i18n")) 104 qApp->installTranslator(qtTranslator); 105 else { 106 delete qtTranslator; 107 qtTranslator = NULL; 108 } 109 } 110 // Now let's load application translation. 111 static QTranslator *translator; 112 if (translator) { 113 qApp->removeTranslator(translator); 114 delete translator; 115 } 116 translator = new QTranslator(); 117 if (lang.compare("en") && !lang.startsWith("en_")) { 118 if (translator->load(lang,"i18n")) 119 qApp->installTranslator(translator); 120 else { 121 if (!ad) 122 QMessageBox(QMessageBox::Warning,trUtf8("Language change"),trUtf8("Unable to load translation language."),QMessageBox::Ok,this).exec(); 123 delete translator; 124 translator = NULL; 125 return false; 126 } 127 } 128 return true; 129 } 130 131 void MainWindow::spinCitiesValueChanged(int n) 76 132 { 77 133 #ifdef Q_OS_WINCE … … 88 144 } 89 145 90 void MainWindow::ChangeSettings() 146 147 void MainWindow::actionFileNewTriggered() 148 { 149 tspmodel->clear(); 150 } 151 152 void MainWindow::actionSettingsPreferencesTriggered() 91 153 { 92 154 SettingsDialog sd(this); … … 95 157 96 158 #ifndef Q_OS_WINCE 97 void MainWindow::PrintSetup() 98 { 99 QPrintDialog pd; 159 void MainWindow::actionFilePrintSetupTriggered() 160 { 161 QPrintDialog pd(printer,this); 162 pd.setOption(QAbstractPrintDialog::PrintSelection,false); 163 pd.setOption(QAbstractPrintDialog::PrintPageRange,false); 100 164 pd.exec(); 101 165 } 102 166 #endif // Q_OS_WINCE 103 167 104 void MainWindow:: Random()168 void MainWindow::buttonRandomClicked() 105 169 { 106 170 tspmodel->randomize(); … … 111 175 } 112 176 113 void MainWindow:: Solve()177 void MainWindow::buttonSolveClicked() 114 178 { 115 179 // TODO: Task solving goes here :-) … … 136 200 } 137 201 138 void MainWindow:: showAbout()202 void MainWindow::actionHelpAboutTriggered() 139 203 { 140 204 // TODO: Normal about window :-) … … 149 213 } 150 214 215 void MainWindow::loadLangList() 216 { 217 QSettings langinfo("i18n/languages.ini",QSettings::IniFormat); 218 langinfo.setIniCodec("UTF-8"); 219 QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files); 220 if (!dir.exists()) 221 return; 222 QFileInfoList langs = dir.entryInfoList(); 223 if (langs.size() <= 0) 224 return; 225 menuSettingsLanguage->addSeparator(); 226 QAction *a; 227 for (int k = 0; k < langs.size(); k++) { 228 QFileInfo lang = langs.at(k); 229 if (!lang.completeBaseName().startsWith("qt_")) { 230 a = menuSettingsLanguage->addAction(langinfo.value(lang.completeBaseName() + "/NativeName",lang.completeBaseName()).toString()); 231 a->setData(lang.completeBaseName()); 232 a->setCheckable(true); 233 a->setActionGroup(groupSettingsLanguageList); 234 if (settings->value("Language",QLocale::system().name()).toString().startsWith(lang.completeBaseName())) 235 a->setChecked(true); 236 } 237 } 238 } 239 240 void MainWindow::actionSettingsLanguageAutodetectTriggered(bool checked) 241 { 242 if (checked) { 243 settings->remove("Language"); 244 QMessageBox(QMessageBox::Information,trUtf8("Language change"),trUtf8("Language will be autodetected on next application start."),QMessageBox::Ok,this).exec(); 245 } else 246 settings->setValue("Language",groupSettingsLanguageList->checkedAction()->data().toString()); 247 } 248 249 void MainWindow::groupSettingsLanguageListTriggered(QAction *action) 250 { 251 if (actionSettingsLanguageAutodetect->isChecked()) { 252 // We have language autodetection. It needs to be disabled to change language. 253 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) { 254 actionSettingsLanguageAutodetect->trigger(); 255 } else 256 return; 257 } 258 if (loadLanguage()) { 259 settings->setValue("Language",action->data().toString()); 260 retranslateUi(this); 261 } else { 262 } 263 } 264 151 265 void MainWindow::closeEvent(QCloseEvent *event) 152 266 { -
src/mainwindow.h
r78282ec778 r899d1b8e15 43 43 void closeEvent(QCloseEvent *event); 44 44 private slots: 45 void ChangeSettings(); 46 void showAbout(); 45 void actionFileNewTriggered(); 46 void actionSettingsPreferencesTriggered(); 47 void actionSettingsLanguageAutodetectTriggered(bool checked); 48 void groupSettingsLanguageListTriggered(QAction *action); 49 void actionHelpAboutTriggered(); 47 50 #ifndef Q_OS_WINCE 48 void PrintSetup();51 void actionFilePrintSetupTriggered(); 49 52 #endif // Q_OS_WINCE 50 void Solve();51 void Random();52 void CitiesNumberChanged(int n);53 void buttonSolveClicked(); 54 void buttonRandomClicked(); 55 void spinCitiesValueChanged(int n); 53 56 private: 54 57 QSettings *settings; 58 QPrinter *printer; 55 59 CTSPModel *tspmodel; 60 QActionGroup *groupSettingsLanguageList; 61 bool loadLanguage(); 62 void loadLangList(); 56 63 }; 57 64 -
src/tspmodel.cpp
r78282ec778 r899d1b8e15 141 141 } 142 142 143 void CTSPModel::clear() 144 { 145 for (int r = 0; r < nCities; r++) 146 for (int c = 0; c < nCities; c++) 147 if (r != c) 148 table[r][c] = 0; 149 emit dataChanged(index(0,0),index(nCities - 1,nCities - 1)); 150 } 151 143 152 void CTSPModel::randomize() 144 153 { -
src/tspmodel.h
r78282ec778 r899d1b8e15 42 42 int numCities() const; 43 43 void setNumCities(int); 44 void clear(); 44 45 void randomize(); 45 46 private:
Note: See TracChangeset
for help on using the changeset viewer.