GeneralGUIPrefGroupUI.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. from PyQt5 import QtWidgets, QtCore
  2. from PyQt5.QtCore import QSettings, Qt
  3. from flatcamGUI.preferences.OptionsGroupUI import OptionsGroupUI2
  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. from flatcamGUI.preferences.OptionUI import OptionUI, CheckboxOptionUI, RadioSetOptionUI, \
  11. SeparatorOptionUI, HeadingOptionUI, ComboboxOptionUI, ColorOptionUI, FullWidthButtonOptionUI, \
  12. SliderWithSpinnerOptionUI
  13. class GeneralGUIPrefGroupUI(OptionsGroupUI2):
  14. def __init__(self, decimals=4, **kwargs):
  15. self.decimals = decimals
  16. super().__init__(**kwargs)
  17. self.setTitle(str(_("GUI Preferences")))
  18. self.layout_field = self.option_dict()["layout"].get_field()
  19. self.layout_field.activated.connect(self.on_layout)
  20. self.theme_field = self.option_dict()["global_theme"].get_field()
  21. self.theme_apply_button = self.option_dict()["__button_apply_theme"].get_field()
  22. self.theme_apply_button.clicked.connect(self.on_theme_change)
  23. self.style_field = self.option_dict()["style"].get_field()
  24. current_style_index = self.style_field.findText(QtWidgets.qApp.style().objectName(), QtCore.Qt.MatchFixedString)
  25. self.style_field.setCurrentIndex(current_style_index)
  26. self.style_field.activated[str].connect(self.handle_style)
  27. self.hdpi_field = self.option_dict()["hdpi"].get_field()
  28. qsettings = QSettings("Open Source", "FlatCAM")
  29. if qsettings.contains("hdpi"):
  30. self.hdpi_field.set_value(qsettings.value('hdpi', type=int))
  31. else:
  32. self.hdpi_field.set_value(False)
  33. self.hdpi_field.stateChanged.connect(self.handle_hdpi)
  34. self.sel_line_field = self.option_dict()["global_sel_line"].get_field()
  35. self.sel_fill_field = self.option_dict()["global_sel_fill"].get_field()
  36. self.sel_alpha_field = self.option_dict()["_global_sel_alpha"].get_field()
  37. self.sel_alpha_field.spinner.valueChanged.connect(self.on_sel_alpha_change)
  38. self.alt_sel_line_field = self.option_dict()["global_alt_sel_line"].get_field()
  39. self.alt_sel_fill_field = self.option_dict()["global_alt_sel_fill"].get_field()
  40. self.alt_sel_alpha_field = self.option_dict()["_global_alt_sel_alpha"].get_field()
  41. self.alt_sel_alpha_field.spinner.valueChanged.connect(self.on_alt_sel_alpha_change)
  42. def build_options(self) -> [OptionUI]:
  43. return [
  44. RadioSetOptionUI(
  45. option="global_theme",
  46. label_text="Theme",
  47. label_tooltip="Select a theme for FlatCAM.\nIt will theme the plot area.",
  48. choices=[
  49. {"label": _("Light"), "value": "white"},
  50. {"label": _("Dark"), "value": "black"}
  51. ],
  52. orientation='vertical'
  53. ),
  54. CheckboxOptionUI(
  55. option="global_gray_icons",
  56. label_text="Use Gray Icons",
  57. label_tooltip="Check this box to use a set of icons with\na lighter (gray) color. To be used when a\nfull dark theme is applied."
  58. ),
  59. FullWidthButtonOptionUI(
  60. option="__button_apply_theme",
  61. label_text="Apply Theme",
  62. label_tooltip="Select a theme for FlatCAM.\n"
  63. "It will theme the plot area.\n"
  64. "The application will restart after change."
  65. ),
  66. SeparatorOptionUI(),
  67. ComboboxOptionUI(
  68. option="layout",
  69. label_text="Layout",
  70. label_tooltip="Select an layout for FlatCAM.\nIt is applied immediately.",
  71. choices=[
  72. "standard",
  73. "compact",
  74. "minimal"
  75. ]
  76. ),
  77. ComboboxOptionUI(
  78. option="style",
  79. label_text="Style",
  80. label_tooltip="Select an style for FlatCAM.\nIt will be applied at the next app start.",
  81. choices=QtWidgets.QStyleFactory.keys()
  82. ),
  83. CheckboxOptionUI(
  84. option="hdpi",
  85. label_text='Activate HDPI Support',
  86. label_tooltip="Enable High DPI support for FlatCAM.\nIt will be applied at the next app start.",
  87. ),
  88. CheckboxOptionUI(
  89. option="global_hover",
  90. label_text='Display Hover Shape',
  91. label_tooltip="Enable display of a hover shape for FlatCAM objects.\nIt is displayed whenever the mouse cursor is hovering\nover any kind of not-selected object.",
  92. ),
  93. CheckboxOptionUI(
  94. option="global_selection_shape",
  95. label_text='Display Selection Shape',
  96. label_tooltip="Enable the display of a selection shape for FlatCAM objects.\n"
  97. "It is displayed whenever the mouse selects an object\n"
  98. "either by clicking or dragging mouse from left to right or\n"
  99. "right to left."
  100. ),
  101. SeparatorOptionUI(),
  102. HeadingOptionUI(label_text="Left-Right Selection Color", label_tooltip=None),
  103. ColorOptionUI(
  104. option="global_sel_line",
  105. label_text="Outline",
  106. label_tooltip="Set the line color for the 'left to right' selection box."
  107. ),
  108. ColorOptionUI(
  109. option="global_sel_fill",
  110. label_text="Fill",
  111. label_tooltip="Set the fill color for the selection box\n"
  112. "in case that the selection is done from left to right.\n"
  113. "First 6 digits are the color and the last 2\n"
  114. "digits are for alpha (transparency) level."
  115. ),
  116. SliderWithSpinnerOptionUI(
  117. option="_global_sel_alpha",
  118. label_text="Alpha",
  119. label_tooltip="Set the fill transparency for the 'left to right' selection box.",
  120. min_value=0, max_value=255, step=1
  121. ),
  122. SeparatorOptionUI(),
  123. HeadingOptionUI(label_text="Right-Left Selection Color", label_tooltip=None),
  124. ColorOptionUI(
  125. option="global_alt_sel_line",
  126. label_text="Outline",
  127. label_tooltip="Set the line color for the 'right to left' selection box."
  128. ),
  129. ColorOptionUI(
  130. option="global_alt_sel_fill",
  131. label_text="Fill",
  132. label_tooltip="Set the fill color for the selection box\n"
  133. "in case that the selection is done from right to left.\n"
  134. "First 6 digits are the color and the last 2\n"
  135. "digits are for alpha (transparency) level."
  136. ),
  137. SliderWithSpinnerOptionUI(
  138. option="_global_alt_sel_alpha",
  139. label_text="Alpha",
  140. label_tooltip="Set the fill transparency for the 'right to left' selection box.",
  141. min_value=0, max_value=255, step=1
  142. ),
  143. SeparatorOptionUI(),
  144. HeadingOptionUI(label_text='Editor Color', label_tooltip=None),
  145. ColorOptionUI(
  146. option="global_draw_color",
  147. label_text="Drawing",
  148. label_tooltip="Set the color for the shape."
  149. ),
  150. ColorOptionUI(
  151. option="global_sel_draw_color",
  152. label_text="Selection",
  153. label_tooltip="Set the color of the shape when selected."
  154. ),
  155. SeparatorOptionUI(),
  156. HeadingOptionUI(label_text='Project Items Color', label_tooltip=None),
  157. ColorOptionUI(
  158. option="global_proj_item_color",
  159. label_text="Enabled",
  160. label_tooltip="Set the color of the items in Project Tab Tree."
  161. ),
  162. ColorOptionUI(
  163. option="global_proj_item_dis_color",
  164. label_text="Disabled",
  165. label_tooltip="Set the color of the items in Project Tab Tree,\n"
  166. "for the case when the items are disabled."
  167. ),
  168. CheckboxOptionUI(
  169. option="global_project_autohide",
  170. label_text="Project AutoHide",
  171. label_tooltip="Check this box if you want the project/selected/tool tab area to\n"
  172. "hide automatically when there are no objects loaded and\n"
  173. "to show whenever a new object is created."
  174. ),
  175. ]
  176. def on_sel_alpha_change(self):
  177. alpha = self.sel_alpha_field.get_value()
  178. fill = self._modify_color_alpha(color=self.sel_fill_field.get_value(), alpha=alpha)
  179. self.sel_fill_field.set_value(fill)
  180. line = self._modify_color_alpha(color=self.sel_line_field.get_value(), alpha=alpha)
  181. self.sel_line_field.set_value(line)
  182. def on_alt_sel_alpha_change(self):
  183. alpha = self.alt_sel_alpha_field.get_value()
  184. fill = self._modify_color_alpha(color=self.alt_sel_fill_field.get_value(), alpha=alpha)
  185. self.alt_sel_fill_field.set_value(fill)
  186. line = self._modify_color_alpha(color=self.alt_sel_line_field.get_value(), alpha=alpha)
  187. self.alt_sel_line_field.set_value(line)
  188. def _modify_color_alpha(self, color: str, alpha: int):
  189. color_without_alpha = color[:7]
  190. if alpha > 255:
  191. return color_without_alpha + "FF"
  192. elif alpha < 0:
  193. return color_without_alpha + "00"
  194. else:
  195. hexalpha = hex(alpha)[2:]
  196. if len(hexalpha) == 1:
  197. hexalpha = "0" + hexalpha
  198. return color_without_alpha + hexalpha
  199. def on_theme_change(self):
  200. # FIXME: this should be moved out to a view model
  201. val = self.theme_field.get_value()
  202. qsettings = QSettings("Open Source", "FlatCAM")
  203. qsettings.setValue("theme", val)
  204. # This will write the setting to the platform specific storage.
  205. del qsettings
  206. self.app.on_app_restart()
  207. def on_layout(self, index=None, lay=None):
  208. if lay:
  209. current_layout = lay
  210. else:
  211. current_layout = self.layout_field.get_value()
  212. self.app.ui.set_layout(current_layout)
  213. @staticmethod
  214. def handle_style(style):
  215. # FIXME: this should be moved out to a view model
  216. # set current style
  217. qsettings = QSettings("Open Source", "FlatCAM")
  218. qsettings.setValue('style', style)
  219. # This will write the setting to the platform specific storage.
  220. del qsettings
  221. @staticmethod
  222. def handle_hdpi(state):
  223. # FIXME: this should be moved out to a view model
  224. # set current HDPI
  225. qsettings = QSettings("Open Source", "FlatCAM")
  226. qsettings.setValue('hdpi', state)
  227. # This will write the setting to the platform specific storage.
  228. del qsettings