GTreeView.cpp 17 KB

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