VBWidgetPropertyModel.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "VBWidgetPropertyModel.h"
  27. #include "VBProperty.h"
  28. #include "VBWidget.h"
  29. #include <LibGfx/Font.h>
  30. VBWidgetPropertyModel::VBWidgetPropertyModel(VBWidget& widget)
  31. : m_widget(widget)
  32. {
  33. }
  34. VBWidgetPropertyModel::~VBWidgetPropertyModel()
  35. {
  36. }
  37. int VBWidgetPropertyModel::row_count(const GUI::ModelIndex&) const
  38. {
  39. return m_widget.m_properties.size();
  40. }
  41. String VBWidgetPropertyModel::column_name(int column) const
  42. {
  43. switch (column) {
  44. case Column::Name:
  45. return "Name";
  46. case Column::Value:
  47. return "Value";
  48. case Column::Type:
  49. return "Type";
  50. default:
  51. ASSERT_NOT_REACHED();
  52. }
  53. }
  54. GUI::Variant VBWidgetPropertyModel::data(const GUI::ModelIndex& index, Role role) const
  55. {
  56. if (role == Role::TextAlignment) {
  57. return Gfx::TextAlignment::CenterLeft;
  58. }
  59. if (role == Role::Font) {
  60. if (index.column() == Column::Name)
  61. return Gfx::Font::default_bold_font();
  62. return {};
  63. }
  64. if (role == Role::Custom) {
  65. auto& property = m_widget.m_properties[index.row()];
  66. if (index.column() == Column::Type)
  67. return (int)property.value().type();
  68. return {};
  69. }
  70. if (role == Role::Display) {
  71. auto& property = m_widget.m_properties[index.row()];
  72. switch (index.column()) {
  73. case Column::Name:
  74. return property.name();
  75. case Column::Value:
  76. return property.value();
  77. case Column::Type:
  78. return to_string(property.value().type());
  79. }
  80. ASSERT_NOT_REACHED();
  81. }
  82. if (role == Role::ForegroundColor) {
  83. auto& property = m_widget.m_properties[index.row()];
  84. switch (index.column()) {
  85. case Column::Name:
  86. return Color::Black;
  87. case Column::Type:
  88. return Color::Blue;
  89. case Column::Value:
  90. return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
  91. }
  92. ASSERT_NOT_REACHED();
  93. }
  94. return {};
  95. }
  96. void VBWidgetPropertyModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
  97. {
  98. ASSERT(index.column() == Column::Value);
  99. auto& property = m_widget.m_properties[index.row()];
  100. ASSERT(!property.is_readonly());
  101. property.set_value(value);
  102. }
  103. bool VBWidgetPropertyModel::is_editable(const GUI::ModelIndex& index) const
  104. {
  105. if (index.column() != Column::Value)
  106. return false;
  107. auto& property = m_widget.m_properties[index.row()];
  108. return !property.is_readonly();
  109. }