ctkLanguageComboBox.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 addLanguages(const QStringList& languages);
  30. void addLanguage(const QString& language);
  31. QString DefaultLanguage;
  32. QString Dir;
  33. };
  34. // ----------------------------------------------------------------------------
  35. ctkLanguageComboBoxPrivate::ctkLanguageComboBoxPrivate(ctkLanguageComboBox &object)
  36. : q_ptr(&object)
  37. {
  38. }
  39. // ----------------------------------------------------------------------------
  40. void ctkLanguageComboBoxPrivate::init()
  41. {
  42. Q_Q(ctkLanguageComboBox);
  43. /// Recover all the translation file from the directory Translations
  44. QDir translationDir = QDir(this->Dir);
  45. QStringList languages = translationDir.entryList(QStringList("*.qm"));
  46. /// Add default language.
  47. if (!this->DefaultLanguage.isEmpty())
  48. {
  49. this->addLanguage(this->DefaultLanguage);
  50. }
  51. /// Add all the languages availables
  52. this->addLanguages(languages);
  53. QObject::connect(q, SIGNAL(currentIndexChanged(int)),
  54. q, SLOT(onLanguageChanged(int)));
  55. }
  56. // ----------------------------------------------------------------------------
  57. void ctkLanguageComboBoxPrivate::addLanguages(const QStringList& languages)
  58. {
  59. foreach(QString language, languages)
  60. {
  61. language.remove(0,language.indexOf('_') + 1);
  62. language.chop(3);
  63. this->addLanguage(language);
  64. }
  65. }
  66. // ----------------------------------------------------------------------------
  67. void ctkLanguageComboBoxPrivate::addLanguage(const QString& language)
  68. {
  69. Q_Q(ctkLanguageComboBox);
  70. QLocale lang(language);
  71. QString icon = ":Icons/Languages/";
  72. icon += language;
  73. icon += ".png";
  74. q->addItem(QIcon(icon), QLocale::languageToString(lang.language()), language);
  75. }
  76. // ----------------------------------------------------------------------------
  77. ctkLanguageComboBox::ctkLanguageComboBox(QWidget* _parent)
  78. : QComboBox(_parent)
  79. , d_ptr(new ctkLanguageComboBoxPrivate(*this))
  80. {
  81. }
  82. // ----------------------------------------------------------------------------
  83. ctkLanguageComboBox::~ctkLanguageComboBox()
  84. {
  85. }
  86. // ----------------------------------------------------------------------------
  87. QString ctkLanguageComboBox::currentLanguage() const
  88. {
  89. return this->itemData(this->currentIndex()).toString();
  90. }
  91. // ----------------------------------------------------------------------------
  92. void ctkLanguageComboBox::setCurrentLanguage(const QString &language)
  93. {
  94. int index = this->findData(QVariant(language));
  95. this->setCurrentIndex(index);
  96. }
  97. // ----------------------------------------------------------------------------
  98. QString ctkLanguageComboBox::defaultLanguage() const
  99. {
  100. Q_D(const ctkLanguageComboBox);
  101. return d->DefaultLanguage;
  102. }
  103. // ----------------------------------------------------------------------------
  104. void ctkLanguageComboBox::setDefaultLanguage(const QString& language)
  105. {
  106. Q_D(ctkLanguageComboBox);
  107. d->DefaultLanguage = language;
  108. }
  109. // ----------------------------------------------------------------------------
  110. QString ctkLanguageComboBox::directory() const
  111. {
  112. Q_D(const ctkLanguageComboBox);
  113. return d->Dir;
  114. }
  115. // ----------------------------------------------------------------------------
  116. void ctkLanguageComboBox::setDirectory(const QString& dir)
  117. {
  118. Q_D(ctkLanguageComboBox);
  119. d->Dir = dir;
  120. d->init();
  121. this->update();
  122. }
  123. // ----------------------------------------------------------------------------
  124. void ctkLanguageComboBox::onLanguageChanged(int index)
  125. {
  126. QVariant lang = this->itemData(index);
  127. //QLocale locale(lang.toString());
  128. emit currentLanguageNameChanged(lang.toString());
  129. }