CreateNewImageDialog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
  3. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "CreateNewImageDialog.h"
  8. #include <AK/Array.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/CheckBox.h>
  13. #include <LibGUI/ColorInput.h>
  14. #include <LibGUI/ComboBox.h>
  15. #include <LibGUI/ItemListModel.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/SpinBox.h>
  18. #include <LibGUI/TextBox.h>
  19. namespace PixelPaint {
  20. CreateNewImageDialog::CreateNewImageDialog(GUI::Window* parent_window)
  21. : Dialog(parent_window)
  22. {
  23. set_title("Create new image");
  24. set_icon(parent_window->icon());
  25. resize(200, 220);
  26. auto main_widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
  27. main_widget->set_fill_with_background_color(true);
  28. main_widget->set_layout<GUI::VerticalBoxLayout>(4);
  29. auto& name_label = main_widget->add<GUI::Label>("Name:");
  30. name_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  31. m_name_textbox = main_widget->add<GUI::TextBox>();
  32. m_name_textbox->on_change = [this] {
  33. m_image_name = m_name_textbox->text();
  34. };
  35. auto default_name = Config::read_string("PixelPaint"sv, "NewImage"sv, "Name"sv);
  36. m_name_textbox->set_text(default_name);
  37. auto& width_label = main_widget->add<GUI::Label>("Width:");
  38. width_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  39. auto& width_spinbox = main_widget->add<GUI::SpinBox>();
  40. auto& height_label = main_widget->add<GUI::Label>("Height:");
  41. height_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  42. auto& height_spinbox = main_widget->add<GUI::SpinBox>();
  43. enum class BackgroundIndex {
  44. Transparent = 0,
  45. White,
  46. Black,
  47. Custom
  48. };
  49. static constexpr AK::Array suggested_backgrounds = {
  50. "Transparent"sv,
  51. "White"sv,
  52. "Black"sv,
  53. "Custom"sv
  54. };
  55. m_background_color = Color::from_string(
  56. Config::read_string("PixelPaint"sv, "NewImage"sv, "Background"sv))
  57. .value_or(Color::Transparent);
  58. auto selected_background_index = [&] {
  59. if (m_background_color == Gfx::Color::Transparent)
  60. return BackgroundIndex::Transparent;
  61. if (m_background_color == Gfx::Color::White)
  62. return BackgroundIndex::White;
  63. if (m_background_color == Gfx::Color::Black)
  64. return BackgroundIndex::Black;
  65. return BackgroundIndex::Custom;
  66. }();
  67. auto& background_label = main_widget->add<GUI::Label>("Background:");
  68. background_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  69. auto& background_color_combo = main_widget->add<GUI::ComboBox>();
  70. auto& background_color_input = main_widget->add<GUI::ColorInput>();
  71. background_color_input.set_visible(false);
  72. background_color_combo.set_only_allow_values_from_model(true);
  73. background_color_combo.set_model(*GUI::ItemListModel<StringView, decltype(suggested_backgrounds)>::create(suggested_backgrounds));
  74. background_color_combo.on_change = [&](auto&, const GUI::ModelIndex& index) {
  75. auto background_index = static_cast<BackgroundIndex>(index.row());
  76. m_background_color = [&]() -> Gfx::Color {
  77. switch (background_index) {
  78. case BackgroundIndex::Transparent:
  79. return Gfx::Color::Transparent;
  80. case BackgroundIndex::White:
  81. return Gfx::Color::White;
  82. case BackgroundIndex::Black:
  83. return Gfx::Color::Black;
  84. default:
  85. return m_background_color;
  86. }
  87. }();
  88. background_color_input.set_color(m_background_color);
  89. background_color_input.set_visible(background_index == BackgroundIndex::Custom);
  90. };
  91. background_color_combo.set_selected_index(to_underlying(selected_background_index));
  92. background_color_input.on_change = [&] {
  93. m_background_color = background_color_input.color();
  94. };
  95. auto& set_defaults_checkbox = main_widget->add<GUI::CheckBox>();
  96. set_defaults_checkbox.set_text(String::from_utf8("Use these settings as default"sv).release_value_but_fixme_should_propagate_errors());
  97. auto& button_container = main_widget->add<GUI::Widget>();
  98. button_container.set_layout<GUI::HorizontalBoxLayout>();
  99. auto& ok_button = button_container.add<GUI::Button>(String::from_utf8_short_string("OK"sv));
  100. ok_button.on_click = [&](auto) {
  101. if (set_defaults_checkbox.is_checked()) {
  102. Config::write_string("PixelPaint"sv, "NewImage"sv, "Name"sv, m_image_name);
  103. Config::write_i32("PixelPaint"sv, "NewImage"sv, "Width"sv, m_image_size.width());
  104. Config::write_i32("PixelPaint"sv, "NewImage"sv, "Height"sv, m_image_size.height());
  105. Config::write_string("PixelPaint"sv, "NewImage"sv, "Background"sv, m_background_color.to_deprecated_string());
  106. }
  107. done(ExecResult::OK);
  108. };
  109. ok_button.set_default(true);
  110. auto& cancel_button = button_container.add<GUI::Button>(String::from_utf8_short_string("Cancel"sv));
  111. cancel_button.on_click = [this](auto) {
  112. done(ExecResult::Cancel);
  113. };
  114. width_spinbox.on_change = [this](int value) {
  115. m_image_size.set_width(value);
  116. };
  117. height_spinbox.on_change = [this](int value) {
  118. m_image_size.set_height(value);
  119. };
  120. width_spinbox.set_range(1, 16384);
  121. height_spinbox.set_range(1, 16384);
  122. auto default_width = Config::read_i32("PixelPaint"sv, "NewImage"sv, "Width"sv, 510);
  123. auto default_height = Config::read_i32("PixelPaint"sv, "NewImage"sv, "Height"sv, 356);
  124. width_spinbox.set_value(default_width);
  125. height_spinbox.set_value(default_height);
  126. }
  127. }