ToolboxWidget.cpp 3.7 KB

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