diff -r 240eb59dfb13 -r 6a730f4d0080 src/eric7/Project/ProjectBrowser.py --- a/src/eric7/Project/ProjectBrowser.py Wed Nov 23 11:09:33 2022 +0100 +++ b/src/eric7/Project/ProjectBrowser.py Wed Nov 23 16:07:16 2022 +0100 @@ -7,12 +7,15 @@ Module implementing the project browser part of the eric UI. """ +import contextlib + from PyQt6.QtCore import Qt, pyqtSignal from PyQt6.QtGui import QColor from PyQt6.QtWidgets import QApplication from eric7 import Preferences from eric7.EricGui import EricPixmapCache +from eric7.EricWidgets import EricMessageBox from eric7.EricWidgets.EricLed import EricClickableLed from eric7.EricWidgets.EricTabWidget import EricTabWidget @@ -26,6 +29,7 @@ SourcesBrowserFlag, TranslationsBrowserFlag, ) +from .ProjectBrowserRepositoryItem import ProjectBrowserRepositoryItem from .ProjectFormsBrowser import ProjectFormsBrowser from .ProjectInterfacesBrowser import ProjectInterfacesBrowser from .ProjectOthersBrowser import ProjectOthersBrowser @@ -135,23 +139,20 @@ } self.__vcsStateChanged(" ") + self.__browserRepository = {} + # create all the individual browsers - self.__browsers = { - # sources browser - "sources": ProjectSourcesBrowser(self.project, self), - # forms browser - "forms": ProjectFormsBrowser(self.project, self), - # resources browser - "resources": ProjectResourcesBrowser(self.project, self), - # translations browser - "translations": ProjectTranslationsBrowser(self.project, self), - # others browser - "others": ProjectOthersBrowser(self.project, self), - # interfaces (IDL) browser - "interfaces": ProjectInterfacesBrowser(self.project, self), - # protocols (protobuf) browser - "protocols": ProjectProtocolsBrowser(self.project, self), - } + for browserClass in ( + ProjectSourcesBrowser, + ProjectFormsBrowser, + ProjectResourcesBrowser, + ProjectTranslationsBrowser, + ProjectOthersBrowser, + # TODO: move the next two browsers to plugins + ProjectInterfacesBrowser, + ProjectProtocolsBrowser, + ): + browserClass(self.project, self) # add signal connection to ourselves self.project.projectOpened.connect(self.__projectOpened) @@ -165,6 +166,82 @@ self.__projectPropertiesChanged() self.setCurrentIndex(0) + def addTypedProjectBrowser(self, browserType, projectBrowserItem): + """ + Public method to add a project browser type to the browser repository. + + @param browserType type of the project browser + @type str + @param projectBrowserItem data structure containing the type specific data + @type ProjectBrowserRepositoryItem + @exception TypeError raised to signal a wrong type for the project browser item + """ + if not isinstance(projectBrowserItem, ProjectBrowserRepositoryItem): + raise TypeError( + "'projectBrowserItem' must be an instance of" + " 'ProjectBrowserRepositoryItem'." + ) + + if browserType in self.__browserRepository: + EricMessageBox.critical( + self.ui, + self.tr("Add Project Browser Type"), + self.tr( + "<p>The project browser type <b>{0}</b> has already been added." + " This attempt will be ignored.</p>" + ), + ) + else: + self.__browserRepository[browserType] = projectBrowserItem + + def removeTypedProjectBrowser(self, browserType): + """ + Public method to remove a browser type from the browsers repository. + + Note: If the browser type is not contained in the repository, the request to + remove it will be ignored silently. + + @param browserType project browser type + @type str + """ + with contextlib.suppress(KeyError): + browserIndex = self.indexOf(self.getProjectBrowser(browserType)) + if browserIndex >= 0: + self.removeTab(browserIndex) + del self.__browserRepository[browserType] + + def getProjectBrowsers(self): + """ + Public method to get references to the individual project browsers. + + @return list of references to project browsers + @rtype list of ProjectBaseBrowser + """ + return [itm.projectBrowser for itm in self.__browserRepository.items()] + + def getProjectBrowser(self, browserType): + """ + Public method to get a reference to the project browser of given type. + + @param browserType type of the requested project browser + @type str + @return reference to the requested browser or None + @rtype ProjectBaseBrowser or None + """ + try: + return self.__browserRepository[browserType].projectBrowser + except KeyError: + return None + + def getProjectBrowserNames(self): + """ + Public method to get the types of the various project browsers. + + @return list of project browser types + @rtype list of str + """ + return list(self.__browserRepository.keys()) + def __setBrowsersAvailable(self, browserFlags): """ Private method to add selected browsers to the project browser. @@ -176,55 +253,83 @@ self.removeTab(0) # step 2: add browsers + # TODO: change the logic after browser flags have been eliminated if browserFlags & SourcesBrowserFlag: index = self.addTab( - self.__browsers["sources"], - EricPixmapCache.getIcon("projectSources"), + self.__browserRepository["sources"].projectBrowser, + self.__browserRepository["sources"].getIcon(), "", ) - self.setTabToolTip(index, self.__browsers["sources"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["sources"].projectBrowser.windowTitle(), + ) if browserFlags & FormsBrowserFlag: index = self.addTab( - self.__browsers["forms"], EricPixmapCache.getIcon("projectForms"), "" + self.__browserRepository["forms"].projectBrowser, + self.__browserRepository["forms"].getIcon(), + "", ) - self.setTabToolTip(index, self.__browsers["forms"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["forms"].projectBrowser.windowTitle(), + ) if browserFlags & ResourcesBrowserFlag: index = self.addTab( - self.__browsers["resources"], - EricPixmapCache.getIcon("projectResources"), + self.__browserRepository["resources"].projectBrowser, + self.__browserRepository["resources"].getIcon(), "", ) - self.setTabToolTip(index, self.__browsers["resources"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["resources"].projectBrowser.windowTitle(), + ) if browserFlags & TranslationsBrowserFlag: index = self.addTab( - self.__browsers["translations"], - EricPixmapCache.getIcon("projectTranslations"), + self.__browserRepository["translations"].projectBrowser, + self.__browserRepository["translations"].getIcon(), "", ) - self.setTabToolTip(index, self.__browsers["translations"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["translations"].projectBrowser.windowTitle(), + ) if browserFlags & InterfacesBrowserFlag: index = self.addTab( - self.__browsers["interfaces"], - EricPixmapCache.getIcon("projectInterfaces"), + self.__browserRepository["interfaces"].projectBrowser, + self.__browserRepository["interfaces"].getIcon(), "", ) - self.setTabToolTip(index, self.__browsers["interfaces"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["interfaces"].projectBrowser.windowTitle(), + ) if browserFlags & ProtocolsBrowserFlag: index = self.addTab( - self.__browsers["protocols"], EricPixmapCache.getIcon("protobuf"), "" + self.__browserRepository["protocols"].projectBrowser, + self.__browserRepository["protocols"].getIcon(), + "", ) - self.setTabToolTip(index, self.__browsers["protocols"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["protocols"].projectBrowser.windowTitle(), + ) if browserFlags & OthersBrowserFlag: index = self.addTab( - self.__browsers["others"], EricPixmapCache.getIcon("projectOthers"), "" + self.__browserRepository["others"].projectBrowser, + self.__browserRepository["others"].getIcon(), + "", ) - self.setTabToolTip(index, self.__browsers["others"].windowTitle()) + self.setTabToolTip( + index, + self.__browserRepository["others"].projectBrowser.windowTitle(), + ) QApplication.processEvents() @@ -283,6 +388,7 @@ self.__setSourcesIcon() + # TODO: move the logic to determine the icon to the sources browser. def __setSourcesIcon(self): """ Private method to set the right icon for the sources browser tab. @@ -306,7 +412,7 @@ icon = EricPixmapCache.getIcon("projectSourcesJavaScript") else: icon = EricPixmapCache.getIcon("projectSources") - self.setTabIcon(self.indexOf(self.__browsers["sources"]), icon) + self.setTabIcon(self.indexOf(self.getProjectBrowser("sources")), icon) def handleEditorChanged(self, fn): """ @@ -322,7 +428,7 @@ if cat not in ("TRANSLATIONS", "OTHERS") ): if self.project.isProjectCategory(fn, fileCategory): - self.__browsers[fileCategory.lower()].selectFile(fn) + self.getProjectBrowser(fileCategory.lower()).selectFile(fn) break def handleEditorLineChanged(self, fn, lineno): @@ -337,36 +443,7 @@ and Preferences.getProject("FollowCursorLine") and self.project.isProjectCategory(fn, "SOURCES") ): - self.__browsers["sources"].selectFileLine(fn, lineno) - - def getProjectBrowsers(self): - """ - Public method to get references to the individual project browsers. - - @return list of references to project browsers - @rtype list of ProjectBaseBrowser - """ - return list(self.__browsers.items()) - - def getProjectBrowser(self, name): - """ - Public method to get a reference to the named project browser. - - @param name name of the requested project browser. - @type str - @return reference to the requested browser or None - @rtype ProjectBaseBrowser or None - """ - return self.__browsers.get(name, None) - - def getProjectBrowserNames(self): - """ - Public method to get the names of the various project browsers. - - @return list of project browser names - @rtype list of str - """ - return list(self.__browsers.keys()) + self.getProjectBrowser("sources").selectFileLine(fn, lineno) def handlePreferencesChanged(self): """