OptionUI.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from typing import Union, Sequence, List
  2. from PyQt5 import QtWidgets
  3. from flatcamGUI.GUIElements import RadioSet, FCCheckBox, FCButton, FCComboBox, FCEntry, FCSpinner, FCColorEntry, \
  4. FCSliderWithSpinner, FCDoubleSpinner
  5. import gettext
  6. import FlatCAMTranslation as fcTranslate
  7. import builtins
  8. fcTranslate.apply_language('strings')
  9. if '_' not in builtins.__dict__:
  10. _ = gettext.gettext
  11. class OptionUI:
  12. def __init__(self, option: str):
  13. self.option = option
  14. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  15. """
  16. Adds the necessary widget to the grid, starting at the supplied row.
  17. Returns the number of rows used (normally 1)
  18. """
  19. raise NotImplementedError()
  20. def get_field(self):
  21. raise NotImplementedError()
  22. class BasicOptionUI(OptionUI):
  23. """Abstract OptionUI that has a label on the left then some other widget on the right"""
  24. def __init__(self, option: str, label_text: str, label_tooltip: Union[str, None] = None, label_bold: bool = False, label_color: Union[str, None] = None):
  25. super().__init__(option=option)
  26. self.label_text = label_text
  27. self.label_tooltip = label_tooltip
  28. self.label_bold = label_bold
  29. self.label_color = label_color
  30. self.label_widget = self.build_label_widget()
  31. self.entry_widget = self.build_entry_widget()
  32. def build_label_widget(self) -> QtWidgets.QLabel:
  33. fmt = "%s:"
  34. if self.label_bold:
  35. fmt = "<b>%s</b>" % fmt
  36. if self.label_color:
  37. fmt = "<span style=\"color:%s;\">%s</span>" % (self.label_color, fmt)
  38. label_widget = QtWidgets.QLabel(fmt % _(self.label_text))
  39. if self.label_tooltip is not None:
  40. label_widget.setToolTip(_(self.label_tooltip))
  41. return label_widget
  42. def build_entry_widget(self) -> QtWidgets.QWidget:
  43. raise NotImplementedError()
  44. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  45. grid.addWidget(self.label_widget, row, 0)
  46. grid.addWidget(self.entry_widget, row, 1)
  47. return 1
  48. def get_field(self):
  49. return self.entry_widget
  50. class LineEntryOptionUI(BasicOptionUI):
  51. def __init__(self, option: str, label_text: str, **kwargs):
  52. super().__init__(option=option, label_text=label_text, **kwargs)
  53. def build_entry_widget(self) -> QtWidgets.QWidget:
  54. return FCEntry()
  55. class RadioSetOptionUI(BasicOptionUI):
  56. def __init__(self, option: str, label_text: str, choices: list, orientation='horizontal', **kwargs):
  57. self.choices = choices
  58. self.orientation = orientation
  59. super().__init__(option=option, label_text=label_text, **kwargs)
  60. def build_entry_widget(self) -> QtWidgets.QWidget:
  61. return RadioSet(choices=self.choices, orientation=self.orientation)
  62. class CheckboxOptionUI(OptionUI):
  63. def __init__(self, option: str, label_text: str, label_tooltip: str):
  64. super().__init__(option=option)
  65. self.label_text = label_text
  66. self.label_tooltip = label_tooltip
  67. self.checkbox_widget = self.build_checkbox_widget()
  68. def build_checkbox_widget(self):
  69. checkbox = FCCheckBox('%s' % _(self.label_text))
  70. checkbox.setToolTip(_(self.label_tooltip))
  71. return checkbox
  72. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  73. grid.addWidget(self.checkbox_widget, row, 0, 1, 3)
  74. return 1
  75. def get_field(self):
  76. return self.checkbox_widget
  77. class ComboboxOptionUI(BasicOptionUI):
  78. def __init__(self, option: str, label_text: str, choices: Sequence, **kwargs):
  79. self.choices = choices
  80. super().__init__(option=option, label_text=label_text, **kwargs)
  81. def build_entry_widget(self):
  82. combo = FCComboBox()
  83. for choice in self.choices:
  84. # don't translate the QCombo items as they are used in QSettings and identified by name
  85. combo.addItem(choice)
  86. return combo
  87. class ColorOptionUI(BasicOptionUI):
  88. def build_entry_widget(self) -> QtWidgets.QWidget:
  89. entry = FCColorEntry()
  90. return entry
  91. class SliderWithSpinnerOptionUI(BasicOptionUI):
  92. def __init__(self, option: str, label_text: str, min_value=0, max_value=100, step=1, **kwargs):
  93. self.min_value = min_value
  94. self.max_value = max_value
  95. self.step = step
  96. super().__init__(option=option, label_text=label_text, **kwargs)
  97. def build_entry_widget(self) -> QtWidgets.QWidget:
  98. entry = FCSliderWithSpinner(min=self.min_value, max=self.max_value, step=self.step)
  99. return entry
  100. class ColorAlphaSliderOptionUI(SliderWithSpinnerOptionUI):
  101. def __init__(self, applies_to: List[str], group, label_text: str, **kwargs):
  102. self.applies_to = applies_to
  103. self.group = group
  104. super().__init__(option="__color_alpha_slider", label_text=label_text, min_value=0, max_value=255, step=1, **kwargs)
  105. self.get_field().valueChanged.connect(self._on_alpha_change)
  106. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  107. for index, field in enumerate(self._get_target_fields()):
  108. field.entry.textChanged.connect(lambda value, i=index: self._on_target_change(target_index=i))
  109. return super().add_to_grid(grid, row)
  110. def _get_target_fields(self):
  111. return list(map(lambda n: self.group.option_dict()[n].get_field(), self.applies_to))
  112. def _on_target_change(self, target_index: int):
  113. field = self._get_target_fields()[target_index]
  114. color = field.get_value()
  115. alpha_part = color[7:]
  116. if len(alpha_part) != 2:
  117. return
  118. alpha = int(alpha_part, 16)
  119. if alpha < 0 or alpha > 255 or self.get_field().get_value() == alpha:
  120. return
  121. self.get_field().set_value(alpha)
  122. def _on_alpha_change(self):
  123. alpha = self.get_field().get_value()
  124. for field in self._get_target_fields():
  125. old_value = field.get_value()
  126. new_value = self._modify_color_alpha(old_value, alpha=alpha)
  127. field.set_value(new_value)
  128. def _modify_color_alpha(self, color: str, alpha: int):
  129. color_without_alpha = color[:7]
  130. if alpha > 255:
  131. return color_without_alpha + "FF"
  132. elif alpha < 0:
  133. return color_without_alpha + "00"
  134. else:
  135. hexalpha = hex(alpha)[2:]
  136. if len(hexalpha) == 1:
  137. hexalpha = "0" + hexalpha
  138. return color_without_alpha + hexalpha
  139. class SpinnerOptionUI(BasicOptionUI):
  140. def __init__(self, option: str, label_text: str, min_value: int, max_value: int, step: int = 1, **kwargs):
  141. self.min_value = min_value
  142. self.max_value = max_value
  143. self.step = step
  144. super().__init__(option=option, label_text=label_text, **kwargs)
  145. def build_entry_widget(self) -> QtWidgets.QWidget:
  146. entry = FCSpinner()
  147. entry.set_range(self.min_value, self.max_value)
  148. entry.set_step(self.step)
  149. entry.setWrapping(True)
  150. return entry
  151. class DoubleSpinnerOptionUI(BasicOptionUI):
  152. def __init__(self, option: str, label_text: str, step: float, decimals: int, min_value=None, max_value=None, suffix=None, **kwargs):
  153. self.min_value = min_value
  154. self.max_value = max_value
  155. self.step = step
  156. self.suffix = suffix
  157. self.decimals = decimals
  158. super().__init__(option=option, label_text=label_text, **kwargs)
  159. def build_entry_widget(self) -> QtWidgets.QWidget:
  160. entry = FCDoubleSpinner(suffix=self.suffix)
  161. entry.set_precision(self.decimals)
  162. entry.setSingleStep(self.step)
  163. if self.min_value is None:
  164. self.min_value = entry.minimum()
  165. else:
  166. entry.setMinimum(self.min_value)
  167. if self.max_value is None:
  168. self.max_value = entry.maximum()
  169. else:
  170. entry.setMaximum(self.max_value)
  171. return entry
  172. class HeadingOptionUI(OptionUI):
  173. def __init__(self, label_text: str, label_tooltip: Union[str, None] = None):
  174. super().__init__(option="__heading")
  175. self.label_text = label_text
  176. self.label_tooltip = label_tooltip
  177. def build_heading_widget(self):
  178. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  179. heading.setToolTip(_(self.label_tooltip))
  180. return heading
  181. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  182. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  183. return 1
  184. def get_field(self):
  185. return None
  186. class SeparatorOptionUI(OptionUI):
  187. def __init__(self):
  188. super().__init__(option="__separator")
  189. def build_separator_widget(self):
  190. separator = QtWidgets.QFrame()
  191. separator.setFrameShape(QtWidgets.QFrame.HLine)
  192. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  193. return separator
  194. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  195. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  196. return 1
  197. def get_field(self):
  198. return None
  199. class FullWidthButtonOptionUI(OptionUI):
  200. def __init__(self, option: str, label_text: str, label_tooltip: str):
  201. super().__init__(option=option)
  202. self.label_text = label_text
  203. self.label_tooltip = label_tooltip
  204. self.button_widget = self.build_button_widget()
  205. def build_button_widget(self):
  206. button = FCButton(_(self.label_text))
  207. button.setToolTip(_(self.label_tooltip))
  208. return button
  209. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  210. grid.addWidget(self.button_widget, row, 0, 1, 3)
  211. return 1
  212. def get_field(self):
  213. return self.button_widget