GeneralGUIPrefGroupUI.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. # FIXME: this should be moved out somewhere else
  209. """
  210. Set the toolbars layout (location)
  211. :param index:
  212. :param lay: Type of layout to be set on the toolbard
  213. :return: None
  214. """
  215. self.app.defaults.report_usage("on_layout()")
  216. if lay:
  217. current_layout = lay
  218. else:
  219. current_layout = self.layout_field.get_value()
  220. lay_settings = QSettings("Open Source", "FlatCAM")
  221. lay_settings.setValue('layout', current_layout)
  222. # This will write the setting to the platform specific storage.
  223. del lay_settings
  224. # first remove the toolbars:
  225. try:
  226. self.app.ui.removeToolBar(self.app.ui.toolbarfile)
  227. self.app.ui.removeToolBar(self.app.ui.toolbargeo)
  228. self.app.ui.removeToolBar(self.app.ui.toolbarview)
  229. self.app.ui.removeToolBar(self.app.ui.toolbarshell)
  230. self.app.ui.removeToolBar(self.app.ui.toolbartools)
  231. self.app.ui.removeToolBar(self.app.ui.exc_edit_toolbar)
  232. self.app.ui.removeToolBar(self.app.ui.geo_edit_toolbar)
  233. self.app.ui.removeToolBar(self.app.ui.grb_edit_toolbar)
  234. self.app.ui.removeToolBar(self.app.ui.snap_toolbar)
  235. self.app.ui.removeToolBar(self.app.ui.toolbarshell)
  236. except Exception:
  237. pass
  238. if current_layout == 'compact':
  239. # ## TOOLBAR INSTALLATION # ##
  240. self.app.ui.toolbarfile = QtWidgets.QToolBar('File Toolbar')
  241. self.app.ui.toolbarfile.setObjectName('File_TB')
  242. self.app.ui.addToolBar(Qt.LeftToolBarArea, self.app.ui.toolbarfile)
  243. self.app.ui.toolbargeo = QtWidgets.QToolBar('Edit Toolbar')
  244. self.app.ui.toolbargeo.setObjectName('Edit_TB')
  245. self.app.ui.addToolBar(Qt.LeftToolBarArea, self.app.ui.toolbargeo)
  246. self.app.ui.toolbarshell = QtWidgets.QToolBar('Shell Toolbar')
  247. self.app.ui.toolbarshell.setObjectName('Shell_TB')
  248. self.app.ui.addToolBar(Qt.LeftToolBarArea, self.app.ui.toolbarshell)
  249. self.app.ui.toolbartools = QtWidgets.QToolBar('Tools Toolbar')
  250. self.app.ui.toolbartools.setObjectName('Tools_TB')
  251. self.app.ui.addToolBar(Qt.LeftToolBarArea, self.app.ui.toolbartools)
  252. self.app.ui.geo_edit_toolbar = QtWidgets.QToolBar('Geometry Editor Toolbar')
  253. # self.app.ui.geo_edit_toolbar.setVisible(False)
  254. self.app.ui.geo_edit_toolbar.setObjectName('GeoEditor_TB')
  255. self.app.ui.addToolBar(Qt.RightToolBarArea, self.app.ui.geo_edit_toolbar)
  256. self.app.ui.toolbarview = QtWidgets.QToolBar('View Toolbar')
  257. self.app.ui.toolbarview.setObjectName('View_TB')
  258. self.app.ui.addToolBar(Qt.RightToolBarArea, self.app.ui.toolbarview)
  259. self.app.ui.addToolBarBreak(area=Qt.RightToolBarArea)
  260. self.app.ui.grb_edit_toolbar = QtWidgets.QToolBar('Gerber Editor Toolbar')
  261. # self.app.ui.grb_edit_toolbar.setVisible(False)
  262. self.app.ui.grb_edit_toolbar.setObjectName('GrbEditor_TB')
  263. self.app.ui.addToolBar(Qt.RightToolBarArea, self.app.ui.grb_edit_toolbar)
  264. self.app.ui.exc_edit_toolbar = QtWidgets.QToolBar('Excellon Editor Toolbar')
  265. self.app.ui.exc_edit_toolbar.setObjectName('ExcEditor_TB')
  266. self.app.ui.addToolBar(Qt.RightToolBarArea, self.app.ui.exc_edit_toolbar)
  267. self.app.ui.snap_toolbar = QtWidgets.QToolBar('Grid Toolbar')
  268. self.app.ui.snap_toolbar.setObjectName('Snap_TB')
  269. self.app.ui.snap_toolbar.setMaximumHeight(30)
  270. self.app.ui.splitter_left.addWidget(self.app.ui.snap_toolbar)
  271. self.app.ui.corner_snap_btn.setVisible(True)
  272. self.app.ui.snap_magnet.setVisible(True)
  273. else:
  274. # ## TOOLBAR INSTALLATION # ##
  275. self.app.ui.toolbarfile = QtWidgets.QToolBar('File Toolbar')
  276. self.app.ui.toolbarfile.setObjectName('File_TB')
  277. self.app.ui.addToolBar(self.app.ui.toolbarfile)
  278. self.app.ui.toolbargeo = QtWidgets.QToolBar('Edit Toolbar')
  279. self.app.ui.toolbargeo.setObjectName('Edit_TB')
  280. self.app.ui.addToolBar(self.app.ui.toolbargeo)
  281. self.app.ui.toolbarview = QtWidgets.QToolBar('View Toolbar')
  282. self.app.ui.toolbarview.setObjectName('View_TB')
  283. self.app.ui.addToolBar(self.app.ui.toolbarview)
  284. self.app.ui.toolbarshell = QtWidgets.QToolBar('Shell Toolbar')
  285. self.app.ui.toolbarshell.setObjectName('Shell_TB')
  286. self.app.ui.addToolBar(self.app.ui.toolbarshell)
  287. self.app.ui.toolbartools = QtWidgets.QToolBar('Tools Toolbar')
  288. self.app.ui.toolbartools.setObjectName('Tools_TB')
  289. self.app.ui.addToolBar(self.app.ui.toolbartools)
  290. self.app.ui.exc_edit_toolbar = QtWidgets.QToolBar('Excellon Editor Toolbar')
  291. # self.app.ui.exc_edit_toolbar.setVisible(False)
  292. self.app.ui.exc_edit_toolbar.setObjectName('ExcEditor_TB')
  293. self.app.ui.addToolBar(self.app.ui.exc_edit_toolbar)
  294. self.app.ui.addToolBarBreak()
  295. self.app.ui.geo_edit_toolbar = QtWidgets.QToolBar('Geometry Editor Toolbar')
  296. # self.app.ui.geo_edit_toolbar.setVisible(False)
  297. self.app.ui.geo_edit_toolbar.setObjectName('GeoEditor_TB')
  298. self.app.ui.addToolBar(self.app.ui.geo_edit_toolbar)
  299. self.app.ui.grb_edit_toolbar = QtWidgets.QToolBar('Gerber Editor Toolbar')
  300. # self.app.ui.grb_edit_toolbar.setVisible(False)
  301. self.app.ui.grb_edit_toolbar.setObjectName('GrbEditor_TB')
  302. self.app.ui.addToolBar(self.app.ui.grb_edit_toolbar)
  303. self.app.ui.snap_toolbar = QtWidgets.QToolBar('Grid Toolbar')
  304. self.app.ui.snap_toolbar.setObjectName('Snap_TB')
  305. # self.app.ui.snap_toolbar.setMaximumHeight(30)
  306. self.app.ui.addToolBar(self.app.ui.snap_toolbar)
  307. self.app.ui.corner_snap_btn.setVisible(False)
  308. self.app.ui.snap_magnet.setVisible(False)
  309. if current_layout == 'minimal':
  310. self.app.ui.toolbarview.setVisible(False)
  311. self.app.ui.toolbarshell.setVisible(False)
  312. self.app.ui.snap_toolbar.setVisible(False)
  313. self.app.ui.geo_edit_toolbar.setVisible(False)
  314. self.app.ui.grb_edit_toolbar.setVisible(False)
  315. self.app.ui.exc_edit_toolbar.setVisible(False)
  316. self.app.ui.lock_toolbar(lock=True)
  317. # add all the actions to the toolbars
  318. self.app.ui.populate_toolbars()
  319. # reconnect all the signals to the toolbar actions
  320. self.app.connect_toolbar_signals()
  321. self.app.ui.grid_snap_btn.setChecked(True)
  322. self.app.ui.on_grid_snap_triggered(state=True)
  323. self.app.ui.grid_gap_x_entry.setText(str(self.app.defaults["global_gridx"]))
  324. self.app.ui.grid_gap_y_entry.setText(str(self.app.defaults["global_gridy"]))
  325. self.app.ui.snap_max_dist_entry.setText(str(self.app.defaults["global_snap_max"]))
  326. self.app.ui.grid_gap_link_cb.setChecked(True)
  327. @staticmethod
  328. def handle_style(style):
  329. # FIXME: this should be moved out to a view model
  330. # set current style
  331. qsettings = QSettings("Open Source", "FlatCAM")
  332. qsettings.setValue('style', style)
  333. # This will write the setting to the platform specific storage.
  334. del qsettings
  335. @staticmethod
  336. def handle_hdpi(state):
  337. # FIXME: this should be moved out to a view model
  338. # set current HDPI
  339. qsettings = QSettings("Open Source", "FlatCAM")
  340. qsettings.setValue('hdpi', state)
  341. # This will write the setting to the platform specific storage.
  342. del qsettings