ctkLanguageComboBox.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDir>
  16. #include <QDebug>
  17. #include <QLocale>
  18. // CTK includes
  19. #include "ctkLanguageComboBox.h"
  20. // ----------------------------------------------------------------------------
  21. class ctkLanguageComboBoxPrivate
  22. {
  23. Q_DECLARE_PUBLIC(ctkLanguageComboBox);
  24. protected:
  25. ctkLanguageComboBox* const q_ptr;
  26. public:
  27. ctkLanguageComboBoxPrivate(ctkLanguageComboBox& object);
  28. void init();
  29. void addLanguageFiles(const QStringList& fileNames);
  30. bool addLanguage(const QString& language);
  31. bool insertLanguage(int index, const QString& language);
  32. bool languageItem(const QString& language,
  33. QIcon& icon, QString& text,QVariant& data);
  34. QString DefaultLanguage;
  35. QString LanguageDirectory;
  36. };
  37. // ----------------------------------------------------------------------------
  38. ctkLanguageComboBoxPrivate::ctkLanguageComboBoxPrivate(ctkLanguageComboBox &object)
  39. : q_ptr(&object)
  40. {
  41. }
  42. // ----------------------------------------------------------------------------
  43. void ctkLanguageComboBoxPrivate::init()
  44. {
  45. Q_Q(ctkLanguageComboBox);
  46. QObject::connect(q, SIGNAL(currentIndexChanged(int)),
  47. q, SLOT(onLanguageChanged(int)));
  48. /// Add default language if any
  49. if (this->DefaultLanguage.isEmpty())
  50. {
  51. this->addLanguage(this->DefaultLanguage);
  52. }
  53. }
  54. // ----------------------------------------------------------------------------
  55. void ctkLanguageComboBoxPrivate::addLanguageFiles(const QStringList& fileNames)
  56. {
  57. foreach(QString fileName, fileNames)
  58. {
  59. QFileInfo file(fileName);
  60. if (!file.exists())
  61. {
  62. qWarning() << "File " << file.absoluteFilePath() << " doesn't exist.";
  63. }
  64. // language is "de_ch" for a file named "/abc/def_de_ch.qm"
  65. QString language = file.completeBaseName();
  66. language.remove(0,language.indexOf('_') + 1);
  67. this->addLanguage(language);
  68. }
  69. }
  70. // ----------------------------------------------------------------------------
  71. bool ctkLanguageComboBoxPrivate::addLanguage(const QString& language)
  72. {
  73. Q_Q(ctkLanguageComboBox);
  74. return this->insertLanguage(q->count(), language);
  75. }
  76. // ----------------------------------------------------------------------------
  77. bool ctkLanguageComboBoxPrivate::insertLanguage(int index, const QString& language)
  78. {
  79. Q_Q(ctkLanguageComboBox);
  80. QIcon icon;
  81. QString text;
  82. QVariant data;
  83. bool res =this->languageItem(language, icon, text, data);
  84. if (res)
  85. {
  86. q->insertItem(index, icon, text, data);
  87. }
  88. return res;
  89. }
  90. // ----------------------------------------------------------------------------
  91. bool ctkLanguageComboBoxPrivate::languageItem(const QString& language,
  92. QIcon& icon,
  93. QString& text,
  94. QVariant& data)
  95. {
  96. QLocale locale(language);
  97. if (language.isEmpty() ||
  98. locale.name() == "C")
  99. {
  100. icon = QIcon();
  101. text = QString();
  102. data = QVariant();
  103. return false;
  104. }
  105. QString countryFlag = locale.name();
  106. countryFlag.remove(0, countryFlag.lastIndexOf('_') + 1);
  107. countryFlag = countryFlag.toLower();
  108. icon = QIcon(QString(":Icons/Languages/%1.png").arg(countryFlag));
  109. text = QLocale::languageToString(locale.language());
  110. data = locale.name();
  111. return true;
  112. }
  113. // ----------------------------------------------------------------------------
  114. ctkLanguageComboBox::ctkLanguageComboBox(QWidget* _parent)
  115. : QComboBox(_parent)
  116. , d_ptr(new ctkLanguageComboBoxPrivate(*this))
  117. {
  118. Q_D(ctkLanguageComboBox);
  119. d->init();
  120. }
  121. // ----------------------------------------------------------------------------
  122. ctkLanguageComboBox::ctkLanguageComboBox(const QString& defaultLanguage,
  123. QWidget* _parent)
  124. : QComboBox(_parent)
  125. , d_ptr(new ctkLanguageComboBoxPrivate(*this))
  126. {
  127. Q_D(ctkLanguageComboBox);
  128. d->DefaultLanguage = defaultLanguage;
  129. d->init();
  130. }
  131. // ----------------------------------------------------------------------------
  132. ctkLanguageComboBox::~ctkLanguageComboBox()
  133. {
  134. }
  135. // ----------------------------------------------------------------------------
  136. QString ctkLanguageComboBox::defaultLanguage() const
  137. {
  138. Q_D(const ctkLanguageComboBox);
  139. return d->DefaultLanguage;
  140. }
  141. // ----------------------------------------------------------------------------
  142. void ctkLanguageComboBox::setDefaultLanguage(const QString& language)
  143. {
  144. Q_D(ctkLanguageComboBox);
  145. bool isValid = false;
  146. if (!d->DefaultLanguage.isEmpty())
  147. {
  148. QIcon icon;
  149. QString text;
  150. QVariant data;
  151. isValid = d->languageItem(language, icon, text, data);
  152. if (isValid)
  153. {
  154. // Replace the default language
  155. this->setItemIcon(0, icon);
  156. this->setItemText(0, text);
  157. this->setItemData(0, data);
  158. }
  159. else
  160. {
  161. this->removeItem(0);
  162. }
  163. }
  164. else
  165. {
  166. isValid = d->insertLanguage(0, language);
  167. }
  168. d->DefaultLanguage = isValid ? this->itemData(0).toString() : QString();
  169. }
  170. // ----------------------------------------------------------------------------
  171. QString ctkLanguageComboBox::currentLanguage() const
  172. {
  173. return this->itemData(this->currentIndex()).toString();
  174. }
  175. // ----------------------------------------------------------------------------
  176. void ctkLanguageComboBox::setCurrentLanguage(const QString &language)
  177. {
  178. int index = this->findData(QVariant(language));
  179. this->setCurrentIndex(index);
  180. }
  181. // ----------------------------------------------------------------------------
  182. QString ctkLanguageComboBox::directory() const
  183. {
  184. Q_D(const ctkLanguageComboBox);
  185. return d->LanguageDirectory;
  186. }
  187. // ----------------------------------------------------------------------------
  188. void ctkLanguageComboBox::setDirectory(const QString& dir)
  189. {
  190. Q_D(ctkLanguageComboBox);
  191. d->LanguageDirectory = dir;
  192. /// Recover all the translation file from the directory Translations
  193. QDir translationDir = QDir(d->LanguageDirectory);
  194. QStringList languages = translationDir.entryList(QStringList("*.qm"));
  195. /// Add all the languages availables
  196. d->addLanguageFiles(languages);
  197. this->update();
  198. }
  199. // ----------------------------------------------------------------------------
  200. void ctkLanguageComboBox::onLanguageChanged(int index)
  201. {
  202. Q_UNUSED(index);
  203. QString currentLanguage = this->currentLanguage();
  204. emit currentLanguageNameChanged(currentLanguage);
  205. }