Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "qxtlanguagecombobox.h"
00027 #include "qxtlanguagecombobox_p.h"
00028 #include <QDir>
00029 #include <QApplication>
00030
00031 class Language;
00032 typedef QList<Language> LanguageList;
00033
00034
00035 static QStringList findQmFiles(const QString& pathToTranslations)
00036 {
00037 QDir dir(pathToTranslations);
00038 QStringList fileNames = dir.entryList(QStringList("*.qm"), QDir::Files, QDir::Name);
00039
00040 QMutableStringListIterator i(fileNames);
00041 while (i.hasNext())
00042 {
00043 i.next();
00044 int start = i.value().indexOf('_');
00045 int end = i.value().lastIndexOf('.');
00046 QString s = i.value().mid(start + 1, end - start - 1).toLower();
00047
00048 i.setValue(s);
00049 }
00050
00051 return fileNames;
00052 }
00053
00054
00055 class Language
00056 {
00057 public:
00058 Language(const QLocale::Language& language)
00059 : _mLanguage(language)
00060 , _mCountryCode("C")
00061 {
00062 QLocale loc(language);
00063 if (loc.language() == language)
00064 _mCountryCode = loc.name().right(2);
00065 else
00066 _mCountryCode = "";
00067
00068 _mDisplayName = qApp->translate("QLocale", qPrintable(QLocale::languageToString(_mLanguage)));
00069 };
00070
00071 bool operator<(const Language& lang) const
00072 {
00073 return _mDisplayName.localeAwareCompare(lang._mDisplayName) < 0;
00074 }
00075
00076 static const LanguageList& getAllLanguages()
00077 {
00078 if (_smAllLanguages.empty())
00079 {
00080
00081 for (int idx = 0; idx < QLocale::LastLanguage; ++idx)
00082 {
00083 QLocale::Language l = static_cast<QLocale::Language>(idx);
00084 if (l == QLocale::LastLanguage)
00085 continue;
00086
00087 if (l == QLocale::Nynorsk)
00088 continue;
00089 if (l == QLocale::C)
00090 continue;
00091
00092 _smAllLanguages.push_back(Language(l));
00093 }
00094
00095 qSort(_smAllLanguages);
00096 }
00097 return _smAllLanguages;
00098 };
00099
00100 static LanguageList getTrLanguages(const QString& translationPath)
00101 {
00102 LanguageList trLanguages;
00103
00104 QStringList qms = findQmFiles(translationPath);
00105 for (int i = 0; i < qms.size(); ++i)
00106 {
00107 QLocale locale(qms[i]);
00108 if (locale.language() == QLocale::C)
00109 continue;
00110 trLanguages.push_back(Language(locale.language()));
00111 }
00112 qSort(trLanguages);
00113 return trLanguages;
00114 };
00115
00116 const QString& name() const
00117 {
00118 return _mDisplayName;
00119 }
00120 const QLocale::Language& language() const
00121 {
00122 return _mLanguage;
00123 }
00124 const QString& countryName() const
00125 {
00126 return _mCountryCode;
00127 }
00128
00129 private:
00130 QLocale::Language _mLanguage;
00131 QString _mDisplayName;
00132 QString _mCountryCode;
00133
00134 static LanguageList _smAllLanguages;
00135 };
00136
00137 LanguageList Language::_smAllLanguages;
00138
00139 class LanguageModel : public QAbstractTableModel
00140 {
00141 public:
00142 LanguageModel(const LanguageList& languages, QObject* parent = 0)
00143 : QAbstractTableModel(parent), _mLanguages(languages)
00144 {
00145 }
00146
00147 int rowCount(const QModelIndex&) const
00148 {
00149 return _mLanguages.size();
00150 }
00151
00152 int columnCount(const QModelIndex&) const
00153 {
00154 return 2;
00155 }
00156
00157 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
00158 {
00159 if (!index.isValid())
00160 return QVariant();
00161 if (_mLanguages.empty())
00162 return QVariant();
00163
00164 int idx = qMin(index.row(), _mLanguages.size());
00165 const Language& l = _mLanguages[idx];
00166 if (role == Qt::DecorationRole)
00167 {
00168 return QIcon(":/flags/" + l.countryName() + ".png");
00169 }
00170
00171 if (role == Qt::DisplayRole)
00172 {
00173 switch (index.column())
00174 {
00175 case 0:
00176 return l.name();
00177 case 1:
00178 return l.language();
00179 default:
00180 return QVariant();
00181 }
00182 }
00183 return QVariant();
00184 }
00185 private:
00186 LanguageList _mLanguages;
00187 };
00188
00189 QxtLanguageComboBoxPrivate::QxtLanguageComboBoxPrivate()
00190 : _mDisplayMode(QxtLanguageComboBox::AllLanguages), _mTranslationPath("."), _mModel(0)
00191 {
00192 }
00193
00194 void QxtLanguageComboBoxPrivate::init()
00195 {
00196 connect(&qxt_p(), SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxCurrentIndexChanged(int)));
00197 }
00198
00199 void QxtLanguageComboBoxPrivate::setTranslationPath(const QString& path)
00200 {
00201 if (_mTranslationPath == path)
00202 return;
00203
00204 _mTranslationPath = path;
00205 reset();
00206 }
00207
00208 void QxtLanguageComboBoxPrivate::setDisplayMode(QxtLanguageComboBox::DisplayMode mode)
00209 {
00210 if (_mDisplayMode == mode && _mModel != 0)
00211 return;
00212
00213 _mDisplayMode = mode;
00214 reset();
00215 }
00216
00217 void QxtLanguageComboBoxPrivate::reset()
00218 {
00219 if (_mModel != 0)
00220 {
00221 delete _mModel;
00222 _mModel = 0;
00223 }
00224
00225 QLocale::Language currentLang = currentLanguage();
00226 if (_mDisplayMode == QxtLanguageComboBox::AllLanguages)
00227 _mModel = new LanguageModel(Language::getAllLanguages(), &qxt_p());
00228 else
00229 _mModel = new LanguageModel(Language::getTrLanguages(_mTranslationPath), &qxt_p());
00230
00231 qxt_p().setModel(_mModel);
00232 qxt_p().setModelColumn(0);
00233
00234 setCurrentLanguage(currentLang);
00235 }
00236
00237 void QxtLanguageComboBoxPrivate::comboBoxCurrentIndexChanged(int)
00238 {
00239 handleLanguageChange();
00240 }
00241
00242 void QxtLanguageComboBoxPrivate::handleLanguageChange()
00243 {
00244 emit qxt_p().currentLanguageChanged(currentLanguage());
00245 emit qxt_p().currentLanguageNameChanged(currentLanguageName());
00246 }
00247
00248 QLocale::Language QxtLanguageComboBoxPrivate::currentLanguage() const
00249 {
00250 if (_mModel == NULL)
00251 return QLocale::C;
00252
00253 QModelIndex idx = _mModel->index(qxt_p().currentIndex(), 1);
00254 QLocale::Language currentLang = static_cast<QLocale::Language>(idx.data().toInt());
00255 return currentLang;
00256 }
00257
00258 QString QxtLanguageComboBoxPrivate::currentLanguageName() const
00259 {
00260 return qxt_p().currentText();
00261 }
00262
00263 void QxtLanguageComboBoxPrivate::setCurrentLanguage(QLocale::Language language)
00264 {
00265
00266 QModelIndex start = _mModel->index(0, 1);
00267 QModelIndexList result = _mModel->match(start, Qt::DisplayRole, language, 1, Qt::MatchExactly);
00268 if (!result.isEmpty())
00269 qxt_p().setCurrentIndex(result.first().row());
00270
00271
00272
00273 handleLanguageChange();
00274 }
00275
00311 QxtLanguageComboBox::QxtLanguageComboBox(QWidget* parent)
00312 : QComboBox(parent)
00313 {
00314 QXT_INIT_PRIVATE(QxtLanguageComboBox);
00315 setDisplayMode(AllLanguages);
00316 setCurrentLanguage(QLocale::system().language());
00317 qxt_d().init();
00318 }
00319
00323 QxtLanguageComboBox::~QxtLanguageComboBox()
00324 {
00325 }
00326
00331 QLocale::Language QxtLanguageComboBox::currentLanguage() const
00332 {
00333 return qxt_d().currentLanguage();
00334 }
00335
00340 QString QxtLanguageComboBox::currentLanguageName() const
00341 {
00342 return qxt_d().currentLanguageName();
00343 }
00344
00345 void QxtLanguageComboBox::setCurrentLanguage(QLocale::Language language)
00346 {
00347 qxt_d().setCurrentLanguage(language);
00348 }
00349
00354 void QxtLanguageComboBox::setDisplayMode(DisplayMode mode)
00355 {
00356 qxt_d().setDisplayMode(mode);
00357 }
00358
00359 QxtLanguageComboBox::DisplayMode QxtLanguageComboBox::displayMode() const
00360 {
00361 return qxt_d().displayMode();
00362 }
00363
00368 void QxtLanguageComboBox::setTranslationPath(const QString& path)
00369 {
00370 qxt_d().setTranslationPath(path);
00371 }
00372
00373 QString QxtLanguageComboBox::translationPath() const
00374 {
00375 return qxt_d().translationPath();
00376 }