ToolboxWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ToolboxWidget.h"
  8. #include "Tools/BrushTool.h"
  9. #include "Tools/BucketTool.h"
  10. #include "Tools/CloneTool.h"
  11. #include "Tools/EllipseTool.h"
  12. #include "Tools/EraseTool.h"
  13. #include "Tools/GradientTool.h"
  14. #include "Tools/GuideTool.h"
  15. #include "Tools/LassoSelectTool.h"
  16. #include "Tools/LineTool.h"
  17. #include "Tools/MoveTool.h"
  18. #include "Tools/PenTool.h"
  19. #include "Tools/PickerTool.h"
  20. #include "Tools/PolygonalSelectTool.h"
  21. #include "Tools/RectangleSelectTool.h"
  22. #include "Tools/RectangleTool.h"
  23. #include "Tools/SprayTool.h"
  24. #include "Tools/TextTool.h"
  25. #include "Tools/WandSelectTool.h"
  26. #include "Tools/ZoomTool.h"
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Toolbar.h>
  31. REGISTER_WIDGET(PixelPaint, ToolboxWidget);
  32. namespace PixelPaint {
  33. ToolboxWidget::ToolboxWidget()
  34. {
  35. set_fill_with_background_color(true);
  36. set_fixed_width(26);
  37. set_layout<GUI::VerticalBoxLayout>(2, 0);
  38. m_action_group.set_exclusive(true);
  39. m_action_group.set_unchecking_allowed(false);
  40. m_toolbar = add<GUI::Toolbar>(Gfx::Orientation::Vertical);
  41. m_toolbar->set_collapsible(true);
  42. setup_tools();
  43. }
  44. void ToolboxWidget::setup_tools()
  45. {
  46. auto add_tool = [&](StringView icon_name, GUI::Shortcut const& shortcut, NonnullOwnPtr<Tool> tool, bool is_default_tool = false) {
  47. auto action = GUI::Action::create_checkable(tool->tool_name(), shortcut, Gfx::Bitmap::load_from_file(DeprecatedString::formatted("/res/icons/pixelpaint/{}.png", icon_name)).release_value_but_fixme_should_propagate_errors(),
  48. [this, tool = tool.ptr()](auto& action) {
  49. if (action.is_checked()) {
  50. on_tool_selection(tool);
  51. m_active_tool = tool;
  52. } else {
  53. on_tool_selection(nullptr);
  54. }
  55. });
  56. m_action_group.add_action(action);
  57. auto& button = m_toolbar->add_action(action);
  58. button.on_context_menu_request = [action = action.ptr(), tool = tool.ptr()](auto& event) {
  59. action->activate();
  60. tool->on_tool_button_contextmenu(event);
  61. };
  62. tool->set_action(action);
  63. m_tools.append(move(tool));
  64. if (is_default_tool) {
  65. action->set_checked(true);
  66. auto default_tool_index = m_tools.size() - 1;
  67. deferred_invoke([&, default_tool_index]() {
  68. VERIFY(m_active_tool == nullptr);
  69. on_tool_selection(m_tools[default_tool_index]);
  70. });
  71. }
  72. };
  73. add_tool("move"sv, { 0, Key_M }, make<MoveTool>());
  74. add_tool("pen"sv, { 0, Key_N }, make<PenTool>(), true);
  75. add_tool("brush"sv, { 0, Key_P }, make<BrushTool>());
  76. add_tool("bucket"sv, { Mod_Shift, Key_B }, make<BucketTool>());
  77. add_tool("spray"sv, { Mod_Shift, Key_S }, make<SprayTool>());
  78. add_tool("picker"sv, { 0, Key_O }, make<PickerTool>());
  79. add_tool("eraser"sv, { Mod_Shift, Key_E }, make<EraseTool>());
  80. add_tool("line"sv, { Mod_Ctrl | Mod_Shift, Key_L }, make<LineTool>());
  81. add_tool("rectangle"sv, { Mod_Ctrl | Mod_Shift, Key_R }, make<RectangleTool>());
  82. add_tool("circle"sv, { Mod_Ctrl | Mod_Shift, Key_E }, make<EllipseTool>());
  83. add_tool("text"sv, { Mod_Ctrl | Mod_Shift, Key_T }, make<TextTool>());
  84. add_tool("zoom"sv, { 0, Key_Z }, make<ZoomTool>());
  85. add_tool("rectangle-select"sv, { 0, Key_R }, make<RectangleSelectTool>());
  86. add_tool("wand-select"sv, { 0, Key_W }, make<WandSelectTool>());
  87. add_tool("polygonal-select"sv, { Mod_Shift, Key_P }, make<PolygonalSelectTool>());
  88. add_tool("lasso-select"sv, { 0, Key_L }, make<LassoSelectTool>());
  89. add_tool("guides"sv, { 0, Key_G }, make<GuideTool>());
  90. add_tool("clone"sv, { 0, Key_C }, make<CloneTool>());
  91. add_tool("gradients"sv, { Mod_Ctrl, Key_G }, make<GradientTool>());
  92. }
  93. }