diff -r 765081822a29 -r 9288af505740 PluginPrintRemover.py --- a/PluginPrintRemover.py Fri Apr 18 16:48:41 2014 +0200 +++ b/PluginPrintRemover.py Wed Apr 23 18:40:28 2014 +0200 @@ -12,7 +12,7 @@ import os from PyQt4.QtCore import QObject, QTranslator, QCoreApplication -from PyQt4.QtGui import QAction +from PyQt4.QtGui import QAction, QMenu from E5Gui.E5Application import e5App @@ -23,7 +23,7 @@ author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True -version = "0.5.0" +version = "0.6.0" className = "PrintRemoverPlugin" packageName = "PrintRemover" shortDescription = "Remove print() like debug statements." @@ -35,6 +35,7 @@ """ string '__NO_REMOVE__' are preserved.""" needsRestart = False pyqtApi = 2 +python2Compatible = True # End-Of-Header error = "" @@ -104,6 +105,8 @@ self.__translator = None self.__loadTranslator() + self.__initMenu() + self.__editors = {} def activate(self): @@ -193,6 +196,14 @@ Preferences.Prefs.settings.setValue( self.PreferencesKey + "/" + key, value) + def __initMenu(self): + """ + Private method to initialize the menu. + """ + self.__menu = QMenu("Remove Outputs") + self.__menu.setEnabled(False) + self.__menu.aboutToShow.connect(self.__showMenu) + def __populateMenu(self, name, menu): """ Private slot to populate the tools menu with our entries. @@ -208,13 +219,8 @@ if not menu.isEmpty(): menu.addSeparator() - # TODO: change this to a dynamically built submenu - for string in self.getPreferences("StartswithStrings"): - act = menu.addAction( - self.tr("Remove '{0}'").format(string), - self.__removeLine) - act.setData(string) - act.setEnabled(editor is not None) + act = menu.addMenu(self.__menu) + act.setEnabled(editor is not None) def __editorOpened(self, editor): """ @@ -222,19 +228,15 @@ @param editor reference to the new editor (QScintilla.Editor) """ - # TODO: change this to a dynamically built submenu menu = editor.getMenu("Tools") if menu is not None: self.__editors[editor] = [] if not menu.isEmpty(): act = menu.addSeparator() self.__editors[editor].append(act) - for string in self.getPreferences("StartswithStrings"): - act = menu.addAction( - self.tr("Remove '{0}'").format(string), - self.__removeLine) - act.setData(string) - self.__editors[editor].append(act) + act = menu.addMenu(self.__menu) + self.__menu.setEnabled(True) + self.__editors[editor].append(act) def __editorClosed(self, editor): """ @@ -244,9 +246,25 @@ """ try: del self.__editors[editor] + if not self.__editors: + self.__menu.setEnabled(False) except KeyError: pass + def __showMenu(self): + """ + Private slot to build the menu hierarchy. + """ + self.__menu.clear() + for string in self.getPreferences("StartswithStrings"): + if string == '--Separator--': + self.__menu.addSeparator() + else: + act = self.__menu.addAction( + self.tr("Remove '{0}'").format(string), + self.__removeLine) + act.setData(string) + def __removeLine(self): """ Private slot to remove lines starting with the selected pattern.