Serenity Operating System
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
8#include "ToolPropertiesWidget.h"
9#include "Tools/Tool.h"
10#include <LibGUI/BoxLayout.h>
11#include <LibGUI/GroupBox.h>
12#include <LibGUI/Label.h>
13
14REGISTER_WIDGET(PixelPaint, ToolPropertiesWidget);
15
16namespace PixelPaint {
17
18ToolPropertiesWidget::ToolPropertiesWidget()
19{
20 set_layout<GUI::VerticalBoxLayout>();
21
22 m_group_box = add<GUI::GroupBox>("Tool properties"sv);
23 m_group_box->set_layout<GUI::VerticalBoxLayout>(8);
24 m_tool_widget_stack = m_group_box->add<GUI::StackWidget>();
25 m_blank_widget = m_tool_widget_stack->add<GUI::Widget>();
26 m_error_label = m_tool_widget_stack->add<GUI::Label>();
27 m_error_label->set_enabled(false);
28}
29
30void ToolPropertiesWidget::set_active_tool(Tool* tool)
31{
32 if (tool == m_active_tool)
33 return;
34
35 m_active_tool = tool;
36 auto active_tool_widget_or_error = tool->get_properties_widget();
37 if (active_tool_widget_or_error.is_error()) {
38 m_active_tool_widget = nullptr;
39 m_error_label->set_text(DeprecatedString::formatted("Error creating tool properties: {}", active_tool_widget_or_error.release_error()));
40 m_tool_widget_stack->set_active_widget(m_error_label);
41 return;
42 }
43 m_active_tool_widget = active_tool_widget_or_error.release_value();
44 if (m_active_tool_widget == nullptr) {
45 m_tool_widget_stack->set_active_widget(m_blank_widget);
46 return;
47 }
48
49 if (!m_tool_widget_stack->is_ancestor_of(*m_active_tool_widget))
50 m_tool_widget_stack->add_child(*m_active_tool_widget);
51
52 m_tool_widget_stack->set_active_widget(m_active_tool_widget);
53}
54}