You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovdCEnumerationSettingView.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "ovdCEnumerationSettingView.h"
  2. #include "../ovd_base.h"
  3. #include <algorithm> // std::sort
  4. #include <map>
  5. namespace OpenViBE {
  6. namespace Designer {
  7. namespace Setting {
  8. static void OnChange(GtkEntry* /*entry*/, gpointer data) { static_cast<CEnumerationSettingView*>(data)->onChange(); }
  9. CEnumerationSettingView::CEnumerationSettingView(Kernel::IBox& box, const size_t index, CString& builderName, const Kernel::IKernelContext& ctx,
  10. const CIdentifier& typeID)
  11. : CAbstractSettingView(box, index, builderName, "settings_collection-comboboxentry_setting_enumeration"), m_typeID(typeID), m_kernelCtx(ctx)
  12. {
  13. GtkWidget* setting = CAbstractSettingView::getEntryFieldWidget();
  14. m_comboBox = GTK_COMBO_BOX(setting);
  15. std::vector<std::string> entries;
  16. for (size_t i = 0; i < m_kernelCtx.getTypeManager().getEnumerationEntryCount(m_typeID); ++i)
  17. {
  18. CString name;
  19. uint64_t value;
  20. if (m_kernelCtx.getTypeManager().getEnumerationEntry(m_typeID, i, name, value)) { entries.push_back(name.toASCIIString()); }
  21. }
  22. std::sort(entries.begin(), entries.end());
  23. GtkTreeIter it;
  24. GtkListStore* list = GTK_LIST_STORE(gtk_combo_box_get_model(m_comboBox));
  25. gtk_combo_box_set_wrap_width(m_comboBox, 0);
  26. gtk_list_store_clear(list);
  27. for (size_t i = 0; i < entries.size(); ++i)
  28. {
  29. gtk_list_store_append(list, &it);
  30. gtk_list_store_set(list, &it, 0, entries[i].c_str(), -1);
  31. m_entriesIdx[CString(entries[i].c_str())] = uint64_t(i);
  32. }
  33. CString value;
  34. box.getSettingValue(index, value);
  35. if (m_entriesIdx.count(value.toASCIIString()) == 0)
  36. {
  37. gtk_list_store_append(list, &it);
  38. gtk_list_store_set(list, &it, 0, value.toASCIIString(), -1);
  39. }
  40. CAbstractSettingView::initializeValue();
  41. g_signal_connect(G_OBJECT(m_comboBox), "changed", G_CALLBACK(OnChange), this);
  42. }
  43. void CEnumerationSettingView::getValue(CString& value) const { value = CString(gtk_combo_box_get_active_text(m_comboBox)); }
  44. void CEnumerationSettingView::setValue(const CString& value)
  45. {
  46. m_onValueSetting = true;
  47. // If the current value of the setting is not in the enumeration list, we will add or replace the last value in the list, so it can be set to this value
  48. if (m_entriesIdx.count(value) == 0)
  49. {
  50. GtkTreeIter it;
  51. GtkListStore* list = GTK_LIST_STORE(gtk_combo_box_get_model(m_comboBox));
  52. int valuesInModel = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(list), nullptr);
  53. if (valuesInModel == int(m_entriesIdx.size()))
  54. {
  55. gtk_list_store_append(list, &it);
  56. valuesInModel += 1;
  57. }
  58. else
  59. {
  60. // We just set the iterator at the end
  61. GtkTreePath* treePath = gtk_tree_path_new_from_indices(valuesInModel - 1, -1);
  62. gtk_tree_model_get_iter(GTK_TREE_MODEL(list), &it, treePath);
  63. gtk_tree_path_free(treePath);
  64. }
  65. gtk_list_store_set(list, &it, 0, value.toASCIIString(), -1);
  66. gtk_combo_box_set_active(m_comboBox, valuesInModel - 1);
  67. }
  68. else { gtk_combo_box_set_active(m_comboBox, gint(m_entriesIdx[value])); }
  69. m_onValueSetting = false;
  70. }
  71. void CEnumerationSettingView::onChange()
  72. {
  73. if (!m_onValueSetting)
  74. {
  75. gchar* value = gtk_combo_box_get_active_text(m_comboBox);
  76. getBox().setSettingValue(getSettingIndex(), value);
  77. g_free(value);
  78. }
  79. }
  80. } // namespace Setting
  81. } // namespace Designer
  82. } // namespace OpenViBE