From 967eec1e52ea865e9ee718da4f78f678f7b03df9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 29 Mar 2019 20:18:15 +0100 Subject: [PATCH] GTreeView: Add expand/collapse buttons to items with children. --- Base/res/icons/treeview-collapse.png | Bin 143 -> 157 bytes Base/res/icons/treeview-expand.png | Bin 151 -> 167 bytes LibGUI/GTreeView.cpp | 16 ++++++++++++++-- LibGUI/GTreeView.h | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Base/res/icons/treeview-collapse.png b/Base/res/icons/treeview-collapse.png index d56f76a6024a3bf6db3ed544412e22c9b91d8dd9..451d870226e65954ffd2b4b275bc95adeddbc3f8 100644 GIT binary patch delta 98 zcmeBYoXa@DDO$+N+UR;&ECU0Bji-xah{WaOl7fOC=N%X}7#J8Byt%($emRr7yE~g2 zn-W7{S40Ajo0}WY$^ZZVFVx{))Rt5$JJx>?M5Q)plB?SdP&O2}j1U-51;KTgRMh=H5N@6Nz pX4#S#MGb>eWCOb-Iyu!D{ygRoP?>-20RsaAgQu&X%Q~loCIH@y9aI1S diff --git a/Base/res/icons/treeview-expand.png b/Base/res/icons/treeview-expand.png index 66f5d1e3b6e701a0d35aee051b5d0186951194b1..7beedb7955352c951cea5d9a2111a163d1e9dfcb 100644 GIT binary patch delta 108 zcmbQvxSVlrin>-D?$cQ7z8xOuubhDcnVI>D3cfB_G)yCVPQ_;<^9E*IGp7_P3! z#;IcLZFhxH=@His>s(&d+=Rso>ary4jMFn%CdK{F&TGCV#t>Rp#ZdT0XsYk6`PB>z O3=E#GelF{r5}E)JxF$>h delta 92 zcmZ3^IGu5VQ>cI$gU(wPX$A%cGfx-C5Q)plB?SdP&O2}j1U-51;KO{*Zi_ZGH8v#% xZa(3J9JdnJ9uF=F&ux6OmE3q3lMZn*aBt$~pXHPn#K6G7;OXk;vd$@?2>?FD8|DB2 diff --git a/LibGUI/GTreeView.cpp b/LibGUI/GTreeView.cpp index 2e7e5ee449..147282f94d 100644 --- a/LibGUI/GTreeView.cpp +++ b/LibGUI/GTreeView.cpp @@ -174,7 +174,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const auto node_text = model.data(index, GModel::Role::Display).to_string(); Rect rect = { x_offset, y_offset, - icon_size() + icon_spacing() + font().width(node_text), item_height() + toggle_size() + icon_spacing() + icon_size() + icon_spacing() + font().width(node_text), item_height() }; if (rect.intersects(visible_content_rect)) { if (callback(index, rect, indent_level) == IterationDecision::Abort) @@ -219,7 +219,7 @@ void GTreeView::paint_event(GPaintEvent& event) auto icon = model.data(index, GModel::Role::Icon); if (icon.is_icon()) { if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size())) - painter.blit(rect.location(), *bitmap, bitmap->rect()); + painter.blit(icon_rect.location(), *bitmap, bitmap->rect()); } Rect text_rect = { icon_rect.right() + 1 + icon_spacing(), rect.y(), @@ -245,6 +245,18 @@ void GTreeView::paint_event(GPaintEvent& event) } index_at_indent = parent_of_index_at_indent; } + + if (model.row_count(index) > 0) { + int toggle_x = indent_width_in_pixels() * indent_level - icon_size() / 2 - 3; + Rect toggle_rect = { toggle_x, rect.y(), toggle_size(), toggle_size() }; + toggle_rect.center_vertically_within(rect); + 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()); + } + return IterationDecision::Continue; }); } diff --git a/LibGUI/GTreeView.h b/LibGUI/GTreeView.h index a3603fcc7f..d8c6e005c8 100644 --- a/LibGUI/GTreeView.h +++ b/LibGUI/GTreeView.h @@ -21,6 +21,7 @@ private: int indent_width_in_pixels() const { return 16; } int icon_size() const { return 16; } int icon_spacing() const { return 4; } + int toggle_size() const { return 9; } template void traverse_in_paint_order(Callback) const;