VBWidgetPropertyModel.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "VBWidgetPropertyModel.h"
  2. #include "VBProperty.h"
  3. #include "VBWidget.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:
  20. return "Name";
  21. case Column::Value:
  22. return "Value";
  23. default:
  24. ASSERT_NOT_REACHED();
  25. }
  26. }
  27. GModel::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) const
  28. {
  29. UNUSED_PARAM(column);
  30. if (column == Column::Name)
  31. return { 110, TextAlignment::CenterLeft, &Font::default_bold_font() };
  32. return { 90, TextAlignment::CenterLeft };
  33. }
  34. GVariant VBWidgetPropertyModel::data(const GModelIndex& index, Role role) const
  35. {
  36. if (role == Role::Display) {
  37. auto& property = *m_widget.m_properties[index.row()];
  38. switch (index.column()) {
  39. case Column::Name:
  40. return property.name();
  41. case Column::Value:
  42. return property.value();
  43. }
  44. ASSERT_NOT_REACHED();
  45. }
  46. if (role == Role::ForegroundColor) {
  47. auto& property = *m_widget.m_properties[index.row()];
  48. switch (index.column()) {
  49. case Column::Name:
  50. return Color::Black;
  51. case Column::Value:
  52. return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
  53. }
  54. ASSERT_NOT_REACHED();
  55. }
  56. return {};
  57. }
  58. void VBWidgetPropertyModel::set_data(const GModelIndex& index, const GVariant& value)
  59. {
  60. ASSERT(index.column() == Column::Value);
  61. auto& property = *m_widget.m_properties[index.row()];
  62. ASSERT(!property.is_readonly());
  63. property.set_value(value);
  64. }
  65. bool VBWidgetPropertyModel::is_editable(const GModelIndex& index) const
  66. {
  67. if (index.column() != Column::Value)
  68. return false;
  69. auto& property = *m_widget.m_properties[index.row()];
  70. return !property.is_readonly();
  71. }