mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:08:11 +00:00
GTreeView: Factor paint traversal into a shared function.
This way it can be used by hit testing as well, guaranteeing that everything stays consistent.
This commit is contained in:
parent
6b72c62c5f
commit
b181263b9f
2 changed files with 63 additions and 74 deletions
|
@ -121,41 +121,14 @@ GModelIndex GTreeView::index_at_content_position(const Point& position) const
|
||||||
{
|
{
|
||||||
if (!model())
|
if (!model())
|
||||||
return { };
|
return { };
|
||||||
auto& model = *this->model();
|
|
||||||
int indent_level = 0;
|
|
||||||
int y_offset = 0;
|
|
||||||
GModelIndex result;
|
GModelIndex result;
|
||||||
Function<bool(const GModelIndex&, GModelIndex&)> hit_test_index = [&] (const GModelIndex& index, GModelIndex& result) {
|
traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect) {
|
||||||
if (index.is_valid()) {
|
if (rect.contains(position)) {
|
||||||
auto& metadata = ensure_metadata_for_index(index);
|
result = index;
|
||||||
auto& node = *(const Node*)index.internal_data();
|
return IterationDecision::Abort;
|
||||||
int x_offset = indent_level * indent_width_in_pixels();
|
|
||||||
auto data = model.data(index, GModel::Role::Display);
|
|
||||||
Rect rect = { x_offset, y_offset, icon_size() + icon_spacing() + font().width(data.to_string()), item_height() };
|
|
||||||
dbgprintf("%s %s (%s)\n", data.to_string().characters(), rect.to_string().characters(), metadata.open ? "open" : "closed");
|
|
||||||
y_offset += item_height();
|
|
||||||
|
|
||||||
if (rect.contains(position)) {
|
|
||||||
result = index;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: Skip traversing children if this index is closed!
|
|
||||||
if (!metadata.open)
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return IterationDecision::Continue;
|
||||||
++indent_level;
|
});
|
||||||
for (int i = 0; i < model.row_count(index); ++i) {
|
|
||||||
auto child_index = model.index(i, 0, index);
|
|
||||||
if (hit_test_index(child_index, result))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
--indent_level;
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
hit_test_index(model.index(0, 0, GModelIndex()), result);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,6 +153,45 @@ void GTreeView::mousedown_event(GMouseEvent& event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
void GTreeView::traverse_in_paint_order(Callback callback) const
|
||||||
|
{
|
||||||
|
ASSERT(model());
|
||||||
|
auto& model = *this->model();
|
||||||
|
int indent_level = 0;
|
||||||
|
int y_offset = 0;
|
||||||
|
auto visible_content_rect = this->visible_content_rect();
|
||||||
|
|
||||||
|
Function<IterationDecision(const GModelIndex&)> traverse_index = [&] (const GModelIndex& index) {
|
||||||
|
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,
|
||||||
|
icon_size() + icon_spacing() + font().width(node_text), item_height()
|
||||||
|
};
|
||||||
|
if (rect.intersects(visible_content_rect)) {
|
||||||
|
if (callback(index, rect) == IterationDecision::Abort)
|
||||||
|
return IterationDecision::Abort;
|
||||||
|
}
|
||||||
|
y_offset += item_height();
|
||||||
|
// NOTE: Skip traversing children if this index is closed!
|
||||||
|
if (!metadata.open)
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
++indent_level;
|
||||||
|
for (int i = 0; i < model.row_count(index); ++i) {
|
||||||
|
if (traverse_index(model.index(i, 0, index)) == IterationDecision::Abort)
|
||||||
|
return IterationDecision::Abort;
|
||||||
|
}
|
||||||
|
--indent_level;
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
};
|
||||||
|
traverse_index(model.index(0, 0, GModelIndex()));
|
||||||
|
}
|
||||||
|
|
||||||
void GTreeView::paint_event(GPaintEvent& event)
|
void GTreeView::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
GFrame::paint_event(event);
|
GFrame::paint_event(event);
|
||||||
|
@ -193,46 +205,20 @@ void GTreeView::paint_event(GPaintEvent& event)
|
||||||
return;
|
return;
|
||||||
auto& model = *this->model();
|
auto& model = *this->model();
|
||||||
|
|
||||||
int indent_level = 0;
|
traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect) {
|
||||||
int y_offset = 0;
|
painter.fill_rect(rect, Color::LightGray);
|
||||||
|
Rect icon_rect = { rect.x(), rect.y(), icon_size(), icon_size() };
|
||||||
Function<void(const GModelIndex&)> render_index = [&] (const GModelIndex& index) {
|
auto icon = model.data(index, GModel::Role::Icon);
|
||||||
if (index.is_valid()) {
|
if (icon.is_icon()) {
|
||||||
auto& metadata = ensure_metadata_for_index(index);
|
if (auto* bitmap = icon.as_icon().bitmap_for_size(icon_size()))
|
||||||
int x_offset = indent_level * indent_width_in_pixels();
|
painter.blit(rect.location(), *bitmap, bitmap->rect());
|
||||||
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()
|
|
||||||
};
|
|
||||||
painter.fill_rect(rect, Color::LightGray);
|
|
||||||
|
|
||||||
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()))
|
|
||||||
painter.blit(rect.location(), *bitmap, bitmap->rect());
|
|
||||||
}
|
|
||||||
|
|
||||||
Rect text_rect = {
|
|
||||||
icon_rect.right() + 1 + icon_spacing(), rect.y(),
|
|
||||||
rect.width() - icon_size() - icon_spacing(), rect.height()
|
|
||||||
};
|
|
||||||
painter.draw_text(text_rect, node_text, TextAlignment::CenterLeft, Color::Black);
|
|
||||||
y_offset += item_height();
|
|
||||||
|
|
||||||
// NOTE: Skip traversing children if this index is closed!
|
|
||||||
if (!metadata.open)
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
Rect text_rect = {
|
||||||
++indent_level;
|
icon_rect.right() + 1 + icon_spacing(), rect.y(),
|
||||||
for (int i = 0; i < model.row_count(index); ++i) {
|
rect.width() - icon_size() - icon_spacing(), rect.height()
|
||||||
auto child_index = model.index(i, 0, index);
|
};
|
||||||
render_index(child_index);
|
auto node_text = model.data(index, GModel::Role::Display).to_string();
|
||||||
}
|
painter.draw_text(text_rect, node_text, TextAlignment::CenterLeft, Color::Black);
|
||||||
--indent_level;
|
return IterationDecision::Continue;
|
||||||
};
|
});
|
||||||
|
|
||||||
render_index(model.index(0, 0, GModelIndex()));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ private:
|
||||||
int icon_size() const { return 16; }
|
int icon_size() const { return 16; }
|
||||||
int icon_spacing() const { return 4; }
|
int icon_spacing() const { return 4; }
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
void traverse_in_paint_order(Callback) const;
|
||||||
|
|
||||||
struct MetadataForIndex;
|
struct MetadataForIndex;
|
||||||
|
|
||||||
MetadataForIndex& ensure_metadata_for_index(const GModelIndex&) const;
|
MetadataForIndex& ensure_metadata_for_index(const GModelIndex&) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue