GTreeView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #include <LibDraw/Palette.h>
  2. #include <LibGUI/GPainter.h>
  3. #include <LibGUI/GScrollBar.h>
  4. #include <LibGUI/GTreeView.h>
  5. //#define DEBUG_ITEM_RECTS
  6. struct GTreeView::MetadataForIndex {
  7. bool open { false };
  8. };
  9. GTreeView::MetadataForIndex& GTreeView::ensure_metadata_for_index(const GModelIndex& index) const
  10. {
  11. ASSERT(index.is_valid());
  12. auto it = m_view_metadata.find(index.internal_data());
  13. if (it != m_view_metadata.end())
  14. return *it->value;
  15. auto new_metadata = make<MetadataForIndex>();
  16. auto& new_metadata_ref = *new_metadata;
  17. m_view_metadata.set(index.internal_data(), move(new_metadata));
  18. return new_metadata_ref;
  19. }
  20. GTreeView::GTreeView(GWidget* parent)
  21. : GAbstractColumnView(parent)
  22. {
  23. set_background_role(ColorRole::Base);
  24. set_size_columns_to_fit_content(true);
  25. set_headers_visible(false);
  26. m_expand_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-expand.png");
  27. m_collapse_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-collapse.png");
  28. }
  29. GTreeView::~GTreeView()
  30. {
  31. }
  32. GModelIndex GTreeView::index_at_event_position(const Point& a_position, bool& is_toggle) const
  33. {
  34. auto position = a_position.translated(0, -header_height()).translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  35. is_toggle = false;
  36. if (!model())
  37. return {};
  38. GModelIndex result;
  39. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int) {
  40. if (toggle_rect.contains(position)) {
  41. result = index;
  42. is_toggle = true;
  43. return IterationDecision::Break;
  44. }
  45. if (rect.contains_vertically(position.y())) {
  46. result = index;
  47. return IterationDecision::Break;
  48. }
  49. return IterationDecision::Continue;
  50. });
  51. return result;
  52. }
  53. void GTreeView::doubleclick_event(GMouseEvent& event)
  54. {
  55. if (!model())
  56. return;
  57. auto& model = *this->model();
  58. bool is_toggle;
  59. auto index = index_at_event_position(event.position(), is_toggle);
  60. if (!index.is_valid())
  61. return;
  62. if (event.button() == GMouseButton::Left) {
  63. if (selection().first() != index)
  64. selection().set(index);
  65. if (model.row_count(index))
  66. toggle_index(index);
  67. else
  68. activate(index);
  69. }
  70. }
  71. void GTreeView::toggle_index(const GModelIndex& index)
  72. {
  73. ASSERT(model()->row_count(index));
  74. auto& metadata = ensure_metadata_for_index(index);
  75. metadata.open = !metadata.open;
  76. update_column_sizes();
  77. update_content_size();
  78. update();
  79. }
  80. template<typename Callback>
  81. void GTreeView::traverse_in_paint_order(Callback callback) const
  82. {
  83. ASSERT(model());
  84. auto& model = *this->model();
  85. int indent_level = 1;
  86. int y_offset = 0;
  87. int tree_column = model.tree_column();
  88. int tree_column_x_offset = 0;
  89. for (int i = 0; i < tree_column; ++i) {
  90. tree_column_x_offset += column_width(i);
  91. }
  92. Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) {
  93. int row_count_at_index = model.row_count(index);
  94. if (index.is_valid()) {
  95. auto& metadata = ensure_metadata_for_index(index);
  96. int x_offset = tree_column_x_offset + horizontal_padding() + indent_level * indent_width_in_pixels();
  97. auto node_text = model.data(index, GModel::Role::Display).to_string();
  98. Rect rect = {
  99. x_offset, y_offset,
  100. icon_size() + icon_spacing() + text_padding() + font().width(node_text) + text_padding(), item_height()
  101. };
  102. Rect toggle_rect;
  103. if (row_count_at_index > 0) {
  104. int toggle_x = tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * indent_level - icon_size() / 2 - 4;
  105. toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() };
  106. toggle_rect.center_vertically_within(rect);
  107. }
  108. if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break)
  109. return IterationDecision::Break;
  110. y_offset += item_height();
  111. // NOTE: Skip traversing children if this index is closed!
  112. if (!metadata.open)
  113. return IterationDecision::Continue;
  114. }
  115. ++indent_level;
  116. int row_count = model.row_count(index);
  117. for (int i = 0; i < row_count; ++i) {
  118. if (traverse_index(model.index(i, model.tree_column(), index)) == IterationDecision::Break)
  119. return IterationDecision::Break;
  120. }
  121. --indent_level;
  122. return IterationDecision::Continue;
  123. };
  124. int root_count = model.row_count();
  125. for (int root_index = 0; root_index < root_count; ++root_index) {
  126. if (traverse_index(model.index(root_index, model.tree_column(), GModelIndex())) == IterationDecision::Break)
  127. break;
  128. }
  129. }
  130. void GTreeView::paint_event(GPaintEvent& event)
  131. {
  132. GFrame::paint_event(event);
  133. GPainter painter(*this);
  134. painter.add_clip_rect(frame_inner_rect());
  135. painter.add_clip_rect(event.rect());
  136. painter.fill_rect(event.rect(), palette().color(background_role()));
  137. if (!model())
  138. return;
  139. auto& model = *this->model();
  140. painter.translate(frame_inner_rect().location());
  141. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  142. auto visible_content_rect = this->visible_content_rect();
  143. int tree_column = model.tree_column();
  144. int tree_column_x_offset = 0;
  145. for (int i = 0; i < tree_column; ++i) {
  146. tree_column_x_offset += column_width(i);
  147. }
  148. int y_offset = header_height();
  149. int painted_row_index = 0;
  150. traverse_in_paint_order([&](const GModelIndex& index, const Rect& a_rect, const Rect& a_toggle_rect, int indent_level) {
  151. if (!a_rect.intersects_vertically(visible_content_rect))
  152. return IterationDecision::Continue;
  153. auto rect = a_rect.translated(0, y_offset);
  154. auto toggle_rect = a_toggle_rect.translated(0, y_offset);
  155. #ifdef DEBUG_ITEM_RECTS
  156. painter.fill_rect(rect, Color::WarmGray);
  157. #endif
  158. bool is_selected_row = selection().contains(index);
  159. Color text_color = palette().color(foreground_role());
  160. if (is_selected_row)
  161. text_color = Color::White;
  162. Color background_color;
  163. Color key_column_background_color;
  164. if (is_selected_row) {
  165. background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
  166. key_column_background_color = is_focused() ? palette().selection() : Color::from_rgb(0x606060);
  167. } else {
  168. if (alternating_row_colors() && (painted_row_index % 2)) {
  169. background_color = Color(220, 220, 220);
  170. key_column_background_color = Color(200, 200, 200);
  171. } else {
  172. background_color = palette().color(background_role());
  173. key_column_background_color = Color(220, 220, 220);
  174. }
  175. }
  176. Rect row_rect { 0, rect.y(), frame_inner_rect().width(), rect.height() };
  177. painter.fill_rect(row_rect, background_color);
  178. int x_offset = 0;
  179. for (int column_index = 0; column_index < model.column_count(); ++column_index) {
  180. if (is_column_hidden(column_index))
  181. continue;
  182. auto column_metadata = model.column_metadata(column_index);
  183. int column_width = this->column_width(column_index);
  184. auto& font = column_metadata.font ? *column_metadata.font : this->font();
  185. painter.draw_rect(toggle_rect, text_color);
  186. if (column_index != tree_column) {
  187. Rect cell_rect(horizontal_padding() + x_offset, rect.y(), column_width, item_height());
  188. auto cell_index = model.sibling(index.row(), column_index, index.parent());
  189. if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
  190. delegate->paint(painter, cell_rect, palette(), model, cell_index);
  191. } else {
  192. auto data = model.data(cell_index);
  193. if (data.is_bitmap()) {
  194. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  195. } else if (data.is_icon()) {
  196. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  197. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  198. } else {
  199. if (!is_selected_row)
  200. text_color = model.data(cell_index, GModel::Role::ForegroundColor).to_color(palette().color(foreground_role()));
  201. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, TextElision::Right);
  202. }
  203. }
  204. } else {
  205. // It's the tree column!
  206. Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
  207. auto icon = model.data(index, GModel::Role::Icon);
  208. if (icon.is_icon()) {
  209. if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size()))
  210. painter.blit(icon_rect.location(), *bitmap, bitmap->rect());
  211. }
  212. Rect text_rect = {
  213. icon_rect.right() + 1 + icon_spacing(), rect.y(),
  214. rect.width() - icon_size() - icon_spacing(), rect.height()
  215. };
  216. auto node_text = model.data(index, GModel::Role::Display).to_string();
  217. painter.draw_text(text_rect, node_text, TextAlignment::Center, text_color);
  218. auto index_at_indent = index;
  219. for (int i = indent_level; i > 0; --i) {
  220. auto parent_of_index_at_indent = index_at_indent.parent();
  221. bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1;
  222. Point a { tree_column_x_offset + horizontal_padding() + indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 };
  223. Point b { a.x(), a.y() + item_height() - 1 };
  224. if (index_at_indent_is_last_in_parent)
  225. b.set_y(rect.center().y());
  226. if (!(i != indent_level && index_at_indent_is_last_in_parent))
  227. painter.draw_line(a, b, Color::MidGray);
  228. if (i == indent_level) {
  229. Point c { a.x(), rect.center().y() };
  230. Point d { c.x() + icon_size() / 2, c.y() };
  231. painter.draw_line(c, d, Color::MidGray);
  232. }
  233. index_at_indent = parent_of_index_at_indent;
  234. }
  235. if (!toggle_rect.is_empty()) {
  236. auto& metadata = ensure_metadata_for_index(index);
  237. if (metadata.open)
  238. painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect());
  239. else
  240. painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect());
  241. }
  242. }
  243. x_offset += column_width + horizontal_padding() * 2;
  244. }
  245. return IterationDecision::Continue;
  246. });
  247. // Untranslate the painter vertically and do the column headers.
  248. painter.translate(0, vertical_scrollbar().value());
  249. paint_headers(painter);
  250. }
  251. void GTreeView::scroll_into_view(const GModelIndex& a_index, Orientation orientation)
  252. {
  253. if (!a_index.is_valid())
  254. return;
  255. Rect found_rect;
  256. traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect&, int) {
  257. if (index == a_index) {
  258. found_rect = rect;
  259. return IterationDecision::Break;
  260. }
  261. return IterationDecision::Continue;
  262. });
  263. GScrollableWidget::scroll_into_view(found_rect, orientation);
  264. }
  265. void GTreeView::did_update_model()
  266. {
  267. m_view_metadata.clear();
  268. GAbstractColumnView::did_update_model();
  269. }
  270. void GTreeView::did_update_selection()
  271. {
  272. GAbstractView::did_update_selection();
  273. ASSERT(model());
  274. auto index = selection().first();
  275. if (!index.is_valid())
  276. return;
  277. #if 0
  278. bool opened_any = false;
  279. for (auto current = index; current.is_valid(); current = current.parent()) {
  280. auto& metadata_for_ancestor = ensure_metadata_for_index(current);
  281. if (!metadata_for_ancestor.open) {
  282. metadata_for_ancestor.open = true;
  283. opened_any = true;
  284. }
  285. }
  286. if (opened_any)
  287. update_content_size();
  288. update();
  289. #endif
  290. if (activates_on_selection())
  291. activate(index);
  292. }
  293. void GTreeView::keydown_event(GKeyEvent& event)
  294. {
  295. if (!model())
  296. return;
  297. auto cursor_index = selection().first();
  298. if (event.key() == KeyCode::Key_Space) {
  299. if (model()->row_count(cursor_index))
  300. toggle_index(cursor_index);
  301. return;
  302. }
  303. if (event.key() == KeyCode::Key_Up) {
  304. GModelIndex previous_index;
  305. GModelIndex found_index;
  306. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  307. if (index == cursor_index) {
  308. found_index = previous_index;
  309. return IterationDecision::Break;
  310. }
  311. previous_index = index;
  312. return IterationDecision::Continue;
  313. });
  314. if (found_index.is_valid()) {
  315. selection().set(found_index);
  316. update();
  317. }
  318. return;
  319. }
  320. if (event.key() == KeyCode::Key_Down) {
  321. GModelIndex previous_index;
  322. GModelIndex found_index;
  323. traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) {
  324. if (previous_index == cursor_index) {
  325. found_index = index;
  326. return IterationDecision::Break;
  327. }
  328. previous_index = index;
  329. return IterationDecision::Continue;
  330. });
  331. if (found_index.is_valid())
  332. selection().set(found_index);
  333. return;
  334. }
  335. if (event.key() == KeyCode::Key_Left) {
  336. if (cursor_index.is_valid() && model()->row_count(cursor_index)) {
  337. auto& metadata = ensure_metadata_for_index(cursor_index);
  338. if (metadata.open) {
  339. metadata.open = false;
  340. update_column_sizes();
  341. update_content_size();
  342. update();
  343. }
  344. return;
  345. }
  346. }
  347. if (event.key() == KeyCode::Key_Right) {
  348. if (cursor_index.is_valid() && model()->row_count(cursor_index)) {
  349. auto& metadata = ensure_metadata_for_index(cursor_index);
  350. if (!metadata.open) {
  351. metadata.open = true;
  352. update_column_sizes();
  353. update_content_size();
  354. update();
  355. }
  356. return;
  357. }
  358. }
  359. }
  360. int GTreeView::item_count() const
  361. {
  362. int count = 0;
  363. traverse_in_paint_order([&](const GModelIndex&, const Rect&, const Rect&, int) {
  364. ++count;
  365. return IterationDecision::Continue;
  366. });
  367. return count;
  368. }
  369. void GTreeView::update_column_sizes()
  370. {
  371. if (!size_columns_to_fit_content())
  372. return;
  373. if (!model())
  374. return;
  375. auto& model = *this->model();
  376. int column_count = model.column_count();
  377. int row_count = model.row_count();
  378. int tree_column = model.tree_column();
  379. int tree_column_x_offset = 0;
  380. for (int column = 0; column < column_count; ++column) {
  381. if (column == tree_column)
  382. continue;
  383. if (is_column_hidden(column))
  384. continue;
  385. int header_width = header_font().width(model.column_name(column));
  386. int column_width = header_width;
  387. for (int row = 0; row < row_count; ++row) {
  388. auto cell_data = model.data(model.index(row, column));
  389. int cell_width = 0;
  390. if (cell_data.is_bitmap()) {
  391. cell_width = cell_data.as_bitmap().width();
  392. } else {
  393. cell_width = font().width(cell_data.to_string());
  394. }
  395. column_width = max(column_width, cell_width);
  396. }
  397. auto& column_data = this->column_data(column);
  398. column_data.width = max(column_data.width, column_width);
  399. column_data.has_initialized_width = true;
  400. if (column < tree_column)
  401. tree_column_x_offset += column_width;
  402. }
  403. int tree_column_header_width = header_font().width(model.column_name(tree_column));
  404. int tree_column_width = tree_column_header_width;
  405. traverse_in_paint_order([&](const GModelIndex&, const Rect& rect, const Rect&, int) {
  406. tree_column_width = max(rect.right() - tree_column_x_offset, tree_column_width);
  407. return IterationDecision::Continue;
  408. });
  409. auto& column_data = this->column_data(tree_column);
  410. column_data.width = max(column_data.width, tree_column_width);
  411. column_data.has_initialized_width = true;
  412. }