| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 | 
							- /*=========================================================================
 
-   Library:   CTK
 
-   Copyright (c) Kitware Inc.
 
-   Licensed under the Apache License, Version 2.0 (the "License");
 
-   you may not use this file except in compliance with the License.
 
-   You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0.txt
 
-   Unless required by applicable law or agreed to in writing, software
 
-   distributed under the License is distributed on an "AS IS" BASIS,
 
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-   See the License for the specific language governing permissions and
 
-   limitations under the License.
 
- =========================================================================*/
 
- // Qt includes
 
- #include <QDir>
 
- #include <QDebug>
 
- #include <QLocale>
 
- // CTK includes
 
- #include "ctkLanguageComboBox.h"
 
- // ----------------------------------------------------------------------------
 
- class ctkLanguageComboBoxPrivate
 
- {
 
-   Q_DECLARE_PUBLIC(ctkLanguageComboBox);
 
- protected:
 
-   ctkLanguageComboBox* const q_ptr;
 
- public:
 
-   ctkLanguageComboBoxPrivate(ctkLanguageComboBox& object);
 
-   void init();
 
-   void addLanguageFiles(const QStringList& fileNames);
 
-   bool addLanguage(const QString& language);
 
-   bool insertLanguage(int index, const QString& language);
 
-   bool languageItem(const QString& language,
 
-                     QIcon& icon, QString& text,QVariant& data);
 
-   QString DefaultLanguage;
 
-   QString LanguageDirectory;
 
- };
 
- // ----------------------------------------------------------------------------
 
- ctkLanguageComboBoxPrivate::ctkLanguageComboBoxPrivate(ctkLanguageComboBox &object)
 
-   : q_ptr(&object)
 
