ResizeImageDialog.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2022, Andrew Smith <andrew@alsmith.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ResizeImageDialog.h"
  7. #include <Applications/PixelPaint/ResizeImageDialogGML.h>
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/CheckBox.h>
  11. #include <LibGUI/ComboBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/RadioButton.h>
  14. #include <LibGUI/SpinBox.h>
  15. namespace PixelPaint {
  16. ResizeImageDialog::ResizeImageDialog(Gfx::IntSize const& suggested_size, GUI::Window* parent_window)
  17. : Dialog(parent_window)
  18. {
  19. m_desired_size.set_width(max(1, suggested_size.width()));
  20. m_desired_size.set_height(max(1, suggested_size.height()));
  21. m_starting_aspect_ratio = m_desired_size.width() / static_cast<float>(m_desired_size.height());
  22. set_title("Resize Image");
  23. resize(260, 228);
  24. set_icon(parent_window->icon());
  25. auto& main_widget = set_main_widget<GUI::Widget>();
  26. if (!main_widget.load_from_gml(resize_image_dialog_gml))
  27. VERIFY_NOT_REACHED();
  28. auto width_spinbox = main_widget.find_descendant_of_type_named<GUI::SpinBox>("width_spinbox");
  29. auto height_spinbox = main_widget.find_descendant_of_type_named<GUI::SpinBox>("height_spinbox");
  30. auto keep_aspect_ratio_checkbox = main_widget.find_descendant_of_type_named<GUI::CheckBox>("keep_aspect_ratio_checkbox");
  31. VERIFY(width_spinbox);
  32. VERIFY(height_spinbox);
  33. VERIFY(keep_aspect_ratio_checkbox);
  34. width_spinbox->set_value(m_desired_size.width());
  35. width_spinbox->on_change = [this, height_spinbox, keep_aspect_ratio_checkbox](int value) {
  36. if (keep_aspect_ratio_checkbox->is_checked()) {
  37. int desired_height = static_cast<int>(roundf(value / m_starting_aspect_ratio));
  38. height_spinbox->set_value(desired_height, GUI::AllowCallback::No);
  39. m_desired_size.set_height(height_spinbox->value());
  40. }
  41. m_desired_size.set_width(value);
  42. };
  43. height_spinbox->set_value(m_desired_size.height());
  44. height_spinbox->on_change = [this, width_spinbox, keep_aspect_ratio_checkbox](int value) {
  45. if (keep_aspect_ratio_checkbox->is_checked()) {
  46. int desired_width = static_cast<int>(roundf(value * m_starting_aspect_ratio));
  47. width_spinbox->set_value(desired_width, GUI::AllowCallback::No);
  48. m_desired_size.set_width(width_spinbox->value());
  49. }
  50. m_desired_size.set_height(value);
  51. };
  52. keep_aspect_ratio_checkbox->on_checked = [this, height_spinbox](bool is_checked) {
  53. if (is_checked) {
  54. int desired_height = static_cast<int>(roundf(m_desired_size.width() / m_starting_aspect_ratio));
  55. height_spinbox->set_value(desired_height, GUI::AllowCallback::No);
  56. m_desired_size.set_height(height_spinbox->value());
  57. }
  58. };
  59. auto nearest_neighbor_radio = main_widget.find_descendant_of_type_named<GUI::RadioButton>("nearest_neighbor_radio");
  60. auto smooth_pixels_radio = main_widget.find_descendant_of_type_named<GUI::RadioButton>("smooth_pixels_radio");
  61. auto bilinear_radio = main_widget.find_descendant_of_type_named<GUI::RadioButton>("bilinear_radio");
  62. auto resize_canvas_radio = main_widget.find_descendant_of_type_named<GUI::RadioButton>("resize_canvas");
  63. VERIFY(nearest_neighbor_radio);
  64. VERIFY(smooth_pixels_radio);
  65. VERIFY(bilinear_radio);
  66. VERIFY(resize_canvas_radio);
  67. m_scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor;
  68. if (bilinear_radio->is_checked()) {
  69. m_scaling_mode = Gfx::Painter::ScalingMode::BilinearBlend;
  70. }
  71. nearest_neighbor_radio->on_checked = [this](bool is_checked) {
  72. if (is_checked)
  73. m_scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor;
  74. };
  75. smooth_pixels_radio->on_checked = [this](bool is_checked) {
  76. if (is_checked)
  77. m_scaling_mode = Gfx::Painter::ScalingMode::SmoothPixels;
  78. };
  79. bilinear_radio->on_checked = [this](bool is_checked) {
  80. if (is_checked)
  81. m_scaling_mode = Gfx::Painter::ScalingMode::BilinearBlend;
  82. };
  83. resize_canvas_radio->on_checked = [this](bool is_checked) {
  84. if (is_checked)
  85. m_scaling_mode = Gfx::Painter::ScalingMode::None;
  86. };
  87. auto ok_button = main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  88. auto cancel_button = main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  89. VERIFY(ok_button);
  90. VERIFY(cancel_button);
  91. ok_button->on_click = [this](auto) {
  92. done(ExecResult::OK);
  93. };
  94. ok_button->set_default(true);
  95. cancel_button->on_click = [this](auto) {
  96. done(ExecResult::Cancel);
  97. };
  98. }
  99. }