GColumnsView.cpp 9.8 KB

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