GColumnsView.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. int separator_height = content_size().height();
  86. if (height() > separator_height)
  87. separator_height = height();
  88. painter.draw_line({ column_x + column.width, 0 }, { column_x + column.width, separator_height }, palette().button());
  89. column_x += column.width + 1;
  90. }
  91. }
  92. void GColumnsView::push_column(GModelIndex& parent_index)
  93. {
  94. ASSERT(model());
  95. // Drop columns at the end.
  96. GModelIndex grandparent = model()->parent_index(parent_index);
  97. for (int i = m_columns.size() - 1; i > 0; i--) {
  98. if (m_columns[i].parent_index == grandparent)
  99. break;
  100. m_columns.shrink(i);
  101. dbg() << "Dropping column " << i;
  102. }
  103. // Add the new column.
  104. dbg() << "Adding a new column";
  105. m_columns.append({ parent_index, 0 });
  106. update_column_sizes();
  107. update();
  108. }
  109. void GColumnsView::update_column_sizes()
  110. {
  111. if (!model())
  112. return;
  113. int total_width = 0;
  114. int total_height = 0;
  115. for (auto& column : m_columns) {
  116. int row_count = model()->row_count(column.parent_index);
  117. int column_height = row_count * item_height();
  118. if (column_height > total_height)
  119. total_height = column_height;
  120. column.width = 10;
  121. for (int row = 0; row < row_count; row++) {
  122. GModelIndex index = model()->index(row, m_model_column, column.parent_index);
  123. ASSERT(index.is_valid());
  124. auto text = model()->data(index).to_string();
  125. int row_width = icon_spacing() + icon_size() + icon_spacing() + font().width(text) + icon_spacing() + s_arrow_bitmap_width + icon_spacing();
  126. if (row_width > column.width)
  127. column.width = row_width;
  128. }
  129. total_width += column.width + 1;
  130. }
  131. set_content_size({ total_width, total_height });
  132. }
  133. GModelIndex GColumnsView::index_at_event_position(const Point& a_position) const
  134. {
  135. if (!model())
  136. return {};
  137. auto position = a_position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  138. int column_x = 0;
  139. for (auto& column : m_columns) {
  140. if (position.x() < column_x)
  141. break;
  142. if (position.x() > column_x + column.width) {
  143. column_x += column.width;
  144. continue;
  145. }
  146. int row = position.y() / item_height();
  147. int row_count = model()->row_count(column.parent_index);
  148. if (row >= row_count)
  149. return {};
  150. return model()->index(row, m_model_column, column.parent_index);
  151. }
  152. return {};
  153. }
  154. void GColumnsView::mousedown_event(GMouseEvent& event)
  155. {
  156. if (!model())
  157. return;
  158. if (event.button() != GMouseButton::Left)
  159. return;
  160. auto index = index_at_event_position(event.position());
  161. if (!index.is_valid()) {
  162. selection().clear();
  163. return;
  164. }
  165. if (event.modifiers() & Mod_Ctrl) {
  166. selection().toggle(index);
  167. } else {
  168. selection().set(index);
  169. if (model()->row_count(index))
  170. push_column(index);
  171. }
  172. }
  173. void GColumnsView::did_update_model()
  174. {
  175. GAbstractView::did_update_model();
  176. // FIXME: Don't drop the columns on minor updates.
  177. dbg() << "Model was updated; dropping columns :(";
  178. m_columns.clear();
  179. m_columns.append({ {}, 0 });
  180. update_column_sizes();
  181. update();
  182. }
  183. void GColumnsView::doubleclick_event(GMouseEvent& event)
  184. {
  185. if (!model())
  186. return;
  187. if (event.button() != GMouseButton::Left)
  188. return;
  189. mousedown_event(event);
  190. activate_selected();
  191. }
  192. void GColumnsView::context_menu_event(GContextMenuEvent& event)
  193. {
  194. if (!model())
  195. return;
  196. auto index = index_at_event_position(event.position());
  197. if (index.is_valid()) {
  198. if (!selection().contains(index))
  199. selection().set(index);
  200. } else {
  201. selection().clear();
  202. }
  203. if (on_context_menu_request)
  204. on_context_menu_request(index, event);
  205. }
  206. void GColumnsView::keydown_event(GKeyEvent& event)
  207. {
  208. if (!model())
  209. return;
  210. auto& model = *this->model();
  211. if (event.key() == KeyCode::Key_Return) {
  212. activate_selected();
  213. return;
  214. }
  215. if (event.key() == KeyCode::Key_Up) {
  216. GModelIndex new_index;
  217. if (!selection().is_empty()) {
  218. auto old_index = selection().first();
  219. auto parent_index = model.parent_index(old_index);
  220. int row = old_index.row() > 0 ? old_index.row() - 1 : 0;
  221. new_index = model.sibling(row, old_index.column(), parent_index);
  222. } else {
  223. new_index = model.index(0, m_model_column, {});
  224. }
  225. if (model.is_valid(new_index)) {
  226. selection().set(new_index);
  227. update();
  228. }
  229. return;
  230. }
  231. if (event.key() == KeyCode::Key_Down) {
  232. GModelIndex new_index;
  233. if (!selection().is_empty()) {
  234. auto old_index = selection().first();
  235. auto parent_index = model.parent_index(old_index);
  236. int row = old_index.row() + 1;
  237. new_index = model.sibling(row, old_index.column(), parent_index);
  238. } else {
  239. new_index = model.index(0, m_model_column, {});
  240. }
  241. if (model.is_valid(new_index)) {
  242. selection().set(new_index);
  243. update();
  244. }
  245. return;
  246. }
  247. if (event.key() == KeyCode::Key_Left) {
  248. GModelIndex new_index;
  249. if (!selection().is_empty()) {
  250. auto old_index = selection().first();
  251. new_index = model.parent_index(old_index);
  252. } else {
  253. new_index = model.index(0, m_model_column, {});
  254. }
  255. if (model.is_valid(new_index)) {
  256. selection().set(new_index);
  257. update();
  258. }
  259. return;
  260. }
  261. if (event.key() == KeyCode::Key_Right) {
  262. GModelIndex old_index, new_index;
  263. if (!selection().is_empty()) {
  264. old_index = selection().first();
  265. new_index = model.index(0, m_model_column, old_index);
  266. } else {
  267. new_index = model.index(0, m_model_column, {});
  268. }
  269. if (model.is_valid(new_index)) {
  270. selection().set(new_index);
  271. if (model.is_valid(old_index))
  272. push_column(old_index);
  273. update();
  274. }
  275. return;
  276. }
  277. }