| 
									
										
										
										
											2019-03-29 04:00:07 +01:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | #include <LibGUI/GScrollBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GTreeView.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  | //#define DEBUG_ITEM_RECTS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | struct GTreeView::MetadataForIndex { | 
					
						
							| 
									
										
										
										
											2019-03-29 17:03:30 +01:00
										 |  |  |     bool open { false }; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GTreeView::MetadataForIndex& GTreeView::ensure_metadata_for_index(const GModelIndex& index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(index.is_valid()); | 
					
						
							|  |  |  |     auto it = m_view_metadata.find(index.internal_data()); | 
					
						
							|  |  |  |     if (it != m_view_metadata.end()) | 
					
						
							|  |  |  |         return *it->value; | 
					
						
							|  |  |  |     auto new_metadata = make<MetadataForIndex>(); | 
					
						
							|  |  |  |     auto& new_metadata_ref = *new_metadata; | 
					
						
							|  |  |  |     m_view_metadata.set(index.internal_data(), move(new_metadata)); | 
					
						
							|  |  |  |     return new_metadata_ref; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 04:00:07 +01:00
										 |  |  | GTreeView::GTreeView(GWidget* parent) | 
					
						
							|  |  |  |     : GAbstractView(parent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-10 03:43:46 +02:00
										 |  |  |     set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(FrameShadow::Sunken); | 
					
						
							| 
									
										
										
										
											2019-03-29 04:58:15 +01:00
										 |  |  |     set_frame_thickness(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  |     m_expand_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-expand.png"); | 
					
						
							|  |  |  |     m_collapse_bitmap = GraphicsBitmap::load_from_file("/res/icons/treeview-collapse.png"); | 
					
						
							| 
									
										
										
										
											2019-09-05 21:37:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     set_should_hide_unnecessary_scrollbars(true); | 
					
						
							| 
									
										
										
										
											2019-03-29 04:00:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GTreeView::~GTreeView() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  | GModelIndex GTreeView::index_at_content_position(const Point& position, bool& is_toggle) const | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |     is_toggle = false; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     if (!model()) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     GModelIndex result; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int) { | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         if (rect.contains(position)) { | 
					
						
							|  |  |  |             result = index; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |             return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |         if (toggle_rect.contains(position)) { | 
					
						
							|  |  |  |             result = index; | 
					
						
							|  |  |  |             is_toggle = true; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |             return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTreeView::mousedown_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							|  |  |  |     auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |     bool is_toggle; | 
					
						
							|  |  |  |     auto index = index_at_content_position(adjusted_position, is_toggle); | 
					
						
							| 
									
										
										
										
											2019-03-29 20:36:15 +01:00
										 |  |  |     if (!index.is_valid()) | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-03-29 20:36:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |     if (selection().first() != index) { | 
					
						
							|  |  |  |         selection().set(index); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 10:17:00 +02:00
										 |  |  |     if (is_toggle && model.row_count(index)) | 
					
						
							|  |  |  |         toggle_index(index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-07 21:45:06 +02:00
										 |  |  | void GTreeView::doubleclick_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							|  |  |  |     auto adjusted_position = event.position().translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness()); | 
					
						
							|  |  |  |     bool is_toggle; | 
					
						
							|  |  |  |     auto index = index_at_content_position(adjusted_position, is_toggle); | 
					
						
							|  |  |  |     if (!index.is_valid()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (selection().first() != index) | 
					
						
							|  |  |  |         selection().set(index); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (model.row_count(index)) | 
					
						
							|  |  |  |         toggle_index(index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 10:17:00 +02:00
										 |  |  | void GTreeView::toggle_index(const GModelIndex& index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(model()->row_count(index)); | 
					
						
							|  |  |  |     auto& metadata = ensure_metadata_for_index(index); | 
					
						
							|  |  |  |     metadata.open = !metadata.open; | 
					
						
							|  |  |  |     update_content_size(); | 
					
						
							|  |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  | template<typename Callback> | 
					
						
							|  |  |  | void GTreeView::traverse_in_paint_order(Callback callback) const | 
					
						
							| 
									
										
										
										
											2019-03-29 04:00:07 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |     ASSERT(model()); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     auto& model = *this->model(); | 
					
						
							| 
									
										
										
										
											2019-08-18 10:17:00 +02:00
										 |  |  |     int indent_level = 1; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     int y_offset = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     Function<IterationDecision(const GModelIndex&)> traverse_index = [&](const GModelIndex& index) { | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |         int row_count_at_index = model.row_count(index); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |         if (index.is_valid()) { | 
					
						
							|  |  |  |             auto& metadata = ensure_metadata_for_index(index); | 
					
						
							|  |  |  |             int x_offset = indent_level * indent_width_in_pixels(); | 
					
						
							|  |  |  |             auto node_text = model.data(index, GModel::Role::Display).to_string(); | 
					
						
							|  |  |  |             Rect rect = { | 
					
						
							|  |  |  |                 x_offset, y_offset, | 
					
						
							| 
									
										
										
										
											2019-03-30 01:42:16 +01:00
										 |  |  |                 icon_size() + icon_spacing() + text_padding() + font().width(node_text) + text_padding(), item_height() | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |             }; | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |             Rect toggle_rect; | 
					
						
							|  |  |  |             if (row_count_at_index > 0) { | 
					
						
							|  |  |  |                 int toggle_x = indent_width_in_pixels() * indent_level - icon_size() / 2 - 4; | 
					
						
							|  |  |  |                 toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() }; | 
					
						
							|  |  |  |                 toggle_rect.center_vertically_within(rect); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |             if (callback(index, rect, toggle_rect, indent_level) == IterationDecision::Break) | 
					
						
							|  |  |  |                 return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |             y_offset += item_height(); | 
					
						
							|  |  |  |             // NOTE: Skip traversing children if this index is closed!
 | 
					
						
							|  |  |  |             if (!metadata.open) | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |                 return IterationDecision::Continue; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ++indent_level; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  |         int row_count = model.row_count(index); | 
					
						
							|  |  |  |         for (int i = 0; i < row_count; ++i) { | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |             if (traverse_index(model.index(i, 0, index)) == IterationDecision::Break) | 
					
						
							|  |  |  |                 return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         --indent_level; | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         return IterationDecision::Continue; | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-08-18 10:17:00 +02:00
										 |  |  |     int root_count = model.row_count(); | 
					
						
							|  |  |  |     for (int root_index = 0; root_index < root_count; ++root_index) { | 
					
						
							|  |  |  |         if (traverse_index(model.index(root_index, 0, GModelIndex())) == IterationDecision::Break) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTreeView::paint_event(GPaintEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GFrame::paint_event(event); | 
					
						
							|  |  |  |     GPainter painter(*this); | 
					
						
							|  |  |  |     painter.add_clip_rect(frame_inner_rect()); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  |     painter.fill_rect(event.rect(), Color::White); | 
					
						
							|  |  |  |     painter.translate(frame_inner_rect().location()); | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |     painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     auto& model = *this->model(); | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |     auto visible_content_rect = this->visible_content_rect(); | 
					
						
							| 
									
										
										
										
											2019-03-29 14:46:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect& toggle_rect, int indent_level) { | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |         if (!rect.intersects(visible_content_rect)) | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  | #ifdef DEBUG_ITEM_RECTS
 | 
					
						
							| 
									
										
										
										
											2019-06-30 09:23:16 +02:00
										 |  |  |         painter.fill_rect(rect, Color::WarmGray); | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-29 20:36:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Color background_color = Color::from_rgb(0xffffff); | 
					
						
							|  |  |  |         Color text_color = Color::from_rgb(0x000000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() }; | 
					
						
							|  |  |  |         auto icon = model.data(index, GModel::Role::Icon); | 
					
						
							|  |  |  |         if (icon.is_icon()) { | 
					
						
							|  |  |  |             if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) | 
					
						
							| 
									
										
										
										
											2019-03-29 20:18:15 +01:00
										 |  |  |                 painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         Rect text_rect = { | 
					
						
							|  |  |  |             icon_rect.right() + 1 + icon_spacing(), rect.y(), | 
					
						
							|  |  |  |             rect.width() - icon_size() - icon_spacing(), rect.height() | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |         if (selection().contains(index)) { | 
					
						
							| 
									
										
										
										
											2019-03-30 01:42:16 +01:00
										 |  |  |             background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060); | 
					
						
							|  |  |  |             text_color = Color::from_rgb(0xffffff); | 
					
						
							|  |  |  |             painter.fill_rect(text_rect, background_color); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         auto node_text = model.data(index, GModel::Role::Display).to_string(); | 
					
						
							| 
									
										
										
										
											2019-03-30 01:42:16 +01:00
										 |  |  |         painter.draw_text(text_rect, node_text, TextAlignment::Center, text_color); | 
					
						
							| 
									
										
										
										
											2019-03-29 19:48:15 +01:00
										 |  |  |         auto index_at_indent = index; | 
					
						
							|  |  |  |         for (int i = indent_level; i >= 0; --i) { | 
					
						
							|  |  |  |             auto parent_of_index_at_indent = index_at_indent.parent(); | 
					
						
							|  |  |  |             bool index_at_indent_is_last_in_parent = index_at_indent.row() == model.row_count(parent_of_index_at_indent) - 1; | 
					
						
							| 
									
										
										
										
											2019-03-30 01:42:16 +01:00
										 |  |  |             Point a { indent_width_in_pixels() * i - icon_size() / 2, rect.y() - 2 }; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  |             Point b { a.x(), a.y() + item_height() - 1 }; | 
					
						
							| 
									
										
										
										
											2019-03-29 19:48:15 +01:00
										 |  |  |             if (index_at_indent_is_last_in_parent) | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  |                 b.set_y(rect.center().y()); | 
					
						
							| 
									
										
										
										
											2019-03-29 19:48:15 +01:00
										 |  |  |             if (!(i != indent_level && index_at_indent_is_last_in_parent)) | 
					
						
							|  |  |  |                 painter.draw_line(a, b, Color::MidGray); | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (i == indent_level) { | 
					
						
							|  |  |  |                 Point c { a.x(), rect.center().y() }; | 
					
						
							|  |  |  |                 Point d { c.x() + icon_size() / 2, c.y() }; | 
					
						
							|  |  |  |                 painter.draw_line(c, d, Color::MidGray); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-03-29 19:48:15 +01:00
										 |  |  |             index_at_indent = parent_of_index_at_indent; | 
					
						
							| 
									
										
										
										
											2019-03-29 18:10:36 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-29 20:18:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 03:44:08 +01:00
										 |  |  |         if (!toggle_rect.is_empty()) { | 
					
						
							| 
									
										
										
										
											2019-03-29 20:18:15 +01:00
										 |  |  |             auto& metadata = ensure_metadata_for_index(index); | 
					
						
							|  |  |  |             if (metadata.open) | 
					
						
							|  |  |  |                 painter.blit(toggle_rect.location(), *m_collapse_bitmap, m_collapse_bitmap->rect()); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 painter.blit(toggle_rect.location(), *m_expand_bitmap, m_expand_bitmap->rect()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 17:30:27 +01:00
										 |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-03-29 04:00:07 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GTreeView::scroll_into_view(const GModelIndex& a_index, Orientation orientation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!a_index.is_valid()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     Rect found_rect; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     traverse_in_paint_order([&](const GModelIndex& index, const Rect& rect, const Rect&, int) { | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  |         if (index == a_index) { | 
					
						
							|  |  |  |             found_rect = rect; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |             return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     GScrollableWidget::scroll_into_view(found_rect, orientation); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  | void GTreeView::did_update_model() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GAbstractView::did_update_model(); | 
					
						
							|  |  |  |     update_content_size(); | 
					
						
							| 
									
										
										
										
											2019-11-11 19:11:55 +01:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  | void GTreeView::did_update_selection() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:57:08 +01:00
										 |  |  |     GAbstractView::did_update_selection(); | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  |     ASSERT(model()); | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |     auto index = selection().first(); | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  |     if (!index.is_valid()) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |     bool opened_any = false; | 
					
						
							| 
									
										
										
										
											2019-08-01 00:32:41 +10:00
										 |  |  |     for (auto current = index; current.is_valid(); current = current.parent()) { | 
					
						
							|  |  |  |         auto& metadata_for_ancestor = ensure_metadata_for_index(current); | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |         if (!metadata_for_ancestor.open) { | 
					
						
							|  |  |  |             metadata_for_ancestor.open = true; | 
					
						
							|  |  |  |             opened_any = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (opened_any) | 
					
						
							|  |  |  |         update_content_size(); | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-05-09 04:56:52 +02:00
										 |  |  |     if (activates_on_selection()) | 
					
						
							|  |  |  |         activate(index); | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GTreeView::update_content_size() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int height = 0; | 
					
						
							|  |  |  |     int width = 0; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     traverse_in_paint_order([&](const GModelIndex&, const Rect& rect, const Rect&, int) { | 
					
						
							| 
									
										
										
										
											2019-03-30 04:57:57 +01:00
										 |  |  |         width = max(width, rect.right()); | 
					
						
							|  |  |  |         height += rect.height(); | 
					
						
							|  |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     set_content_size({ width, height }); | 
					
						
							| 
									
										
										
										
											2019-03-30 03:27:25 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GTreeView::keydown_event(GKeyEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!model()) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |     auto cursor_index = selection().first(); | 
					
						
							| 
									
										
										
										
											2019-08-18 10:17:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Space) { | 
					
						
							|  |  |  |         if (model()->row_count(cursor_index)) | 
					
						
							|  |  |  |             toggle_index(cursor_index); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |     if (event.key() == KeyCode::Key_Up) { | 
					
						
							|  |  |  |         GModelIndex previous_index; | 
					
						
							|  |  |  |         GModelIndex found_index; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) { | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |             if (index == cursor_index) { | 
					
						
							|  |  |  |                 found_index = previous_index; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |                 return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             previous_index = index; | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if (found_index.is_valid()) { | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |             selection().set(found_index); | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (event.key() == KeyCode::Key_Down) { | 
					
						
							|  |  |  |         GModelIndex previous_index; | 
					
						
							|  |  |  |         GModelIndex found_index; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         traverse_in_paint_order([&](const GModelIndex& index, const Rect&, const Rect&, int) { | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |             if (previous_index == cursor_index) { | 
					
						
							|  |  |  |                 found_index = index; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |                 return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             previous_index = index; | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if (found_index.is_valid()) | 
					
						
							| 
									
										
										
										
											2019-09-07 20:15:33 +02:00
										 |  |  |             selection().set(found_index); | 
					
						
							| 
									
										
										
										
											2019-04-02 03:18:26 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |