icon-view.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if defined(Hiro_IconView)
  2. auto mIconView::allocate() -> pObject* {
  3. return new pIconView(*this);
  4. }
  5. auto mIconView::destruct() -> void {
  6. for(auto& item : state.items) item->destruct();
  7. mWidget::destruct();
  8. }
  9. //
  10. auto mIconView::append(sIconViewItem item) -> type& {
  11. state.items.append(item);
  12. item->setParent(this, itemCount() - 1);
  13. signal(append, item);
  14. return *this;
  15. }
  16. auto mIconView::backgroundColor() const -> Color {
  17. return state.backgroundColor;
  18. }
  19. auto mIconView::batchable() const -> bool {
  20. return state.batchable;
  21. }
  22. auto mIconView::batched() const -> vector<IconViewItem> {
  23. vector<IconViewItem> items;
  24. for(auto& item : state.items) {
  25. if(item->selected()) items.append(item);
  26. }
  27. return items;
  28. }
  29. auto mIconView::doActivate() const -> void {
  30. if(state.onActivate) return state.onActivate();
  31. }
  32. auto mIconView::doChange() const -> void {
  33. if(state.onChange) return state.onChange();
  34. }
  35. auto mIconView::doContext() const -> void {
  36. if(state.onContext) return state.onContext();
  37. }
  38. auto mIconView::flow() const -> Orientation {
  39. return state.flow;
  40. }
  41. auto mIconView::foregroundColor() const -> Color {
  42. return state.foregroundColor;
  43. }
  44. auto mIconView::item(unsigned position) const -> IconViewItem {
  45. if(position < itemCount()) return state.items[position];
  46. return {};
  47. }
  48. auto mIconView::itemCount() const -> unsigned {
  49. return state.items.size();
  50. }
  51. auto mIconView::items() const -> vector<IconViewItem> {
  52. vector<IconViewItem> items;
  53. for(auto& item : state.items) items.append(item);
  54. return items;
  55. }
  56. auto mIconView::onActivate(const function<void ()>& callback) -> type& {
  57. state.onActivate = callback;
  58. return *this;
  59. }
  60. auto mIconView::onChange(const function<void ()>& callback) -> type& {
  61. state.onChange = callback;
  62. return *this;
  63. }
  64. auto mIconView::onContext(const function<void ()>& callback) -> type& {
  65. state.onContext = callback;
  66. return *this;
  67. }
  68. auto mIconView::orientation() const -> Orientation {
  69. return state.orientation;
  70. }
  71. auto mIconView::remove(sIconViewItem item) -> type& {
  72. signal(remove, item);
  73. state.items.remove(item->offset());
  74. for(auto n : range(item->offset(), itemCount())) {
  75. state.items[n]->adjustOffset(-1);
  76. }
  77. item->setParent();
  78. return *this;
  79. }
  80. auto mIconView::reset() -> type& {
  81. signal(reset);
  82. for(auto& item : state.items) item->setParent();
  83. state.items.reset();
  84. return *this;
  85. }
  86. auto mIconView::selected() const -> IconViewItem {
  87. for(auto& item : state.items) {
  88. if(item->selected()) return item;
  89. }
  90. return {};
  91. }
  92. auto mIconView::setBackgroundColor(Color color) -> type& {
  93. state.backgroundColor = color;
  94. signal(setBackgroundColor, color);
  95. return *this;
  96. }
  97. auto mIconView::setBatchable(bool batchable) -> type& {
  98. state.batchable = batchable;
  99. signal(setBatchable, batchable);
  100. return *this;
  101. }
  102. auto mIconView::setFlow(Orientation flow) -> type& {
  103. state.flow = flow;
  104. signal(setFlow, flow);
  105. return *this;
  106. }
  107. auto mIconView::setForegroundColor(Color color) -> type& {
  108. state.foregroundColor = color;
  109. signal(setForegroundColor, color);
  110. return *this;
  111. }
  112. auto mIconView::setOrientation(Orientation orientation) -> type& {
  113. state.orientation = orientation;
  114. signal(setOrientation, orientation);
  115. return *this;
  116. }
  117. auto mIconView::setParent(mObject* parent, signed offset) -> type& {
  118. for(auto& item : reverse(state.items)) item->destruct();
  119. mObject::setParent(parent, offset);
  120. for(auto& item : state.items) item->setParent(this, item->offset());
  121. return *this;
  122. }
  123. auto mIconView::setSelected(const vector<signed>& selections) -> type& {
  124. bool selectAll = selections(0, 0) == ~0;
  125. for(auto& item : state.items) item->state.selected = selectAll;
  126. if(selectAll) return signal(setItemSelectedAll), *this;
  127. if(!selections) return signal(setItemSelectedNone), *this;
  128. for(auto& position : selections) {
  129. if(position >= itemCount()) continue;
  130. state.items[position]->state.selected = true;
  131. }
  132. signal(setItemSelected, selections);
  133. return *this;
  134. }
  135. #endif