VBWidgetPropertyModel.cpp 2.5 KB

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