OptionUI.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_red: bool = False):
  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_red = label_red
  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_red:
  37. fmt = "<span style=\"color:red;\">%s</span>" % 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 RadioSetOptionUI(BasicOptionUI):
  51. def __init__(self, option: str, label_text: str, choices: list, orientation='horizontal', **kwargs):
  52. self.choices = choices
  53. self.orientation = orientation
  54. super().__init__(option=option, label_text=label_text, **kwargs)
  55. def build_entry_widget(self) -> QtWidgets.QWidget:
  56. return RadioSet(choices=self.choices, orientation=self.orientation)
  57. class CheckboxOptionUI(OptionUI):
  58. def __init__(self, option: str, label_text: str, label_tooltip: str):
  59. super().__init__(option=option)
  60. self.label_text = label_text
  61. self.label_tooltip = label_tooltip
  62. self.checkbox_widget = self.build_checkbox_widget()
  63. def build_checkbox_widget(self):
  64. checkbox = FCCheckBox('%s' % _(self.label_text))
  65. checkbox.setToolTip(_(self.label_tooltip))
  66. return checkbox
  67. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  68. grid.addWidget(self.checkbox_widget, row, 0, 1, 3)
  69. return 1
  70. def get_field(self):
  71. return self.checkbox_widget
  72. class ComboboxOptionUI(BasicOptionUI):
  73. def __init__(self, option: str, label_text: str, choices: Sequence, **kwargs):
  74. self.choices = choices
  75. super().__init__(option=option, label_text=label_text, **kwargs)
  76. def build_entry_widget(self):
  77. combo = FCComboBox()
  78. for choice in self.choices:
  79. # don't translate the QCombo items as they are used in QSettings and identified by name
  80. combo.addItem(choice)
  81. return combo
  82. class ColorOptionUI(BasicOptionUI):
  83. def build_entry_widget(self) -> QtWidgets.QWidget:
  84. entry = FCColorEntry()
  85. return entry
  86. class SliderWithSpinnerOptionUI(BasicOptionUI):
  87. def __init__(self, option: str, label_text: str, min_value=0, max_value=100, step=1, **kwargs):
  88. self.min_value = min_value
  89. self.max_value = max_value
  90. self.step = step
  91. super().__init__(option=option, label_text=label_text, **kwargs)
  92. def build_entry_widget(self) -> QtWidgets.QWidget:
  93. entry = FCSliderWithSpinner(min=self.min_value, max=self.max_value, step=self.step)
  94. return entry
  95. class ColorAlphaSliderOptionUI(SliderWithSpinnerOptionUI):
  96. def __init__(self, applies_to: List[str], group, label_text: str, **kwargs):
  97. self.applies_to = applies_to
  98. self.group = group
  99. super().__init__(option="__color_alpha_slider", label_text=label_text, min_value=0, max_value=255, step=1, **kwargs)
  100. self.get_field().valueChanged.connect(self._on_alpha_change)
  101. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  102. for index, field in enumerate(self._get_target_fields()):
  103. field.entry.textChanged.connect(lambda value, i=index: self._on_target_change(target_index=i))
  104. return super().add_to_grid(grid, row)
  105. def _get_target_fields(self):
  106. return list(map(lambda n: self.group.option_dict()[n].get_field(), self.applies_to))
  107. def _on_target_change(self, target_index: int):
  108. field = self._get_target_fields()[target_index]
  109. color = field.get_value()
  110. alpha_part = color[7:]
  111. if len(alpha_part) != 2:
  112. return
  113. alpha = int(alpha_part, 16)
  114. if alpha < 0 or alpha > 255 or self.get_field().get_value() == alpha:
  115. return
  116. self.get_field().set_value(alpha)
  117. def _on_alpha_change(self):
  118. alpha = self.get_field().get_value()
  119. for field in self._get_target_fields():
  120. old_value = field.get_value()
  121. new_value = self._modify_color_alpha(old_value, alpha=alpha)
  122. field.set_value(new_value)
  123. def _modify_color_alpha(self, color: str, alpha: int):
  124. color_without_alpha = color[:7]
  125. if alpha > 255:
  126. return color_without_alpha + "FF"
  127. elif alpha < 0:
  128. return color_without_alpha + "00"
  129. else:
  130. hexalpha = hex(alpha)[2:]
  131. if len(hexalpha) == 1:
  132. hexalpha = "0" + hexalpha
  133. return color_without_alpha + hexalpha
  134. class SpinnerOptionUI(BasicOptionUI):
  135. def __init__(self, option: str, label_text: str, min_value: int, max_value: int, step: int = 1, **kwargs):
  136. self.min_value = min_value
  137. self.max_value = max_value
  138. self.step = step
  139. super().__init__(option=option, label_text=label_text, **kwargs)
  140. def build_entry_widget(self) -> QtWidgets.QWidget:
  141. entry = FCSpinner()
  142. entry.set_range(self.min_value, self.max_value)
  143. entry.set_step(self.step)
  144. entry.setWrapping(True)
  145. return entry
  146. class DoubleSpinnerOptionUI(BasicOptionUI):
  147. def __init__(self, option: str, label_text: str, step: float, decimals: int, min_value=None, max_value=None, **kwargs):
  148. self.min_value = min_value
  149. self.max_value = max_value
  150. self.step = step
  151. self.decimals = decimals
  152. super().__init__(option=option, label_text=label_text, **kwargs)
  153. def build_entry_widget(self) -> QtWidgets.QWidget:
  154. entry = FCDoubleSpinner()
  155. entry.set_precision(self.decimals)
  156. entry.setSingleStep(self.step)
  157. if self.min_value is None:
  158. self.min_value = entry.minimum()
  159. else:
  160. entry.setMinimum(self.min_value)
  161. if self.max_value is None:
  162. self.max_value = entry.maximum()
  163. else:
  164. entry.setMaximum(self.max_value)
  165. return entry
  166. class HeadingOptionUI(OptionUI):
  167. def __init__(self, label_text: str, label_tooltip: Union[str, None] = None):
  168. super().__init__(option="__heading")
  169. self.label_text = label_text
  170. self.label_tooltip = label_tooltip
  171. def build_heading_widget(self):
  172. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  173. heading.setToolTip(_(self.label_tooltip))
  174. return heading
  175. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  176. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  177. return 1
  178. def get_field(self):
  179. return None
  180. class SeparatorOptionUI(OptionUI):
  181. def __init__(self):
  182. super().__init__(option="__separator")
  183. def build_separator_widget(self):
  184. separator = QtWidgets.QFrame()
  185. separator.setFrameShape(QtWidgets.QFrame.HLine)
  186. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  187. return separator
  188. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  189. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  190. return 1
  191. def get_field(self):
  192. return None
  193. class FullWidthButtonOptionUI(OptionUI):
  194. def __init__(self, option: str, label_text: str, label_tooltip: str):
  195. super().__init__(option=option)
  196. self.label_text = label_text
  197. self.label_tooltip = label_tooltip
  198. self.button_widget = self.build_button_widget()
  199. def build_button_widget(self):
  200. button = FCButton(_(self.label_text))
  201. button.setToolTip(_(self.label_tooltip))
  202. return button
  203. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  204. grid.addWidget(self.button_widget, row, 0, 1, 3)
  205. return 1
  206. def get_field(self):
  207. return self.button_widget