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.

ovdCColorSettingView.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "ovdCColorSettingView.h"
  2. #include "../ovd_base.h"
  3. #include <cmath>
  4. namespace OpenViBE {
  5. namespace Designer {
  6. namespace Setting {
  7. static int Color2Percent(const guint16 color) { return int(round(color / 655.350)); } // c * 100 / 65535
  8. static guint16 Percent2Color(const int color) { return guint16(color * 655.35); } // c * 65535 / 100
  9. static void OnButtonSettingColorChoosePressed(GtkColorButton* /*button*/, gpointer data) { static_cast<CColorSettingView*>(data)->selectColor(); }
  10. static void OnChange(GtkEntry* /*entry*/, gpointer data) { static_cast<CColorSettingView*>(data)->onChange(); }
  11. CColorSettingView::CColorSettingView(Kernel::IBox& box, const size_t index, CString& builderName, const Kernel::IKernelContext& ctx)
  12. : CAbstractSettingView(box, index, builderName, "settings_collection-hbox_setting_color"), m_kernelCtx(ctx)
  13. {
  14. GtkWidget* settingWidget = CAbstractSettingView::getEntryFieldWidget();
  15. std::vector<GtkWidget*> widgets;
  16. CAbstractSettingView::extractWidget(settingWidget, widgets);
  17. m_entry = GTK_ENTRY(widgets[0]);
  18. m_button = GTK_COLOR_BUTTON(widgets[1]);
  19. g_signal_connect(G_OBJECT(m_entry), "changed", G_CALLBACK(OnChange), this);
  20. g_signal_connect(G_OBJECT(m_button), "color-set", G_CALLBACK(OnButtonSettingColorChoosePressed), this);
  21. CAbstractSettingView::initializeValue();
  22. }
  23. void CColorSettingView::getValue(CString& value) const { value = CString(gtk_entry_get_text(m_entry)); }
  24. void CColorSettingView::setValue(const CString& value)
  25. {
  26. m_onValueSetting = true;
  27. int r = 0, g = 0, b = 0;
  28. sscanf(m_kernelCtx.getConfigurationManager().expand(value).toASCIIString(), "%i,%i,%i", &r, &g, &b);
  29. GdkColor color;
  30. color.red = Percent2Color(r);
  31. color.green = Percent2Color(g);
  32. color.blue = Percent2Color(b);
  33. gtk_color_button_set_color(m_button, &color);
  34. gtk_entry_set_text(m_entry, value);
  35. m_onValueSetting = false;
  36. }
  37. void CColorSettingView::selectColor()
  38. {
  39. GdkColor color;
  40. gtk_color_button_get_color(m_button, &color);
  41. const std::string value = std::to_string(Color2Percent(color.red)) + "," + std::to_string(Color2Percent(color.green)) + ","
  42. + std::to_string(Color2Percent(color.blue));
  43. getBox().setSettingValue(getSettingIndex(), value.c_str());
  44. setValue(value.c_str());
  45. }
  46. void CColorSettingView::onChange()
  47. {
  48. if (!m_onValueSetting)
  49. {
  50. const gchar* value = gtk_entry_get_text(m_entry);
  51. getBox().setSettingValue(getSettingIndex(), value);
  52. }
  53. }
  54. } // namespace Setting
  55. } // namespace Designer
  56. } // namespace OpenViBE