ImageEditor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ImageEditor.h"
  10. #include "Image.h"
  11. #include "Layer.h"
  12. #include "Tools/MoveTool.h"
  13. #include "Tools/Tool.h"
  14. #include <AK/LexicalPath.h>
  15. #include <LibConfig/Client.h>
  16. #include <LibFileSystemAccessClient/Client.h>
  17. #include <LibGUI/Command.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/Painter.h>
  20. #include <LibGfx/DisjointRectSet.h>
  21. #include <LibGfx/Palette.h>
  22. #include <LibGfx/Rect.h>
  23. namespace PixelPaint {
  24. ImageEditor::ImageEditor(NonnullRefPtr<Image> image)
  25. : m_image(move(image))
  26. , m_title("Untitled")
  27. , m_selection(*this)
  28. {
  29. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  30. m_undo_stack.push(make<ImageUndoCommand>(*m_image));
  31. m_image->add_client(*this);
  32. set_original_rect(m_image->rect());
  33. set_scale_bounds(0.1f, 100.0f);
  34. m_pixel_grid_threshold = (float)Config::read_i32("PixelPaint", "PixelGrid", "Threshold", 15);
  35. m_show_pixel_grid = Config::read_bool("PixelPaint", "PixelGrid", "Show", true);
  36. m_show_rulers = Config::read_bool("PixelPaint", "Rulers", "Show", true);
  37. m_show_guides = Config::read_bool("PixelPaint", "Guides", "Show", true);
  38. }
  39. ImageEditor::~ImageEditor()
  40. {
  41. m_image->remove_client(*this);
  42. }
  43. void ImageEditor::did_complete_action()
  44. {
  45. m_undo_stack.push(make<ImageUndoCommand>(*m_image));
  46. }
  47. bool ImageEditor::undo()
  48. {
  49. if (!m_undo_stack.can_undo())
  50. return false;
  51. /* Without this you need to undo twice to actually start undoing stuff.
  52. * This is due to the fact that the top of the UndoStack contains the snapshot of the currently
  53. * shown image but what we actually want to restore is the snapshot right below it.
  54. * Doing "undo->undo->redo" restores the 2nd topmost snapshot on the stack while lowering the
  55. * stack pointer only by 1. This is important because we want the UndoStack's pointer to always point
  56. * at the currently shown snapshot, otherwise doing 'undo->undo->draw something else' would delete
  57. * one of the snapshots.
  58. * This works because UndoStack::undo first decrements the stack pointer and then restores the snapshot,
  59. * while UndoStack::redo first restores the snapshot and then increments the stack pointer.
  60. */
  61. m_undo_stack.undo();
  62. m_undo_stack.undo();
  63. m_undo_stack.redo();
  64. layers_did_change();
  65. return true;
  66. }
  67. bool ImageEditor::redo()
  68. {
  69. if (!m_undo_stack.can_redo())
  70. return false;
  71. m_undo_stack.redo();
  72. layers_did_change();
  73. return true;
  74. }
  75. void ImageEditor::set_title(String title)
  76. {
  77. m_title = move(title);
  78. if (on_title_change)
  79. on_title_change(m_title);
  80. }
  81. void ImageEditor::set_path(String path)
  82. {
  83. m_path = move(path);
  84. set_title(LexicalPath::basename(m_path));
  85. }
  86. void ImageEditor::paint_event(GUI::PaintEvent& event)
  87. {
  88. GUI::Frame::paint_event(event);
  89. GUI::Painter painter(*this);
  90. painter.add_clip_rect(event.rect());
  91. painter.add_clip_rect(frame_inner_rect());
  92. {
  93. Gfx::DisjointRectSet background_rects;
  94. background_rects.add(frame_inner_rect());
  95. background_rects.shatter(content_rect());
  96. for (auto& rect : background_rects.rects())
  97. painter.fill_rect(rect, palette().color(Gfx::ColorRole::Tray));
  98. }
  99. Gfx::StylePainter::paint_transparency_grid(painter, content_rect(), palette());
  100. painter.draw_rect(content_rect().inflated(2, 2), Color::Black);
  101. m_image->paint_into(painter, content_rect());
  102. if (m_active_layer && m_show_active_layer_boundary) {
  103. painter.draw_rect(enclosing_int_rect(content_to_frame_rect(m_active_layer->relative_rect())).inflated(2, 2), Color::Black);
  104. }
  105. if (m_show_pixel_grid && scale() > m_pixel_grid_threshold) {
  106. auto event_image_rect = enclosing_int_rect(frame_to_content_rect(event.rect())).inflated(1, 1);
  107. auto image_rect = m_image->rect().inflated(1, 1).intersected(event_image_rect);
  108. for (auto i = image_rect.left(); i < image_rect.right(); i++) {
  109. auto start_point = content_to_frame_position({ i, image_rect.top() }).to_type<int>();
  110. auto end_point = content_to_frame_position({ i, image_rect.bottom() }).to_type<int>();
  111. painter.draw_line(start_point, end_point, Color::LightGray);
  112. }
  113. for (auto i = image_rect.top(); i < image_rect.bottom(); i++) {
  114. auto start_point = content_to_frame_position({ image_rect.left(), i }).to_type<int>();
  115. auto end_point = content_to_frame_position({ image_rect.right(), i }).to_type<int>();
  116. painter.draw_line(start_point, end_point, Color::LightGray);
  117. }
  118. }
  119. if (m_show_guides) {
  120. for (auto& guide : m_guides) {
  121. if (guide.orientation() == Guide::Orientation::Horizontal) {
  122. int y_coordinate = (int)content_to_frame_position({ 0.0f, guide.offset() }).y();
  123. painter.draw_line({ 0, y_coordinate }, { rect().width(), y_coordinate }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  124. } else if (guide.orientation() == Guide::Orientation::Vertical) {
  125. int x_coordinate = (int)content_to_frame_position({ guide.offset(), 0.0f }).x();
  126. painter.draw_line({ x_coordinate, 0 }, { x_coordinate, rect().height() }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  127. }
  128. }
  129. }
  130. if (!m_selection.is_empty())
  131. m_selection.paint(painter);
  132. if (m_show_rulers) {
  133. const auto ruler_bg_color = palette().color(Gfx::ColorRole::InactiveSelection);
  134. const auto ruler_fg_color = palette().color(Gfx::ColorRole::Ruler);
  135. const auto ruler_text_color = palette().color(Gfx::ColorRole::InactiveSelectionText);
  136. const auto mouse_indicator_color = Color::White;
  137. // Ruler background
  138. painter.fill_rect({ { 0, 0 }, { m_ruler_thickness, rect().height() } }, ruler_bg_color);
  139. painter.fill_rect({ { 0, 0 }, { rect().width(), m_ruler_thickness } }, ruler_bg_color);
  140. const auto ruler_step = calculate_ruler_step_size();
  141. const auto editor_origin_to_image = frame_to_content_position({ 0, 0 });
  142. const auto editor_max_to_image = frame_to_content_position({ width(), height() });
  143. // Horizontal ruler
  144. painter.draw_line({ 0, m_ruler_thickness }, { rect().width(), m_ruler_thickness }, ruler_fg_color);
  145. const auto x_start = floor(editor_origin_to_image.x()) - ((int)floor(editor_origin_to_image.x()) % ruler_step) - ruler_step;
  146. for (int x = x_start; x < editor_max_to_image.x(); x += ruler_step) {
  147. const int num_sub_divisions = min(ruler_step, 10);
  148. for (int x_sub = 0; x_sub < num_sub_divisions; ++x_sub) {
  149. const int x_pos = x + (int)(ruler_step * x_sub / num_sub_divisions);
  150. const int editor_x_sub = content_to_frame_position({ x_pos, 0 }).x();
  151. const int line_length = (x_sub % 2 == 0) ? m_ruler_thickness / 3 : m_ruler_thickness / 6;
  152. painter.draw_line({ editor_x_sub, m_ruler_thickness - line_length }, { editor_x_sub, m_ruler_thickness }, ruler_fg_color);
  153. }
  154. const int editor_x = content_to_frame_position({ x, 0 }).x();
  155. painter.draw_line({ editor_x, 0 }, { editor_x, m_ruler_thickness }, ruler_fg_color);
  156. painter.draw_text({ { editor_x + 2, 0 }, { m_ruler_thickness, m_ruler_thickness - 2 } }, String::formatted("{}", x), painter.font(), Gfx::TextAlignment::CenterLeft, ruler_text_color);
  157. }
  158. // Vertical ruler
  159. painter.draw_line({ m_ruler_thickness, 0 }, { m_ruler_thickness, rect().height() }, ruler_fg_color);
  160. const auto y_start = floor(editor_origin_to_image.y()) - ((int)floor(editor_origin_to_image.y()) % ruler_step) - ruler_step;
  161. for (int y = y_start; y < editor_max_to_image.y(); y += ruler_step) {
  162. const int num_sub_divisions = min(ruler_step, 10);
  163. for (int y_sub = 0; y_sub < num_sub_divisions; ++y_sub) {
  164. const int y_pos = y + (int)(ruler_step * y_sub / num_sub_divisions);
  165. const int editor_y_sub = content_to_frame_position({ 0, y_pos }).y();
  166. const int line_length = (y_sub % 2 == 0) ? m_ruler_thickness / 3 : m_ruler_thickness / 6;
  167. painter.draw_line({ m_ruler_thickness - line_length, editor_y_sub }, { m_ruler_thickness, editor_y_sub }, ruler_fg_color);
  168. }
  169. const int editor_y = content_to_frame_position({ 0, y }).y();
  170. painter.draw_line({ 0, editor_y }, { m_ruler_thickness, editor_y }, ruler_fg_color);
  171. painter.draw_text({ { 0, editor_y - m_ruler_thickness }, { m_ruler_thickness, m_ruler_thickness } }, String::formatted("{}", y), painter.font(), Gfx::TextAlignment::BottomRight, ruler_text_color);
  172. }
  173. // Mouse position indicator
  174. const Gfx::IntPoint indicator_x({ m_mouse_position.x(), m_ruler_thickness });
  175. const Gfx::IntPoint indicator_y({ m_ruler_thickness, m_mouse_position.y() });
  176. painter.draw_triangle(indicator_x, indicator_x + Gfx::IntPoint(-m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), indicator_x + Gfx::IntPoint(m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), mouse_indicator_color);
  177. painter.draw_triangle(indicator_y, indicator_y + Gfx::IntPoint(-m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), indicator_y + Gfx::IntPoint(-m_mouse_indicator_triangle_size, m_mouse_indicator_triangle_size), mouse_indicator_color);
  178. // Top left square
  179. painter.fill_rect({ { 0, 0 }, { m_ruler_thickness, m_ruler_thickness } }, ruler_bg_color);
  180. }
  181. }
  182. int ImageEditor::calculate_ruler_step_size() const
  183. {
  184. const auto step_target = 80 / scale();
  185. const auto max_factor = 5;
  186. for (int factor = 0; factor < max_factor; ++factor) {
  187. if (step_target <= 1 * (float)pow(10, factor))
  188. return 1 * pow(10, factor);
  189. if (step_target <= 2 * (float)pow(10, factor))
  190. return 2 * pow(10, factor);
  191. if (step_target <= 5 * (float)pow(10, factor))
  192. return 5 * pow(10, factor);
  193. }
  194. return 1 * pow(10, max_factor);
  195. }
  196. Gfx::IntRect ImageEditor::mouse_indicator_rect_x() const
  197. {
  198. const Gfx::IntPoint top_left({ m_ruler_thickness, m_ruler_thickness - m_mouse_indicator_triangle_size });
  199. const Gfx::IntSize size({ width() + 1, m_mouse_indicator_triangle_size + 1 });
  200. return Gfx::IntRect(top_left, size);
  201. }
  202. Gfx::IntRect ImageEditor::mouse_indicator_rect_y() const
  203. {
  204. const Gfx::IntPoint top_left({ m_ruler_thickness - m_mouse_indicator_triangle_size, m_ruler_thickness });
  205. const Gfx::IntSize size({ m_mouse_indicator_triangle_size + 1, height() + 1 });
  206. return Gfx::IntRect(top_left, size);
  207. }
  208. void ImageEditor::second_paint_event(GUI::PaintEvent& event)
  209. {
  210. if (m_active_tool)
  211. m_active_tool->on_second_paint(m_active_layer, event);
  212. }
  213. GUI::MouseEvent ImageEditor::event_with_pan_and_scale_applied(GUI::MouseEvent const& event) const
  214. {
  215. auto image_position = frame_to_content_position(event.position());
  216. return {
  217. static_cast<GUI::Event::Type>(event.type()),
  218. Gfx::IntPoint(image_position.x(), image_position.y()),
  219. event.buttons(),
  220. event.button(),
  221. event.modifiers(),
  222. event.wheel_delta_x(),
  223. event.wheel_delta_y(),
  224. };
  225. }
  226. GUI::MouseEvent ImageEditor::event_adjusted_for_layer(GUI::MouseEvent const& event, Layer const& layer) const
  227. {
  228. auto image_position = frame_to_content_position(event.position());
  229. image_position.translate_by(-layer.location().x(), -layer.location().y());
  230. return {
  231. static_cast<GUI::Event::Type>(event.type()),
  232. Gfx::IntPoint(image_position.x(), image_position.y()),
  233. event.buttons(),
  234. event.button(),
  235. event.modifiers(),
  236. event.wheel_delta_x(),
  237. event.wheel_delta_y(),
  238. };
  239. }
  240. void ImageEditor::mousedown_event(GUI::MouseEvent& event)
  241. {
  242. if (event.button() == GUI::MouseButton::Middle) {
  243. start_panning(event.position());
  244. set_override_cursor(Gfx::StandardCursor::Drag);
  245. return;
  246. }
  247. if (!m_active_tool)
  248. return;
  249. if (is<MoveTool>(*m_active_tool)) {
  250. if (auto* other_layer = layer_at_editor_position(event.position())) {
  251. set_active_layer(other_layer);
  252. }
  253. }
  254. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  255. auto image_event = event_with_pan_and_scale_applied(event);
  256. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  257. m_active_tool->on_mousedown(m_active_layer.ptr(), tool_event);
  258. }
  259. void ImageEditor::mousemove_event(GUI::MouseEvent& event)
  260. {
  261. m_mouse_position = event.position();
  262. if (m_show_rulers) {
  263. update(mouse_indicator_rect_x());
  264. update(mouse_indicator_rect_y());
  265. }
  266. if (is_panning()) {
  267. GUI::AbstractZoomPanWidget::mousemove_event(event);
  268. return;
  269. }
  270. auto image_event = event_with_pan_and_scale_applied(event);
  271. if (on_image_mouse_position_change) {
  272. on_image_mouse_position_change(image_event.position());
  273. }
  274. if (!m_active_tool)
  275. return;
  276. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  277. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  278. m_active_tool->on_mousemove(m_active_layer.ptr(), tool_event);
  279. }
  280. void ImageEditor::mouseup_event(GUI::MouseEvent& event)
  281. {
  282. set_override_cursor(m_active_cursor);
  283. if (event.button() == GUI::MouseButton::Middle) {
  284. stop_panning();
  285. return;
  286. }
  287. if (!m_active_tool)
  288. return;
  289. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  290. auto image_event = event_with_pan_and_scale_applied(event);
  291. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  292. m_active_tool->on_mouseup(m_active_layer.ptr(), tool_event);
  293. }
  294. void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
  295. {
  296. if (!m_active_tool)
  297. return;
  298. m_active_tool->on_context_menu(m_active_layer, event);
  299. }
  300. void ImageEditor::keydown_event(GUI::KeyEvent& event)
  301. {
  302. if (event.key() == Key_Delete && !selection().is_empty() && active_layer()) {
  303. active_layer()->erase_selection(selection());
  304. return;
  305. }
  306. if (m_active_tool)
  307. m_active_tool->on_keydown(event);
  308. }
  309. void ImageEditor::keyup_event(GUI::KeyEvent& event)
  310. {
  311. if (m_active_tool)
  312. m_active_tool->on_keyup(event);
  313. }
  314. void ImageEditor::enter_event(Core::Event&)
  315. {
  316. set_override_cursor(m_active_cursor);
  317. }
  318. void ImageEditor::leave_event(Core::Event&)
  319. {
  320. set_override_cursor(Gfx::StandardCursor::None);
  321. if (on_leave)
  322. on_leave();
  323. }
  324. void ImageEditor::set_active_layer(Layer* layer)
  325. {
  326. if (m_active_layer == layer)
  327. return;
  328. m_active_layer = layer;
  329. if (m_active_layer) {
  330. VERIFY(&m_active_layer->image() == m_image.ptr());
  331. size_t index = 0;
  332. for (; index < m_image->layer_count(); ++index) {
  333. if (&m_image->layer(index) == layer)
  334. break;
  335. }
  336. if (on_active_layer_change)
  337. on_active_layer_change(layer);
  338. } else {
  339. if (on_active_layer_change)
  340. on_active_layer_change({});
  341. }
  342. }
  343. void ImageEditor::set_active_tool(Tool* tool)
  344. {
  345. if (m_active_tool == tool)
  346. return;
  347. if (m_active_tool)
  348. m_active_tool->clear();
  349. m_active_tool = tool;
  350. if (m_active_tool) {
  351. m_active_tool->setup(*this);
  352. m_active_tool->on_tool_activation();
  353. m_active_cursor = m_active_tool->cursor();
  354. set_override_cursor(m_active_cursor);
  355. }
  356. }
  357. void ImageEditor::update_tool_cursor()
  358. {
  359. if (m_active_tool) {
  360. m_active_cursor = m_active_tool->cursor();
  361. set_override_cursor(m_active_cursor);
  362. }
  363. }
  364. void ImageEditor::set_guide_visibility(bool show_guides)
  365. {
  366. if (m_show_guides == show_guides)
  367. return;
  368. m_show_guides = show_guides;
  369. if (on_set_guide_visibility)
  370. on_set_guide_visibility(m_show_guides);
  371. update();
  372. }
  373. void ImageEditor::set_ruler_visibility(bool show_rulers)
  374. {
  375. if (m_show_rulers == show_rulers)
  376. return;
  377. m_show_rulers = show_rulers;
  378. if (on_set_ruler_visibility)
  379. on_set_ruler_visibility(m_show_rulers);
  380. update();
  381. }
  382. void ImageEditor::set_pixel_grid_visibility(bool show_pixel_grid)
  383. {
  384. if (m_show_pixel_grid == show_pixel_grid)
  385. return;
  386. m_show_pixel_grid = show_pixel_grid;
  387. update();
  388. }
  389. void ImageEditor::layers_did_change()
  390. {
  391. update();
  392. }
  393. Color ImageEditor::color_for(GUI::MouseButton button) const
  394. {
  395. if (button == GUI::MouseButton::Primary)
  396. return m_primary_color;
  397. if (button == GUI::MouseButton::Secondary)
  398. return m_secondary_color;
  399. VERIFY_NOT_REACHED();
  400. }
  401. Color ImageEditor::color_for(GUI::MouseEvent const& event) const
  402. {
  403. if (event.buttons() & GUI::MouseButton::Primary)
  404. return m_primary_color;
  405. if (event.buttons() & GUI::MouseButton::Secondary)
  406. return m_secondary_color;
  407. VERIFY_NOT_REACHED();
  408. }
  409. void ImageEditor::set_primary_color(Color color)
  410. {
  411. if (m_primary_color == color)
  412. return;
  413. m_primary_color = color;
  414. if (on_primary_color_change)
  415. on_primary_color_change(color);
  416. }
  417. void ImageEditor::set_secondary_color(Color color)
  418. {
  419. if (m_secondary_color == color)
  420. return;
  421. m_secondary_color = color;
  422. if (on_secondary_color_change)
  423. on_secondary_color_change(color);
  424. }
  425. Layer* ImageEditor::layer_at_editor_position(Gfx::IntPoint const& editor_position)
  426. {
  427. auto image_position = frame_to_content_position(editor_position);
  428. for (ssize_t i = m_image->layer_count() - 1; i >= 0; --i) {
  429. auto& layer = m_image->layer(i);
  430. if (!layer.is_visible())
  431. continue;
  432. if (layer.relative_rect().contains(Gfx::IntPoint(image_position.x(), image_position.y())))
  433. return const_cast<Layer*>(&layer);
  434. }
  435. return nullptr;
  436. }
  437. void ImageEditor::fit_image_to_view(FitType type)
  438. {
  439. auto viewport_rect = rect();
  440. if (m_show_rulers) {
  441. viewport_rect = {
  442. viewport_rect.x() + m_ruler_thickness,
  443. viewport_rect.y() + m_ruler_thickness,
  444. viewport_rect.width() - m_ruler_thickness,
  445. viewport_rect.height() - m_ruler_thickness
  446. };
  447. }
  448. const float border_ratio = 0.95f;
  449. auto image_size = image().size();
  450. auto height_ratio = floorf(border_ratio * viewport_rect.height()) / (float)image_size.height();
  451. auto width_ratio = floorf(border_ratio * viewport_rect.width()) / (float)image_size.width();
  452. float new_scale = 1.0f;
  453. switch (type) {
  454. case FitType::Width:
  455. new_scale = width_ratio;
  456. break;
  457. case FitType::Height:
  458. new_scale = height_ratio;
  459. break;
  460. case FitType::Image:
  461. new_scale = min(height_ratio, width_ratio);
  462. break;
  463. }
  464. float offset = m_show_rulers ? -m_ruler_thickness / (scale() * 2.0f) : 0.0f;
  465. set_origin(Gfx::FloatPoint(offset, offset));
  466. set_scale(new_scale);
  467. }
  468. void ImageEditor::image_did_change(Gfx::IntRect const& modified_image_rect)
  469. {
  470. update(content_rect().intersected(enclosing_int_rect(content_to_frame_rect(modified_image_rect))));
  471. }
  472. void ImageEditor::image_did_change_rect(Gfx::IntRect const& new_image_rect)
  473. {
  474. set_original_rect(new_image_rect);
  475. set_content_rect(new_image_rect);
  476. }
  477. void ImageEditor::image_select_layer(Layer* layer)
  478. {
  479. set_active_layer(layer);
  480. }
  481. bool ImageEditor::request_close()
  482. {
  483. if (!undo_stack().is_current_modified())
  484. return true;
  485. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), path(), undo_stack().last_unmodified_timestamp());
  486. if (result == GUI::MessageBox::ExecYes) {
  487. save_project();
  488. return true;
  489. }
  490. if (result == GUI::MessageBox::ExecNo)
  491. return true;
  492. return false;
  493. }
  494. void ImageEditor::save_project()
  495. {
  496. if (path().is_empty() || m_loaded_from_image) {
  497. save_project_as();
  498. return;
  499. }
  500. auto response = FileSystemAccessClient::Client::the().try_request_file(window(), path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
  501. if (response.is_error())
  502. return;
  503. auto result = save_project_to_file(*response.value());
  504. if (result.is_error()) {
  505. GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", path(), result.error()));
  506. return;
  507. }
  508. undo_stack().set_current_unmodified();
  509. }
  510. void ImageEditor::save_project_as()
  511. {
  512. auto response = FileSystemAccessClient::Client::the().try_save_file(window(), "untitled", "pp");
  513. if (response.is_error())
  514. return;
  515. auto file = response.value();
  516. auto result = save_project_to_file(*file);
  517. if (result.is_error()) {
  518. GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", file->filename(), result.error()));
  519. return;
  520. }
  521. set_path(file->filename());
  522. set_loaded_from_image(false);
  523. undo_stack().set_current_unmodified();
  524. }
  525. Result<void, String> ImageEditor::save_project_to_file(Core::File& file) const
  526. {
  527. StringBuilder builder;
  528. JsonObjectSerializer json(builder);
  529. m_image->serialize_as_json(json);
  530. auto json_guides = json.add_array("guides");
  531. for (const auto& guide : m_guides) {
  532. auto json_guide = json_guides.add_object();
  533. json_guide.add("offset"sv, (double)guide.offset());
  534. if (guide.orientation() == Guide::Orientation::Vertical)
  535. json_guide.add("orientation", "vertical");
  536. else if (guide.orientation() == Guide::Orientation::Horizontal)
  537. json_guide.add("orientation", "horizontal");
  538. json_guide.finish();
  539. }
  540. json_guides.finish();
  541. json.finish();
  542. if (!file.write(builder.string_view()))
  543. return String { file.error_string() };
  544. return {};
  545. }
  546. void ImageEditor::set_show_active_layer_boundary(bool show)
  547. {
  548. if (m_show_active_layer_boundary == show)
  549. return;
  550. m_show_active_layer_boundary = show;
  551. update();
  552. }
  553. void ImageEditor::set_loaded_from_image(bool loaded_from_image)
  554. {
  555. m_loaded_from_image = loaded_from_image;
  556. }
  557. }