ToolboxWidget.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ToolboxWidget.h"
  7. #include "BrushTool.h"
  8. #include "BucketTool.h"
  9. #include "EllipseTool.h"
  10. #include "EraseTool.h"
  11. #include "LineTool.h"
  12. #include "MoveTool.h"
  13. #include "PenTool.h"
  14. #include "PickerTool.h"
  15. #include "RectangleTool.h"
  16. #include "SprayTool.h"
  17. #include "ZoomTool.h"
  18. #include <LibGUI/Action.h>
  19. #include <LibGUI/BoxLayout.h>
  20. #include <LibGUI/Button.h>
  21. #include <LibGUI/Toolbar.h>
  22. REGISTER_WIDGET(PixelPaint, ToolboxWidget);
  23. namespace PixelPaint {
  24. ToolboxWidget::ToolboxWidget()
  25. {
  26. set_fill_with_background_color(true);
  27. set_fixed_width(26);
  28. set_layout<GUI::VerticalBoxLayout>();
  29. layout()->set_spacing(0);
  30. layout()->set_margins({ 2, 2, 2, 2 });
  31. m_action_group.set_exclusive(true);
  32. m_action_group.set_unchecking_allowed(false);
  33. m_toolbar = add<GUI::Toolbar>(Gfx::Orientation::Vertical);
  34. setup_tools();
  35. }
  36. ToolboxWidget::~ToolboxWidget()
  37. {
  38. }
  39. void ToolboxWidget::setup_tools()
  40. {
  41. auto add_tool = [&](String name, StringView const& icon_name, GUI::Shortcut const& shortcut, NonnullOwnPtr<Tool> tool) {
  42. auto action = GUI::Action::create_checkable(move(name), shortcut, Gfx::Bitmap::load_from_file(String::formatted("/res/icons/pixelpaint/{}.png", icon_name)),
  43. [this, tool = tool.ptr()](auto& action) {
  44. if (action.is_checked())
  45. on_tool_selection(tool);
  46. else
  47. on_tool_selection(nullptr);
  48. });
  49. m_action_group.add_action(action);
  50. auto& button = m_toolbar->add_action(action);
  51. button.on_context_menu_request = [action = action.ptr(), tool = tool.ptr()](auto& event) {
  52. action->activate();
  53. tool->on_tool_button_contextmenu(event);
  54. };
  55. tool->set_action(action);
  56. m_tools.append(move(tool));
  57. };
  58. add_tool("Move", "move", { 0, Key_M }, make<MoveTool>());
  59. add_tool("Pen", "pen", { 0, Key_N }, make<PenTool>());
  60. add_tool("Brush", "brush", { 0, Key_P }, make<BrushTool>());
  61. add_tool("Bucket Fill", "bucket", { Mod_Shift, Key_B }, make<BucketTool>());
  62. add_tool("Spray", "spray", { Mod_Shift, Key_S }, make<SprayTool>());
  63. add_tool("Color Picker", "picker", { 0, Key_O }, make<PickerTool>());
  64. add_tool("Erase", "eraser", { Mod_Shift, Key_E }, make<EraseTool>());
  65. add_tool("Line", "line", { Mod_Ctrl | Mod_Shift, Key_L }, make<LineTool>());
  66. add_tool("Rectangle", "rectangle", { Mod_Ctrl | Mod_Shift, Key_R }, make<RectangleTool>());
  67. add_tool("Ellipse", "circle", { Mod_Ctrl | Mod_Shift, Key_E }, make<EllipseTool>());
  68. add_tool("Zoom", "zoom", { 0, Key_Z }, make<ZoomTool>());
  69. }
  70. }