GTreeView.cpp 17 KB

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