Changeset 899d1b8e15 in tspsg for src


Ignore:
Timestamp:
Jun 24, 2009, 6:54:10 PM (16 years ago)
Author:
Oleksii Serdiuk
Branches:
0.1.3.145-beta1-symbian, 0.1.4.170-beta2-bb10, appveyor, imgbot, master, readme
Children:
ac4cb71650
Parents:
78282ec778
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.
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/main.cpp

    r78282ec778 r899d1b8e15  
    3333        app.setOrganizationDomain("www.leppsville.com");
    3434        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"));
    4436MainWindow mainwindow;
    4537        mainwindow.show();
  • src/mainwindow.cpp

    r78282ec778 r899d1b8e15  
    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{
  • src/mainwindow.h

    r78282ec778 r899d1b8e15  
    4343        void closeEvent(QCloseEvent *event);
    4444private 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();
    4750#ifndef Q_OS_WINCE
    48         void PrintSetup();
     51        void actionFilePrintSetupTriggered();
    4952#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);
    5356private:
    5457        QSettings *settings;
     58        QPrinter *printer;
    5559        CTSPModel *tspmodel;
     60        QActionGroup *groupSettingsLanguageList;
     61        bool loadLanguage();
     62        void loadLangList();
    5663};
    5764
  • src/tspmodel.cpp

    r78282ec778 r899d1b8e15  
    141141}
    142142
     143void 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
    143152void CTSPModel::randomize()
    144153{
  • src/tspmodel.h

    r78282ec778 r899d1b8e15  
    4242        int numCities() const;
    4343        void setNumCities(int);
     44        void clear();
    4445        void randomize();
    4546private:
Note: See TracChangeset for help on using the changeset viewer.