ElementFormControlDataSelect.cpp 7.4 KB

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