PropertyTableModel.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
  5. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/JsonObject.h>
  10. #include <AK/JsonValue.h>
  11. #include <AK/QuickSort.h>
  12. #include <LibWebView/PropertyTableModel.h>
  13. namespace WebView {
  14. PropertyTableModel::PropertyTableModel(Type type, JsonValue const& properties)
  15. {
  16. properties.as_object().for_each_member([&](auto const& property_name, auto const& property_value) {
  17. switch (type) {
  18. case PropertyTableModel::Type::ARIAProperties:
  19. m_values.empend(MUST(String::from_deprecated_string(property_name)), String {});
  20. property_value.as_object().for_each_member([&](auto const& property_name, auto const& property_value) {
  21. m_values.empend(MUST(String::from_deprecated_string(property_name)), MUST(String::from_deprecated_string(property_value.to_deprecated_string())));
  22. });
  23. break;
  24. case PropertyTableModel::Type::StyleProperties:
  25. m_values.empend(MUST(String::from_deprecated_string(property_name)), MUST(String::from_deprecated_string(property_value.to_deprecated_string())));
  26. break;
  27. }
  28. });
  29. quick_sort(m_values, [](auto const& a, auto const& b) {
  30. return a.name < b.name;
  31. });
  32. }
  33. PropertyTableModel::~PropertyTableModel() = default;
  34. int PropertyTableModel::row_count(ModelIndex const&) const
  35. {
  36. return static_cast<int>(m_values.size());
  37. }
  38. int PropertyTableModel::column_count(ModelIndex const&) const
  39. {
  40. return 2;
  41. }
  42. ErrorOr<String> PropertyTableModel::column_name(int column_index) const
  43. {
  44. switch (static_cast<Column>(column_index)) {
  45. case Column::PropertyName:
  46. return "Name"_string;
  47. case Column::PropertyValue:
  48. return "Value"_string;
  49. }
  50. VERIFY_NOT_REACHED();
  51. }
  52. ModelIndex PropertyTableModel::index(int row, int column, ModelIndex const&) const
  53. {
  54. return { row, column };
  55. }
  56. String PropertyTableModel::text_for_display(ModelIndex const& index) const
  57. {
  58. auto const& value = m_values[index.row];
  59. switch (static_cast<Column>(index.column)) {
  60. case Column::PropertyName:
  61. return value.name;
  62. case Column::PropertyValue:
  63. return value.value;
  64. }
  65. VERIFY_NOT_REACHED();
  66. }
  67. }