DataControllerDefault.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. namespace Core {
  36. DataControllerValue::DataControllerValue(Element* element) : DataController(element)
  37. {}
  38. DataControllerValue::~DataControllerValue()
  39. {
  40. if (Element* element = GetElement())
  41. {
  42. element->RemoveEventListener(EventId::Change, this);
  43. }
  44. }
  45. bool DataControllerValue::Initialize(DataModel& model, Element* element, const String& variable_name, const String& /*modifier*/)
  46. {
  47. RMLUI_ASSERT(element);
  48. DataAddress variable_address = model.ResolveAddress(variable_name, element);
  49. if (variable_address.empty())
  50. return false;
  51. if (model.GetVariable(variable_address))
  52. address = std::move(variable_address);
  53. element->AddEventListener(EventId::Change, this);
  54. return true;
  55. }
  56. void DataControllerValue::ProcessEvent(Event& event)
  57. {
  58. if (Element* element = GetElement())
  59. {
  60. const auto& parameters = event.GetParameters();
  61. auto it = parameters.find("value");
  62. if (it == parameters.end())
  63. {
  64. 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());
  65. return;
  66. }
  67. SetValue(it->second);
  68. }
  69. }
  70. void DataControllerValue::Release()
  71. {
  72. delete this;
  73. }
  74. void DataControllerValue::SetValue(const Variant& value)
  75. {
  76. Element* element = GetElement();
  77. if (!element)
  78. return;
  79. DataModel* model = element->GetDataModel();
  80. if (!model)
  81. return;
  82. if (DataVariable variable = model->GetVariable(address))
  83. {
  84. variable.Set(value);
  85. model->DirtyVariable(address.front().name);
  86. }
  87. }
  88. DataControllerEvent::DataControllerEvent(Element* element) : DataController(element)
  89. {}
  90. DataControllerEvent::~DataControllerEvent()
  91. {
  92. if (Element* element = GetElement())
  93. {
  94. if (id != EventId::Invalid)
  95. element->RemoveEventListener(id, this);
  96. }
  97. }
  98. bool DataControllerEvent::Initialize(DataModel& model, Element* element, const String& expression_str, const String& modifier)
  99. {
  100. RMLUI_ASSERT(element);
  101. expression = std::make_unique<DataExpression>(expression_str);
  102. DataExpressionInterface interface(&model, element);
  103. if (!expression->Parse(interface, true))
  104. return false;
  105. id = EventSpecificationInterface::GetIdOrInsert(modifier);
  106. if (id == EventId::Invalid)
  107. {
  108. 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());
  109. return false;
  110. }
  111. element->AddEventListener(id, this);
  112. return true;
  113. }
  114. void DataControllerEvent::ProcessEvent(Event& event)
  115. {
  116. if (!expression)
  117. return;
  118. if (Element* element = GetElement())
  119. {
  120. DataExpressionInterface interface(element->GetDataModel(), element, &event);
  121. Variant unused_value_out;
  122. expression->Run(interface, unused_value_out);
  123. }
  124. }
  125. void DataControllerEvent::Release()
  126. {
  127. delete this;
  128. }
  129. }
  130. }