ElementDataGridRow.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef RMLUICONTROLSELEMENTDATAGRIDROW_H
  29. #define RMLUICONTROLSELEMENTDATAGRIDROW_H
  30. #include "Header.h"
  31. #include "DataSourceListener.h"
  32. #include "DataQuery.h"
  33. #include "../Core/Element.h"
  34. #include <queue>
  35. namespace Rml {
  36. namespace Controls {
  37. class ElementDataGrid;
  38. /**
  39. Class for rows inside a data table. Used for both the header and the individual rows.
  40. @author Robert Curry
  41. */
  42. class RMLUICONTROLS_API ElementDataGridRow : public Core::Element, public DataSourceListener
  43. {
  44. friend class ElementDataGrid;
  45. public:
  46. ElementDataGridRow(const Rml::Core::String& tag);
  47. virtual ~ElementDataGridRow();
  48. void Initialise(ElementDataGrid* parent_grid, ElementDataGridRow* parent_row = nullptr, int child_index = -1, ElementDataGridRow* header_row = nullptr, int depth = -1);
  49. void SetChildIndex(int child_index);
  50. int GetDepth();
  51. void SetDataSource(const Rml::Core::String& data_source_name);
  52. /// Checks dirty children and cells, and loads them if necessary.
  53. /// @return True if any children were updated.
  54. bool UpdateChildren();
  55. /// Returns the number of children that aren't dirty (have been loaded)
  56. int GetNumLoadedChildren();
  57. /// Removes all the child cells and fetches them again from the data source.
  58. void RefreshRows();
  59. /// Returns whether this row is expanded or not.
  60. bool IsRowExpanded();
  61. /// Shows all of this row's descendants.
  62. void ExpandRow();
  63. /// Hides all of this row's descendants.
  64. void CollapseRow();
  65. /// Expands the row if collapsed, or collapses the row if expanded.
  66. void ToggleRow();
  67. /// Returns the index of this row, relative to its parent.
  68. int GetParentRelativeIndex();
  69. /// Returns the index of this row, relative to the table rather than its parent.
  70. int GetTableRelativeIndex();
  71. /// Returns the parent row of this row.
  72. ElementDataGridRow* GetParentRow();
  73. /// Returns the grid that this row belongs to.
  74. ElementDataGrid* GetParentGrid();
  75. protected:
  76. virtual void OnDataSourceDestroy(DataSource* data_source);
  77. virtual void OnRowAdd(DataSource* data_source, const Rml::Core::String& table, int first_row_added, int num_rows_added);
  78. virtual void OnRowRemove(DataSource* data_source, const Rml::Core::String& table, int first_row_removed, int num_rows_removed);
  79. virtual void OnRowChange(DataSource* data_source, const Rml::Core::String& table, int first_row_changed, int num_rows_changed);
  80. virtual void OnRowChange(DataSource* data_source, const Rml::Core::String& table);
  81. private:
  82. typedef std::queue< ElementDataGridRow* > RowQueue;
  83. typedef std::vector< ElementDataGridRow* > RowList;
  84. // Called when a row change (addition or removal) occurs in one of our
  85. // children. Causes the table row index to be dirtied on all following
  86. // children.
  87. void ChildChanged(int child_index);
  88. // Checks if any columns are dependent on the number of children
  89. // present, and refreshes them from the data source if they are.
  90. void RefreshChildDependentCells();
  91. // Forces the row to recalculate its relative table index the next time
  92. // it is requested.
  93. void DirtyTableRelativeIndex();
  94. // Works out what the table relative index is for a given child.
  95. int GetChildTableRelativeIndex(int child_index);
  96. // Adds children underneath this row, and fetches their contents (and
  97. // possible children) from the row's data source. If first_row is left
  98. // as the default -1, the rows are appended at the end of the list.
  99. void AddChildren(int first_row_added = -1, int num_rows_added = 1);
  100. // Removes this rows children, and their children, etc, from the table.
  101. // If the num_rows_removed parameter is left as the -1 default, it'll
  102. // default to the rest of the children after the first row.
  103. void RemoveChildren(int first_row_removed = 0, int num_rows_removed = -1);
  104. // Marks children as dirty and dispatches the event.
  105. void ChangeChildren(int first_row_changed = 0, int num_rows_changed = -1);
  106. // Returns the number of rows under this row (children, grandchildren, etc)
  107. int GetNumDescendants();
  108. // Adds or refreshes the cell contents, and undirties the row's cells.
  109. void Load(const Rml::Controls::DataQuery& row_information);
  110. // Finds all children that have cell information missing (either though being
  111. // refreshed or not being loaded yet) and reloads them.
  112. void LoadChildren(float time_slice);
  113. // Loads a specific set of children. Called by the above function.
  114. void LoadChildren(int first_row_to_load, int num_rows_to_load, Rml::Core::Time time_slice);
  115. // Sets the dirty_cells flag on this row, and lets our ancestors know.
  116. void DirtyCells();
  117. // Sets the dirty children flag on this row and the row's ancestors.
  118. void DirtyRow();
  119. // This row has one or more cells that need loading.
  120. bool dirty_cells;
  121. // This row has one or more children that have either dirty flag set.
  122. bool dirty_children;
  123. // Shows this row, and, if this was was expanded before it was hidden, its children as well.
  124. void Show();
  125. // Hides this row and all descendants.
  126. void Hide();
  127. bool row_expanded;
  128. int table_relative_index;
  129. bool table_relative_index_dirty;
  130. ElementDataGrid* parent_grid;
  131. ElementDataGridRow* parent_row;
  132. int child_index;
  133. int depth;
  134. RowList children;
  135. // The data source and table that the children are fetched from.
  136. DataSource* data_source;
  137. Rml::Core::String data_table;
  138. };
  139. }
  140. }
  141. #endif