table-view.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #if defined(Hiro_TableView)
  2. auto mTableView::allocate() -> pObject* {
  3. return new pTableView(*this);
  4. }
  5. auto mTableView::destruct() -> void {
  6. for(auto& item : state.items) item->destruct();
  7. for(auto& column : state.columns) column->destruct();
  8. mWidget::destruct();
  9. }
  10. //
  11. auto mTableView::alignment() const -> Alignment {
  12. return state.alignment;
  13. }
  14. auto mTableView::append(sTableViewColumn column) -> type& {
  15. state.columns.append(column);
  16. column->setParent(this, columnCount() - 1);
  17. signal(append, column);
  18. return *this;
  19. }
  20. auto mTableView::append(sTableViewItem item) -> type& {
  21. state.items.append(item);
  22. item->setParent(this, itemCount() - 1);
  23. signal(append, item);
  24. return *this;
  25. }
  26. auto mTableView::backgroundColor() const -> Color {
  27. return state.backgroundColor;
  28. }
  29. auto mTableView::batchable() const -> bool {
  30. return state.batchable;
  31. }
  32. auto mTableView::batched() const -> vector<TableViewItem> {
  33. vector<TableViewItem> items;
  34. for(auto& item : state.items) {
  35. if(item->selected()) items.append(item);
  36. }
  37. return items;
  38. }
  39. auto mTableView::bordered() const -> bool {
  40. return state.bordered;
  41. }
  42. auto mTableView::column(uint position) const -> TableViewColumn {
  43. if(position < columnCount()) return state.columns[position];
  44. return {};
  45. }
  46. auto mTableView::columnCount() const -> uint {
  47. return state.columns.size();
  48. }
  49. auto mTableView::columns() const -> vector<TableViewColumn> {
  50. vector<TableViewColumn> columns;
  51. for(auto& column : columns) columns.append(column);
  52. return columns;
  53. }
  54. auto mTableView::doActivate(sTableViewCell cell) const -> void {
  55. if(state.onActivate) return state.onActivate(cell);
  56. }
  57. auto mTableView::doChange() const -> void {
  58. if(state.onChange) return state.onChange();
  59. }
  60. auto mTableView::doContext() const -> void {
  61. if(state.onContext) return state.onContext();
  62. }
  63. auto mTableView::doEdit(sTableViewCell cell) const -> void {
  64. if(state.onEdit) return state.onEdit(cell);
  65. }
  66. auto mTableView::doSort(sTableViewColumn column) const -> void {
  67. if(state.onSort) return state.onSort(column);
  68. }
  69. auto mTableView::doToggle(sTableViewCell cell) const -> void {
  70. if(state.onToggle) return state.onToggle(cell);
  71. }
  72. auto mTableView::foregroundColor() const -> Color {
  73. return state.foregroundColor;
  74. }
  75. auto mTableView::headered() const -> bool {
  76. return state.headered;
  77. }
  78. auto mTableView::item(unsigned position) const -> TableViewItem {
  79. if(position < itemCount()) return state.items[position];
  80. return {};
  81. }
  82. auto mTableView::itemCount() const -> unsigned {
  83. return state.items.size();
  84. }
  85. auto mTableView::items() const -> vector<TableViewItem> {
  86. vector<TableViewItem> items;
  87. for(auto& item : state.items) items.append(item);
  88. return items;
  89. }
  90. auto mTableView::onActivate(const function<void (TableViewCell)>& callback) -> type& {
  91. state.onActivate = callback;
  92. return *this;
  93. }
  94. auto mTableView::onChange(const function<void ()>& callback) -> type& {
  95. state.onChange = callback;
  96. return *this;
  97. }
  98. auto mTableView::onContext(const function<void ()>& callback) -> type& {
  99. state.onContext = callback;
  100. return *this;
  101. }
  102. auto mTableView::onEdit(const function<void (TableViewCell)>& callback) -> type& {
  103. state.onEdit = callback;
  104. return *this;
  105. }
  106. auto mTableView::onSort(const function<void (TableViewColumn)>& callback) -> type& {
  107. state.onSort = callback;
  108. return *this;
  109. }
  110. auto mTableView::onToggle(const function<void (TableViewCell)>& callback) -> type& {
  111. state.onToggle = callback;
  112. return *this;
  113. }
  114. auto mTableView::remove(sTableViewColumn column) -> type& {
  115. signal(remove, column);
  116. state.columns.remove(column->offset());
  117. for(uint n : range(column->offset(), columnCount())) {
  118. state.columns[n]->adjustOffset(-1);
  119. }
  120. column->setParent();
  121. return *this;
  122. }
  123. auto mTableView::remove(sTableViewItem item) -> type& {
  124. signal(remove, item);
  125. state.items.remove(item->offset());
  126. for(uint n : range(item->offset(), itemCount())) {
  127. state.items[n]->adjustOffset(-1);
  128. }
  129. item->setParent();
  130. return *this;
  131. }
  132. auto mTableView::reset() -> type& {
  133. while(state.items) remove(state.items.last());
  134. while(state.columns) remove(state.columns.last());
  135. return *this;
  136. }
  137. auto mTableView::resizeColumns() -> type& {
  138. signal(resizeColumns);
  139. return *this;
  140. }
  141. auto mTableView::selectAll() -> type& {
  142. if(!state.batchable) return *this;
  143. for(auto& item : state.items) {
  144. item->setSelected(true);
  145. }
  146. return *this;
  147. }
  148. auto mTableView::selectNone() -> type& {
  149. for(auto& item : state.items) {
  150. item->setSelected(false);
  151. }
  152. return *this;
  153. }
  154. auto mTableView::selected() const -> TableViewItem {
  155. for(auto& item : state.items) {
  156. if(item->selected()) return item;
  157. }
  158. return {};
  159. }
  160. auto mTableView::setAlignment(Alignment alignment) -> type& {
  161. state.alignment = alignment;
  162. signal(setAlignment, alignment);
  163. return *this;
  164. }
  165. auto mTableView::setBackgroundColor(Color color) -> type& {
  166. state.backgroundColor = color;
  167. signal(setBackgroundColor, color);
  168. return *this;
  169. }
  170. auto mTableView::setBatchable(bool batchable) -> type& {
  171. state.batchable = batchable;
  172. signal(setBatchable, batchable);
  173. return *this;
  174. }
  175. auto mTableView::setBordered(bool bordered) -> type& {
  176. state.bordered = bordered;
  177. signal(setBordered, bordered);
  178. return *this;
  179. }
  180. auto mTableView::setForegroundColor(Color color) -> type& {
  181. state.foregroundColor = color;
  182. signal(setForegroundColor, color);
  183. return *this;
  184. }
  185. auto mTableView::setHeadered(bool headered) -> type& {
  186. state.headered = headered;
  187. signal(setHeadered, headered);
  188. return *this;
  189. }
  190. auto mTableView::setParent(mObject* parent, signed offset) -> type& {
  191. for(auto& item : reverse(state.items)) item->destruct();
  192. for(auto& column : reverse(state.columns)) column->destruct();
  193. mObject::setParent(parent, offset);
  194. for(auto& column : state.columns) column->setParent(this, column->offset());
  195. for(auto& item : state.items) item->setParent(this, item->offset());
  196. return *this;
  197. }
  198. auto mTableView::setSortable(bool sortable) -> type& {
  199. state.sortable = sortable;
  200. signal(setSortable, sortable);
  201. return *this;
  202. }
  203. auto mTableView::sort() -> type& {
  204. Sort sorting = Sort::None;
  205. uint offset = 0;
  206. for(auto& column : state.columns) {
  207. if(column->sorting() == Sort::None) continue;
  208. sorting = column->sorting();
  209. offset = column->offset();
  210. break;
  211. }
  212. auto sorted = state.items;
  213. sorted.sort([&](auto& lhs, auto& rhs) {
  214. string x = offset < lhs->cellCount() ? lhs->state.cells[offset]->state.text : "";
  215. string y = offset < rhs->cellCount() ? rhs->state.cells[offset]->state.text : "";
  216. if(sorting == Sort::Ascending ) return string::icompare(x, y) < 0;
  217. if(sorting == Sort::Descending) return string::icompare(y, x) < 0;
  218. return false; //unreachable
  219. });
  220. while(state.items) remove(state.items.last());
  221. for(auto& item : sorted) append(item);
  222. return *this;
  223. }
  224. auto mTableView::sortable() const -> bool {
  225. return state.sortable;
  226. }
  227. #endif