ElementFormControlDataSelect.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "../../Include/RmlUi/Controls/ElementFormControlDataSelect.h"
  29. #include "../../Include/RmlUi/Controls/DataQuery.h"
  30. #include "../../Include/RmlUi/Controls/DataSource.h"
  31. #include "../../Include/RmlUi/Controls/DataFormatter.h"
  32. #include "WidgetDropDown.h"
  33. namespace Rml {
  34. namespace Controls {
  35. // Constructs a new ElementFormControlDataSelect.
  36. ElementFormControlDataSelect::ElementFormControlDataSelect(const Rml::Core::String& tag) : ElementFormControlSelect(tag)
  37. {
  38. data_source = nullptr;
  39. initialised = false;
  40. }
  41. ElementFormControlDataSelect::~ElementFormControlDataSelect()
  42. {
  43. if (data_source != nullptr) {
  44. data_source->DetachListener(this);
  45. data_source = nullptr;
  46. }
  47. }
  48. // Sets the data source the control's options are driven from.
  49. void ElementFormControlDataSelect::SetDataSource(const Rml::Core::String& _data_source)
  50. {
  51. SetAttribute("source", _data_source);
  52. }
  53. // If a new data source has been set on the control, this will attach to it and build the initial
  54. // options.
  55. void ElementFormControlDataSelect::OnUpdate()
  56. {
  57. if (!initialised)
  58. {
  59. initialised = true;
  60. if (ParseDataSource(data_source, data_table, GetAttribute< Rml::Core::String >("source", "")))
  61. {
  62. data_source->AttachListener(this);
  63. BuildOptions();
  64. }
  65. }
  66. }
  67. // Checks for changes to the data source or formatting attributes.
  68. void ElementFormControlDataSelect::OnAttributeChange(const Core::ElementAttributes& changed_attributes)
  69. {
  70. ElementFormControlSelect::OnAttributeChange(changed_attributes);
  71. if (changed_attributes.find("source") != changed_attributes.end())
  72. {
  73. if (data_source != nullptr) {
  74. data_source->DetachListener(this);
  75. data_source = nullptr;
  76. }
  77. initialised = false;
  78. }
  79. else if (changed_attributes.find("fields") != changed_attributes.end() ||
  80. changed_attributes.find("valuefield") != changed_attributes.end() ||
  81. changed_attributes.find("formatter") != changed_attributes.end())
  82. {
  83. BuildOptions();
  84. }
  85. }
  86. // Detaches from the data source and rebuilds the options.
  87. void ElementFormControlDataSelect::OnDataSourceDestroy(DataSource* _data_source)
  88. {
  89. if (data_source == _data_source)
  90. {
  91. data_source->DetachListener(this);
  92. data_source = nullptr;
  93. data_table = "";
  94. BuildOptions();
  95. }
  96. }
  97. // Rebuilds the available options from the data source.
  98. void ElementFormControlDataSelect::OnRowAdd(DataSource* RMLUI_UNUSED_PARAMETER(data_source), const Rml::Core::String& table, int RMLUI_UNUSED_PARAMETER(first_row_added), int RMLUI_UNUSED_PARAMETER(num_rows_added))
  99. {
  100. RMLUI_UNUSED(data_source);
  101. RMLUI_UNUSED(first_row_added);
  102. RMLUI_UNUSED(num_rows_added);
  103. if (table == data_table)
  104. BuildOptions();
  105. }
  106. // Rebuilds the available options from the data source.
  107. void ElementFormControlDataSelect::OnRowRemove(DataSource* RMLUI_UNUSED_PARAMETER(data_source), const Rml::Core::String& table, int RMLUI_UNUSED_PARAMETER(first_row_removed), int RMLUI_UNUSED_PARAMETER(num_rows_removed))
  108. {
  109. RMLUI_UNUSED(data_source);
  110. RMLUI_UNUSED(first_row_removed);
  111. RMLUI_UNUSED(num_rows_removed);
  112. if (table == data_table)
  113. BuildOptions();
  114. }
  115. // Rebuilds the available options from the data source.
  116. void ElementFormControlDataSelect::OnRowChange(DataSource* RMLUI_UNUSED_PARAMETER(data_source), const Rml::Core::String& table, int RMLUI_UNUSED_PARAMETER(first_row_changed), int RMLUI_UNUSED_PARAMETER(num_rows_changed))
  117. {
  118. RMLUI_UNUSED(data_source);
  119. RMLUI_UNUSED(first_row_changed);
  120. RMLUI_UNUSED(num_rows_changed);
  121. if (table == data_table)
  122. BuildOptions();
  123. }
  124. // Rebuilds the available options from the data source.
  125. void ElementFormControlDataSelect::OnRowChange(DataSource* RMLUI_UNUSED_PARAMETER(data_source), const Rml::Core::String& table)
  126. {
  127. RMLUI_UNUSED(data_source);
  128. if (table == data_table)
  129. BuildOptions();
  130. }
  131. // Builds the option list from the data source.
  132. void ElementFormControlDataSelect::BuildOptions()
  133. {
  134. widget->ClearOptions();
  135. if (data_source == nullptr)
  136. return;
  137. // Store the old selection value and index. These will be used to restore the selection to the
  138. // most appropriate option after the options have been rebuilt.
  139. Rml::Core::String old_value = GetValue();
  140. int old_selection = GetSelection();
  141. Rml::Core::String fields_attribute = GetAttribute<Rml::Core::String>("fields", "");
  142. Rml::Core::String valuefield_attribute = GetAttribute<Rml::Core::String>("valuefield", "");
  143. Rml::Core::String data_formatter_attribute = GetAttribute<Rml::Core::String>("formatter", "");
  144. DataFormatter* data_formatter = nullptr;
  145. // Process the attributes.
  146. if (fields_attribute.empty())
  147. {
  148. Core::Log::Message(Rml::Core::Log::LT_ERROR, "DataQuery failed, no fields specified for %s.", GetTagName().c_str());
  149. return;
  150. }
  151. if (valuefield_attribute.empty())
  152. {
  153. valuefield_attribute = fields_attribute.substr(0, fields_attribute.find(","));
  154. }
  155. if (!data_formatter_attribute.empty())
  156. {
  157. data_formatter = DataFormatter::GetDataFormatter(data_formatter_attribute);
  158. if (!data_formatter)
  159. Core::Log::Message(Rml::Core::Log::LT_WARNING, "Unable to find data formatter named '%s', formatting skipped.", data_formatter_attribute.c_str());
  160. }
  161. // Build a list of attributes
  162. Rml::Core::String fields(valuefield_attribute);
  163. fields += ",";
  164. fields += fields_attribute;
  165. DataQuery query(data_source, data_table, fields);
  166. while (query.NextRow())
  167. {
  168. Rml::Core::StringList fields;
  169. Rml::Core::String value = query.Get<Rml::Core::String>(0, "");
  170. for (size_t i = 1; i < query.GetNumFields(); ++i)
  171. fields.push_back(query.Get< Rml::Core::String>(i, ""));
  172. Rml::Core::String formatted("");
  173. if (fields.size() > 0)
  174. formatted = fields[0];
  175. if (data_formatter)
  176. data_formatter->FormatData(formatted, fields);
  177. // Add the data as an option.
  178. widget->AddOption(formatted, value, -1, false);
  179. }
  180. // If an option was selected before, attempt to restore the selection to it.
  181. if (old_selection > -1)
  182. {
  183. // Try to find a selection with the same value as the previous one.
  184. for (int i = 0; i < GetNumOptions(); ++i)
  185. {
  186. SelectOption* option = GetOption(i);
  187. if (option->GetValue() == old_value)
  188. {
  189. widget->SetSelection(i, true);
  190. return;
  191. }
  192. }
  193. // Failed to find an option with the same value. Attempt to at least set the same index.
  194. int new_selection = Rml::Core::Math::Clamp(old_selection, 0, GetNumOptions() - 1);
  195. if (GetNumOptions() == 0)
  196. new_selection = -1;
  197. widget->SetSelection(new_selection, true);
  198. }
  199. }
  200. }
  201. }