StylePropertiesModel.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibWeb/CSS/PropertyID.h>
  8. #include <LibWeb/CSS/StyleProperties.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/StylePropertiesModel.h>
  11. namespace Web {
  12. StylePropertiesModel::StylePropertiesModel(const CSS::StyleProperties& properties)
  13. : m_properties(properties)
  14. {
  15. properties.for_each_property([&](auto property_id, auto& property_value) {
  16. Value value;
  17. value.name = CSS::string_from_property_id(property_id);
  18. value.value = property_value.to_string();
  19. m_values.append(value);
  20. });
  21. quick_sort(m_values, [](auto& a, auto& b) { return a.name < b.name; });
  22. }
  23. int StylePropertiesModel::row_count(const GUI::ModelIndex&) const
  24. {
  25. return m_values.size();
  26. }
  27. String StylePropertiesModel::column_name(int column_index) const
  28. {
  29. switch (column_index) {
  30. case Column::PropertyName:
  31. return "Name";
  32. case Column::PropertyValue:
  33. return "Value";
  34. default:
  35. VERIFY_NOT_REACHED();
  36. }
  37. }
  38. GUI::Variant StylePropertiesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  39. {
  40. auto& value = m_values[index.row()];
  41. if (role == GUI::ModelRole::Display) {
  42. if (index.column() == Column::PropertyName)
  43. return value.name;
  44. if (index.column() == Column::PropertyValue)
  45. return value.value;
  46. }
  47. return {};
  48. }
  49. }