File: pLocaleModel.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 (332 lines) | stat: -rw-r--r-- 10,479 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/****************************************************************************
    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 "pLocaleModel.h"
#include "pIconManager.h"

#include <QLocale>
#include <QDebug>

#define pLocaleModelColumnCount 1

bool caseInsensitiveLessThanStringLocale( const QString& left, const QString& right )
{
    return pLocaleModel::localeDisplayText( left ).toLower() < pLocaleModel::localeDisplayText( right ).toLower();
}

pLocaleModel::pLocaleModel( QObject* parent )
    : QAbstractItemModel( parent )
{
    mIsCheckable = false;
    
    populate();
    //debug();
}

pLocaleModel::~pLocaleModel()
{
}

QModelIndex pLocaleModel::index( int row, int column, const QModelIndex& parent ) const
{
    if ( parent.isValid() ) {
        const QString parentLocale = indexToLocale( parent );
        QStringList* list = mChildrenLocales.contains( parentLocale ) ? &mChildrenLocales[ parentLocale ] : 0;
        
        if ( !list || row < 0 || row >= list->count() || column < 0 || column >= pLocaleModelColumnCount ) {
            return QModelIndex();
        }
        
        return createIndex( row, column, &(*list)[ row ] );
    }
    
    if ( row < 0 || row >= mLocales.count() || column < 0 || column >= pLocaleModelColumnCount ) {
        return QModelIndex();
    }
    
    return createIndex( row, column, &mLocales[ row ] );
}

QModelIndex pLocaleModel::parent( const QModelIndex& child ) const
{
    const QLocale childLocale( indexToLocale( child ) );
    const QLocale parentLocale( childLocale.language() );
    return parentLocale == childLocale ? QModelIndex() : localeToIndex( parentLocale.name() );
}

int pLocaleModel::rowCount( const QModelIndex& parent ) const
{
    const QString locale = indexToLocale( parent );
    return parent == QModelIndex() ? mLocales.count() : mChildrenLocales.value( locale ).count();
}

int pLocaleModel::columnCount( const QModelIndex& parent ) const
{
    Q_UNUSED( parent );
    return pLocaleModelColumnCount;
}

QVariant pLocaleModel::data( const QModelIndex& index, int role ) const
{
    if ( index.isValid() ) {
        const QString name = indexToLocale( index );
        const QString countryCode = name.section( '_', 1 );
        
        switch ( role ) {
            case Qt::DecorationRole:
                return pIconManager::icon( QString( "%1.png" ).arg( countryCode.toLower() ), ":/country-flags" );
            case Qt::DisplayRole:
                return pLocaleModel::localeDisplayText( name );
            case Qt::ToolTipRole:
                return name;
            case Qt::CheckStateRole: {
                if ( !mIsCheckable ) {
                    return QVariant();
                }
                
                Qt::CheckState state = Qt::CheckState( mData.value( name ).value( role, Qt::Unchecked ).toInt() );
                
                if ( state == Qt::Unchecked ) {
                    const QStringList locales = mChildrenLocales.value( name );
                    bool hasChildrenChecked = false;
                
                    foreach ( const QString& locale, locales ) {
                        if ( mData.value( locale ).value( Qt::CheckStateRole ).toInt() == Qt::Checked ) {
                            hasChildrenChecked = true;
                            break; // foreach
                        }
                    }
                    
                    if ( hasChildrenChecked ) {
                        state = Qt::PartiallyChecked;
                    }
                }
                
                return state;
            }
            default:
                break;
        }
        
        return mData.value( name ).value( role );
    }
    
    return QVariant();
}

Qt::ItemFlags pLocaleModel::flags( const QModelIndex& index ) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags( index );
    
    if ( mIsCheckable ) {
        flags |= Qt::ItemIsUserCheckable;
    }
    
    return flags;
}

bool pLocaleModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
    if ( !index.isValid() ) {
        return false;
    }
    
    switch ( role ) {
        case Qt::DecorationRole:
        case Qt::DisplayRole:
        case Qt::ToolTipRole:
            return false;
        default:
            break;
    }
    
    const QString name = indexToLocale( index );
    
    if ( !value.isNull() ) {
        mData[ name ][ role ] = value;
    }
    else if ( mData.value( name ).contains( role ) ) {
        mData[ name ].remove( role );
        
        if ( mData[ name ].isEmpty() ) {
            mData.remove( name );
        }
    }
    
    emit dataChanged( index, index );
    
    // update parent partially check state
    if ( role == Qt::CheckStateRole ) {
        const QModelIndex parent = index.parent();
        emit dataChanged( parent, parent );
    }
    
    return true;
}

QModelIndex pLocaleModel::localeToIndex( const QString& locale ) const
{
    int row = mLocales.indexOf( locale );
    
    if ( row != -1 ) {
        return createIndex( row, 0, &mLocales[ row ] );
    }
    
    const QString parentLocale = QLocale( QLocale( locale ).name().section( '_', 0, 0 ) ).name();
    row = mChildrenLocales.value( parentLocale ).indexOf( locale );
    QStringList* list = row != -1 ? &mChildrenLocales[ parentLocale ] : 0;
    
    if ( !list || row < 0 || row >= list->count() ) {
        return QModelIndex();
    }
    
    return createIndex( row, 0, &(*list)[ row ] );
}

QString pLocaleModel::indexToLocale( const QModelIndex& index ) const
{
    return index.isValid() ? *static_cast<QString*>( index.internalPointer() ) : QString::null;
}

bool pLocaleModel::isCheckable() const
{
    return mIsCheckable;
}

void pLocaleModel::setCheckable( bool checkable )
{
    mIsCheckable = checkable;
    emit dataChanged( index( 0, 0 ), index( rowCount() -1, columnCount() -1 ) );
}

QStringList pLocaleModel::checkedLocales() const
{
    QStringList names;
    
    foreach ( const QString& name, mData.keys() ) {
        if ( mData[ name ].value( Qt::CheckStateRole ).toInt() == Qt::Checked ) {
            names << name;
        }
    }
    
    return names;
}

void pLocaleModel::setCheckedLocales( const QStringList& locales, bool checked )
{
    foreach ( QString name, locales ) {
        // fix not complet locale ( only language and not country )
        if ( name.count( "_" ) == 0 ) {
            name = QLocale( name ).name();
        }
        
        if ( checked ) {
            mData[ name ][ Qt::CheckStateRole ] = Qt::Checked;
        }
        else if ( mData.value( name ).contains( Qt::CheckStateRole ) ) {
            mData[ name ].remove( Qt::CheckStateRole );
            
            if ( mData[ name ].isEmpty() ) {
                mData.remove( name );
            }
        }
    }
    
    emit dataChanged( index( 0, 0 ), index( rowCount() -1, columnCount() -1 ) );
}

QString pLocaleModel::localeDisplayText( const QString& name )
{
    const QLocale locale( name );
    const QString language = QLocale::languageToString( locale.language() );
    const QString country = QLocale::countryToString( locale.country() );
    return QString( "%1 (%2)" ).arg( language ).arg( country );
}

void pLocaleModel::populate()
{
    QSet<QString> names;
    QHash<QString, QSet<QString> > childNames;
    
    for ( int i = QLocale::C +1; i < QLocale::LastLanguage; i++ ) {
        const QLocale::Language language = QLocale::Language( i );
        
        if ( language == QLocale::C ) {
            continue;
        }
        
        foreach ( const QLocale::Country& country, QLocale::countriesForLanguage( language ) ) {
            const QLocale locale( language, country );
            const QString languageCode = locale.name().section( '_', 0, 0 );
            const QLocale localeParent( languageCode );
            const QString localeParentName = localeParent.name();
            
            names << localeParentName;
            
            if ( locale != localeParent ) {
                childNames[ localeParentName ] << locale.name();
            }
        }
    }
    
    mLocales = names.toList();
    qSort( mLocales.begin(), mLocales.end(), caseInsensitiveLessThanStringLocale );
    
    foreach ( const QString& name, childNames.keys() ) {
        QStringList locales = childNames[ name ].toList();
        qSort( locales.begin(), locales.end(), caseInsensitiveLessThanStringLocale );
        mChildrenLocales[ name ] = locales;
    }
}

void pLocaleModel::debug( const QModelIndex& root, int prof ) const
{
    static bool ok = false;
    
    if ( !ok ) {
        ok = true;
        
        qWarning() << mLocales << mLocales.count();
        foreach ( const QString& name, mLocales ) {
            qWarning() << qPrintable( QString( 0, QChar( '\t' ) ) ) << qPrintable( name );
            
            foreach ( const QString& subName, mChildrenLocales.value( name ) ) {
                qWarning() << qPrintable( QString( 1, QChar( '\t' ) ) ) << qPrintable( subName );
            }
        }
    }
    
    if ( root.isValid() ) {
        const QString indent = QString( prof, '\t' );
        qWarning() << qPrintable( indent ) << qPrintable( root.data().toString() ) << indexToLocale( root ) << root;
        
        prof++;
    }
    
    for ( int i = 0; i < rowCount( root ); i++ ) {
        const QModelIndex index = this->index( i, 0, root );
        Q_ASSERT( index.parent() == root );
        debug( index, prof );
    }
    
    if ( root.isValid() ) {
        prof--;
    }
    
    Q_ASSERT( prof < 2 );
}