File: TranslationDialog.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (152 lines) | stat: -rw-r--r-- 5,306 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    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 St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "TranslationDialog.h"
#include "ui_TranslationDialog.h"
#include "TranslationManager.h"
#include "pIconManager.h"

#include <pPathListEditor.h>

#include <QFileDialog>
#include <QDebug>

TranslationDialog::TranslationDialog( TranslationManager* translationManager, QWidget* parent )
    : QDialog( parent )
{
    Q_ASSERT( translationManager );
    mTranslationManager = translationManager;
    
    //setAttribute( Qt::WA_DeleteOnClose );
    
    ui = new Ui::TranslationDialog;
    ui->setupUi( this );
    ui->tbReload->click();
}

TranslationDialog::~TranslationDialog()
{
    delete ui;
}

QString TranslationDialog::selectedLocale() const
{
    const QTreeWidgetItem* currentItem = ui->twLocales->selectedItems().value( 0 );
    return currentItem ? currentItem->data( 0, Qt::UserRole ).toString() : mTranslationManager->currentLocale().name();
}

QString TranslationDialog::getLocale( TranslationManager* translationManager, QWidget* parent )
{
    TranslationDialog dlg( translationManager, parent );
    
    if ( dlg.exec() == QDialog::Accepted ) {
        return dlg.selectedLocale();
    }
    
    return QString::null;
}

void TranslationDialog::on_tbLocate_clicked()
{
    QDialog dlg( this );
    pPathListEditor editor( this, tr( "Choose folders containg your application translations" ), QApplication::applicationDirPath() );
    QDialogButtonBox buttons( this );
    QVBoxLayout vl( &dlg );
    vl.addWidget( &editor );
    vl.addWidget( &buttons );
    
    buttons.setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
    editor.setValues( mTranslationManager->translationsPaths() );
    
    connect( &buttons, SIGNAL( rejected() ), &dlg, SLOT( reject() ) );
    connect( &buttons, SIGNAL( accepted() ), &dlg, SLOT( accept() ) );
    
    if ( dlg.exec() == QDialog::Rejected ) {
        return;
    }
    
    mTranslationManager->setTranslationsPaths( editor.values() );
    ui->tbReload->click();
}

QTreeWidgetItem* TranslationDialog::newItem( const QLocale& locale )
{
    const QString language = QLocale::languageToString( locale.language() );
    const QString country = QLocale::countryToString( locale.country() );
    const QString countryCode = locale.name().section( '_', 1 );
    
    QTreeWidgetItem* item = new QTreeWidgetItem;
    item->setIcon( 0, pIconManager::icon( QString( "%1.png" ).arg( countryCode.toLower() ), ":/country-flags" ) );
    item->setText( 0, QString( "%1 (%2)" ).arg( language ).arg( country ) );
    item->setToolTip( 0, locale.name() );
    item->setData( 0, Qt::UserRole, locale.name() );
    return item;
}

QTreeWidgetItem* TranslationDialog::rootItem( const QLocale& locale )
{
    QTreeWidgetItem* item = mRootItems.value( locale.name() );
    
    if ( !item ) {
        item = newItem( locale );
        ui->twLocales->addTopLevelItem( item );
        mRootItems[ locale.name() ] = item;
    }
    
    return item;
}

void TranslationDialog::on_tbReload_clicked()
{
    // reload translations if needed
    if ( mTranslationManager->availableLocales().isEmpty() ) {
        mTranslationManager->reloadTranslations();
    }
    
    // keep current locale
    const QString currentLocale = selectedLocale();
    
    // clear items
    ui->twLocales->clear();
    mRootItems.clear();
    
    // create new ones
    foreach ( const QLocale& _locale, mTranslationManager->availableQLocales() ) {
        const QLocale locale = _locale.language() == QLocale::C ? QLocale( QLocale::English ) : _locale;
        QTreeWidgetItem* rootItem = this->rootItem( QLocale( locale.language() ) );
        
        if ( rootItem->data( 0, Qt::UserRole ).toString() == locale.name() ) {
            continue;
        }
        
        rootItem->addChild( newItem( locale ) );
    }
    
    // sort items
    ui->twLocales->sortByColumn( 0, Qt::AscendingOrder );
    
    // restore locale
    QAbstractItemModel* model = ui->twLocales->model();
    QModelIndex index = model->match( model->index( 0, 0 ), Qt::UserRole, currentLocale, 1, Qt::MatchFixedString | Qt::MatchWrap ).value( 0 );
    
    if ( !index.isValid() ) {
        index = model->match( model->index( 0, 0 ), Qt::UserRole, currentLocale, 1, Qt::MatchStartsWith | Qt::MatchWrap ).value( 0 );
    }
    
    ui->twLocales->setCurrentIndex( index );
}