ColumnsView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/ColumnsView.h>
  27. #include <LibGUI/Model.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/ScrollBar.h>
  30. #include <LibGfx/CharacterBitmap.h>
  31. #include <LibGfx/Palette.h>
  32. namespace GUI {
  33. static const char* s_arrow_bitmap_data = {
  34. " "
  35. " # "
  36. " ## "
  37. " ### "
  38. " #### "
  39. " ### "
  40. " ## "
  41. " # "
  42. " "
  43. };
  44. static const int s_arrow_bitmap_width = 9;
  45. static const int s_arrow_bitmap_height = 9;
  46. ColumnsView::ColumnsView()
  47. {
  48. set_fill_with_background_color(true);
  49. set_background_role(ColorRole::Base);
  50. set_foreground_role(ColorRole::BaseText);
  51. m_columns.append({ {}, 0 });
  52. }
  53. ColumnsView::~ColumnsView()
  54. {
  55. }
  56. void ColumnsView::select_all()
  57. {
  58. Vector<Column> columns_for_selection;
  59. selection().for_each_index([&](auto& index) {
  60. for (auto& column : m_columns) {
  61. if (column.parent_index == index.parent()) {
  62. columns_for_selection.append(column);
  63. return;
  64. }
  65. }
  66. ASSERT_NOT_REACHED();
  67. });
  68. for (Column& column : columns_for_selection) {
  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. selection().add(index);
  73. }
  74. }
  75. }
  76. void ColumnsView::paint_event(PaintEvent& event)
  77. {
  78. AbstractView::paint_event(event);
  79. if (!model())
  80. return;
  81. Painter painter(*this);
  82. painter.add_clip_rect(frame_inner_rect());
  83. painter.add_clip_rect(event.rect());
  84. painter.translate(frame_thickness(), frame_thickness());
  85. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  86. int column_x = 0;
  87. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  88. for (size_t i = 0; i < m_columns.size(); i++) {
  89. auto& column = m_columns[i];
  90. auto* next_column = i + 1 == m_columns.size() ? nullptr : &m_columns[i + 1];
  91. ASSERT(column.width > 0);
  92. int row_count = model()->row_count(column.parent_index);
  93. for (int row = 0; row < row_count; row++) {
  94. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  95. ASSERT(index.is_valid());
  96. bool is_selected_row = selection().contains(index);
  97. Color background_color = palette().color(background_role());
  98. Color text_color = palette().color(foreground_role());
  99. if (next_column != nullptr && next_column->parent_index == index) {
  100. background_color = palette().inactive_selection();
  101. text_color = palette().inactive_selection_text();
  102. }
  103. if (is_selected_row) {
  104. background_color = selection_color;
  105. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  106. }
  107. Gfx::IntRect row_rect { column_x, row * item_height(), column.width, item_height() };
  108. painter.fill_rect(row_rect, background_color);
  109. auto icon = index.data(ModelRole::Icon);
  110. Gfx::IntRect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() };
  111. icon_rect.center_vertically_within(row_rect);
  112. if (icon.is_icon()) {
  113. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  114. if (is_selected_row) {
  115. auto tint = selection_color.with_alpha(100);
  116. painter.blit_filtered(icon_rect.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  117. } else if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row()) {
  118. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  119. } else {
  120. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  121. }
  122. }
  123. }
  124. Gfx::IntRect text_rect = {
  125. icon_rect.right() + 1 + icon_spacing(), row * item_height(),
  126. column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - s_arrow_bitmap_width - icon_spacing(), item_height()
  127. };
  128. draw_item_text(painter, index, is_selected_row, text_rect, index.data().to_string(), font_for_index(index), Gfx::TextAlignment::CenterLeft, Gfx::TextElision::None);
  129. if (is_focused() && index == cursor_index()) {
  130. painter.draw_rect(row_rect, palette().color(background_role()));
  131. painter.draw_focus_rect(row_rect, palette().focus_outline());
  132. }
  133. if (has_pending_drop() && index == drop_candidate_index()) {
  134. painter.draw_rect(row_rect, palette().selection(), true);
  135. }
  136. bool expandable = model()->row_count(index) > 0;
  137. if (expandable) {
  138. Gfx::IntRect arrow_rect = {
  139. text_rect.right() + 1 + icon_spacing(), 0,
  140. s_arrow_bitmap_width, s_arrow_bitmap_height
  141. };
  142. arrow_rect.center_vertically_within(row_rect);
  143. static auto& arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_arrow_bitmap_data, s_arrow_bitmap_width, s_arrow_bitmap_height).leak_ref();
  144. painter.draw_bitmap(arrow_rect.location(), arrow_bitmap, text_color);
  145. }
  146. }
  147. int separator_height = content_size().height();
  148. if (height() > separator_height)
  149. separator_height = height();
  150. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  151. column_x += column.width + 1;
  152. }
  153. }
  154. void ColumnsView::push_column(const ModelIndex& parent_index)
  155. {
  156. ASSERT(model());
  157. // Drop columns at the end.
  158. ModelIndex grandparent = model()->parent_index(parent_index);
  159. for (int i = m_columns.size() - 1; i > 0; i--) {
  160. if (m_columns[i].parent_index == grandparent)
  161. break;
  162. m_columns.shrink(i);
  163. dbgln("Dropping column {}", i);
  164. }
  165. // Add the new column.
  166. dbgln("Adding a new column");
  167. m_columns.append({ parent_index, 0 });
  168. update_column_sizes();
  169. update();
  170. }
  171. void ColumnsView::update_column_sizes()
  172. {
  173. if (!model())
  174. return;
  175. int total_width = 0;
  176. int total_height = 0;
  177. for (auto& column : m_columns) {
  178. int row_count = model()->row_count(column.parent_index);
  179. int column_height = row_count * item_height();
  180. if (column_height > total_height)
  181. total_height = column_height;
  182. column.width = 10;
  183. for (int row = 0; row < row_count; row++) {
  184. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  185. ASSERT(index.is_valid());
  186. auto text = index.data().to_string();
  187. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
  188. if (row_width > column.width)
  189. column.width = row_width;
  190. }
  191. total_width += column.width + 1;
  192. }
  193. set_content_size({ total_width, total_height });
  194. }
  195. ModelIndex ColumnsView::index_at_event_position(const Gfx::IntPoint& a_position) const
  196. {
  197. if (!model())
  198. return {};
  199. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  200. int column_x = 0;
  201. for (auto& column : m_columns) {
  202. if (position.x() < column_x)
  203. break;
  204. if (position.x() > column_x + column.width) {
  205. column_x += column.width;
  206. continue;
  207. }
  208. int row = position.y() / item_height();
  209. int row_count = model()->row_count(column.parent_index);
  210. if (row >= row_count)
  211. return {};
  212. return model()->index(row, m_model_column, column.parent_index);
  213. }
  214. return {};
  215. }
  216. void ColumnsView::mousedown_event(MouseEvent& event)
  217. {
  218. AbstractView::mousedown_event(event);
  219. if (!model())
  220. return;
  221. if (event.button() != MouseButton::Left)
  222. return;
  223. auto index = index_at_event_position(event.position());
  224. if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
  225. if (model()->row_count(index))
  226. push_column(index);
  227. }
  228. }
  229. void ColumnsView::model_did_update(unsigned flags)
  230. {
  231. AbstractView::model_did_update(flags);
  232. // FIXME: Don't drop the columns on minor updates.
  233. dbgln("Model was updated; dropping columns :(");
  234. m_columns.clear();
  235. m_columns.append({ {}, 0 });
  236. update_column_sizes();
  237. update();
  238. }
  239. void ColumnsView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  240. {
  241. if (!model())
  242. return;
  243. auto& model = *this->model();
  244. if (!cursor_index().is_valid()) {
  245. set_cursor(model.index(0, m_model_column, {}), SelectionUpdate::Set);
  246. return;
  247. }
  248. ModelIndex new_index;
  249. auto cursor_parent = model.parent_index(cursor_index());
  250. switch (movement) {
  251. case CursorMovement::Up: {
  252. int row = cursor_index().row() > 0 ? cursor_index().row() - 1 : 0;
  253. new_index = model.index(row, cursor_index().column(), cursor_parent);
  254. break;
  255. }
  256. case CursorMovement::Down: {
  257. int row = cursor_index().row() + 1;
  258. new_index = model.index(row, cursor_index().column(), cursor_parent);
  259. break;
  260. }
  261. case CursorMovement::Left:
  262. new_index = cursor_parent;
  263. break;
  264. case CursorMovement::Right:
  265. new_index = model.index(0, m_model_column, cursor_index());
  266. if (model.is_valid(new_index)) {
  267. if (model.is_valid(cursor_index()))
  268. push_column(cursor_index());
  269. update();
  270. break;
  271. }
  272. default:
  273. break;
  274. }
  275. if (new_index.is_valid())
  276. set_cursor(new_index, selection_update);
  277. }
  278. Gfx::IntRect ColumnsView::content_rect(const ModelIndex& index) const
  279. {
  280. if (!index.is_valid())
  281. return {};
  282. int column_x = 0;
  283. for (auto& column : m_columns) {
  284. if (column.parent_index == index.parent())
  285. return { column_x + icon_size(), index.row() * item_height(), column.width - icon_size(), item_height() };
  286. column_x += column.width + 1;
  287. }
  288. return {};
  289. }
  290. }