- {
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBoxPrivate::init()
 
- {
 
-   Q_Q(ctkLanguageComboBox);
 
-   QObject::connect(q, SIGNAL(currentIndexChanged(int)),
 
-                    q, SLOT(onLanguageChanged(int)));
 
-   /// Add default language if any
 
-   if (this->DefaultLanguage.isEmpty())
 
-     {
 
-     this->addLanguage(this->DefaultLanguage);
 
-     }
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBoxPrivate::addLanguageFiles(const QStringList& fileNames)
 
- {
 
-   foreach(QString fileName, fileNames)
 
-     {
 
-     QFileInfo file(fileName);
 
-     if (!file.exists())
 
-       {
 
-       qWarning() << "File " << file.absoluteFilePath() << " doesn't exist.";
 
-       }
 
-     // language is "de_ch" for a file named "/abc/def_de_ch.qm"
 
-     QString language = file.completeBaseName();
 
-     language.remove(0,language.indexOf('_') + 1);
 
-     this->addLanguage(language);
 
-     }
 
- }
 
- // ----------------------------------------------------------------------------
 
- bool ctkLanguageComboBoxPrivate::addLanguage(const QString& language)
 
- {
 
-   Q_Q(ctkLanguageComboBox);
 
-   return this->insertLanguage(q->count(), language);
 
- }
 
- // ----------------------------------------------------------------------------
 
- bool ctkLanguageComboBoxPrivate::insertLanguage(int index, const QString& language)
 
- {
 
-   Q_Q(ctkLanguageComboBox);
 
-   QIcon icon;
 
-   QString text;
 
-   QVariant data;
 
-   bool res =this->languageItem(language, icon, text, data);
 
-   if (res)
 
-     {
 
-     q->insertItem(index, icon, text, data);
 
-     }
 
-   return res;
 
- }
 
- // ----------------------------------------------------------------------------
 
- bool ctkLanguageComboBoxPrivate::languageItem(const QString& language,
 
-                                               QIcon& icon,
 
-                                               QString& text,
 
-                                               QVariant& data)
 
- {
 
-   QLocale locale(language);
 
-   if (language.isEmpty() ||
 
-       locale.name() == "C")
 
-     {
 
-     icon = QIcon();
 
-     text = QString();
 
-     data = QVariant();
 
-     return false;
 
-     }
 
-   QString countryFlag = locale.name();
 
-   countryFlag.remove(0, countryFlag.lastIndexOf('_') + 1);
 
-   countryFlag = countryFlag.toLower();
 
-   icon = QIcon(QString(":Icons/Languages/%1.png").arg(countryFlag));
 
-   text = QLocale::languageToString(locale.language());
 
-   data = locale.name();
 
-   return true;
 
- }
 
- // ----------------------------------------------------------------------------
 
- ctkLanguageComboBox::ctkLanguageComboBox(QWidget* _parent)
 
-   : QComboBox(_parent)
 
-   , d_ptr(new ctkLanguageComboBoxPrivate(*this))
 
- {
 
-   Q_D(ctkLanguageComboBox);
 
-   d->init();
 
- }
 
- // ----------------------------------------------------------------------------
 
- ctkLanguageComboBox::ctkLanguageComboBox(const QString& defaultLanguage,
 
-                                          QWidget* _parent)
 
-   : QComboBox(_parent)
 
-   , d_ptr(new ctkLanguageComboBoxPrivate(*this))
 
- {
 
-   Q_D(ctkLanguageComboBox);
 
-   d->DefaultLanguage = defaultLanguage;
 
-   d->init();
 
- }
 
- // ----------------------------------------------------------------------------
 
- ctkLanguageComboBox::~ctkLanguageComboBox()
 
- {
 
- }
 
- // ----------------------------------------------------------------------------
 
- QString ctkLanguageComboBox::defaultLanguage() const
 
- {
 
-   Q_D(const ctkLanguageComboBox);
 
-   return d->DefaultLanguage;
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBox::setDefaultLanguage(const QString& language)
 
- {
 
-   Q_D(ctkLanguageComboBox);
 
-   bool isValid = false;
 
-   if (!d->DefaultLanguage.isEmpty())
 
-     {
 
-     QIcon icon;
 
-     QString text;
 
-     QVariant data;
 
-     isValid = d->languageItem(language, icon, text, data);
 
-     if (isValid)
 
-       {
 
-       // Replace the default language
 
-       this->setItemIcon(0, icon);
 
-       this->setItemText(0, text);
 
-       this->setItemData(0, data);
 
-       }
 
-     else
 
-       {
 
-       this->removeItem(0);
 
-       }
 
-     }
 
-   else
 
-     {
 
-     isValid = d->insertLanguage(0, language);
 
-     }
 
-   d->DefaultLanguage = isValid ? this->itemData(0).toString() : QString();
 
- }
 
- // ----------------------------------------------------------------------------
 
- QString ctkLanguageComboBox::currentLanguage() const
 
- {
 
-   return this->itemData(this->currentIndex()).toString();
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBox::setCurrentLanguage(const QString &language)
 
- {
 
-   int index = this->findData(QVariant(language));
 
-   this->setCurrentIndex(index);
 
- }
 
- // ----------------------------------------------------------------------------
 
- QString ctkLanguageComboBox::directory() const
 
- {
 
-   Q_D(const ctkLanguageComboBox);
 
-   return d->LanguageDirectory;
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBox::setDirectory(const QString& dir)
 
- {
 
-   Q_D(ctkLanguageComboBox);
 
-   d->LanguageDirectory = dir;
 
-   /// Recover all the translation file from the directory Translations
 
-   QDir translationDir = QDir(d->LanguageDirectory);
 
-   QStringList files;
 
-   QStringList fileNames = translationDir.entryList(QStringList("*.qm"));
 
-   foreach(const QString& fileName, fileNames)
 
-     {
 
-     files << translationDir.filePath(fileName);
 
-     }
 
-   /// Add all the languages availables
 
-   d->addLanguageFiles(files);
 
-   this->update();
 
- }
 
- // ----------------------------------------------------------------------------
 
- void ctkLanguageComboBox::onLanguageChanged(int index)
 
- {
 
-   Q_UNUSED(index);
 
-   QString currentLanguage = this->currentLanguage();
 
-   emit currentLanguageNameChanged(currentLanguage);
 
- }
 
 
  |