LayerPropertiesWidget.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "LayerPropertiesWidget.h"
  8. #include "Layer.h"
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/CheckBox.h>
  11. #include <LibGUI/GroupBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/OpacitySlider.h>
  14. #include <LibGUI/TextBox.h>
  15. #include <LibGfx/Font/Font.h>
  16. REGISTER_WIDGET(PixelPaint, LayerPropertiesWidget);
  17. namespace PixelPaint {
  18. LayerPropertiesWidget::LayerPropertiesWidget()
  19. {
  20. set_layout<GUI::VerticalBoxLayout>();
  21. auto& group_box = add<GUI::GroupBox>("Layer properties"sv);
  22. group_box.set_layout<GUI::VerticalBoxLayout>(8);
  23. auto& name_container = group_box.add<GUI::Widget>();
  24. name_container.set_fixed_height(20);
  25. name_container.set_layout<GUI::HorizontalBoxLayout>();
  26. auto& name_label = name_container.add<GUI::Label>("Name:");
  27. name_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  28. name_label.set_fixed_size(80, 20);
  29. m_name_textbox = name_container.add<GUI::TextBox>();
  30. m_name_textbox->set_fixed_height(20);
  31. m_name_textbox->on_change = [this] {
  32. if (m_layer)
  33. m_layer->set_name(m_name_textbox->text());
  34. };
  35. auto& opacity_container = group_box.add<GUI::Widget>();
  36. opacity_container.set_fixed_height(20);
  37. opacity_container.set_layout<GUI::HorizontalBoxLayout>();
  38. auto& opacity_label = opacity_container.add<GUI::Label>("Opacity:");
  39. opacity_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  40. opacity_label.set_fixed_size(80, 20);
  41. m_opacity_slider = opacity_container.add<GUI::HorizontalOpacitySlider>();
  42. m_opacity_slider->set_range(0, 100);
  43. m_opacity_slider->on_change = [this](int value) {
  44. if (m_layer)
  45. m_layer->set_opacity_percent(value);
  46. };
  47. m_visibility_checkbox = group_box.add<GUI::CheckBox>(String::from_utf8_short_string("Visible"sv));
  48. m_visibility_checkbox->set_fixed_height(20);
  49. m_visibility_checkbox->on_checked = [this](bool checked) {
  50. if (m_layer)
  51. m_layer->set_visible(checked);
  52. };
  53. }
  54. void LayerPropertiesWidget::set_layer(Layer* layer)
  55. {
  56. if (m_layer == layer)
  57. return;
  58. if (layer) {
  59. m_layer = layer;
  60. m_name_textbox->set_text(layer->name());
  61. m_opacity_slider->set_value(layer->opacity_percent());
  62. m_visibility_checkbox->set_checked(layer->is_visible());
  63. set_enabled(true);
  64. } else {
  65. m_layer = nullptr;
  66. set_enabled(false);
  67. }
  68. }
  69. }