OptionUI.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from typing import Union
  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: str):
  25. super().__init__(option=option)
  26. self.label_text = label_text
  27. self.label_tooltip = label_tooltip
  28. self.label_widget = self.build_label_widget()
  29. self.entry_widget = self.build_entry_widget()
  30. def build_label_widget(self) -> QtWidgets.QLabel:
  31. label_widget = QtWidgets.QLabel('%s:' % _(self.label_text))
  32. if self.label_tooltip is not None:
  33. label_widget.setToolTip(_(self.label_tooltip))
  34. return label_widget
  35. def build_entry_widget(self) -> QtWidgets.QWidget:
  36. raise NotImplementedError()
  37. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  38. grid.addWidget(self.label_widget, row, 0)
  39. grid.addWidget(self.entry_widget, row, 1)
  40. return 1
  41. def get_field(self):
  42. return self.entry_widget
  43. class RadioSetOptionUI(BasicOptionUI):
  44. def __init__(self, option: str, label_text: str, label_tooltip: str, choices: list, orientation='horizontal'):
  45. self.choices = choices
  46. self.orientation = orientation
  47. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  48. def build_entry_widget(self) -> QtWidgets.QWidget:
  49. return RadioSet(choices=self.choices, orientation=self.orientation)
  50. class CheckboxOptionUI(OptionUI):
  51. def __init__(self, option: str, label_text: str, label_tooltip: str):
  52. super().__init__(option=option)
  53. self.label_text = label_text
  54. self.label_tooltip = label_tooltip
  55. self.checkbox_widget = self.build_checkbox_widget()
  56. def build_checkbox_widget(self):
  57. checkbox = FCCheckBox('%s' % _(self.label_text))
  58. checkbox.setToolTip(_(self.label_tooltip))
  59. return checkbox
  60. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  61. grid.addWidget(self.checkbox_widget, row, 0, 1, 3)
  62. return 1
  63. def get_field(self):
  64. return self.checkbox_widget
  65. class ComboboxOptionUI(BasicOptionUI):
  66. def __init__(self, option: str, label_text: str, label_tooltip: str, choices: list):
  67. self.choices = choices
  68. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  69. def build_entry_widget(self):
  70. combo = FCComboBox()
  71. for choice in self.choices:
  72. # don't translate the QCombo items as they are used in QSettings and identified by name
  73. combo.addItem(choice)
  74. return combo
  75. class ColorOptionUI(BasicOptionUI):
  76. def build_entry_widget(self) -> QtWidgets.QWidget:
  77. entry = FCColorEntry()
  78. return entry
  79. class SliderWithSpinnerOptionUI(BasicOptionUI):
  80. def __init__(self, option: str, label_text: str, label_tooltip: str, min_value=0, max_value=100, step=1):
  81. self.min_value = min_value
  82. self.max_value = max_value
  83. self.step = step
  84. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  85. def build_entry_widget(self) -> QtWidgets.QWidget:
  86. entry = FCSliderWithSpinner(min=self.min_value, max=self.max_value, step=self.step)
  87. return entry
  88. class DoubleSpinnerOptionUI(BasicOptionUI):
  89. def __init__(self, option: str, label_text: str, label_tooltip: str, step: float, decimals: int, min_value=None, max_value=None):
  90. self.min_value = min_value
  91. self.max_value = max_value
  92. self.step = step
  93. self.decimals = decimals
  94. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  95. def build_entry_widget(self) -> QtWidgets.QWidget:
  96. entry = FCDoubleSpinner()
  97. entry.set_precision(self.decimals)
  98. entry.setSingleStep(self.step)
  99. if self.min_value is None:
  100. self.min_value = entry.minimum()
  101. else:
  102. entry.setMinimum(self.min_value)
  103. if self.max_value is None:
  104. self.max_value = entry.maximum()
  105. else:
  106. entry.setMaximum(self.max_value)
  107. return entry
  108. class HeadingOptionUI(OptionUI):
  109. def __init__(self, label_text: str, label_tooltip: Union[str, None]):
  110. super().__init__(option="__heading")
  111. self.label_text = label_text
  112. self.label_tooltip = label_tooltip
  113. def build_heading_widget(self):
  114. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  115. heading.setToolTip(_(self.label_tooltip))
  116. return heading
  117. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  118. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  119. return 1
  120. def get_field(self):
  121. return None
  122. class SeparatorOptionUI(OptionUI):
  123. def __init__(self):
  124. super().__init__(option="__separator")
  125. def build_separator_widget(self):
  126. separator = QtWidgets.QFrame()
  127. separator.setFrameShape(QtWidgets.QFrame.HLine)
  128. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  129. return separator
  130. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  131. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  132. return 1
  133. def get_field(self):
  134. return None
  135. class FullWidthButtonOptionUI(OptionUI):
  136. def __init__(self, option: str, label_text: str, label_tooltip: str):
  137. super().__init__(option=option)
  138. self.label_text = label_text
  139. self.label_tooltip = label_tooltip
  140. self.button_widget = self.build_button_widget()
  141. def build_button_widget(self):
  142. button = FCButton(_(self.label_text))
  143. button.setToolTip(_(self.label_tooltip))
  144. return button
  145. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  146. grid.addWidget(self.button_widget, row, 0, 1, 3)
  147. return 1
  148. def get_field(self):
  149. return self.button_widget