PixelPaint: Use a StackWidget in ToolPropertiesWidget

Previously changing tools while simultaneously using a widget like a
slider would result in a event_dispatch() failure.
Instead use a StackWidget to set the active widget.
This commit is contained in:
Marcus Nilsson 2021-08-06 11:57:56 +02:00 committed by Andreas Kling
parent 2e6fc13b1e
commit 4a57a4a0b3
2 changed files with 15 additions and 5 deletions

View file

@ -20,6 +20,8 @@ ToolPropertiesWidget::ToolPropertiesWidget()
m_group_box = add<GUI::GroupBox>("Tool properties");
auto& layout = m_group_box->set_layout<GUI::VerticalBoxLayout>();
layout.set_margins({ 10, 20, 10, 10 });
m_tool_widget_stack = m_group_box->add<GUI::StackWidget>();
m_blank_widget = m_tool_widget_stack->add<GUI::Widget>();
}
void ToolPropertiesWidget::set_active_tool(Tool* tool)
@ -27,13 +29,18 @@ void ToolPropertiesWidget::set_active_tool(Tool* tool)
if (tool == m_active_tool)
return;
if (m_active_tool_widget != nullptr)
m_group_box->remove_child(*m_active_tool_widget);
m_active_tool = tool;
m_active_tool_widget = tool->get_properties_widget();
if (m_active_tool_widget != nullptr)
m_group_box->add_child(*m_active_tool_widget);
if (m_active_tool_widget == nullptr) {
m_tool_widget_stack->set_active_widget(m_blank_widget);
return;
}
if (!m_tool_widget_stack->is_ancestor_of(*m_active_tool_widget))
m_tool_widget_stack->add_child(*m_active_tool_widget);
m_tool_widget_stack->set_active_widget(m_active_tool_widget);
}
ToolPropertiesWidget::~ToolPropertiesWidget()

View file

@ -8,6 +8,7 @@
#include <AK/RefPtr.h>
#include <LibGUI/Forward.h>
#include <LibGUI/StackWidget.h>
#include <LibGUI/Widget.h>
namespace PixelPaint {
@ -28,6 +29,8 @@ private:
RefPtr<GUI::GroupBox> m_group_box;
Tool* m_active_tool { nullptr };
RefPtr<GUI::StackWidget> m_tool_widget_stack;
RefPtr<GUI::Widget> m_blank_widget;
GUI::Widget* m_active_tool_widget { nullptr };
};