OptionUI.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from typing import Union
  2. from PyQt5 import QtWidgets
  3. from flatcamGUI.GUIElements import RadioSet, FCCheckBox, FCButton, FCComboBox, FCEntry, FCSpinner, FCColorEntry
  4. import gettext
  5. import FlatCAMTranslation as fcTranslate
  6. import builtins
  7. fcTranslate.apply_language('strings')
  8. if '_' not in builtins.__dict__:
  9. _ = gettext.gettext
  10. class OptionUI:
  11. def __init__(self, option: str):
  12. self.option = option
  13. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  14. """
  15. Adds the necessary widget to the grid, starting at the supplied row.
  16. Returns the number of rows used (normally 1)
  17. """
  18. raise NotImplementedError()
  19. def get_field(self):
  20. raise NotImplementedError()
  21. class BasicOptionUI(OptionUI):
  22. """Abstract OptionUI that has a label on the left then some other widget on the right"""
  23. def __init__(self, option: str, label_text: str, label_tooltip: str):
  24. super().__init__(option=option)
  25. self.label_text = label_text
  26. self.label_tooltip = label_tooltip
  27. self.label_widget = self.build_label_widget()
  28. self.entry_widget = self.build_entry_widget()
  29. def build_label_widget(self) -> QtWidgets.QLabel:
  30. label_widget = QtWidgets.QLabel('%s:' % _(self.label_text))
  31. if self.label_tooltip is not None:
  32. label_widget.setToolTip(_(self.label_tooltip))
  33. return label_widget
  34. def build_entry_widget(self) -> QtWidgets.QWidget:
  35. raise NotImplementedError()
  36. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  37. grid.addWidget(self.label_widget, row, 0)
  38. grid.addWidget(self.entry_widget, row, 1)
  39. return 1
  40. def get_field(self):
  41. return self.entry_widget
  42. class RadioSetOptionUI(BasicOptionUI):
  43. def __init__(self, option: str, label_text: str, label_tooltip: str, choices: list, orientation='horizontal'):
  44. self.choices = choices
  45. self.orientation = orientation
  46. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  47. def build_entry_widget(self) -> QtWidgets.QWidget:
  48. return RadioSet(choices=self.choices, orientation=self.orientation)
  49. class CheckboxOptionUI(OptionUI):
  50. def __init__(self, option: str, label_text: str, label_tooltip: str):
  51. super().__init__(option=option)
  52. self.label_text = label_text
  53. self.label_tooltip = label_tooltip
  54. self.checkbox_widget = self.build_checkbox_widget()
  55. def build_checkbox_widget(self):
  56. checkbox = FCCheckBox('%s' % _(self.label_text))
  57. checkbox.setToolTip(_(self.label_tooltip))
  58. return checkbox
  59. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  60. grid.addWidget(self.checkbox_widget, row, 0, 1, 3)
  61. return 1
  62. def get_field(self):
  63. return self.checkbox_widget
  64. class ComboboxOptionUI(BasicOptionUI):
  65. def __init__(self, option: str, label_text: str, label_tooltip: str, choices: list):
  66. self.choices = choices
  67. super().__init__(option=option, label_text=label_text, label_tooltip=label_tooltip)
  68. def build_entry_widget(self):
  69. combo = FCComboBox()
  70. for choice in self.choices:
  71. # don't translate the QCombo items as they are used in QSettings and identified by name
  72. combo.addItem(choice)
  73. return combo
  74. class ColorOptionUI(BasicOptionUI):
  75. def build_entry_widget(self) -> QtWidgets.QWidget:
  76. entry = FCColorEntry()
  77. return entry
  78. class HeadingOptionUI(OptionUI):
  79. def __init__(self, label_text: str, label_tooltip: Union[str, None]):
  80. super().__init__(option="__heading")
  81. self.label_text = label_text
  82. self.label_tooltip = label_tooltip
  83. def build_heading_widget(self):
  84. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  85. heading.setToolTip(_(self.label_tooltip))
  86. return heading
  87. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  88. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  89. return 1
  90. def get_field(self):
  91. return None
  92. class SeparatorOptionUI(OptionUI):
  93. def __init__(self):
  94. super().__init__(option="__separator")
  95. def build_separator_widget(self):
  96. separator = QtWidgets.QFrame()
  97. separator.setFrameShape(QtWidgets.QFrame.HLine)
  98. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  99. return separator
  100. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  101. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  102. return 1
  103. def get_field(self):
  104. return None
  105. class FullWidthButtonOptionUI(OptionUI):
  106. def __init__(self, option: str, label_text: str, label_tooltip: str):
  107. super().__init__(option=option)
  108. self.label_text = label_text
  109. self.label_tooltip = label_tooltip
  110. self.button_widget = self.build_button_widget()
  111. def build_button_widget(self):
  112. button = FCButton(_(self.label_text))
  113. button.setToolTip(_(self.label_tooltip))
  114. return button
  115. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  116. grid.addWidget(self.button_widget, row, 0, 1, 3)
  117. return 1
  118. def get_field(self):
  119. return self.button_widget