AbstractSlider.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGUI/Widget.h>
  8. namespace GUI {
  9. class AbstractSlider : public Widget {
  10. C_OBJECT_ABSTRACT(AbstractSlider);
  11. public:
  12. virtual ~AbstractSlider() override;
  13. void set_orientation(Orientation value);
  14. Orientation orientation() const { return m_orientation; }
  15. int value() const { return m_value; }
  16. int min() const { return m_min; }
  17. int max() const { return m_max; }
  18. int step() const { return m_step; }
  19. int page_step() const { return m_page_step; }
  20. bool jump_to_cursor() const { return m_jump_to_cursor; }
  21. bool is_min() const { return m_value == m_min; }
  22. bool is_max() const { return m_value == m_max; }
  23. void set_range(int min, int max);
  24. virtual void set_value(int, AllowCallback = AllowCallback::Yes);
  25. void set_min(int min) { set_range(min, max()); }
  26. void set_max(int max) { set_range(min(), max); }
  27. void set_step(int step) { m_step = step; }
  28. void set_page_step(int page_step);
  29. void set_jump_to_cursor(bool b) { m_jump_to_cursor = b; }
  30. virtual void increase_slider_by(int delta) { set_value(value() + delta); }
  31. virtual void decrease_slider_by(int delta) { set_value(value() - delta); }
  32. virtual void increase_slider_by_page_steps(int page_steps) { set_value(value() + page_step() * page_steps); }
  33. virtual void decrease_slider_by_page_steps(int page_steps) { set_value(value() - page_step() * page_steps); }
  34. virtual void increase_slider_by_steps(int steps) { set_value(value() + step() * steps); }
  35. virtual void decrease_slider_by_steps(int steps) { set_value(value() - step() * steps); }
  36. Function<void(int)> on_change;
  37. protected:
  38. explicit AbstractSlider(Orientation = Orientation::Vertical);
  39. private:
  40. void set_knob_hovered(bool);
  41. int m_value { 0 };
  42. int m_min { 0 };
  43. int m_max { 0 };
  44. int m_step { 1 };
  45. int m_page_step { 10 };
  46. bool m_jump_to_cursor { false };
  47. Orientation m_orientation { Orientation::Horizontal };
  48. };
  49. }