GColumnsView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <LibDraw/CharacterBitmap.h>
  27. #include <LibGUI/GColumnsView.h>
  28. #include <LibGUI/GPainter.h>
  29. #include <LibGUI/GScrollBar.h>
  30. static const char* s_arrow_bitmap_data = {
  31. " "
  32. " # "
  33. " ## "
  34. " ### "
  35. " #### "
  36. " ### "
  37. " ## "
  38. " # "
  39. " "
  40. };
  41. static const int s_arrow_bitmap_width = 9;
  42. static const int s_arrow_bitmap_height = 9;
  43. GColumnsView::GColumnsView(GWidget* parent)
  44. : GAbstractView(parent)
  45. {
  46. set_fill_with_background_color(true);
  47. set_background_role(ColorRole::Base);
  48. set_foreground_role(ColorRole::BaseText);
  49. set_frame_shape(FrameShape::Container);
  50. set_frame_shadow(FrameShadow::Sunken);
  51. set_frame_thickness(2);
  52. m_columns.append({ {}, 0 });
  53. }
  54. GColumnsView::~GColumnsView()
  55. {
  56. }
  57. void GColumnsView::paint_event(GPaintEvent& event)
  58. {
  59. GAbstractView::paint_event(event);
  60. if (!model())
  61. return;
  62. GPainter painter(*this);
  63. painter.add_clip_rect(frame_inner_rect());
  64. painter.add_clip_rect(event.rect());
  65. painter.translate(frame_thickness(), frame_thickness());
  66. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  67. int column_x = 0;
  68. for (int 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. ASSERT(column.width > 0);
  72. int row_count = model()->row_count(column.parent_index);
  73. for (int row = 0; row < row_count; row++) {
  74. GModelIndex index = model()->index(row, m_model_column, column.parent_index);
  75. ASSERT(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 = background_color.blend(palette().selection().with_alpha(100));
  81. if (is_selected_row) {
  82. background_color = palette().selection();
  83. text_color = palette().selection_text();
  84. }
  85. Rect row_rect { column_x, row * item_height(), column.width, item_height() };
  86. painter.fill_rect(row_rect, background_color);
  87. auto icon = model()->data(index, GModel::Role::Icon);
  88. Rect 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. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  93. Rect text_rect = {
  94. icon_rect.right() + 1 + icon_spacing(), row * item_height(),
  95. column.width - icon_spacing() - icon_size() - icon_spacing() - icon_spacing() - s_arrow_bitmap_width - icon_spacing(), item_height()
  96. };
  97. auto text = model()->data(index).to_string();
  98. painter.draw_text(text_rect, text, TextAlignment::CenterLeft, text_color);
  99. bool expandable = model()->row_count(index) > 0;
  100. if (expandable) {
  101. Rect arrow_rect = {
  102. text_rect.right() + 1 + icon_spacing(), 0,
  103. s_arrow_bitmap_width, s_arrow_bitmap_height
  104. };
  105. arrow_rect.center_vertically_within(row_rect);
  106. static auto& arrow_bitmap = CharacterBitmap::create_from_ascii(s_arrow_bitmap_data, s_arrow_bitmap_width, s_arrow_bitmap_height).leak_ref();
  107. painter.draw_bitmap(arrow_rect.location(), arrow_bitmap, text_color);
  108. }
  109. }
  110. int separator_height = content_size().height();
  111. if (height() > separator_height)
  112. separator_height = height();
  113. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  114. column_x += column.width + 1;
  115. }
  116. }
  117. void GColumnsView::push_column(GModelIndex& parent_index)
  118. {
  119. ASSERT(model());
  120. // Drop columns at the end.
  121. GModelIndex grandparent = model()->parent_index(parent_index);
  122. for (int i = m_columns.size() - 1; i > 0; i--) {
  123. if (m_columns[i].parent_index == grandparent)
  124. break;
  125. m_columns.shrink(i);
  126. dbg() << "Dropping column " << i;
  127. }
  128. // Add the new column.
  129. dbg() << "Adding a new column";
  130. m_columns.append({ parent_index, 0 });
  131. update_column_sizes();
  132. update();
  133. }
  134. void GColumnsView::update_column_sizes()
  135. {
  136. if (!model())
  137. return;
  138. int total_width = 0;
  139. int total_height = 0;
  140. for (auto& column : m_columns) {
  141. int row_count = model()->row_count(column.parent_index);
  142. int column_height = row_count * item_height();
  143. if (column_height > total_height)
  144. total_height = column_height;
  145. column.width = 10;
  146. for (int row = 0; row < row_count; row++) {
  147. GModelIndex index = model()->index(row, m_model_column, column.parent_index);
  148. ASSERT(index.is_valid());
  149. auto text = model()->data(index).to_string();
  150. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
  151. if (row_width > column.width)
  152. column.width = row_width;
  153. }
  154. total_width += column.width + 1;
  155. }
  156. set_content_size({ total_width, total_height });
  157. }
  158. GModelIndex GColumnsView::index_at_event_position(const Point& a_position) const
  159. {
  160. if (!model())
  161. return {};
  162. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  163. int column_x = 0;
  164. for (auto& column : m_columns) {
  165. if (position.x() < column_x)
  166. break;
  167. if (position.x() > column_x + column.width) {
  168. column_x += column.width;
  169. continue;
  170. }
  171. int row = position.y() / item_height();
  172. int row_count = model()->row_count(column.parent_index);
  173. if (row >= row_count)
  174. return {};
  175. return model()->index(row, m_model_column, column.parent_index);
  176. }
  177. return {};
  178. }
  179. void GColumnsView::mousedown_event(GMouseEvent& event)
  180. {
  181. GAbstractView::mousedown_event(event);
  182. if (!model())
  183. return;
  184. if (event.button() != GMouseButton::Left)
  185. return;
  186. auto index = index_at_event_position(event.position());
  187. if (index.is_valid() && !(event.modifiers() & Mod_Ctrl)) {
  188. if (model()->row_count(index))
  189. push_column(index);
  190. }
  191. }
  192. void GColumnsView::did_update_model()
  193. {
  194. GAbstractView::did_update_model();
  195. // FIXME: Don't drop the columns on minor updates.
  196. dbg() << "Model was updated; dropping columns :(";
  197. m_columns.clear();
  198. m_columns.append({ {}, 0 });
  199. update_column_sizes();
  200. update();
  201. }
  202. void GColumnsView::keydown_event(GKeyEvent& event)
  203. {
  204. if (!model())
  205. return;
  206. auto& model = *this->model();
  207. if (event.key() == KeyCode::Key_Return) {
  208. activate_selected();
  209. return;
  210. }
  211. if (event.key() == KeyCode::Key_Up) {
  212. GModelIndex new_index;
  213. if (!selection().is_empty()) {
  214. auto old_index = selection().first();
  215. auto parent_index = model.parent_index(old_index);
  216. int row = old_index.row() > 0 ? old_index.row() - 1 : 0;
  217. new_index = model.sibling(row, old_index.column(), parent_index);
  218. } else {
  219. new_index = model.index(0, m_model_column, {});
  220. }
  221. if (model.is_valid(new_index)) {
  222. selection().set(new_index);
  223. update();
  224. }
  225. return;
  226. }
  227. if (event.key() == KeyCode::Key_Down) {
  228. GModelIndex new_index;
  229. if (!selection().is_empty()) {
  230. auto old_index = selection().first();
  231. auto parent_index = model.parent_index(old_index);
  232. int row = old_index.row() + 1;
  233. new_index = model.sibling(row, old_index.column(), parent_index);
  234. } else {
  235. new_index = model.index(0, m_model_column, {});
  236. }
  237. if (model.is_valid(new_index)) {
  238. selection().set(new_index);
  239. update();
  240. }
  241. return;
  242. }
  243. if (event.key() == KeyCode::Key_Left) {
  244. GModelIndex new_index;
  245. if (!selection().is_empty()) {
  246. auto old_index = selection().first();
  247. new_index = model.parent_index(old_index);
  248. } else {
  249. new_index = model.index(0, m_model_column, {});
  250. }
  251. if (model.is_valid(new_index)) {
  252. selection().set(new_index);
  253. update();
  254. }
  255. return;
  256. }
  257. if (event.key() == KeyCode::Key_Right) {
  258. GModelIndex old_index, new_index;
  259. if (!selection().is_empty()) {
  260. old_index = selection().first();
  261. new_index = model.index(0, m_model_column, old_index);
  262. } else {
  263. new_index = model.index(0, m_model_column, {});
  264. }
  265. if (model.is_valid(new_index)) {
  266. selection().set(new_index);
  267. if (model.is_valid(old_index))
  268. push_column(old_index);
  269. update();
  270. }
  271. return;
  272. }
  273. }