StylePropertiesModel.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  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()
  22. {
  23. }
  24. int StylePropertiesModel::row_count(GUI::ModelIndex const&) const
  25. {
  26. return m_values.size();
  27. }
  28. String StylePropertiesModel::column_name(int column_index) const
  29. {
  30. switch (column_index) {
  31. case Column::PropertyName:
  32. return "Name";
  33. case Column::PropertyValue:
  34. return "Value";
  35. default:
  36. VERIFY_NOT_REACHED();
  37. }
  38. }
  39. GUI::Variant StylePropertiesModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  40. {
  41. auto& value = m_values[index.row()];
  42. if (role == GUI::ModelRole::Display) {
  43. if (index.column() == Column::PropertyName)
  44. return value.name;
  45. if (index.column() == Column::PropertyValue)
  46. return value.value;
  47. }
  48. return {};
  49. }
  50. }