ElementDataGridRow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "../../Include/Rocket/Controls/ElementDataGridRow.h"
  28. #include "../../Include/Rocket/Core.h"
  29. #include "../../Include/Rocket/Controls/DataSource.h"
  30. #include "../../Include/Rocket/Controls/DataFormatter.h"
  31. #include "../../Include/Rocket/Controls/ElementDataGrid.h"
  32. #include "../../Include/Rocket/Controls/ElementDataGridCell.h"
  33. namespace Rocket {
  34. namespace Controls {
  35. const float MAX_UPDATE_TIME = 0.01f;
  36. ElementDataGridRow::ElementDataGridRow(const Rocket::Core::String& tag) : Core::Element(tag)
  37. {
  38. parent_grid = NULL;
  39. parent_row = NULL;
  40. child_index = -1;
  41. depth = -1;
  42. data_source = NULL;
  43. table_relative_index = -1;
  44. table_relative_index_dirty = true;
  45. dirty_cells = true;
  46. dirty_children = false;
  47. row_expanded = true;
  48. SetProperty("white-space", "nowrap");
  49. SetProperty("display", Rocket::Core::Property(Rocket::Core::DISPLAY_INLINE_BLOCK, Rocket::Core::Property::KEYWORD));
  50. }
  51. ElementDataGridRow::~ElementDataGridRow()
  52. {
  53. if (data_source)
  54. {
  55. data_source->DetachListener(this);
  56. data_source = NULL;
  57. }
  58. }
  59. void ElementDataGridRow::Initialise(ElementDataGrid* _parent_grid, ElementDataGridRow* _parent_row, int _child_index, ElementDataGridRow* header_row, int _depth)
  60. {
  61. parent_grid = _parent_grid;
  62. parent_row = _parent_row;
  63. child_index = _child_index;
  64. depth = _depth;
  65. // We start all the rows collapsed, except for the root row.
  66. if (child_index != -1)
  67. {
  68. row_expanded = false;
  69. }
  70. int num_columns = parent_grid->GetNumColumns();
  71. Rocket::Core::XMLAttributes cell_attributes;
  72. for (int i = 0; i < num_columns; i++)
  73. {
  74. ElementDataGridCell* cell = dynamic_cast< ElementDataGridCell* >(Core::Factory::InstanceElement(this, "#rktctl_datagridcell", "datagridcell", cell_attributes));
  75. cell->Initialise(i, header_row->GetChild(i));
  76. cell->SetProperty("display", Rocket::Core::Property(Rocket::Core::DISPLAY_INLINE_BLOCK, Rocket::Core::Property::KEYWORD));
  77. AppendChild(cell);
  78. cell->RemoveReference();
  79. }
  80. }
  81. void ElementDataGridRow::SetChildIndex(int _child_index)
  82. {
  83. if (child_index != _child_index)
  84. {
  85. child_index = _child_index;
  86. if (parent_row)
  87. {
  88. parent_row->ChildChanged(child_index);
  89. }
  90. }
  91. }
  92. int ElementDataGridRow::GetDepth()
  93. {
  94. return depth;
  95. }
  96. void ElementDataGridRow::SetDataSource(const Rocket::Core::String& data_source_name)
  97. {
  98. if (data_source != NULL)
  99. {
  100. data_source->DetachListener(this);
  101. data_source = NULL;
  102. }
  103. if (ParseDataSource(data_source, data_table, data_source_name))
  104. {
  105. data_source->AttachListener(this);
  106. RefreshRows();
  107. }
  108. }
  109. bool ElementDataGridRow::UpdateChildren()
  110. {
  111. if (dirty_children)
  112. {
  113. float start_time = Core::GetSystemInterface()->GetElapsedTime();
  114. RowQueue dirty_rows;
  115. dirty_rows.push(this);
  116. while (!dirty_rows.empty())
  117. {
  118. ElementDataGridRow* dirty_row = dirty_rows.front();
  119. dirty_rows.pop();
  120. float time_slice = MAX_UPDATE_TIME - (Core::GetSystemInterface()->GetElapsedTime() - start_time);
  121. if (time_slice <= 0.0f)
  122. break;
  123. dirty_row->LoadChildren(time_slice);
  124. for (size_t i = 0; i < dirty_row->children.size(); i++)
  125. {
  126. if (dirty_row->children[i]->dirty_cells || dirty_row->children[i]->dirty_children)
  127. {
  128. dirty_rows.push(dirty_row->children[i]);
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. return false;
  135. }
  136. // Returns the number of children that aren't dirty (have been loaded)
  137. int ElementDataGridRow::GetNumLoadedChildren()
  138. {
  139. int num_loaded_children = 0;
  140. for (size_t i = 0; i < children.size(); i++)
  141. {
  142. if (!children[i]->dirty_cells)
  143. {
  144. num_loaded_children++;
  145. }
  146. num_loaded_children += children[i]->GetNumLoadedChildren();
  147. }
  148. return num_loaded_children;
  149. }
  150. bool ElementDataGridRow::IsRowExpanded()
  151. {
  152. return row_expanded;
  153. }
  154. void ElementDataGridRow::ExpandRow()
  155. {
  156. row_expanded = true;
  157. for (size_t i = 0; i < children.size(); i++)
  158. {
  159. children[i]->Show();
  160. }
  161. DirtyLayout();
  162. }
  163. void ElementDataGridRow::CollapseRow()
  164. {
  165. row_expanded = false;
  166. for (size_t i = 0; i < children.size(); i++)
  167. {
  168. children[i]->Hide();
  169. }
  170. DirtyLayout();
  171. }
  172. void ElementDataGridRow::ToggleRow()
  173. {
  174. if (row_expanded)
  175. {
  176. CollapseRow();
  177. }
  178. else
  179. {
  180. ExpandRow();
  181. }
  182. }
  183. // Returns the index of this row, relative to its parent.
  184. int ElementDataGridRow::GetParentRelativeIndex()
  185. {
  186. return child_index;
  187. }
  188. // Returns the index of this row, relative to the table rather than its parent.
  189. int ElementDataGridRow::GetTableRelativeIndex()
  190. {
  191. if (!parent_row)
  192. {
  193. return -1;
  194. }
  195. if (table_relative_index_dirty)
  196. {
  197. table_relative_index = parent_row->GetChildTableRelativeIndex(child_index);
  198. table_relative_index_dirty = false;
  199. }
  200. return table_relative_index;
  201. }
  202. // Returns the parent row of this row.
  203. ElementDataGridRow* ElementDataGridRow::GetParentRow()
  204. {
  205. return parent_row;
  206. }
  207. // Returns the grid that this row belongs to.
  208. ElementDataGrid* ElementDataGridRow::GetParentGrid()
  209. {
  210. return parent_grid;
  211. }
  212. void ElementDataGridRow::OnDataSourceDestroy(DataSource* ROCKET_UNUSED_PARAMETER(_data_source))
  213. {
  214. if(data_source != NULL)
  215. {
  216. data_source->DetachListener(this);
  217. data_source = NULL;
  218. }
  219. RemoveChildren();
  220. }
  221. void ElementDataGridRow::OnRowAdd(DataSource* _data_source, const Rocket::Core::String& _data_table, int first_row_added, int num_rows_added)
  222. {
  223. if (_data_source == data_source && _data_table == data_table)
  224. AddChildren(first_row_added, num_rows_added);
  225. }
  226. void ElementDataGridRow::OnRowRemove(DataSource* _data_source, const Rocket::Core::String& _data_table, int first_row_removed, int num_rows_removed)
  227. {
  228. if (_data_source == data_source && _data_table == data_table)
  229. RemoveChildren(first_row_removed, num_rows_removed);
  230. }
  231. void ElementDataGridRow::OnRowChange(DataSource* _data_source, const Rocket::Core::String& _data_table, int first_row_changed, int num_rows_changed)
  232. {
  233. if (_data_source == data_source && _data_table == data_table)
  234. ChangeChildren(first_row_changed, num_rows_changed);
  235. }
  236. void ElementDataGridRow::OnRowChange(DataSource* _data_source, const Rocket::Core::String& _data_table)
  237. {
  238. if (_data_source == data_source && _data_table == data_table)
  239. RefreshRows();
  240. }
  241. // Removes all the child cells and fetches them again from the data source.
  242. void ElementDataGridRow::RefreshRows()
  243. {
  244. // Remove all our child rows from the table.
  245. RemoveChildren();
  246. // Load the children from the data source.
  247. if (data_source != NULL)
  248. {
  249. int num_rows = data_source->GetNumRows(data_table);
  250. if (num_rows > 0)
  251. {
  252. AddChildren(0, num_rows);
  253. }
  254. }
  255. }
  256. // Called when a row change (addition or removal) occurs in one of our
  257. // children.
  258. void ElementDataGridRow::ChildChanged(int child_row_index)
  259. {
  260. for (int i = child_row_index + 1; i < (int)children.size(); i++)
  261. {
  262. children[i]->DirtyTableRelativeIndex();
  263. }
  264. if (parent_row)
  265. {
  266. parent_row->ChildChanged(child_index);
  267. }
  268. }
  269. // Checks if any columns are dependent on the number of children present, and
  270. // refreshes them from the data source if they are.
  271. void ElementDataGridRow::RefreshChildDependentCells()
  272. {
  273. if (child_index != -1)
  274. {
  275. for (int i = 0; i < parent_grid->GetNumColumns(); i++)
  276. {
  277. const ElementDataGrid::Column* column = parent_grid->GetColumn(i);
  278. if (column->refresh_on_child_change)
  279. {
  280. DirtyCells();
  281. }
  282. }
  283. }
  284. }
  285. // Called whenever a row is added or removed above ours.
  286. void ElementDataGridRow::DirtyTableRelativeIndex()
  287. {
  288. for (size_t i = 0; i < children.size(); i++)
  289. {
  290. children[i]->DirtyTableRelativeIndex();
  291. }
  292. table_relative_index_dirty = true;
  293. }
  294. int ElementDataGridRow::GetChildTableRelativeIndex(int child_index)
  295. {
  296. // We start with our index, then count down each of the children until we
  297. // reach child_index. For each child we skip by add one (for the child
  298. // itself) and all of its descendants.
  299. int child_table_index = GetTableRelativeIndex() + 1;
  300. for (int i = 0; i < child_index; i++)
  301. {
  302. child_table_index++;
  303. child_table_index += children[i]->GetNumDescendants();
  304. }
  305. return child_table_index;
  306. }
  307. // Adds children underneath this row, and fetches their contents (and possible
  308. // children) from the row's data source.
  309. void ElementDataGridRow::AddChildren(int first_row_added, int num_rows_added)
  310. {
  311. if (first_row_added == -1)
  312. {
  313. first_row_added = (int)children.size();
  314. }
  315. // prevent relayout of the document while adding rows
  316. Core::ElementDocument* document = parent_grid->GetOwnerDocument();
  317. document->LockLayout(true);
  318. // We need to make a row for each new child, then pass through the cell
  319. // information and the child's data source (if one exists.)
  320. if (data_source != NULL)
  321. {
  322. for (int i = 0; i < num_rows_added; i++)
  323. {
  324. int row_index = first_row_added + i;
  325. // Make a new row:
  326. ElementDataGridRow* new_row = parent_grid->AddRow(this, row_index);
  327. children.insert(children.begin() + row_index, new_row);
  328. if (!row_expanded)
  329. {
  330. new_row->SetProperty("display", "none");
  331. }
  332. }
  333. for (int i = first_row_added + num_rows_added; i < (int)children.size(); i++)
  334. {
  335. children[i]->SetChildIndex(i);
  336. children[i]->DirtyTableRelativeIndex();
  337. }
  338. if (parent_row)
  339. {
  340. parent_row->ChildChanged(child_index);
  341. }
  342. }
  343. document->LockLayout(false);
  344. RefreshChildDependentCells();
  345. DirtyRow();
  346. Rocket::Core::Dictionary parameters;
  347. parameters.Set("first_row_added", GetChildTableRelativeIndex(first_row_added));
  348. parameters.Set("num_rows_added", num_rows_added);
  349. parent_grid->DispatchEvent("rowadd", parameters);
  350. }
  351. void ElementDataGridRow::RemoveChildren(int first_row_removed, int num_rows_removed)
  352. {
  353. if (num_rows_removed == -1)
  354. {
  355. num_rows_removed = (int)children.size() - first_row_removed;
  356. }
  357. // prevent relayout of the document while removing rows
  358. Core::ElementDocument* document = parent_grid->GetOwnerDocument();
  359. document->LockLayout(true);
  360. for (int i = num_rows_removed - 1; i >= 0; i--)
  361. {
  362. children[first_row_removed + i]->RemoveChildren();
  363. parent_grid->RemoveRows(children[first_row_removed + i]->GetTableRelativeIndex());
  364. }
  365. children.erase(children.begin() + first_row_removed, children.begin() + (first_row_removed + num_rows_removed));
  366. for (int i = first_row_removed; i < (int) children.size(); i++)
  367. {
  368. children[i]->SetChildIndex(i);
  369. children[i]->DirtyTableRelativeIndex();
  370. }
  371. document->LockLayout(false);
  372. Rocket::Core::Dictionary parameters;
  373. parameters.Set("first_row_removed", GetChildTableRelativeIndex(first_row_removed));
  374. parameters.Set("num_rows_removed", num_rows_removed);
  375. parent_grid->DispatchEvent("rowremove", parameters);
  376. }
  377. void ElementDataGridRow::ChangeChildren(int first_row_changed, int num_rows_changed)
  378. {
  379. for (int i = first_row_changed; i < first_row_changed + num_rows_changed; i++)
  380. children[i]->DirtyCells();
  381. Rocket::Core::Dictionary parameters;
  382. parameters.Set("first_row_changed", GetChildTableRelativeIndex(first_row_changed));
  383. parameters.Set("num_rows_changed", num_rows_changed);
  384. parent_grid->DispatchEvent("rowchange", parameters);
  385. }
  386. // Returns the number of rows under this row (children, grandchildren, etc)
  387. int ElementDataGridRow::GetNumDescendants()
  388. {
  389. int num_descendants = (int)children.size();
  390. for (size_t i = 0; i < children.size(); i++)
  391. num_descendants += children[i]->GetNumDescendants();
  392. return num_descendants;
  393. }
  394. // Adds the cell contents, and marks the row as loaded.
  395. void ElementDataGridRow::Load(const DataQuery& row_information)
  396. {
  397. // Check for a data source. If they're both set then we set
  398. // ourselves up with it.
  399. if (row_information.IsFieldSet(DataSource::CHILD_SOURCE))
  400. {
  401. Rocket::Core::String data_source = row_information.Get< Rocket::Core::String >(DataSource::CHILD_SOURCE, "");
  402. if (!data_source.Empty())
  403. {
  404. SetDataSource(data_source);
  405. }
  406. else
  407. {
  408. // If we've no data source, then we should remove any children.
  409. RemoveChildren();
  410. }
  411. }
  412. // Now load our cells.
  413. for (int i = 0; i < parent_grid->GetNumColumns(); i++)
  414. {
  415. Core::Element* cell = GetChild(i);
  416. if (cell)
  417. {
  418. // Fetch the column:
  419. const ElementDataGrid::Column* column = parent_grid->GetColumn(i);
  420. // Now we use the column's formatter to process the raw data into the
  421. // XML string, and parse that into the actual Core::Elements. If there is
  422. // no formatter, then we just send through the raw text, in CVS form.
  423. Rocket::Core::StringList raw_data;
  424. for (size_t i = 0; i < column->fields.size(); i++)
  425. {
  426. if (column->fields[i] == DataSource::DEPTH)
  427. {
  428. raw_data.push_back(Rocket::Core::String(8, "%d", depth));
  429. }
  430. else if (column->fields[i] == DataSource::NUM_CHILDREN)
  431. {
  432. raw_data.push_back(Rocket::Core::String(8, "%d", children.size()));
  433. }
  434. else
  435. {
  436. raw_data.push_back(row_information.Get< Rocket::Core::String >(column->fields[i], ""));
  437. }
  438. }
  439. Rocket::Core::String cell_string;
  440. if (column->formatter)
  441. {
  442. column->formatter->FormatData(cell_string, raw_data);
  443. }
  444. else
  445. {
  446. for (size_t i = 0; i < raw_data.size(); i++)
  447. {
  448. if (i > 0)
  449. {
  450. cell_string.Append(",");
  451. }
  452. cell_string.Append(raw_data[i]);
  453. }
  454. }
  455. // Remove all the cell's current contents.
  456. while (cell->GetNumChildren(true) > 0)
  457. {
  458. cell->RemoveChild(cell->GetChild(0));
  459. }
  460. // Add the new contents to the cell.
  461. Core::Factory::InstanceElementText(cell, cell_string);
  462. }
  463. else
  464. {
  465. ROCKET_ERROR;
  466. }
  467. }
  468. dirty_cells = false;
  469. }
  470. // Instantiates the children that haven't been fully loaded yet.
  471. void ElementDataGridRow::LoadChildren(float time_slice)
  472. {
  473. float start_time = Core::GetSystemInterface()->GetElapsedTime();
  474. int data_query_offset = -1;
  475. int data_query_limit = -1;
  476. // Iterate through the list of children and find the holes of unloaded
  477. // rows.
  478. // - If we find an unloaded element, and we haven't set the offset
  479. // (beginning of the hole), then we've hit a new hole. We set the offset
  480. // to the current index and the limit (size of the hole) to 1. If we've
  481. // found a hole already and we find an unloaded element, then we
  482. // increase the size of the hole.
  483. // - The end of a hole is either a loaded element or the end of the list.
  484. // In either case, we check if we have a hole that's unfilled, and if
  485. // so, we fill it.
  486. bool any_dirty_children = false;
  487. for (size_t i = 0; i < children.size() && (Core::GetSystemInterface()->GetElapsedTime() - start_time) < time_slice; i++)
  488. {
  489. if (children[i]->dirty_cells)
  490. {
  491. any_dirty_children = true;
  492. if (data_query_offset == -1)
  493. {
  494. data_query_offset = (int)i;
  495. data_query_limit = 1;
  496. }
  497. else
  498. {
  499. data_query_limit++;
  500. }
  501. }
  502. else if (children[i]->dirty_children)
  503. {
  504. any_dirty_children = true;
  505. }
  506. bool end_of_list = i == children.size() - 1;
  507. bool unfilled_hole = data_query_offset != -1;
  508. bool end_of_hole_found = !children[i]->dirty_cells;
  509. // If this is the last element and we've found no holes (or filled them
  510. // all in) then all our children are loaded.
  511. if (end_of_list && !unfilled_hole)
  512. {
  513. if (!any_dirty_children)
  514. {
  515. dirty_children = false;
  516. }
  517. }
  518. // Otherwise, if we have a hole outstanding and we've either hit the
  519. // end of the list or the end of the hole, fill the hole.
  520. else if (unfilled_hole && (end_of_list || end_of_hole_found))
  521. {
  522. float load_time_slice = time_slice - (Core::GetSystemInterface()->GetElapsedTime() - start_time);
  523. LoadChildren(data_query_offset, data_query_limit, load_time_slice);
  524. data_query_offset = -1;
  525. data_query_limit = -1;
  526. }
  527. }
  528. if (children.empty())
  529. {
  530. dirty_children = false;
  531. }
  532. }
  533. void ElementDataGridRow::LoadChildren(int first_row_to_load, int num_rows_to_load, Rocket::Core::Time time_slice)
  534. {
  535. float start_time = Core::GetSystemInterface()->GetElapsedTime();
  536. // Now fetch these new children from the data source, pass them
  537. // through each column's data formatter, and add them as our new
  538. // child rows.
  539. Rocket::Core::String column_query = parent_grid->GetAllColumnFields() + "," + DataSource::CHILD_SOURCE;
  540. DataQuery query(data_source, data_table, column_query, first_row_to_load, num_rows_to_load);
  541. for (int i = 0; i < num_rows_to_load; i++)
  542. {
  543. int index = first_row_to_load + i;
  544. if (!query.NextRow())
  545. {
  546. Core::Log::Message(Rocket::Core::Log::LT_WARNING, "Failed to load row %d from data source %s", i, data_table.CString());
  547. }
  548. // Now load the child with the row in the query.
  549. children[index]->Load(query);
  550. if (Core::GetSystemInterface()->GetElapsedTime() - start_time > time_slice)
  551. {
  552. break;
  553. }
  554. }
  555. }
  556. void ElementDataGridRow::DirtyCells()
  557. {
  558. dirty_cells = true;
  559. if (parent_row)
  560. {
  561. parent_row->DirtyRow();
  562. }
  563. }
  564. void ElementDataGridRow::DirtyRow()
  565. {
  566. dirty_children = true;
  567. if (parent_row)
  568. {
  569. parent_row->DirtyRow();
  570. }
  571. }
  572. // Sets this row's child rows to be visible.
  573. void ElementDataGridRow::Show()
  574. {
  575. SetProperty("display", "inline-block");
  576. if (row_expanded)
  577. {
  578. for (size_t i = 0; i < children.size(); i++)
  579. {
  580. children[i]->Show();
  581. }
  582. }
  583. }
  584. // Sets this row's children to be invisible.
  585. void ElementDataGridRow::Hide()
  586. {
  587. SetProperty("display", "none");
  588. for (size_t i = 0; i < children.size(); i++)
  589. {
  590. children[i]->Hide();
  591. }
  592. }
  593. }
  594. }