StylePropertiesModel.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "StylePropertiesModel.h"
  8. #include <AK/QuickSort.h>
  9. namespace Web {
  10. StylePropertiesModel::StylePropertiesModel(JsonObject properties)
  11. : m_properties(move(properties))
  12. {
  13. m_properties.for_each_member([&](auto& property_name, auto& property_value) {
  14. Value value;
  15. value.name = property_name;
  16. value.value = property_value.to_string();
  17. m_values.append(value);
  18. });
  19. quick_sort(m_values, [](auto& a, auto& b) { return a.name < b.name; });
  20. }
  21. StylePropertiesModel::~StylePropertiesModel() = default;
  22. int StylePropertiesModel::row_count(GUI::ModelIndex const&) const
  23. {
  24. return m_values.size();
  25. }
  26. String StylePropertiesModel::column_name(int column_index) const
  27. {
  28. switch (column_index) {
  29. case Column::PropertyName:
  30. return "Name";
  31. case Column::PropertyValue:
  32. return "Value";
  33. default:
  34. VERIFY_NOT_REACHED();
  35. }
  36. }
  37. GUI::Variant StylePropertiesModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  38. {
  39. auto& value = m_values[index.row()];
  40. if (role == GUI::ModelRole::Display) {
  41. if (index.column() == Column::PropertyName)
  42. return value.name;
  43. if (index.column() == Column::PropertyValue)
  44. return value.value;
  45. }
  46. return {};
  47. }
  48. Vector<GUI::ModelIndex> StylePropertiesModel::matches(StringView searching, unsigned flags, GUI::ModelIndex const& parent)
  49. {
  50. if (m_values.is_empty())
  51. return {};
  52. Vector<GUI::ModelIndex> found_indices;
  53. for (auto it = m_values.begin(); !it.is_end(); ++it) {
  54. GUI::ModelIndex index = this->index(it.index(), Column::PropertyName, parent);
  55. if (!string_matches(data(index, GUI::ModelRole::Display).as_string(), searching, flags))
  56. continue;
  57. found_indices.append(index);
  58. if (flags & FirstMatchOnly)
  59. break;
  60. }
  61. return found_indices;
  62. }
  63. }