Changeset 29 in tspsg-svn for trunk/src/mainwindow.cpp


Ignore:
Timestamp:
Jun 24, 2009, 6:54:10 PM (15 years ago)
Author:
laleppa
Message:

+ Selected application language is now saved and restored.
+ Added language selection entries to main menu.
+ On-the-fly language switching.
+ Loading corresponding Qt library language, if exists.

  • File/New? sets all table cells to zeros.
  • Translation updates to reflect recent changes.
  • Renamed language files from language names to language codes.
  • Renamed some slot to have unified signal/slot naming.
  • Printer settings are now persistant until application close.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/mainwindow.cpp

    r27 r29  
    3131        : QMainWindow(parent)
    3232{
     33        settings = new QSettings(QSettings::IniFormat,QSettings::UserScope,"TSPSG","tspsg");
     34        loadLanguage();
    3335        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();
    3541        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)));
    4454QRect rect = geometry();
    4555#ifdef Q_OS_WINCE
     
    7383}
    7484
    75 void MainWindow::CitiesNumberChanged(int n)
     85bool MainWindow::loadLanguage()
     86{
     87// i18n
     88bool ad = settings->value("Language","").toString().isEmpty();
     89QString lang = settings->value("Language",QLocale::system().name()).toString();
     90static 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.
     111static 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
     131void MainWindow::spinCitiesValueChanged(int n)
    76132{
    77133#ifdef Q_OS_WINCE
     
    88144}
    89145
    90 void MainWindow::ChangeSettings()
     146
     147void MainWindow::actionFileNewTriggered()
     148{
     149        tspmodel->clear();
     150}
     151
     152void MainWindow::actionSettingsPreferencesTriggered()
    91153{
    92154SettingsDialog sd(this);
     
    95157
    96158#ifndef Q_OS_WINCE
    97 void MainWindow::PrintSetup()
    98 {
    99 QPrintDialog pd;
     159void MainWindow::actionFilePrintSetupTriggered()
     160{
     161QPrintDialog pd(printer,this);
     162        pd.setOption(QAbstractPrintDialog::PrintSelection,false);
     163        pd.setOption(QAbstractPrintDialog::PrintPageRange,false);
    100164        pd.exec();
    101165}
    102166#endif // Q_OS_WINCE
    103167
    104 void MainWindow::Random()
     168void MainWindow::buttonRandomClicked()
    105169{
    106170        tspmodel->randomize();
     
    111175}
    112176
    113 void MainWindow::Solve()
     177void MainWindow::buttonSolveClicked()
    114178{
    115179        // TODO: Task solving goes here :-)
     
    136200}
    137201
    138 void MainWindow::showAbout()
     202void MainWindow::actionHelpAboutTriggered()
    139203{
    140204        // TODO: Normal about window :-)
     
    149213}
    150214
     215void MainWindow::loadLangList()
     216{
     217QSettings langinfo("i18n/languages.ini",QSettings::IniFormat);
     218        langinfo.setIniCodec("UTF-8");
     219QDir dir("i18n","*.qm",QDir::Name | QDir::IgnoreCase,QDir::Files);
     220        if (!dir.exists())
     221                return;
     222QFileInfoList langs = dir.entryInfoList();
     223        if (langs.size() <= 0)
     224                return;
     225        menuSettingsLanguage->addSeparator();
     226QAction *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
     240void 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
     249void 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
    151265void MainWindow::closeEvent(QCloseEvent *event)
    152266{
Note: See TracChangeset for help on using the changeset viewer.