StylePropertiesModel.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "StylePropertiesModel.h"
  27. #include <LibHTML/CSS/PropertyID.h>
  28. #include <LibHTML/DOM/Document.h>
  29. #include <LibHTML/CSS/StyleProperties.h>
  30. StylePropertiesModel::StylePropertiesModel(const StyleProperties& properties)
  31. : m_properties(properties)
  32. {
  33. properties.for_each_property([&](auto property_id, auto& property_value) {
  34. Value value;
  35. value.name = CSS::string_from_property_id(property_id);
  36. value.value = property_value.to_string();
  37. m_values.append(value);
  38. });
  39. }
  40. int StylePropertiesModel::row_count(const GUI::ModelIndex&) const
  41. {
  42. return m_values.size();
  43. }
  44. String StylePropertiesModel::column_name(int column_index) const
  45. {
  46. switch (column_index) {
  47. case Column::PropertyName:
  48. return "Name";
  49. case Column::PropertyValue:
  50. return "Value";
  51. default:
  52. ASSERT_NOT_REACHED();
  53. }
  54. }
  55. GUI::Variant StylePropertiesModel::data(const GUI::ModelIndex& index, Role role) const
  56. {
  57. auto& value = m_values[index.row()];
  58. if (role == Role::Display) {
  59. if (index.column() == Column::PropertyName)
  60. return value.name;
  61. if (index.column() == Column::PropertyValue)
  62. return value.value;
  63. }
  64. return {};
  65. }
  66. void StylePropertiesModel::update()
  67. {
  68. did_update();
  69. }