ColumnsView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/ColumnsView.h>
  8. #include <LibGUI/Model.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGUI/Scrollbar.h>
  11. #include <LibGfx/CharacterBitmap.h>
  12. #include <LibGfx/Palette.h>
  13. namespace GUI {
  14. static constexpr Gfx::CharacterBitmap s_arrow_bitmap {
  15. " "
  16. " # "
  17. " ## "
  18. " ### "
  19. " #### "
  20. " ### "
  21. " ## "
  22. " # "
  23. " ",
  24. 9, 9
  25. };
  26. ColumnsView::ColumnsView()
  27. {
  28. set_fill_with_background_color(true);
  29. set_background_role(ColorRole::Base);
  30. set_foreground_role(ColorRole::BaseText);
  31. m_columns.append({ {}, 0 });
  32. }
  33. void ColumnsView::select_all()
  34. {
  35. Vector<Column> columns_for_selection;
  36. selection().for_each_index([&](auto& index) {
  37. for (auto& column : m_columns) {
  38. if (column.parent_index == index.parent()) {
  39. columns_for_selection.append(column);
  40. return;
  41. }
  42. }
  43. VERIFY_NOT_REACHED();
  44. });
  45. for (Column& column : columns_for_selection) {
  46. int row_count = model()->row_count(column.parent_index);
  47. for (int row = 0; row < row_count; row++) {
  48. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  49. selection().add(index);
  50. }
  51. }
  52. }
  53. void ColumnsView::paint_event(PaintEvent& event)
  54. {
  55. AbstractView::paint_event(event);
  56. if (!model())
  57. return;
  58. Painter painter(*this);
  59. painter.add_clip_rect(frame_inner_rect());
  60. painter.add_clip_rect(event.rect());
  61. painter.translate(frame_thickness(), frame_thickness());
  62. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  63. int column_x = 0;
  64. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  65. for (size_t i = 0; i < m_columns.size(); i++) {
  66. auto& column = m_columns[i];
  67. auto* next_column = i + 1 == m_columns.size() ? nullptr : &m_columns[i + 1];
  68. VERIFY(column.width > 0);
  69. int row_count = model()->row_count(column.parent_index);
  70. for (int row = 0; row < row_count; row++) {
  71. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  72. VERIFY(index.is_valid());
  73. bool is_selected_row = selection().contains(index);
  74. Color background_color = palette().color(background_role());
  75. Color text_color = palette().color(foreground_role());
  76. if (next_column != nullptr && next_column->parent_index == index) {
  77. background_color = palette().inactive_selection();
  78. text_color = palette().inactive_selection_text();
  79. }
  80. if (is_selected_row) {
  81. background_color = selection_color;
  82. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  83. }
  84. Gfx::IntRect row_rect { column_x, row * item_height(), column.width, item_height() };
  85. if (m_edit_index.row() != row)
  86. painter.fill_rect(row_rect, background_color);
  87. auto icon = index.data(ModelRole::Icon);
  88. Gfx::IntRect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() };
  89. icon_rect.center_vertically_within(row_rect);
  90. if (icon.is_icon()) {
  91. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  92. if (is_selected_row) {
  93. auto tint = selection_color.with_alpha(100);
  94. painter.blit_filtered(icon_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  95. } else if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) {
  96. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  97. } else {
  98. auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  99. painter.blit(icon_rect.location(), *bitmap, bitmap->rect(), opacity);
  100. }
  101. }
  102. }
  103. Gfx::IntRect text_rect = {
  104. icon_rect.right() + 1 + icon_spacing(), row * item_height(),
  105. column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - static_cast<int>(s_arrow_bitmap.width()) - icon_spacing(), item_height()
  106. };
  107. draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::CenterLeft, Gfx::TextElision::None);
  108. if (is_focused() && index == cursor_index()) {
  109. painter.draw_rect(row_rect, palette().color(background_role()));
  110. painter.draw_focus_rect(row_rect, palette().focus_outline());
  111. }
  112. if (has_pending_drop() && index == drop_candidate_index()) {
  113. painter.draw_rect(row_rect, palette().selection(), true);
  114. }
  115. bool expandable = model()->row_count(index) > 0;
  116. if (expandable) {
  117. Gfx::IntRect arrow_rect = {
  118. text_rect.right() + 1 + icon_spacing(), 0,
  119. s_arrow_bitmap.width(), s_arrow_bitmap.height()
  120. };
  121. arrow_rect.center_vertically_within(row_rect);
  122. painter.draw_bitmap(arrow_rect.location(), s_arrow_bitmap, text_color);
  123. }
  124. }
  125. int separator_height = content_size().height();
  126. if (height() > separator_height)
  127. separator_height = height();
  128. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  129. column_x += column.width + 1;
  130. }
  131. }
  132. void ColumnsView::push_column(ModelIndex const& parent_index)
  133. {
  134. VERIFY(model());
  135. // Drop columns at the end.
  136. ModelIndex grandparent = model()->parent_index(parent_index);
  137. for (int i = m_columns.size() - 1; i > 0; i--) {
  138. if (m_columns[i].parent_index == grandparent)
  139. break;
  140. m_columns.shrink(i);
  141. dbgln("Dropping column {}", i);
  142. }
  143. // Add the new column.
  144. dbgln("Adding a new column");
  145. m_columns.append({ parent_index, 0 });
  146. update_column_sizes();
  147. update();
  148. }
  149. void ColumnsView::update_column_sizes()
  150. {
  151. if (!model())
  152. return;
  153. int total_width = 0;
  154. int total_height = 0;
  155. for (auto& column : m_columns) {
  156. int row_count = model()->row_count(column.parent_index);
  157. int column_height = row_count * item_height();
  158. if (column_height > total_height)
  159. total_height = column_height;
  160. column.width = 10;
  161. for (int row = 0; row < row_count; row++) {
  162. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  163. VERIFY(index.is_valid());
  164. auto text = index.data().to_string();
  165. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap.width() + icon_spacing();
  166. if (row_width > column.width)
  167. column.width = row_width;
  168. }
  169. total_width += column.width + 1;
  170. }
  171. set_content_size({ total_width, total_height });
  172. }
  173. ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& a_position) const
  174. {
  175. if (!model())
  176. return {};
  177. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  178. int column_x = 0;
  179. for (auto& column : m_columns) {
  180. if (position.x() < column_x)
  181. break;
  182. if (position.x() > column_x + column.width) {
  183. column_x += column.width;
  184. continue;
  185. }
  186. int row = position.y() / item_height();
  187. int row_count = model()->row_count(column.parent_index);
  188. if (row >= row_count)
  189. return {};
  190. return model()->index(row, m_model_column, column.parent_index);
  191. }
  192. return {};
  193. }
  194. void ColumnsView::mousedown_event(MouseEvent& event)
  195. {
  196. AbstractView::mousedown_event(event);
  197. if (!model())
  198. return;
  199. if (event.button() != MouseButton::Primary)
  200. return;
  201. auto index = index_at_event_position(event.position());
  202. if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
  203. if (model()->row_count(index))
  204. push_column(index);
  205. }
  206. }
  207. void ColumnsView::model_did_update(unsigned flags)
  208. {
  209. AbstractView::model_did_update(flags);
  210. // FIXME: Don't drop the columns on minor updates.
  211. m_columns.clear();
  212. m_columns.append({ {}, 0 });
  213. update_column_sizes();
  214. update();
  215. }
  216. void ColumnsView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  217. {
  218. if (!model())
  219. return;
  220. auto& model = *this->model();
  221. if (!cursor_index().is_valid()) {
  222. set_cursor(model.index(0, m_model_column, {}), SelectionUpdate::Set);
  223. return;
  224. }
  225. ModelIndex new_index;
  226. auto cursor_parent = model.parent_index(cursor_index());
  227. switch (movement) {
  228. case CursorMovement::Up: {
  229. int row = cursor_index().row() > 0 ? cursor_index().row() - 1 : 0;
  230. new_index = model.index(row, cursor_index().column(), cursor_parent);
  231. break;
  232. }
  233. case CursorMovement::Down: {
  234. int row = cursor_index().row() + 1;
  235. new_index = model.index(row, cursor_index().column(), cursor_parent);
  236. break;
  237. }
  238. case CursorMovement::Left:
  239. new_index = cursor_parent;
  240. break;
  241. case CursorMovement::Right:
  242. new_index = model.index(0, m_model_column, cursor_index());
  243. if (model.is_within_range(new_index)) {
  244. if (model.is_within_range(cursor_index()))
  245. push_column(cursor_index());
  246. update();
  247. }
  248. break;
  249. default:
  250. break;
  251. }
  252. if (new_index.is_valid())
  253. set_cursor(new_index, selection_update);
  254. }
  255. Gfx::IntRect ColumnsView::content_rect(ModelIndex const& index) const
  256. {
  257. if (!index.is_valid())
  258. return {};
  259. int column_x = 0;
  260. for (auto& column : m_columns) {
  261. if (column.parent_index == index.parent())
  262. return { column_x + icon_size(), index.row() * item_height(), column.width - icon_size(), item_height() };
  263. column_x += column.width + 1;
  264. }
  265. return {};
  266. }
  267. Gfx::IntRect ColumnsView::paint_invalidation_rect(ModelIndex const& index) const
  268. {
  269. auto rect = content_rect(index);
  270. rect.translate_by(-icon_size(), 0);
  271. rect.set_width(rect.width() + icon_size());
  272. return rect;
  273. }
  274. }