LanguagePreferences.cpp 3.63 KB
Newer Older
Ludovic Fauvet's avatar
Ludovic Fauvet committed
1
2
3
4
5
6
/*****************************************************************************
 * Preferences.cpp: Preferences window
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Christophe Courtaut <christophe.courtaut@gmail.com>
7
 * Authors: Clement CHAVANCE <kinder@vlmcorg>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
23
#include <QVariant>
Ludovic Fauvet's avatar
Ludovic Fauvet committed
24

25
#include "SettingsManager.h"
26
27
#include "LanguagePreferences.h"
#include "ui_LanguagePreferences.h"
28

29
30
#include <QDir>

31
QTranslator* LanguagePreferences::m_currentLang = NULL;
32

33
34
LanguagePreferences::LanguagePreferences( QWidget *parent )
    : PreferenceWidget( parent )
35
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
36
    m_ui.setupUi( this );
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    QDir            dir( "ts/", "*.ts", QDir::Name | QDir::IgnoreCase, QDir::Files );
    QStringList     tss = dir.entryList();

    foreach ( const QString& tsFileName, tss )
    {
        QString     localeStr;
        int         localePos = tsFileName.lastIndexOf( "vlmc_");
        int         dotPos = tsFileName.lastIndexOf( ".ts" );
        if ( localePos < 0 || dotPos < 0 )
        {
            qWarning() << "Invalid translation file:" << tsFileName;
            continue ;
        }
        localePos += 5;
        localeStr = tsFileName.mid( localePos, dotPos - localePos );
        QLocale     locale( localeStr );
        m_ui.comboBoxLanguage->addItem( QLocale::countryToString( locale.country() ) + " / "
                                        + QLocale::languageToString( locale.language() ), localeStr );
    }
    m_ui.comboBoxLanguage->addItem( "UnitedStates / English",    "en" );
58
59
}

60
LanguagePreferences::~LanguagePreferences() {}
61

62
void LanguagePreferences::load()
Christophe Courtaut's avatar
Christophe Courtaut committed
63
{
64
65
66
67
    const QString& part = m_defaults ? "default" : m_settName;
    SettingsManager* setMan = SettingsManager::getInstance();
    QVariant    lang = setMan->getValue( part, "VLMCLang" );
    int idx = m_ui.comboBoxLanguage->findData( lang );
Christophe Courtaut's avatar
Christophe Courtaut committed
68

69
70
71
    if ( idx != -1 )
        m_ui.comboBoxLanguage->setCurrentIndex( idx );
    return ;
72
73
}

74
void LanguagePreferences::save()
75
{
76
77
78
79
80
    SettingsManager*    setMan = SettingsManager::getInstance();
    QVariant lang = m_ui.comboBoxLanguage->itemData( m_ui.comboBoxLanguage->currentIndex() );

    setMan->setValue( m_settName, "VLMCLang", lang );
    changeLang( lang.toString() );
81
82
}

83
void LanguagePreferences::changeLang( QString langValue )
84
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
85
    if ( m_currentLang != NULL )
86
    {
Ludovic Fauvet's avatar
Ludovic Fauvet committed
87
88
89
        qApp->removeTranslator( m_currentLang );
        delete m_currentLang;
        m_currentLang = NULL;
90
    }
Ludovic Fauvet's avatar
Ludovic Fauvet committed
91
    if ( !langValue.isEmpty() )
92
93
    {
        m_currentLang = new QTranslator();
94
        m_currentLang->load( langValue, ":/Lang/" );
95
96
        qApp->installTranslator( m_currentLang );
    }
97
}
98
99
100
101
102
103
104
105
106
107
108
109
110

void LanguagePreferences::changeEvent( QEvent *e )
{
    QWidget::changeEvent( e );
    switch ( e->type() )
    {
    case QEvent::LanguageChange:
        m_ui.retranslateUi( this );
        break;
    default:
        break;
    }
}