VBWidgetPropertyModel.cpp 3.9 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 <LibDraw/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::Model::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) const
  55. {
  56. UNUSED_PARAM(column);
  57. if (column == Column::Name)
  58. return { 110, Gfx::TextAlignment::CenterLeft, &Gfx::Font::default_bold_font() };
  59. return { 90, Gfx::TextAlignment::CenterLeft };
  60. }
  61. GUI::Variant VBWidgetPropertyModel::data(const GUI::ModelIndex& index, Role role) const
  62. {
  63. if (role == Role::Custom) {
  64. auto& property = m_widget.m_properties[index.row()];
  65. if (index.column() == Column::Type)
  66. return (int)property.value().type();
  67. return {};
  68. }
  69. if (role == Role::Display) {
  70. auto& property = m_widget.m_properties[index.row()];
  71. switch (index.column()) {
  72. case Column::Name:
  73. return property.name();
  74. case Column::Value:
  75. return property.value();
  76. case Column::Type:
  77. return to_string(property.value().type());
  78. }
  79. ASSERT_NOT_REACHED();
  80. }
  81. if (role == Role::ForegroundColor) {
  82. auto& property = m_widget.m_properties[index.row()];
  83. switch (index.column()) {
  84. case Column::Name:
  85. return Color::Black;
  86. case Column::Type:
  87. return Color::Blue;
  88. case Column::Value:
  89. return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
  90. }
  91. ASSERT_NOT_REACHED();
  92. }
  93. return {};
  94. }
  95. void VBWidgetPropertyModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& value)
  96. {
  97. ASSERT(index.column() == Column::Value);
  98. auto& property = m_widget.m_properties[index.row()];
  99. ASSERT(!property.is_readonly());
  100. property.set_value(value);
  101. }
  102. bool VBWidgetPropertyModel::is_editable(const GUI::ModelIndex& index) const
  103. {
  104. if (index.column() != Column::Value)
  105. return false;
  106. auto& property = m_widget.m_properties[index.row()];
  107. return !property.is_readonly();
  108. }