ColumnsView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGUI/ColumnsView.h>
  9. #include <LibGUI/Model.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGUI/Scrollbar.h>
  12. #include <LibGfx/CharacterBitmap.h>
  13. #include <LibGfx/Palette.h>
  14. namespace GUI {
  15. static constexpr Gfx::CharacterBitmap s_arrow_bitmap {
  16. " "
  17. " # "
  18. " ## "
  19. " ### "
  20. " #### "
  21. " ### "
  22. " ## "
  23. " # "
  24. " "sv,
  25. 9, 9
  26. };
  27. ColumnsView::ColumnsView()
  28. {
  29. set_fill_with_background_color(true);
  30. set_background_role(ColorRole::Base);
  31. set_foreground_role(ColorRole::BaseText);
  32. m_columns.append({ {}, 0 });
  33. }
  34. void ColumnsView::select_all()
  35. {
  36. Vector<Column> columns_for_selection;
  37. selection().for_each_index([&](auto& index) {
  38. for (auto& column : m_columns) {
  39. if (column.parent_index == index.parent()) {
  40. columns_for_selection.append(column);
  41. return;
  42. }
  43. }
  44. VERIFY_NOT_REACHED();
  45. });
  46. for (Column& column : columns_for_selection) {
  47. int row_count = model()->row_count(column.parent_index);
  48. for (int row = 0; row < row_count; row++) {
  49. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  50. selection().add(index);
  51. }
  52. }
  53. }
  54. void ColumnsView::second_paint_event(PaintEvent& event)
  55. {
  56. if (!m_rubber_banding)
  57. return;
  58. Painter painter(*this);
  59. painter.add_clip_rect(event.rect());
  60. painter.add_clip_rect(widget_inner_rect());
  61. // Columns start rendering relative to the widget inner rect. We also account for horizontal scroll here.
  62. int column_x = widget_inner_rect().left() - horizontal_scrollbar().value();
  63. for (auto const& column : m_columns) {
  64. if (m_rubber_band_origin_column.parent_index == column.parent_index)
  65. break;
  66. column_x += column.width + 1;
  67. }
  68. // After walking all columns to the current one we get its bounds relative to the widget inner rect and scroll position.
  69. auto column_left = column_x;
  70. auto column_right = column_x + m_rubber_band_origin_column.width;
  71. // The rubber band rect always stays inside the widget inner rect, the vertical component is handled by mousemove
  72. auto rubber_band_left = clamp(column_left, widget_inner_rect().left(), widget_inner_rect().right() + 1);
  73. auto rubber_band_right = clamp(column_right, widget_inner_rect().left(), widget_inner_rect().right() + 1);
  74. auto rubber_band_rect = Gfx::IntRect::from_two_points({ rubber_band_left, m_rubber_band_origin }, { rubber_band_right, m_rubber_band_current });
  75. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  76. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  77. }
  78. void ColumnsView::paint_event(PaintEvent& event)
  79. {
  80. AbstractView::paint_event(event);
  81. if (!model())
  82. return;
  83. Painter painter(*this);
  84. painter.add_clip_rect(frame_inner_rect());
  85. painter.add_clip_rect(event.rect());
  86. painter.translate(frame_thickness(), frame_thickness());
  87. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  88. int column_x = 0;
  89. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  90. for (size_t i = 0; i < m_columns.size(); i++) {
  91. auto& column = m_columns[i];
  92. auto* next_column = i + 1 == m_columns.size() ? nullptr : &m_columns[i + 1];
  93. VERIFY(column.width > 0);
  94. int row_count = model()->row_count(column.parent_index);
  95. for (int row = 0; row < row_count; row++) {
  96. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  97. VERIFY(index.is_valid());
  98. bool is_selected_row = selection().contains(index);
  99. Color background_color = palette().color(background_role());
  100. Color text_color = palette().color(foreground_role());
  101. if (next_column != nullptr && next_column->parent_index == index) {
  102. background_color = palette().inactive_selection();
  103. text_color = palette().inactive_selection_text();
  104. }
  105. if (is_selected_row) {
  106. background_color = selection_color;
  107. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  108. }
  109. Gfx::IntRect row_rect { column_x, row * item_height(), column.width, item_height() };
  110. if (m_edit_index.row() != row)
  111. painter.fill_rect(row_rect, background_color);
  112. auto icon = index.data(ModelRole::Icon);
  113. Gfx::IntRect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() };
  114. icon_rect.center_vertically_within(row_rect);
  115. if (icon.is_icon()) {
  116. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  117. if (is_selected_row) {
  118. auto tint = selection_color.with_alpha(100);
  119. painter.blit_filtered(icon_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  120. } else if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) {
  121. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  122. } else {
  123. auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  124. painter.blit(icon_rect.location(), *bitmap, bitmap->rect(), opacity);
  125. }
  126. }
  127. }
  128. Gfx::IntRect text_rect = {
  129. icon_rect.right() + 1 + icon_spacing(), row * item_height(),
  130. column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - static_cast<int>(s_arrow_bitmap.width()) - icon_spacing(), item_height()
  131. };
  132. draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::CenterLeft, Gfx::TextElision::None);
  133. if (is_focused() && index == cursor_index()) {
  134. painter.draw_rect(row_rect, palette().color(background_role()));
  135. painter.draw_focus_rect(row_rect, palette().focus_outline());
  136. }
  137. if (has_pending_drop() && index == drop_candidate_index()) {
  138. painter.draw_rect(row_rect, palette().selection(), true);
  139. }
  140. bool expandable = model()->row_count(index) > 0;
  141. if (expandable) {
  142. Gfx::IntRect arrow_rect = {
  143. text_rect.right() + 1 + icon_spacing(), 0,
  144. s_arrow_bitmap.width(), s_arrow_bitmap.height()
  145. };
  146. arrow_rect.center_vertically_within(row_rect);
  147. painter.draw_bitmap(arrow_rect.location(), s_arrow_bitmap, text_color);
  148. }
  149. }
  150. int separator_height = content_size().height();
  151. if (height() > separator_height)
  152. separator_height = height();
  153. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  154. column_x += column.width + 1;
  155. }
  156. }
  157. void ColumnsView::push_column(ModelIndex const& parent_index)
  158. {
  159. VERIFY(model());
  160. // Drop columns at the end.
  161. ModelIndex grandparent = model()->parent_index(parent_index);
  162. for (int i = m_columns.size() - 1; i > 0; i--) {
  163. if (m_columns[i].parent_index == grandparent)
  164. break;
  165. m_columns.shrink(i);
  166. dbgln("Dropping column {}", i);
  167. }
  168. // Add the new column.
  169. dbgln("Adding a new column");
  170. m_columns.append({ parent_index, 0 });
  171. update_column_sizes();
  172. update();
  173. }
  174. void ColumnsView::update_column_sizes()
  175. {
  176. if (!model())
  177. return;
  178. int total_width = 0;
  179. int total_height = 0;
  180. for (auto& column : m_columns) {
  181. int row_count = model()->row_count(column.parent_index);
  182. int column_height = row_count * item_height();
  183. if (column_height > total_height)
  184. total_height = column_height;
  185. column.width = 10;
  186. for (int row = 0; row < row_count; row++) {
  187. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  188. VERIFY(index.is_valid());
  189. auto text = index.data().to_string();
  190. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap.width() + icon_spacing();
  191. if (row_width > column.width)
  192. column.width = row_width;
  193. }
  194. total_width += column.width + 1;
  195. }
  196. set_content_size({ total_width, total_height });
  197. }
  198. Optional<ColumnsView::Column> ColumnsView::column_at_event_position(Gfx::IntPoint const& a_position) const
  199. {
  200. if (!model())
  201. return {};
  202. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  203. int column_x = 0;
  204. for (auto const& column : m_columns) {
  205. if (position.x() < column_x)
  206. break;
  207. if (position.x() > column_x + column.width) {
  208. column_x += column.width;
  209. continue;
  210. }
  211. return column;
  212. }
  213. return {};
  214. }
  215. void ColumnsView::select_range(ModelIndex const& index)
  216. {
  217. auto min_row = min(selection_start_index().row(), index.row());
  218. auto max_row = max(selection_start_index().row(), index.row());
  219. auto parent = index.parent();
  220. clear_selection();
  221. for (auto row = min_row; row <= max_row; ++row) {
  222. auto new_index = model()->index(row, m_model_column, parent);
  223. if (new_index.is_valid())
  224. toggle_selection(new_index);
  225. }
  226. }
  227. ModelIndex ColumnsView::index_at_event_position_in_column(Gfx::IntPoint const& position, Column const& column) const
  228. {
  229. int row = position.y() / item_height();
  230. int row_count = model()->row_count(column.parent_index);
  231. if (row >= row_count)
  232. return {};
  233. return model()->index(row, m_model_column, column.parent_index);
  234. }
  235. ModelIndex ColumnsView::index_at_event_position(Gfx::IntPoint const& position) const
  236. {
  237. auto const& column = column_at_event_position(position);
  238. if (!column.has_value())
  239. return {};
  240. return index_at_event_position_in_column(position, *column);
  241. }
  242. void ColumnsView::mousedown_event(MouseEvent& event)
  243. {
  244. AbstractView::mousedown_event(event);
  245. if (!model())
  246. return;
  247. if (event.button() != MouseButton::Primary)
  248. return;
  249. auto column = column_at_event_position(event.position());
  250. if (!column.has_value())
  251. return;
  252. auto index = index_at_event_position_in_column(event.position(), *column);
  253. if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
  254. if (model()->row_count(index))
  255. push_column(index);
  256. return;
  257. }
  258. if (selection_mode() == SelectionMode::MultiSelection) {
  259. m_rubber_banding = true;
  260. m_rubber_band_origin_column = *column;
  261. m_rubber_band_origin = event.position().y();
  262. m_rubber_band_current = event.position().y();
  263. }
  264. }
  265. void ColumnsView::mousemove_event(MouseEvent& event)
  266. {
  267. if (m_rubber_banding) {
  268. m_rubber_band_current = clamp(event.position().y(), widget_inner_rect().top(), widget_inner_rect().bottom() + 1);
  269. auto parent = m_rubber_band_origin_column.parent_index;
  270. int row_count = model()->row_count(parent);
  271. clear_selection();
  272. set_suppress_update_on_selection_change(true);
  273. for (int row = 0; row < row_count; row++) {
  274. auto index = model()->index(row, m_model_column, parent);
  275. VERIFY(index.is_valid());
  276. int row_top = row * item_height();
  277. int row_bottom = row * item_height() + item_height();
  278. if ((m_rubber_band_origin > row_top && m_rubber_band_current < row_top) || (m_rubber_band_origin > row_bottom && m_rubber_band_current < row_bottom)) {
  279. add_selection(index);
  280. }
  281. }
  282. set_suppress_update_on_selection_change(false);
  283. update();
  284. }
  285. AbstractView::mousemove_event(event);
  286. }
  287. void ColumnsView::mouseup_event(MouseEvent& event)
  288. {
  289. if (m_rubber_banding && event.button() == MouseButton::Primary) {
  290. m_rubber_banding = false;
  291. update();
  292. }
  293. }
  294. void ColumnsView::model_did_update(unsigned flags)
  295. {
  296. AbstractView::model_did_update(flags);
  297. // FIXME: Don't drop the columns on minor updates.
  298. m_columns.clear();
  299. m_columns.append({ {}, 0 });
  300. update_column_sizes();
  301. update();
  302. }
  303. void ColumnsView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  304. {
  305. if (!model())
  306. return;
  307. auto& model = *this->model();
  308. if (!cursor_index().is_valid()) {
  309. set_cursor(model.index(0, m_model_column, {}), SelectionUpdate::Set);
  310. return;
  311. }
  312. ModelIndex new_index;
  313. auto cursor_parent = model.parent_index(cursor_index());
  314. switch (movement) {
  315. case CursorMovement::Up: {
  316. int row = cursor_index().row() > 0 ? cursor_index().row() - 1 : 0;
  317. new_index = model.index(row, cursor_index().column(), cursor_parent);
  318. break;
  319. }
  320. case CursorMovement::Down: {
  321. int row = cursor_index().row() + 1;
  322. new_index = model.index(row, cursor_index().column(), cursor_parent);
  323. break;
  324. }
  325. case CursorMovement::Left:
  326. new_index = cursor_parent;
  327. break;
  328. case CursorMovement::Right:
  329. new_index = model.index(0, m_model_column, cursor_index());
  330. if (model.is_within_range(new_index)) {
  331. if (model.is_within_range(cursor_index()))
  332. push_column(cursor_index());
  333. update();
  334. }
  335. break;
  336. default:
  337. break;
  338. }
  339. if (new_index.is_valid())
  340. set_cursor(new_index, selection_update);
  341. }
  342. Gfx::IntRect ColumnsView::content_rect(ModelIndex const& index) const
  343. {
  344. if (!index.is_valid())
  345. return {};
  346. int column_x = 0;
  347. for (auto& column : m_columns) {
  348. if (column.parent_index == index.parent())
  349. return { column_x + icon_size(), index.row() * item_height(), column.width - icon_size(), item_height() };
  350. column_x += column.width + 1;
  351. }
  352. return {};
  353. }
  354. Gfx::IntRect ColumnsView::paint_invalidation_rect(ModelIndex const& index) const
  355. {
  356. auto rect = content_rect(index);
  357. rect.translate_by(-icon_size(), 0);
  358. rect.set_width(rect.width() + icon_size());
  359. return rect;
  360. }
  361. }