VBWidgetPropertyModel.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "VBWidgetPropertyModel.h"
  2. #include "VBWidget.h"
  3. #include "VBProperty.h"
  4. #include <SharedGraphics/Font.h>
  5. VBWidgetPropertyModel::VBWidgetPropertyModel(VBWidget& widget)
  6. : m_widget(widget)
  7. {
  8. }
  9. VBWidgetPropertyModel::~VBWidgetPropertyModel()
  10. {
  11. }
  12. int VBWidgetPropertyModel::row_count(const GModelIndex&) const
  13. {
  14. return m_widget.m_properties.size();
  15. }
  16. String VBWidgetPropertyModel::column_name(int column) const
  17. {
  18. switch (column) {
  19. case Column::Name: return "Name";
  20. case Column::Value: return "Value";
  21. default: ASSERT_NOT_REACHED();
  22. }
  23. }
  24. GModel::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) const
  25. {
  26. UNUSED_PARAM(column);
  27. if (column == Column::Name)
  28. return { 110, TextAlignment::CenterLeft, &Font::default_bold_font() };
  29. return { 90, TextAlignment::CenterLeft };
  30. }
  31. GVariant VBWidgetPropertyModel::data(const GModelIndex& index, Role role) const
  32. {
  33. if (role == Role::Display) {
  34. auto& property = *m_widget.m_properties[index.row()];
  35. switch (index.column()) {
  36. case Column::Name: return property.name();
  37. case Column::Value: return property.value();
  38. }
  39. ASSERT_NOT_REACHED();
  40. }
  41. if (role == Role::ForegroundColor) {
  42. auto& property = *m_widget.m_properties[index.row()];
  43. switch (index.column()) {
  44. case Column::Name: return Color::Black;
  45. case Column::Value: return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
  46. }
  47. ASSERT_NOT_REACHED();
  48. }
  49. return { };
  50. }
  51. void VBWidgetPropertyModel::set_data(const GModelIndex& index, const GVariant& value)
  52. {
  53. ASSERT(index.column() == Column::Value);
  54. auto& property = *m_widget.m_properties[index.row()];
  55. ASSERT(!property.is_readonly());
  56. property.set_value(value);
  57. }
  58. bool VBWidgetPropertyModel::is_editable(const GModelIndex& index) const
  59. {
  60. if (index.column() != Column::Value)
  61. return false;
  62. auto& property = *m_widget.m_properties[index.row()];
  63. return !property.is_readonly();
  64. }