OptionUI.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from typing import Union
  2. from PyQt5 import QtWidgets
  3. from flatcamGUI.GUIElements import RadioSet, FCCheckBox, FCButton, FCComboBox, FCEntry, FCSpinner, FCColorEntry, \
  4. FCSliderWithSpinner
  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=0, max=100, step=1):
  81. self.min = min
  82. self.max = max
  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, max=self.max, step=self.step)
  87. return entry
  88. class HeadingOptionUI(OptionUI):
  89. def __init__(self, label_text: str, label_tooltip: Union[str, None]):
  90. super().__init__(option="__heading")
  91. self.label_text = label_text
  92. self.label_tooltip = label_tooltip
  93. def build_heading_widget(self):
  94. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  95. heading.setToolTip(_(self.label_tooltip))
  96. return heading
  97. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  98. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  99. return 1
  100. def get_field(self):
  101. return None
  102. class SeparatorOptionUI(OptionUI):
  103. def __init__(self):
  104. super().__init__(option="__separator")
  105. def build_separator_widget(self):
  106. separator = QtWidgets.QFrame()
  107. separator.setFrameShape(QtWidgets.QFrame.HLine)
  108. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  109. return separator
  110. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  111. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  112. return 1
  113. def get_field(self):
  114. return None
  115. class FullWidthButtonOptionUI(OptionUI):
  116. def __init__(self, option: str, label_text: str, label_tooltip: str):
  117. super().__init__(option=option)
  118. self.label_text = label_text
  119. self.label_tooltip = label_tooltip
  120. self.button_widget = self.build_button_widget()
  121. def build_button_widget(self):
  122. button = FCButton(_(self.label_text))
  123. button.setToolTip(_(self.label_tooltip))
  124. return button
  125. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  126. grid.addWidget(self.button_widget, row, 0, 1, 3)
  127. return 1
  128. def get_field(self):
  129. return self.button_widget