ColumnsView.cpp 11 KB

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