ColumnsView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. for (size_t i = 0; i < m_columns.size(); i++) {
  88. auto& column = m_columns[i];
  89. auto* next_column = i + 1 == m_columns.size() ? nullptr : &m_columns[i + 1];
  90. ASSERT(column.width > 0);
  91. int row_count = model()->row_count(column.parent_index);
  92. for (int row = 0; row < row_count; row++) {
  93. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  94. ASSERT(index.is_valid());
  95. bool is_selected_row = selection().contains(index);
  96. Color background_color = palette().color(background_role());
  97. Color text_color = palette().color(foreground_role());
  98. if (next_column != nullptr && next_column->parent_index == index) {
  99. background_color = palette().inactive_selection();
  100. text_color = palette().inactive_selection_text();
  101. }
  102. if (is_selected_row) {
  103. background_color = palette().selection();
  104. text_color = palette().selection_text();
  105. }
  106. Gfx::Rect row_rect { column_x, row * item_height(), column.width, item_height() };
  107. painter.fill_rect(row_rect, background_color);
  108. auto icon = model()->data(index, Model::Role::Icon);
  109. Gfx::Rect icon_rect = { column_x + icon_spacing(), 0, icon_size(), icon_size() };
  110. icon_rect.center_vertically_within(row_rect);
  111. if (icon.is_icon()) {
  112. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) {
  113. if (m_hovered_index.is_valid() && m_hovered_index.parent() == index.parent() && m_hovered_index.row() == index.row())
  114. painter.blit_brightened(icon_rect.location(), *bitmap, bitmap->rect());
  115. else
  116. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  117. }
  118. }
  119. Gfx::Rect text_rect = {
  120. icon_rect.right() + 1 + icon_spacing(), row * item_height(),
  121. column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - s_arrow_bitmap_width - icon_spacing(), item_height()
  122. };
  123. auto text = model()->data(index).to_string();
  124. painter.draw_text(text_rect, text, Gfx::TextAlignment::CenterLeft, text_color);
  125. bool expandable = model()->row_count(index) > 0;
  126. if (expandable) {
  127. Gfx::Rect arrow_rect = {
  128. text_rect.right() + 1 + icon_spacing(), 0,
  129. s_arrow_bitmap_width, s_arrow_bitmap_height
  130. };
  131. arrow_rect.center_vertically_within(row_rect);
  132. static auto& arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_arrow_bitmap_data, s_arrow_bitmap_width, s_arrow_bitmap_height).leak_ref();
  133. painter.draw_bitmap(arrow_rect.location(), arrow_bitmap, text_color);
  134. }
  135. }
  136. int separator_height = content_size().height();
  137. if (height() > separator_height)
  138. separator_height = height();
  139. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  140. column_x += column.width + 1;
  141. }
  142. }
  143. void ColumnsView::push_column(ModelIndex& parent_index)
  144. {
  145. ASSERT(model());
  146. // Drop columns at the end.
  147. ModelIndex grandparent = model()->parent_index(parent_index);
  148. for (int i = m_columns.size() - 1; i > 0; i--) {
  149. if (m_columns[i].parent_index == grandparent)
  150. break;
  151. m_columns.shrink(i);
  152. dbg() << "Dropping column " << i;
  153. }
  154. // Add the new column.
  155. dbg() << "Adding a new column";
  156. m_columns.append({ parent_index, 0 });
  157. update_column_sizes();
  158. update();
  159. }
  160. void ColumnsView::update_column_sizes()
  161. {
  162. if (!model())
  163. return;
  164. int total_width = 0;
  165. int total_height = 0;
  166. for (auto& column : m_columns) {
  167. int row_count = model()->row_count(column.parent_index);
  168. int column_height = row_count * item_height();
  169. if (column_height > total_height)
  170. total_height = column_height;
  171. column.width = 10;
  172. for (int row = 0; row < row_count; row++) {
  173. ModelIndex index = model()->index(row, m_model_column, column.parent_index);
  174. ASSERT(index.is_valid());
  175. auto text = model()->data(index).to_string();
  176. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
  177. if (row_width > column.width)
  178. column.width = row_width;
  179. }
  180. total_width += column.width + 1;
  181. }
  182. set_content_size({ total_width, total_height });
  183. }
  184. ModelIndex ColumnsView::index_at_event_position(const Gfx::Point& a_position) const
  185. {
  186. if (!model())
  187. return {};
  188. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  189. int column_x = 0;
  190. for (auto& column : m_columns) {
  191. if (position.x() < column_x)
  192. break;
  193. if (position.x() > column_x + column.width) {
  194. column_x += column.width;
  195. continue;
  196. }
  197. int row = position.y() / item_height();
  198. int row_count = model()->row_count(column.parent_index);
  199. if (row >= row_count)
  200. return {};
  201. return model()->index(row, m_model_column, column.parent_index);
  202. }
  203. return {};
  204. }
  205. void ColumnsView::mousedown_event(MouseEvent& event)
  206. {
  207. AbstractView::mousedown_event(event);
  208. if (!model())
  209. return;
  210. if (event.button() != MouseButton::Left)
  211. return;
  212. auto index = index_at_event_position(event.position());
  213. if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
  214. if (model()->row_count(index))
  215. push_column(index);
  216. }
  217. }
  218. void ColumnsView::did_update_model()
  219. {
  220. AbstractView::did_update_model();
  221. // FIXME: Don't drop the columns on minor updates.
  222. dbg() << "Model was updated; dropping columns :(";
  223. m_columns.clear();
  224. m_columns.append({ {}, 0 });
  225. update_column_sizes();
  226. update();
  227. }
  228. void ColumnsView::keydown_event(KeyEvent& event)
  229. {
  230. if (!model())
  231. return;
  232. auto& model = *this->model();
  233. if (event.key() == KeyCode::Key_Return) {
  234. activate_selected();
  235. return;
  236. }
  237. if (event.key() == KeyCode::Key_Up) {
  238. ModelIndex new_index;
  239. if (!selection().is_empty()) {
  240. auto old_index = selection().first();
  241. auto parent_index = model.parent_index(old_index);
  242. int row = old_index.row() > 0 ? old_index.row() - 1 : 0;
  243. new_index = model.sibling(row, old_index.column(), parent_index);
  244. } else {
  245. new_index = model.index(0, m_model_column, {});
  246. }
  247. if (model.is_valid(new_index)) {
  248. selection().set(new_index);
  249. update();
  250. }
  251. return;
  252. }
  253. if (event.key() == KeyCode::Key_Down) {
  254. ModelIndex new_index;
  255. if (!selection().is_empty()) {
  256. auto old_index = selection().first();
  257. auto parent_index = model.parent_index(old_index);
  258. int row = old_index.row() + 1;
  259. new_index = model.sibling(row, old_index.column(), parent_index);
  260. } else {
  261. new_index = model.index(0, m_model_column, {});
  262. }
  263. if (model.is_valid(new_index)) {
  264. selection().set(new_index);
  265. update();
  266. }
  267. return;
  268. }
  269. if (event.key() == KeyCode::Key_Left) {
  270. ModelIndex new_index;
  271. if (!selection().is_empty()) {
  272. auto old_index = selection().first();
  273. new_index = model.parent_index(old_index);
  274. } else {
  275. new_index = model.index(0, m_model_column, {});
  276. }
  277. if (model.is_valid(new_index)) {
  278. selection().set(new_index);
  279. update();
  280. }
  281. return;
  282. }
  283. if (event.key() == KeyCode::Key_Right) {
  284. ModelIndex old_index, new_index;
  285. if (!selection().is_empty()) {
  286. old_index = selection().first();
  287. new_index = model.index(0, m_model_column, old_index);
  288. } else {
  289. new_index = model.index(0, m_model_column, {});
  290. }
  291. if (model.is_valid(new_index)) {
  292. selection().set(new_index);
  293. if (model.is_valid(old_index))
  294. push_column(old_index);
  295. update();
  296. }
  297. return;
  298. }
  299. }
  300. }