GTreeView.cpp 17 KB

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