OptionUI.py 5.2 KB

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