ImageEditor.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2022, Mustafa Quraish <mustafa@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include "Guide.h"
  10. #include "Image.h"
  11. #include "Selection.h"
  12. #include <AK/Variant.h>
  13. #include <LibGUI/AbstractZoomPanWidget.h>
  14. #include <LibGUI/Frame.h>
  15. #include <LibGUI/UndoStack.h>
  16. #include <LibGfx/Point.h>
  17. namespace PixelPaint {
  18. class Layer;
  19. class Tool;
  20. class ImageEditor final
  21. : public GUI::AbstractZoomPanWidget
  22. , public ImageClient {
  23. C_OBJECT(ImageEditor);
  24. public:
  25. virtual ~ImageEditor() override;
  26. Image const& image() const { return m_image; }
  27. Image& image() { return m_image; }
  28. Layer* active_layer() { return m_active_layer; }
  29. void set_active_layer(Layer*);
  30. Tool* active_tool() { return m_active_tool; }
  31. void set_active_tool(Tool*);
  32. void update_tool_cursor();
  33. void did_complete_action();
  34. bool undo();
  35. bool redo();
  36. auto& undo_stack() { return m_undo_stack; }
  37. String const& path() const { return m_path; }
  38. void set_path(String);
  39. String const& title() const { return m_title; }
  40. void set_title(String);
  41. void add_guide(NonnullRefPtr<Guide> guide) { m_guides.append(guide); }
  42. void remove_guide(Guide const& guide)
  43. {
  44. m_guides.remove_first_matching([&](auto& entry) { return &guide == entry.ptr(); });
  45. }
  46. void clear_guides() { m_guides.clear(); }
  47. void layers_did_change();
  48. Layer* layer_at_editor_position(Gfx::IntPoint const&);
  49. enum class FitType {
  50. Width,
  51. Height,
  52. Image
  53. };
  54. void fit_image_to_view(FitType type = FitType::Image);
  55. Color primary_color() const { return m_primary_color; }
  56. void set_primary_color(Color);
  57. Color secondary_color() const { return m_secondary_color; }
  58. void set_secondary_color(Color);
  59. Selection& selection() { return m_selection; }
  60. Selection const& selection() const { return m_selection; }
  61. Color color_for(GUI::MouseEvent const&) const;
  62. Color color_for(GUI::MouseButton) const;
  63. Function<void(Color)> on_primary_color_change;
  64. Function<void(Color)> on_secondary_color_change;
  65. Function<void(Layer*)> on_active_layer_change;
  66. Function<void(String const&)> on_title_change;
  67. Function<void(Gfx::IntPoint const&)> on_image_mouse_position_change;
  68. Function<void(void)> on_leave;
  69. bool request_close();
  70. void save_project_as();
  71. void save_project();
  72. NonnullRefPtrVector<Guide> const& guides() const { return m_guides; }
  73. bool guide_visibility() { return m_show_guides; }
  74. void set_guide_visibility(bool show_guides);
  75. Function<void(bool)> on_set_guide_visibility;
  76. bool ruler_visibility() { return m_show_rulers; }
  77. void set_ruler_visibility(bool);
  78. Function<void(bool)> on_set_ruler_visibility;
  79. bool pixel_grid_visibility() const { return m_show_pixel_grid; }
  80. void set_pixel_grid_visibility(bool show_pixel_grid);
  81. bool show_active_layer_boundary() const { return m_show_active_layer_boundary; }
  82. void set_show_active_layer_boundary(bool);
  83. void set_loaded_from_image(bool);
  84. private:
  85. explicit ImageEditor(NonnullRefPtr<Image>);
  86. virtual void paint_event(GUI::PaintEvent&) override;
  87. virtual void second_paint_event(GUI::PaintEvent&) override;
  88. virtual void mousedown_event(GUI::MouseEvent&) override;
  89. virtual void mousemove_event(GUI::MouseEvent&) override;
  90. virtual void mouseup_event(GUI::MouseEvent&) override;
  91. virtual void keydown_event(GUI::KeyEvent&) override;
  92. virtual void keyup_event(GUI::KeyEvent&) override;
  93. virtual void context_menu_event(GUI::ContextMenuEvent&) override;
  94. virtual void enter_event(Core::Event&) override;
  95. virtual void leave_event(Core::Event&) override;
  96. virtual void image_did_change(Gfx::IntRect const&) override;
  97. virtual void image_did_change_rect(Gfx::IntRect const&) override;
  98. virtual void image_select_layer(Layer*) override;
  99. GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const;
  100. GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const;
  101. Result<void, String> save_project_to_file(Core::File&) const;
  102. int calculate_ruler_step_size() const;
  103. Gfx::IntRect mouse_indicator_rect_x() const;
  104. Gfx::IntRect mouse_indicator_rect_y() const;
  105. NonnullRefPtr<Image> m_image;
  106. RefPtr<Layer> m_active_layer;
  107. GUI::UndoStack m_undo_stack;
  108. String m_path;
  109. String m_title;
  110. NonnullRefPtrVector<Guide> m_guides;
  111. bool m_show_guides { true };
  112. bool m_show_rulers { true };
  113. bool m_show_pixel_grid { true };
  114. bool m_show_active_layer_boundary { true };
  115. Tool* m_active_tool { nullptr };
  116. Color m_primary_color { Color::Black };
  117. Color m_secondary_color { Color::White };
  118. Gfx::IntPoint m_mouse_position;
  119. int m_ruler_thickness { 20 };
  120. int m_mouse_indicator_triangle_size { 5 };
  121. float m_pixel_grid_threshold { 15.0f };
  122. Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> m_active_cursor { Gfx::StandardCursor::None };
  123. Selection m_selection;
  124. bool m_loaded_from_image { true };
  125. };
  126. }