From a5384d50d8fc30797854ad3f4ab5d7e48d3965f0 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Wed, 29 Apr 2020 10:09:07 +0300 Subject: [PATCH 1/2] - more PEP8 changes - in PreferencesUI.PreferencesUIManager class I removed the need to pass reference to the App class since it this was available through the 'ui' parameter - some fixes due to recent refactoring - minor bugs fixed (not so visible) - promoted some methods to be static - set the default layout on first run to the 'minimal' value --- CHANGELOG.md | 6 ++ FlatCAMApp.py | 38 ++++------ defaults.py | 8 +-- flatcamGUI/FlatCAMGUI.py | 10 ++- flatcamGUI/PreferencesUI.py | 136 ++++++++++++++++++++---------------- 5 files changed, 105 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa6b284c..9d4e0328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ CHANGELOG for FlatCAM beta 29.04.2020 - added a try-except clause in the FlatCAMTranslation.restart_program() when closing the Listener and the thread that runs it to adjust to MacOS usage +- more PEP8 changes +- in PreferencesUI.PreferencesUIManager class I removed the need to pass reference to the App class since it this was available through the 'ui' parameter +- some fixes due to recent refactoring +- minor bugs fixed (not so visible) +- promoted some methods to be static +- set the default layout on first run to the 'minimal' value 28.04.2020 diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 579bb07a..624de538 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -433,11 +433,6 @@ class App(QtCore.QObject): if user_defaults: self.defaults.load(filename=current_defaults_path) - if self.defaults["global_gray_icons"] is False: - self.resource_location = 'share' - else: - self.resource_location = 'share/dark_resources' - if self.defaults['units'] == 'MM': self.decimals = int(self.defaults['decimals_metric']) else: @@ -450,9 +445,6 @@ class App(QtCore.QObject): self.current_units = self.defaults['units'] - - - # ########################################################################################################### # #################################### SETUP OBJECT CLASSES ################################################# # ########################################################################################################### @@ -550,7 +542,7 @@ class App(QtCore.QObject): # ########################################################################################################### self.preferencesUiManager = PreferencesUIManager(defaults=self.defaults, data_path=self.data_path, ui=self.ui, - inform=self.inform, app=self) + inform=self.inform) self.preferencesUiManager.defaults_write_form() # When the self.defaults dictionary changes will update the Preferences GUI forms @@ -564,7 +556,7 @@ class App(QtCore.QObject): if self.defaults["first_run"] is True: # ONLY AT FIRST STARTUP INIT THE GUI LAYOUT TO 'COMPACT' - initial_lay = 'compact' + initial_lay = 'minimal' self.ui.general_defaults_form.general_gui_group.on_layout(lay=initial_lay) # Set the combobox in Preferences to the current layout @@ -6031,7 +6023,7 @@ class App(QtCore.QObject): # FIXME: doing this based on translated title doesn't seem very robust. if title == _("Preferences"): - self.uiPreferencesManager.on_close_preferences_tab() + self.preferencesUiManager.on_close_preferences_tab() if title == _("Tools Database"): # disconnect the signals from the table widget in tab @@ -6067,6 +6059,18 @@ class App(QtCore.QObject): self.book_dialog_tab.rebuild_actions() self.book_dialog_tab.deleteLater() + def on_plotarea_tab_closed(self, tab_idx): + """ + + :param tab_idx: Index of the Tab from the plotarea that was closed + :return: + """ + widget = self.ui.plot_tab_area.widget(tab_idx) + + if widget is not None: + widget.deleteLater() + self.ui.plot_tab_area.removeTab(tab_idx) + def on_flipy(self): """ Executed when the menu entry in Options -> Flip on Y axis is clicked. @@ -10747,18 +10751,6 @@ class App(QtCore.QObject): self.autosave_timer.setInterval(int(self.defaults['global_autosave_timeout'])) self.autosave_timer.start() - def on_plotarea_tab_closed(self, tab_idx): - """ - - :param tab_idx: Index of the Tab from the plotarea that was closed - :return: - """ - widget = self.ui.plot_tab_area.widget(tab_idx) - - if widget is not None: - widget.deleteLater() - self.ui.plot_tab_area.removeTab(tab_idx) - def on_options_app2project(self): """ Callback for Options->Transfer Options->App=>Project. Copies options diff --git a/defaults.py b/defaults.py index fea8c774..42901b6e 100644 --- a/defaults.py +++ b/defaults.py @@ -25,7 +25,7 @@ class FlatCAMDefaults: # Global APP Preferences "decimals_inch": 4, "decimals_metric": 4, - "version": 8.992, # defaults format version, not necessarily equal to app version + "version": 8.992, # defaults format version, not necessarily equal to app version "first_run": True, "units": "MM", "global_serial": 0, @@ -695,7 +695,7 @@ class FlatCAMDefaults: self.current_defaults.update(self.factory_defaults) self.old_defaults_found = False - ##### Pass-through to the defaults LoudDict ##### + # #### Pass-through to the defaults LoudDict ##### def __len__(self): return self.defaults.__len__() @@ -715,9 +715,7 @@ class FlatCAMDefaults: # Unfortunately this method alone is not enough to pass through the other magic methods above. return self.defaults.__getattribute__(item) - - ##### Additional Methods ##### - + # #### Additional Methods ##### def write(self, filename: str): """Saves the defaults to a file on disk""" with open(filename, "w") as file: diff --git a/flatcamGUI/FlatCAMGUI.py b/flatcamGUI/FlatCAMGUI.py index 9046aace..bb40b298 100644 --- a/flatcamGUI/FlatCAMGUI.py +++ b/flatcamGUI/FlatCAMGUI.py @@ -2573,9 +2573,9 @@ class FlatCAMGUI(QtWidgets.QMainWindow): self.run_script_btn = self.toolbarshell.addAction( QtGui.QIcon(self.app.resource_location + '/script16.png'), _('Run Script ...')) - # ######################################################################## - # ## Tools Toolbar # ## - # ######################################################################## + # ######################################################################### + # ######################### Tools Toolbar ################################# + # ######################################################################### self.dblsided_btn = self.toolbartools.addAction( QtGui.QIcon(self.app.resource_location + '/doubleside32.png'), _("2Sided Tool")) self.align_btn = self.toolbartools.addAction( @@ -2619,6 +2619,10 @@ class FlatCAMGUI(QtWidgets.QMainWindow): QtGui.QIcon(self.app.resource_location + '/fiducials_32.png'), _("Fiducials Tool")) self.cal_btn = self.toolbartools.addAction( QtGui.QIcon(self.app.resource_location + '/calibrate_32.png'), _("Calibration Tool")) + self.punch_btn = self.toolbartools.addAction( + QtGui.QIcon(self.app.resource_location + '/punch32.png'), _("Punch Gerber Tool")) + self.invert_btn = self.toolbartools.addAction( + QtGui.QIcon(self.app.resource_location + '/invert32.png'), _("Invert Gerber Tool")) # ######################################################################## # ## Excellon Editor Toolbar # ## diff --git a/flatcamGUI/PreferencesUI.py b/flatcamGUI/PreferencesUI.py index 91f64364..e7c32c7b 100644 --- a/flatcamGUI/PreferencesUI.py +++ b/flatcamGUI/PreferencesUI.py @@ -3,6 +3,8 @@ # File Author: Marius Adrian Stanciu (c) # # Date: 10/10/2019 # # MIT Licence # +# # +# Modified by David Robertson 29.04.2020 # # ########################################################## import os @@ -14,6 +16,7 @@ import logging import gettext import FlatCAMTranslation as fcTranslate import builtins + log = logging.getLogger('PreferencesUI') fcTranslate.apply_language('strings') if '_' not in builtins.__dict__: @@ -26,11 +29,18 @@ else: machinist_setting = 0 -class PreferencesUIManager(): +class PreferencesUIManager: + + def __init__(self, defaults: FlatCAMDefaults, data_path: str, ui, inform): + """ + Class that control the Preferences Tab + + :param defaults: a dictionary storage where all the application settings are stored + :param data_path: a path to the file where all the preferences are stored for persistence + :param ui: reference to the FlatCAMGUI class which constructs the UI + :param inform: a pyqtSignal used to display information's in the StatusBar of the GUI + """ - def __init__(self, defaults: FlatCAMDefaults, data_path: str, ui, inform, app): - # FIXME: Ideally we would not pass in the app here - self.app = app self.defaults = defaults self.data_path = data_path self.ui = ui @@ -627,7 +637,7 @@ class PreferencesUIManager(): try: value = def_dict[field] - log.debug("value is "+str(value)+ " and factor is "+str(factor)) + log.debug("value is " + str(value) + " and factor is "+str(factor)) if factor is not None: value *= factor @@ -913,7 +923,7 @@ class PreferencesUIManager(): self.defaults.load(filename=os.path.join(self.data_path, 'current_defaults.FlatConfig')) # Re-fresh project options - self.app.on_options_app2project() + self.ui.app.on_options_app2project() settgs = QSettings("Open Source", "FlatCAM") @@ -956,7 +966,7 @@ class PreferencesUIManager(): pass self.defaults_write_form(source_dict=self.defaults.current_defaults) self.ui.general_defaults_form.general_app_group.units_radio.activated_custom.connect( - lambda: self.app.on_toggle_units(no_pref=False)) + lambda: self.ui.app.on_toggle_units(no_pref=False)) self.defaults.update(self.defaults.current_defaults) # Preferences save, update the color of the Preferences Tab text @@ -1016,7 +1026,7 @@ class PreferencesUIManager(): self.inform.emit('[success] %s' % _("Preferences saved.")) # update the autosave timer - self.app.save_project_auto_update() + self.ui.app.save_project_auto_update() def save_toolbar_view(self): """ @@ -1111,7 +1121,7 @@ class PreferencesUIManager(): msgbox.setText(_("One or more values are changed.\n" "Do you want to save the Preferences?")) msgbox.setWindowTitle(_("Save Preferences")) - msgbox.setWindowIcon(QtGui.QIcon(self.app.resource_location + '/save_as.png')) + msgbox.setWindowIcon(QtGui.QIcon(self.ui.app.resource_location + '/save_as.png')) bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole) msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole) @@ -1506,9 +1516,9 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI): grid0.addWidget(self.layout_combo, 4, 1) # Set the current index for layout_combo - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("layout"): - layout = settings.value('layout', type=str) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("layout"): + layout = qsettings.value('layout', type=str) idx = self.layout_combo.findText(layout.capitalize()) self.layout_combo.setCurrentIndex(idx) @@ -1535,9 +1545,9 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI): "It will be applied at the next app start.") ) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("hdpi"): - self.hdpi_cb.set_value(settings.value('hdpi', type=int)) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("hdpi"): + self.hdpi_cb.set_value(qsettings.value('hdpi', type=int)) else: self.hdpi_cb.set_value(False) self.hdpi_cb.stateChanged.connect(self.handle_hdpi) @@ -1826,7 +1836,7 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI): # Setting Editor Draw colors signals self.draw_color_entry.editingFinished.connect(self.on_draw_color_entry) - self.draw_color_button.clicked.connect( self.on_draw_color_button) + self.draw_color_button.clicked.connect(self.on_draw_color_button) self.sel_draw_color_entry.editingFinished.connect(self.on_sel_draw_color_entry) self.sel_draw_color_button.clicked.connect(self.on_sel_draw_color_button) @@ -1839,32 +1849,33 @@ class GeneralGUIPrefGroupUI(OptionsGroupUI): self.layout_combo.activated.connect(self.on_layout) - def on_theme_change(self): val = self.theme_radio.get_value() - t_settings = QSettings("Open Source", "FlatCAM") - t_settings.setValue('theme', val) + qsettings = QSettings("Open Source", "FlatCAM") + qsettings.setValue('theme', val) # This will write the setting to the platform specific storage. - del t_settings + del qsettings self.app.on_app_restart() - def handle_style(self, style): + @staticmethod + def handle_style(style): # set current style - settings = QSettings("Open Source", "FlatCAM") - settings.setValue('style', style) + qsettings = QSettings("Open Source", "FlatCAM") + qsettings.setValue('style', style) # This will write the setting to the platform specific storage. - del settings + del qsettings - def handle_hdpi(self, state): + @staticmethod + def handle_hdpi(state): # set current HDPI - settings = QSettings("Open Source", "FlatCAM") - settings.setValue('hdpi', state) + qsettings = QSettings("Open Source", "FlatCAM") + qsettings.setValue('hdpi', state) # This will write the setting to the platform specific storage. - del settings + del qsettings # Setting selection colors (left - right) handlers def on_sf_color_entry(self): @@ -2394,9 +2405,9 @@ class GeneralAPPSetGroupUI(OptionsGroupUI): self.notebook_font_size_spinner.set_range(8, 40) self.notebook_font_size_spinner.setWrapping(True) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("notebook_font_size"): - self.notebook_font_size_spinner.set_value(settings.value('notebook_font_size', type=int)) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("notebook_font_size"): + self.notebook_font_size_spinner.set_value(qsettings.value('notebook_font_size', type=int)) else: self.notebook_font_size_spinner.set_value(12) @@ -2413,9 +2424,9 @@ class GeneralAPPSetGroupUI(OptionsGroupUI): self.axis_font_size_spinner.set_range(0, 40) self.axis_font_size_spinner.setWrapping(True) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("axis_font_size"): - self.axis_font_size_spinner.set_value(settings.value('axis_font_size', type=int)) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("axis_font_size"): + self.axis_font_size_spinner.set_value(qsettings.value('axis_font_size', type=int)) else: self.axis_font_size_spinner.set_value(8) @@ -2433,8 +2444,8 @@ class GeneralAPPSetGroupUI(OptionsGroupUI): self.textbox_font_size_spinner.set_range(8, 40) self.textbox_font_size_spinner.setWrapping(True) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): self.textbox_font_size_spinner.set_value(settings.value('textbox_font_size', type=int)) else: self.textbox_font_size_spinner.set_value(10) @@ -2799,8 +2810,8 @@ class GeneralAppPrefGroupUI(OptionsGroupUI): _("Enable display of the splash screen at application startup.") ) - settings = QSettings("Open Source", "FlatCAM") - if settings.value("splash_screen"): + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.value("splash_screen"): self.splash_cb.set_value(True) else: self.splash_cb.set_value(False) @@ -3024,12 +3035,13 @@ class GeneralAppPrefGroupUI(OptionsGroupUI): self.language_apply_btn.clicked.connect(lambda: fcTranslate.on_language_apply_click(app=self.app, restart=True)) - def on_splash_changed(self, state): - settings = QSettings("Open Source", "FlatCAM") - settings.setValue('splash_screen', 1) if state else settings.setValue('splash_screen', 0) + @staticmethod + def on_splash_changed(state): + qsettings = QSettings("Open Source", "FlatCAM") + qsettings.setValue('splash_screen', 1) if state else qsettings.setValue('splash_screen', 0) # This will write the setting to the platform specific storage. - del settings + del qsettings class GerberGenPrefGroupUI(OptionsGroupUI): @@ -3037,6 +3049,7 @@ class GerberGenPrefGroupUI(OptionsGroupUI): # OptionsGroupUI.__init__(self, "Gerber General Preferences", parent=parent) super(GerberGenPrefGroupUI, self).__init__(self) + self.parent = parent self.setTitle(str(_("Gerber General"))) self.decimals = decimals @@ -3233,7 +3246,7 @@ class GerberGenPrefGroupUI(OptionsGroupUI): def on_pf_color_entry(self): self.app.defaults['gerber_plot_fill'] = self.pf_color_entry.get_value()[:7] + \ self.app.defaults['gerber_plot_fill'][7:9] - self.pf_color_button.setStyleSheet("background-color:%s" % str(self.defaults['gerber_plot_fill'])[:7]) + self.pf_color_button.setStyleSheet("background-color:%s" % str(self.app.defaults['gerber_plot_fill'])[:7]) def on_pf_color_button(self): current_color = QtGui.QColor(self.app.defaults['gerber_plot_fill'][:7]) @@ -6167,9 +6180,9 @@ class CNCJobOptPrefGroupUI(OptionsGroupUI): ) self.layout.addWidget(self.export_gcode_label) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 font = QtGui.QFont() @@ -6244,9 +6257,9 @@ class CNCJobAdvOptPrefGroupUI(OptionsGroupUI): ) self.layout.addWidget(toolchangelabel) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 font = QtGui.QFont() @@ -8030,7 +8043,6 @@ class ToolsTransformPrefGroupUI(OptionsGroupUI): grid0.addWidget(self.buffer_rounded_cb, 19, 0, 1, 2) - self.layout.addStretch() @@ -9740,9 +9752,9 @@ class FAExcPrefGroupUI(OptionsGroupUI): ) self.vertical_lay.addWidget(list_label) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 @@ -9813,9 +9825,9 @@ class FAGcoPrefGroupUI(OptionsGroupUI): ) self.layout.addWidget(self.gco_list_label) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 @@ -9883,9 +9895,9 @@ class FAGrbPrefGroupUI(OptionsGroupUI): ) self.layout.addWidget(self.grb_list_label) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 @@ -9955,9 +9967,9 @@ class AutoCompletePrefGroupUI(OptionsGroupUI): ) self.layout.addWidget(self.grb_list_label) - settings = QSettings("Open Source", "FlatCAM") - if settings.contains("textbox_font_size"): - tb_fsize = settings.value('textbox_font_size', type=int) + qsettings = QSettings("Open Source", "FlatCAM") + if qsettings.contains("textbox_font_size"): + tb_fsize = qsettings.value('textbox_font_size', type=int) else: tb_fsize = 10 From 2ca6e2e3f17f0e55859ee3252978bc4922a33e19 Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Wed, 29 Apr 2020 10:48:47 +0300 Subject: [PATCH 2/2] - modified the method that detects which tab was closed in the Plot Area so it will no longer depend on it's translated text but on it's objectName set on the QTab creation --- CHANGELOG.md | 1 + FlatCAMApp.py | 21 ++++++++++----------- flatcamGUI/GUIElements.py | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d4e0328..f48fab82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ CHANGELOG for FlatCAM beta - minor bugs fixed (not so visible) - promoted some methods to be static - set the default layout on first run to the 'minimal' value +- modified the method that detects which tab was closed in the Plot Area so it will no longer depend on it's translated text but on it's objectName set on the QTab creation 28.04.2020 diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 624de538..5188af31 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -3474,6 +3474,7 @@ class App(QtCore.QObject): # BookDialog(app=self, storage=self.defaults["global_bookmarks"], parent=self.ui).exec_() self.book_dialog_tab = BookmarkManager(app=self, storage=self.defaults["global_bookmarks"], parent=self.ui) + self.book_dialog_tab.setObjectName("bookmarks_tab") # add the tab if it was closed self.ui.plot_tab_area.addTab(self.book_dialog_tab, _("Bookmarks Manager")) @@ -6013,19 +6014,17 @@ class App(QtCore.QObject): else: self.inform.emit('[ERROR_NOTCL] %s' % _("Adding tool from DB is not allowed for this object.")) - def on_plot_area_tab_closed(self, title): + def on_plot_area_tab_closed(self, tab_obj_name): """ - Executed whenever a tab is closed in the Plot Area. + Executed whenever a QTab is closed in the Plot Area. - :param title: The name of the tab that was closed. + :param title: The objectName of the Tab that was closed. This objectName is assigned on Tab creation :return: """ - # FIXME: doing this based on translated title doesn't seem very robust. - if title == _("Preferences"): + if tab_obj_name == "preferences_tab": self.preferencesUiManager.on_close_preferences_tab() - - if title == _("Tools Database"): + elif tab_obj_name == "database_tab": # disconnect the signals from the table widget in tab self.tools_db_tab.ui_disconnect() @@ -6051,13 +6050,13 @@ class App(QtCore.QObject): self.inform.emit('') return self.tools_db_tab.deleteLater() - - if title == _("Code Editor"): + elif tab_obj_name == "text_editor_tab": self.toggle_codeeditor = False - - if title == _("Bookmarks Manager"): + elif tab_obj_name == "bookmarks_tab": self.book_dialog_tab.rebuild_actions() self.book_dialog_tab.deleteLater() + else: + return def on_plotarea_tab_closed(self, tab_idx): """ diff --git a/flatcamGUI/GUIElements.py b/flatcamGUI/GUIElements.py index c43549ff..e550e98f 100644 --- a/flatcamGUI/GUIElements.py +++ b/flatcamGUI/GUIElements.py @@ -2088,9 +2088,9 @@ class FCDetachableTab2(FCDetachableTab): :param currentIndex: :return: """ - idx = self.currentIndex() - - self.tab_closed_signal.emit(self.tabText(idx)) + # idx = self.currentIndex() + self.tab_name = self.widget(currentIndex).objectName() + self.tab_closed_signal.emit(self.tab_name) self.removeTab(currentIndex)