knob.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from typing import Optional
  2. from ..events import Handler, ValueChangeEventArguments
  3. from .label import Label
  4. from .mixins.color_elements import TextColorElement
  5. from .mixins.disableable_element import DisableableElement
  6. from .mixins.value_element import ValueElement
  7. class Knob(ValueElement, DisableableElement, TextColorElement):
  8. def __init__(self,
  9. value: float = 0.0,
  10. *,
  11. min: float = 0.0, # pylint: disable=redefined-builtin
  12. max: float = 1.0, # pylint: disable=redefined-builtin
  13. step: float = 0.01,
  14. color: Optional[str] = 'primary',
  15. center_color: Optional[str] = None,
  16. track_color: Optional[str] = None,
  17. size: Optional[str] = None,
  18. show_value: bool = False,
  19. on_change: Optional[Handler[ValueChangeEventArguments]] = None,
  20. ) -> None:
  21. """Knob
  22. This element is based on Quasar's `QKnob <https://quasar.dev/vue-components/knob>`_ component.
  23. The element is used to take a number input from the user through mouse/touch panning.
  24. :param value: the initial value (default: 0.0)
  25. :param min: the minimum value (default: 0.0)
  26. :param max: the maximum value (default: 1.0)
  27. :param step: the step size (default: 0.01)
  28. :param color: knob color (either a Quasar, Tailwind, or CSS color or `None`, default: "primary")
  29. :param center_color: color name for the center part of the component, examples: primary, teal-10
  30. :param track_color: color name for the track of the component, examples: primary, teal-10
  31. :param size: size in CSS units, including unit name or standard size name (xs|sm|md|lg|xl), examples: 16px, 2rem
  32. :param show_value: whether to show the value as text
  33. :param on_change: callback to execute when the value changes
  34. """
  35. super().__init__(tag='q-knob', value=value, on_value_change=on_change, throttle=0.05, text_color=color)
  36. self._props['min'] = min
  37. self._props['max'] = max
  38. self._props['step'] = step
  39. self._props['show-value'] = True # NOTE: enable default slot, e.g. for nested icon
  40. if center_color:
  41. self._props['center-color'] = center_color
  42. if track_color:
  43. self._props['track-color'] = track_color
  44. if size:
  45. self._props['size'] = size
  46. self.label: Optional[Label] = None
  47. if show_value:
  48. with self:
  49. self.label = Label().bind_text_from(self, 'value')