diff -r fb0ef164f536 -r 698ae46f40a4 eric6/Preferences/ConfigurationDialog.py --- a/eric6/Preferences/ConfigurationDialog.py Fri Apr 02 11:59:41 2021 +0200 +++ b/eric6/Preferences/ConfigurationDialog.py Sat May 01 14:27:20 2021 +0200 @@ -7,6 +7,8 @@ Module implementing a dialog for the configuration of eric. """ +import contextlib +import enum import os import types @@ -46,7 +48,7 @@ @param pageName name of the configuration page (string) @param iconFile file name of the icon to be shown (string) """ - super(ConfigurationPageItem, self).__init__(parent, [text]) + super().__init__(parent, [text]) self.setIcon(0, UI.PixmapCache.getIcon(iconFile)) self.__pageName = pageName @@ -60,6 +62,16 @@ return self.__pageName +class ConfigurationMode(enum.Enum): + """ + Class defining the various modes of the configuration widget. + """ + DEFAULTMODE = 0 + TRAYSTARTERMODE = 1 + HEXEDITORMODE = 2 + WEBBROWSERMODE = 3 + + class ConfigurationWidget(QWidget): """ Class implementing a dialog for the configuration of eric. @@ -75,28 +87,24 @@ accepted = pyqtSignal() rejected = pyqtSignal() - DefaultMode = 0 - HelpBrowserMode = 1 - TrayStarterMode = 2 - HexEditorMode = 3 - WebBrowserMode = 4 - - def __init__(self, parent=None, fromEric=True, displayMode=DefaultMode, + def __init__(self, parent=None, fromEric=True, + displayMode=ConfigurationMode.DEFAULTMODE, expandedEntries=None): """ Constructor - @param parent The parent widget of this dialog. (QWidget) + @param parent reference to the parent widget + @type QWidget @param fromEric flag indicating a dialog generation from within the - eric ide (boolean) + eric IDE + @type bool @param displayMode mode of the configuration dialog - (DefaultMode, HelpBrowserMode, TrayStarterMode, HexEditorMode, - WebBrowserMode) - @exception RuntimeError raised to indicate an invalid dialog mode + @type ConfigurationMode @param expandedEntries list of entries to be shown expanded - (list of strings) + @type list of str """ - super(ConfigurationWidget, self).__init__(parent) + super().__init__(parent) + self.fromEric = fromEric self.displayMode = displayMode self.__webEngine = getWebBrowserSupport() == "QtWebEngine" @@ -122,7 +130,7 @@ e5App().registerObject("VirtualEnvManager", self.virtualenvManager) - if displayMode == ConfigurationWidget.DefaultMode: + if displayMode == ConfigurationMode.DEFAULTMODE: self.configItems = { # key : [display string, pixmap name, dialog module name or # page creation function, parent key, @@ -359,7 +367,7 @@ self.configItems.update( e5App().getObject("PluginManager").getPluginConfigData()) - elif displayMode == ConfigurationWidget.WebBrowserMode: + elif displayMode == ConfigurationMode.WEBBROWSERMODE: self.configItems = { # key : [display string, pixmap name, dialog module name or # page creation function, parent key, @@ -402,7 +410,7 @@ "WebBrowserSpellCheckingPage", None, None], } - elif displayMode == ConfigurationWidget.TrayStarterMode: + elif displayMode == ConfigurationMode.TRAYSTARTERMODE: self.configItems = { # key : [display string, pixmap name, dialog module name or # page creation function, parent key, @@ -415,7 +423,7 @@ "TrayStarterPage", None, None], } - elif displayMode == ConfigurationWidget.HexEditorMode: + elif displayMode == ConfigurationMode.HEXEDITORMODE: self.configItems = { # key : [display string, pixmap name, dialog module name or # page creation function, parent key, @@ -428,9 +436,6 @@ "HexEditorPage", None, None], } - else: - raise RuntimeError("Illegal mode value: {0}".format(displayMode)) - # generate the list entries self.__expandedEntries = [] for key in sorted(self.configItems.keys()): @@ -445,9 +450,11 @@ self.itmDict[key] = ConfigurationPageItem(pitm, pageData[0], key, pageData[1]) self.itmDict[key].setData(0, Qt.ItemDataRole.UserRole, key) - if (not self.fromEric or - displayMode != ConfigurationWidget.DefaultMode or - key in expandedEntries): + if ( + not self.fromEric or + displayMode != ConfigurationMode.DEFAULTMODE or + key in expandedEntries + ): self.itmDict[key].setExpanded(True) self.configList.sortByColumn(0, Qt.SortOrder.AscendingOrder) @@ -460,14 +467,13 @@ self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.rejected) - if displayMode in [ConfigurationWidget.HelpBrowserMode, - ConfigurationWidget.TrayStarterMode, - ConfigurationWidget.HexEditorMode, - ConfigurationWidget.WebBrowserMode]: + if displayMode in [ConfigurationMode.TRAYSTARTERMODE, + ConfigurationMode.HEXEDITORMODE, + ConfigurationMode.WEBBROWSERMODE]: self.configListSearch.hide() - if displayMode not in [ConfigurationWidget.TrayStarterMode, - ConfigurationWidget.HexEditorMode]: + if displayMode not in [ConfigurationMode.TRAYSTARTERMODE, + ConfigurationMode.HEXEDITORMODE]: self.__initLexers() def accept(self): @@ -566,7 +572,7 @@ self.buttonBox.setObjectName("buttonBox") if ( not self.fromEric and - self.displayMode == ConfigurationWidget.DefaultMode + self.displayMode == ConfigurationMode.DEFAULTMODE ): self.buttonBox.button(QDialogButtonBox.StandardButton.Apply).hide() self.buttonBox.button( @@ -612,14 +618,13 @@ text = text.lower() for index in range(parent.childCount()): itm = parent.child(index) - if itm.childCount() > 0: - enable = ( - self.__searchChildItems(itm, text) or - text == "" or - text in itm.text(0).lower() - ) - else: - enable = text == "" or text in itm.text(0).lower() + enable = ( + (self.__searchChildItems(itm, text) or + text == "" or + text in itm.text(0).lower()) + if itm.childCount() > 0 else + (text == "" or text in itm.text(0).lower()) + ) if enable: childEnabled = True itm.setDisabled(not enable) @@ -638,10 +643,8 @@ self.lexers = {} for language in QScintilla.Lexers.getSupportedLanguages(): if language not in self.lexers: - try: + with contextlib.suppress(PreferencesLexerLanguageError): self.lexers[language] = PreferencesLexer(language, self) - except PreferencesLexerLanguageError: - pass def __importConfigurationPage(self, name): """ @@ -692,10 +695,8 @@ if page is not None: self.configStack.addWidget(page) pageData[-1] = page - try: + with contextlib.suppress(AttributeError): page.setMode(self.displayMode) - except AttributeError: - pass return page def showConfigurationPageByName(self, pageName, setCurrent=True): @@ -904,7 +905,7 @@ """ return ( self.__webEngine or - self.displayMode == ConfigurationWidget.WebBrowserMode + self.displayMode == ConfigurationMode.WEBBROWSERMODE ) @@ -919,30 +920,28 @@ preferencesChanged = pyqtSignal() masterPasswordChanged = pyqtSignal(str, str) - DefaultMode = ConfigurationWidget.DefaultMode - HelpBrowserMode = ConfigurationWidget.HelpBrowserMode - TrayStarterMode = ConfigurationWidget.TrayStarterMode - HexEditorMode = ConfigurationWidget.HexEditorMode - WebBrowserMode = ConfigurationWidget.WebBrowserMode - def __init__(self, parent=None, name=None, modal=False, - fromEric=True, displayMode=ConfigurationWidget.DefaultMode, + fromEric=True, + displayMode=ConfigurationMode.DEFAULTMODE, expandedEntries=None): """ Constructor - @param parent The parent widget of this dialog. (QWidget) - @param name The name of this dialog. string - @param modal Flag indicating a modal dialog. (boolean) + @param parent reference to the parent widget + @type QWidget + @param name name of the dialog + @type str + @param modal flag indicating a modal dialog + @type bool @param fromEric flag indicating a dialog generation from within the - eric ide (boolean) + eric IDE + @type bool @param displayMode mode of the configuration dialog - (DefaultMode, HelpBrowserMode, TrayStarterMode, HexEditorMode, - WebBrowserMode) + @type ConfigurationMode @param expandedEntries list of entries to be shown expanded - (list of strings) + @type list of str """ - super(ConfigurationDialog, self).__init__(parent) + super().__init__(parent) if name: self.setObjectName(name) self.setModal(modal) @@ -1015,7 +1014,7 @@ """ Public method to accept the dialog. """ - super(ConfigurationDialog, self).accept() + super().accept() class ConfigurationWindow(E5MainWindow): @@ -1028,7 +1027,7 @@ @param parent reference to the parent widget (QWidget) """ - super(ConfigurationWindow, self).__init__(parent) + super().__init__(parent) self.cw = ConfigurationWidget(self, fromEric=False) size = self.cw.size()