OptionUI.py 11 KB

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