2
0

DataView.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "../../Include/RmlUi/Core/DataTypes.h"
  3. #include "../../Include/RmlUi/Core/Header.h"
  4. #include "../../Include/RmlUi/Core/Traits.h"
  5. #include "../../Include/RmlUi/Core/Types.h"
  6. namespace Rml {
  7. class Element;
  8. class DataModel;
  9. class DataViewInstancer : public NonCopyMoveable {
  10. public:
  11. DataViewInstancer() {}
  12. virtual ~DataViewInstancer() {}
  13. virtual DataViewPtr InstanceView(Element* element) = 0;
  14. };
  15. template <typename T>
  16. class DataViewInstancerDefault final : public DataViewInstancer {
  17. public:
  18. DataViewPtr InstanceView(Element* element) override { return DataViewPtr(new T(element)); }
  19. };
  20. /**
  21. Data view.
  22. Data views are used to present a data variable in the document by different means.
  23. A data view is declared in the document by the element attribute:
  24. data-[type]-[modifier]="[expression]"
  25. The modifier may or may not be required depending on the data view.
  26. */
  27. class DataView : public Releasable {
  28. public:
  29. virtual ~DataView();
  30. // Initialize the data view.
  31. // @param[in] model The data model the view will be attached to.
  32. // @param[in] element The element which spawned the view.
  33. // @param[in] expression The value of the element's 'data-' attribute which spawned the view (see class documentation).
  34. // @param[in] modifier The modifier for the given view type (see class documentation).
  35. // @return True on success.
  36. virtual bool Initialize(DataModel& model, Element* element, const String& expression, const String& modifier) = 0;
  37. // Update the data view.
  38. // Returns true if the update resulted in a document change.
  39. virtual bool Update(DataModel& model) = 0;
  40. // Returns the list of data variable name(s) which can modify this view.
  41. virtual StringList GetVariableNameList() const = 0;
  42. // Returns the attached element if it still exists.
  43. Element* GetElement() const;
  44. // Data views are first sorted by the depth of the attached element in the
  45. // document tree, then optionally by an offset specified for each data view.
  46. int GetSortOrder() const;
  47. // Returns true if the element still exists.
  48. bool IsValid() const;
  49. protected:
  50. // @param[in] element The element this data view is attached to.
  51. // @param[in] sort_offset A number [-1000, 999] specifying the update order of this
  52. // data view at the same tree depth, negative numbers are updated first.
  53. DataView(Element* element, int sort_offset);
  54. private:
  55. ObserverPtr<Element> attached_element;
  56. int sort_order;
  57. };
  58. class DataViews : NonCopyMoveable {
  59. public:
  60. DataViews();
  61. ~DataViews();
  62. void Add(DataViewPtr view);
  63. void OnElementRemove(Element* element);
  64. bool Update(DataModel& model, const DirtyVariables& dirty_variables);
  65. private:
  66. using DataViewList = Vector<DataViewPtr>;
  67. DataViewList views;
  68. DataViewList views_to_add;
  69. DataViewList views_to_remove;
  70. using NameViewMap = UnorderedMultimap<String, DataView*>;
  71. NameViewMap name_view_map;
  72. };
  73. } // namespace Rml