GColorPicker.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/GBoxLayout.h>
  27. #include <LibGUI/GButton.h>
  28. #include <LibGUI/GColorPicker.h>
  29. #include <LibGUI/GFrame.h>
  30. #include <LibGUI/GSpinBox.h>
  31. #include <LibGUI/GWidget.h>
  32. GColorPicker::GColorPicker(Color color, CObject* parent)
  33. : GDialog(parent)
  34. , m_color(color)
  35. {
  36. set_title("Edit Color");
  37. build();
  38. }
  39. GColorPicker::~GColorPicker()
  40. {
  41. }
  42. void GColorPicker::build()
  43. {
  44. auto horizontal_container = GWidget::construct();
  45. horizontal_container->set_fill_with_background_color(true);
  46. horizontal_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  47. horizontal_container->layout()->set_margins({ 4, 4, 4, 4 });
  48. set_main_widget(horizontal_container);
  49. auto left_vertical_container = GWidget::construct(horizontal_container.ptr());
  50. left_vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  51. auto right_vertical_container = GWidget::construct(horizontal_container.ptr());
  52. right_vertical_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  53. enum RGBComponent {
  54. Red,
  55. Green,
  56. Blue
  57. };
  58. m_preview_widget = GFrame::construct(right_vertical_container);
  59. auto pal = m_preview_widget->palette();
  60. pal.set_color(ColorRole::Background, m_color);
  61. m_preview_widget->set_fill_with_background_color(true);
  62. m_preview_widget->set_palette(pal);
  63. right_vertical_container->layout()->add_spacer();
  64. auto cancel_button = GButton::construct("Cancel", right_vertical_container);
  65. cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  66. cancel_button->set_preferred_size(0, 20);
  67. cancel_button->on_click = [&](auto&) {
  68. done(GDialog::ExecCancel);
  69. };
  70. auto ok_button = GButton::construct("Okay", right_vertical_container);
  71. ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  72. ok_button->set_preferred_size(0, 20);
  73. ok_button->on_click = [&](auto&) {
  74. done(GDialog::ExecOK);
  75. };
  76. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  77. auto spinbox = GSpinBox::construct(left_vertical_container);
  78. spinbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  79. spinbox->set_preferred_size(0, 20);
  80. spinbox->set_min(0);
  81. spinbox->set_max(255);
  82. spinbox->set_value(initial_value);
  83. spinbox->on_change = [this, component](auto value) {
  84. if (component == Red)
  85. m_color.set_red(value);
  86. if (component == Green)
  87. m_color.set_green(value);
  88. if (component == Blue)
  89. m_color.set_blue(value);
  90. auto pal = m_preview_widget->palette();
  91. pal.set_color(ColorRole::Background, m_color);
  92. m_preview_widget->set_palette(pal);
  93. m_preview_widget->update();
  94. };
  95. return spinbox;
  96. };
  97. make_spinbox(Red, m_color.red());
  98. make_spinbox(Green, m_color.green());
  99. make_spinbox(Blue, m_color.blue());
  100. }