ElementDataGridRow.cpp 17 KB

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