DataControllerDefault.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "DataControllerDefault.h"
  29. #include "../../Include/RmlUi/Core/DataController.h"
  30. #include "../../Include/RmlUi/Core/DataModel.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "DataExpression.h"
  33. #include "EventSpecification.h"
  34. namespace Rml {
  35. DataControllerValue::DataControllerValue(Element* element) : DataController(element)
  36. {}
  37. DataControllerValue::~DataControllerValue()
  38. {
  39. if (Element* element = GetElement())
  40. {
  41. element->RemoveEventListener(EventId::Change, this);
  42. }
  43. }
  44. bool DataControllerValue::Initialize(DataModel& model, Element* element, const String& variable_name, const String& /*modifier*/)
  45. {
  46. RMLUI_ASSERT(element);
  47. DataAddress variable_address = model.ResolveAddress(variable_name, element);
  48. if (variable_address.empty())
  49. return false;
  50. if (model.GetVariable(variable_address))
  51. address = std::move(variable_address);
  52. element->AddEventListener(EventId::Change, this);
  53. return true;
  54. }
  55. void DataControllerValue::ProcessEvent(Event& event)
  56. {
  57. if (Element* element = GetElement())
  58. {
  59. const auto& parameters = event.GetParameters();
  60. auto it = parameters.find("value");
  61. if (it == parameters.end())
  62. {
  63. Log::Message(Log::LT_WARNING, "A 'change' event was received, but it did not contain a value. During processing of 'data-value' in %s", element->GetAddress().c_str());
  64. return;
  65. }
  66. SetValue(it->second);
  67. }
  68. }
  69. void DataControllerValue::Release()
  70. {
  71. delete this;
  72. }
  73. void DataControllerValue::SetValue(const Variant& value)
  74. {
  75. Element* element = GetElement();
  76. if (!element)
  77. return;
  78. DataModel* model = element->GetDataModel();
  79. if (!model)
  80. return;
  81. if (DataVariable variable = model->GetVariable(address))
  82. {
  83. variable.Set(value);
  84. model->DirtyVariable(address.front().name);
  85. }
  86. }
  87. DataControllerEvent::DataControllerEvent(Element* element) : DataController(element)
  88. {}
  89. DataControllerEvent::~DataControllerEvent()
  90. {
  91. if (Element* element = GetElement())
  92. {
  93. if (id != EventId::Invalid)
  94. element->RemoveEventListener(id, this);
  95. }
  96. }
  97. bool DataControllerEvent::Initialize(DataModel& model, Element* element, const String& expression_str, const String& modifier)
  98. {
  99. RMLUI_ASSERT(element);
  100. expression = MakeUnique<DataExpression>(expression_str);
  101. DataExpressionInterface expr_interface(&model, element);
  102. if (!expression->Parse(expr_interface, true))
  103. return false;
  104. id = EventSpecificationInterface::GetIdOrInsert(modifier);
  105. if (id == EventId::Invalid)
  106. {
  107. Log::Message(Log::LT_WARNING, "Event type '%s' could not be recognized, while adding 'data-event' to %s", modifier.c_str(), element->GetAddress().c_str());
  108. return false;
  109. }
  110. element->AddEventListener(id, this);
  111. return true;
  112. }
  113. void DataControllerEvent::ProcessEvent(Event& event)
  114. {
  115. if (!expression)
  116. return;
  117. if (Element* element = GetElement())
  118. {
  119. DataExpressionInterface expr_interface(element->GetDataModel(), element, &event);
  120. Variant unused_value_out;
  121. expression->Run(expr_interface, unused_value_out);
  122. }
  123. }
  124. void DataControllerEvent::Release()
  125. {
  126. delete this;
  127. }
  128. } // namespace Rml