OptionUI.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return FCColorEntry()
  77. class HeadingOptionUI(OptionUI):
  78. def __init__(self, label_text: str, label_tooltip: Union[str, None]):
  79. super().__init__(option="__heading")
  80. self.label_text = label_text
  81. self.label_tooltip = label_tooltip
  82. def build_heading_widget(self):
  83. heading = QtWidgets.QLabel('<b>%s</b>' % _(self.label_text))
  84. heading.setToolTip(_(self.label_tooltip))
  85. return heading
  86. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  87. grid.addWidget(self.build_heading_widget(), row, 0, 1, 2)
  88. return 1
  89. def get_field(self):
  90. return None
  91. class SeparatorOptionUI(OptionUI):
  92. def __init__(self):
  93. super().__init__(option="__separator")
  94. def build_separator_widget(self):
  95. separator = QtWidgets.QFrame()
  96. separator.setFrameShape(QtWidgets.QFrame.HLine)
  97. separator.setFrameShadow(QtWidgets.QFrame.Sunken)
  98. return separator
  99. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  100. grid.addWidget(self.build_separator_widget(), row, 0, 1, 2)
  101. return 1
  102. def get_field(self):
  103. return None
  104. class FullWidthButtonOptionUI(OptionUI):
  105. def __init__(self, option: str, label_text: str, label_tooltip: str):
  106. super().__init__(option=option)
  107. self.label_text = label_text
  108. self.label_tooltip = label_tooltip
  109. self.button_widget = self.build_button_widget()
  110. def build_button_widget(self):
  111. button = FCButton(_(self.label_text))
  112. button.setToolTip(_(self.label_tooltip))
  113. return button
  114. def add_to_grid(self, grid: QtWidgets.QGridLayout, row: int) -> int:
  115. grid.addWidget(self.button_widget, row, 0, 1, 3)
  116. return 1
  117. def get_field(self):
  118. return self.button_widget