Browse Source

LibGUI: Draw a 1px line tree alongside the GTreeView icons.

Andreas Kling 6 years ago
parent
commit
eb182bcafc
4 changed files with 30 additions and 12 deletions
  1. 5 3
      LibGUI/GFileSystemModel.cpp
  2. 10 0
      LibGUI/GModel.cpp
  3. 1 0
      LibGUI/GModel.h
  4. 14 9
      LibGUI/GTreeView.cpp

+ 5 - 3
LibGUI/GFileSystemModel.cpp

@@ -18,10 +18,10 @@ struct GFileSystemModel::Node {
     GModelIndex index(const GFileSystemModel& model) const
     GModelIndex index(const GFileSystemModel& model) const
     {
     {
         if (!parent)
         if (!parent)
-            return { };
+            return model.create_index(0, 0, (void*)this);
         for (int row = 0; row < parent->children.size(); ++row) {
         for (int row = 0; row < parent->children.size(); ++row) {
             if (parent->children[row] == this)
             if (parent->children[row] == this)
-                return model.create_index(row, 0, parent);
+                return model.create_index(row, 0, (void*)this);
         }
         }
         ASSERT_NOT_REACHED();
         ASSERT_NOT_REACHED();
     }
     }
@@ -138,8 +138,10 @@ GModelIndex GFileSystemModel::parent_index(const GModelIndex& index) const
     if (!index.is_valid())
     if (!index.is_valid())
         return { };
         return { };
     auto& node = *(const Node*)index.internal_data();
     auto& node = *(const Node*)index.internal_data();
-    if (!node.parent)
+    if (!node.parent) {
+        ASSERT(&node == m_root);
         return { };
         return { };
+    }
     return node.parent->index(*this);
     return node.parent->index(*this);
 }
 }
 
 

+ 10 - 0
LibGUI/GModel.cpp

@@ -49,3 +49,13 @@ GModelIndex GModel::create_index(int row, int column, void* data) const
 {
 {
     return GModelIndex(*this, row, column, data);
     return GModelIndex(*this, row, column, data);
 }
 }
+
+GModelIndex GModel::sibling(int row, int column, const GModelIndex& parent) const
+{
+    if (!parent.is_valid())
+        return { };
+    int row_count = this->row_count(parent);
+    if (row < 0 || row > row_count)
+        return { };
+    return index(row, column, parent);
+}

+ 1 - 0
LibGUI/GModel.h

@@ -56,6 +56,7 @@ public:
     virtual GModelIndex parent_index(const GModelIndex&) const { return { }; }
     virtual GModelIndex parent_index(const GModelIndex&) const { return { }; }
     virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
     virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const { return create_index(row, column); }
     virtual void activate(const GModelIndex&) { }
     virtual void activate(const GModelIndex&) { }
