ToolPropertiesWidget.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ToolPropertiesWidget.h"
  8. #include "Tools/Tool.h"
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/GroupBox.h>
  11. #include <LibGUI/Label.h>
  12. REGISTER_WIDGET(PixelPaint, ToolPropertiesWidget);
  13. namespace PixelPaint {
  14. ToolPropertiesWidget::ToolPropertiesWidget()
  15. {
  16. set_layout<GUI::VerticalBoxLayout>();
  17. m_group_box = add<GUI::GroupBox>("Tool properties"sv);
  18. m_group_box->set_layout<GUI::VerticalBoxLayout>(8);
  19. m_tool_widget_stack = m_group_box->add<GUI::StackWidget>();
  20. m_blank_widget = m_tool_widget_stack->add<GUI::Widget>();
  21. m_error_label = m_tool_widget_stack->add<GUI::Label>();
  22. m_error_label->set_enabled(false);
  23. }
  24. void ToolPropertiesWidget::set_active_tool(Tool* tool)
  25. {
  26. if (tool == m_active_tool)
  27. return;
  28. m_active_tool = tool;
  29. auto active_tool_widget_or_error = tool->get_properties_widget();
  30. if (active_tool_widget_or_error.is_error()) {
  31. m_active_tool_widget = nullptr;
  32. m_error_label->set_text(DeprecatedString::formatted("Error creating tool properties: {}", active_tool_widget_or_error.release_error()));
  33. m_tool_widget_stack->set_active_widget(m_error_label);
  34. return;
  35. }
  36. m_active_tool_widget = active_tool_widget_or_error.release_value();
  37. if (m_active_tool_widget == nullptr) {
  38. m_tool_widget_stack->set_active_widget(m_blank_widget);
  39. return;
  40. }
  41. if (!m_tool_widget_stack->is_ancestor_of(*m_active_tool_widget))
  42. m_tool_widget_stack->add_child(*m_active_tool_widget);
  43. m_tool_widget_stack->set_active_widget(m_active_tool_widget);
  44. }
  45. }