|
@@ -34,11 +34,14 @@ protected:
|
|
|
public:
|
|
|
ctkLanguageComboBoxPrivate(ctkLanguageComboBox& object);
|
|
|
void init();
|
|
|
- void addLanguages(const QStringList& languages);
|
|
|
- void addLanguage(const QString& language);
|
|
|
-
|
|
|
- QString DefaultLanguage;
|
|
|
- QString Dir;
|
|
|
+ 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;
|
|
|
};
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
@@ -51,68 +54,103 @@ ctkLanguageComboBoxPrivate::ctkLanguageComboBoxPrivate(ctkLanguageComboBox &obje
|
|
|
void ctkLanguageComboBoxPrivate::init()
|
|
|
{
|
|
|
Q_Q(ctkLanguageComboBox);
|
|
|
- /// Recover all the translation file from the directory Translations
|
|
|
- QDir translationDir = QDir(this->Dir);
|
|
|
|
|
|
- QStringList languages = translationDir.entryList(QStringList("*.qm"));
|
|
|
+ QObject::connect(q, SIGNAL(currentIndexChanged(int)),
|
|
|
+ q, SLOT(onLanguageChanged(int)));
|
|
|
|
|
|
- /// Add default language.
|
|
|
- if (!this->DefaultLanguage.isEmpty())
|
|
|
+ /// Add default language if any
|
|
|
+ if (this->DefaultLanguage.isEmpty())
|
|
|
{
|
|
|
this->addLanguage(this->DefaultLanguage);
|
|
|
}
|
|
|
- /// Add all the languages availables
|
|
|
- this->addLanguages(languages);
|
|
|
-
|
|
|
- QObject::connect(q, SIGNAL(currentIndexChanged(int)),
|
|
|
- q, SLOT(onLanguageChanged(int)));
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-void ctkLanguageComboBoxPrivate::addLanguages(const QStringList& languages)
|
|
|
+void ctkLanguageComboBoxPrivate::addLanguageFiles(const QStringList& fileNames)
|
|
|
{
|
|
|
- foreach(QString language, languages)
|
|
|
+ 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);
|
|
|
- language.chop(3);
|
|
|
this->addLanguage(language);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-void ctkLanguageComboBoxPrivate::addLanguage(const QString& language)
|
|
|
+bool ctkLanguageComboBoxPrivate::addLanguage(const QString& language)
|
|
|
{
|
|
|
Q_Q(ctkLanguageComboBox);
|
|
|
- QLocale lang(language);
|
|
|
- QString icon = ":Icons/Languages/";
|
|
|
- icon += language;
|
|
|
- icon += ".png";
|
|
|
- q->addItem(QIcon(icon), QLocale::languageToString(lang.language()), language);
|
|
|
+ return this->insertLanguage(q->count(), language);
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-ctkLanguageComboBox::ctkLanguageComboBox(QWidget* _parent)
|
|
|
- : QComboBox(_parent)
|
|
|
- , d_ptr(new ctkLanguageComboBoxPrivate(*this))
|
|
|
+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;
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-ctkLanguageComboBox::~ctkLanguageComboBox()
|
|
|
+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;
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-QString ctkLanguageComboBox::currentLanguage() const
|
|
|
+ctkLanguageComboBox::ctkLanguageComboBox(QWidget* _parent)
|
|
|
+ : QComboBox(_parent)
|
|
|
+ , d_ptr(new ctkLanguageComboBoxPrivate(*this))
|
|
|
{
|
|
|
- return this->itemData(this->currentIndex()).toString();
|
|
|
+ Q_D(ctkLanguageComboBox);
|
|
|
+ d->init();
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
-void ctkLanguageComboBox::setCurrentLanguage(const QString &language)
|
|
|
+ctkLanguageComboBox::ctkLanguageComboBox(const QString& defaultLanguage,
|
|
|
+ QWidget* _parent)
|
|
|
+ : QComboBox(_parent)
|
|
|
+ , d_ptr(new ctkLanguageComboBoxPrivate(*this))
|
|
|
+{
|
|
|
+ Q_D(ctkLanguageComboBox);
|
|
|
+ d->DefaultLanguage = defaultLanguage;
|
|
|
+ d->init();
|
|
|
+}
|
|
|
+
|
|
|
+// ----------------------------------------------------------------------------
|
|
|
+ctkLanguageComboBox::~ctkLanguageComboBox()
|
|
|
{
|
|
|
- int index = this->findData(QVariant(language));
|
|
|
- this->setCurrentIndex(index);
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
@@ -126,29 +164,73 @@ QString ctkLanguageComboBox::defaultLanguage() const
|
|
|
void ctkLanguageComboBox::setDefaultLanguage(const QString& language)
|
|
|
{
|
|
|
Q_D(ctkLanguageComboBox);
|
|
|
- d->DefaultLanguage = language;
|
|
|
+ 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->Dir;
|
|
|
+ return d->LanguageDirectory;
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
void ctkLanguageComboBox::setDirectory(const QString& dir)
|
|
|
{
|
|
|
Q_D(ctkLanguageComboBox);
|
|
|
- d->Dir = dir;
|
|
|
- d->init();
|
|
|
+
|
|
|
+ d->LanguageDirectory = dir;
|
|
|
+
|
|
|
+ /// Recover all the translation file from the directory Translations
|
|
|
+ QDir translationDir = QDir(d->LanguageDirectory);
|
|
|
+ QStringList languages = translationDir.entryList(QStringList("*.qm"));
|
|
|
+
|
|
|
+ /// Add all the languages availables
|
|
|
+ d->addLanguageFiles(languages);
|
|
|
+
|
|
|
this->update();
|
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
void ctkLanguageComboBox::onLanguageChanged(int index)
|
|
|
{
|
|
|
- QVariant lang = this->itemData(index);
|
|
|
- //QLocale locale(lang.toString());
|
|
|
- emit currentLanguageNameChanged(lang.toString());
|
|
|
+ Q_UNUSED(index);
|
|
|
+ QString currentLanguage = this->currentLanguage();
|
|
|
+ emit currentLanguageNameChanged(currentLanguage);
|
|
|
}
|