+    virtual GModelIndex sibling(int row, int column, const GModelIndex& parent) const;
 
 
     bool is_valid(const GModelIndex& index) const
     bool is_valid(const GModelIndex& index) const
     {
     {

+ 14 - 9
LibGUI/GTreeView.cpp

@@ -127,7 +127,7 @@ GModelIndex GTreeView::index_at_content_position(const Point& position) const
     if (!model())
     if (!model())
         return { };
         return { };
     GModelIndex result;
     GModelIndex result;
-    traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect, int, bool) {
+    traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect, int) {
         if (rect.contains(position)) {
         if (rect.contains(position)) {
             result = index;
             result = index;
             return IterationDecision::Abort;
             return IterationDecision::Abort;
@@ -167,7 +167,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
     int y_offset = 0;
     int y_offset = 0;
     auto visible_content_rect = this->visible_content_rect();
     auto visible_content_rect = this->visible_content_rect();
 
 
-    Function<IterationDecision(const GModelIndex&, bool)> traverse_index = [&] (const GModelIndex& index, bool is_last_in_parent) {
+    Function<IterationDecision(const GModelIndex&)> traverse_index = [&] (const GModelIndex& index) {
         if (index.is_valid()) {
         if (index.is_valid()) {
             auto& metadata = ensure_metadata_for_index(index);
             auto& metadata = ensure_metadata_for_index(index);
             int x_offset = indent_level * indent_width_in_pixels();
             int x_offset = indent_level * indent_width_in_pixels();
@@ -177,7 +177,7 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
                 icon_size() + icon_spacing() + font().width(node_text), item_height()
                 icon_size() + icon_spacing() + font().width(node_text), item_height()
             };
             };
             if (rect.intersects(visible_content_rect)) {
             if (rect.intersects(visible_content_rect)) {
-                if (callback(index, rect, indent_level, is_last_in_parent) == IterationDecision::Abort)
+                if (callback(index, rect, indent_level) == IterationDecision::Abort)
                     return IterationDecision::Abort;
                     return IterationDecision::Abort;
             }
             }
             y_offset += item_height();
             y_offset += item_height();
@@ -189,13 +189,13 @@ void GTreeView::traverse_in_paint_order(Callback callback) const
         ++indent_level;
         ++indent_level;
         int row_count = model.row_count(index);
         int row_count = model.row_count(index);
         for (int i = 0; i < row_count; ++i) {
         for (int i = 0; i < row_count; ++i) {
-            if (traverse_index(model.index(i, 0, index), i == row_count - 1) == IterationDecision::Abort)
+            if (traverse_index(model.index(i, 0, index)) == IterationDecision::Abort)
                 return IterationDecision::Abort;
                 return IterationDecision::Abort;
         }
         }
         --indent_level;
         --indent_level;
         return IterationDecision::Continue;
         return IterationDecision::Continue;
     };
     };
-    traverse_index(model.index(0, 0, GModelIndex()), true);
+    traverse_index(model.index(0, 0, GModelIndex()));
 }
 }
 
 
 void GTreeView::paint_event(GPaintEvent& event)
 void GTreeView::paint_event(GPaintEvent& event)
@@ -211,7 +211,7 @@ void GTreeView::paint_event(GPaintEvent& event)
         return;
         return;
     auto& model = *this->model();
     auto& model = *this->model();
 
 
-    traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect, int indent_level, bool is_last_in_parent) {
+    traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect, int indent_level) {
 #ifdef DEBUG_ITEM_RECTS
 #ifdef DEBUG_ITEM_RECTS
         painter.fill_rect(rect, Color::LightGray);
         painter.fill_rect(rect, Color::LightGray);
 #endif
 #endif
@@ -227,18 +227,23 @@ void GTreeView::paint_event(GPaintEvent& event)
         };
         };
         auto node_text = model.data(index, GModel::Role::Display).to_string();
         auto node_text = model.data(index, GModel::Role::Display).to_string();
         painter.draw_text(text_rect, node_text, TextAlignment::CenterLeft, Color::Black);
         painter.draw_text(text_rect, node_text, TextAlignment::CenterLeft, Color::Black);
-        for (int i = 0; i <= indent_level; ++i) {
+        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;
             Point a { indent_width_in_pixels() * i - icon_size() / 2, rect.y() };
             Point a { indent_width_in_pixels() * i - icon_size() / 2, rect.y() };
             Point b { a.x(), a.y() + item_height() - 1 };
             Point b { a.x(), a.y() + item_height() - 1 };
-            if (i == indent_level && is_last_in_parent)
+            if (index_at_indent_is_last_in_parent)
                 b.set_y(rect.center().y());
                 b.set_y(rect.center().y());
-            painter.draw_line(a, b, Color::MidGray);
+            if (!(i != indent_level && index_at_indent_is_last_in_parent))
+                painter.draw_line(a, b, Color::MidGray);
 
 
             if (i == indent_level) {
             if (i == indent_level) {
                 Point c { a.x(), rect.center().y() };
                 Point c { a.x(), rect.center().y() };
                 Point d { c.x() + icon_size() / 2, c.y() };
                 Point d { c.x() + icon_size() / 2, c.y() };
                 painter.draw_line(c, d, Color::MidGray);
                 painter.draw_line(c, d, Color::MidGray);
             }
             }
+            index_at_indent = parent_of_index_at_indent;
         }
         }
         return IterationDecision::Continue;
         return IterationDecision::Continue;
     });
     });