LayerPropertiesWidget.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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");
  22. auto& layout = group_box.set_layout<GUI::VerticalBoxLayout>();
  23. layout.set_margins({ 8 });
  24. auto& name_container = group_box.add<GUI::Widget>();
  25. name_container.set_fixed_height(20);
  26. name_container.set_layout<GUI::HorizontalBoxLayout>();
  27. auto& name_label = name_container.add<GUI::Label>("Name:");
  28. name_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  29. name_label.set_fixed_size(80, 20);
  30. m_name_textbox = name_container.add<GUI::TextBox>();
  31. m_name_textbox->set_fixed_height(20);
  32. m_name_textbox->on_change = [this] {
  33. if (m_layer)
  34. m_layer->set_name(m_name_textbox->text());
  35. };
  36. auto& opacity_container = group_box.add<GUI::Widget>();
  37. opacity_container.set_fixed_height(20);
  38. opacity_container.set_layout<GUI::HorizontalBoxLayout>();
  39. auto& opacity_label = opacity_container.add<GUI::Label>("Opacity:");
  40. opacity_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  41. opacity_label.set_fixed_size(80, 20);
  42. m_opacity_slider = opacity_container.add<GUI::OpacitySlider>();
  43. m_opacity_slider->set_range(0, 100);
  44. m_opacity_slider->on_change = [this](int value) {
  45. if (m_layer)
  46. m_layer->set_opacity_percent(value);
  47. };
  48. m_visibility_checkbox = group_box.add<GUI::CheckBox>("Visible");
  49. m_visibility_checkbox->set_fixed_height(20);
  50. m_visibility_checkbox->on_checked = [this](bool checked) {
  51. if (m_layer)
  52. m_layer->set_visible(checked);
  53. };
  54. }
  55. void LayerPropertiesWidget::set_layer(Layer* layer)
  56. {
  57. if (m_layer == layer)
  58. return;
  59. if (layer) {
  60. m_layer = layer;
  61. m_name_textbox->set_text(layer->name());
  62. m_opacity_slider->set_value(layer->opacity_percent());
  63. m_visibility_checkbox->set_checked(layer->is_visible());
  64. set_enabled(true);
  65. } else {
  66. m_layer = nullptr;
  67. set_enabled(false);
  68. }
  69. }
  70. }