PluginSelectionEncloser.py

branch
eric7
changeset 51
318d7ebbdce2
parent 48
767eb5905e08
child 52
d2119f1dd5b3
--- a/PluginSelectionEncloser.py	Tue Jun 01 18:01:14 2021 +0200
+++ b/PluginSelectionEncloser.py	Tue Jun 01 18:45:45 2021 +0200
@@ -11,10 +11,11 @@
 import os
 import json
 
-from PyQt5.QtCore import QObject, QTranslator, QCoreApplication
-from PyQt5.QtWidgets import QAction, QMenu
+from PyQt6.QtCore import pyqtSlot, QObject, QTranslator, QCoreApplication
+from PyQt6.QtGui import QAction
+from PyQt6.QtWidgets import QMenu
 
-from E5Gui.E5Application import e5App
+from EricWidgets.EricApplication import ericApp
 
 import Preferences
 
@@ -23,7 +24,7 @@
 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
 autoactivate = True
 deactivateable = True
-version = "3.2.1"
+version = "1.0.0"
 className = "SelectionEncloserPlugin"
 packageName = "SelectionEncloser"
 shortDescription = "Enclose the selection with a string."
@@ -46,7 +47,9 @@
     Module function to create the Selection Encloser configuration page.
     
     @param configDlg reference to the configuration dialog
+    @type ConfigurationWidget
     @return reference to the configuration page
+    @rtype SelectionEncloserPage
     """
     global selectionEncloserPluginObject
     from SelectionEncloser.ConfigurationPage.SelectionEncloserPage import (
@@ -60,15 +63,9 @@
     Module function returning data as required by the configuration dialog.
     
     @return dictionary containing the relevant data
+    @rtype dict
     """
-    try:
-        usesDarkPalette = e5App().usesDarkPalette()
-    except AttributeError:
-        # for eric6 < 20.4
-        from PyQt5.QtGui import QPalette
-        palette = e5App().palette()
-        lightness = palette.color(QPalette.Window).lightness()
-        usesDarkPalette = lightness <= 128
+    usesDarkPalette = ericApp().usesDarkPalette()
     iconSuffix = "dark" if usesDarkPalette else "light"
     
     return {
@@ -98,7 +95,8 @@
         """
         Constructor
         
-        @param ui reference to the user interface object (UI.UserInterface)
+        @param ui reference to the user interface object
+        @type UserInterface
         """
         super().__init__(ui)
         self.__ui = ui
@@ -140,7 +138,8 @@
         """
         Public method to activate this plugin.
         
-        @return tuple of None and activation status (boolean)
+        @return tuple of None and activation status
+        @rtype (None, bool)
         """
         global error
         error = ""     # clear previous error
@@ -158,12 +157,12 @@
             act = menu.addMenu(self.__menu)
             self.__mainActions.append(act)
         
-        e5App().getObject("ViewManager").editorOpenedEd.connect(
+        ericApp().getObject("ViewManager").editorOpenedEd.connect(
             self.__editorOpened)
-        e5App().getObject("ViewManager").editorClosedEd.connect(
+        ericApp().getObject("ViewManager").editorClosedEd.connect(
             self.__editorClosed)
         
-        for editor in e5App().getObject("ViewManager").getOpenEditors():
+        for editor in ericApp().getObject("ViewManager").getOpenEditors():
             self.__editorOpened(editor)
         
         return None, True
@@ -180,9 +179,9 @@
                 menu.removeAction(act)
         self.__mainActions = []
 
-        e5App().getObject("ViewManager").editorOpenedEd.disconnect(
+        ericApp().getObject("ViewManager").editorOpenedEd.disconnect(
             self.__editorOpened)
-        e5App().getObject("ViewManager").editorClosedEd.disconnect(
+        ericApp().getObject("ViewManager").editorClosedEd.disconnect(
             self.__editorClosed)
         
         for editor, acts in self.__editors.items():
@@ -207,7 +206,7 @@
                 loaded = translator.load(translation, locale_dir)
                 if loaded:
                     self.__translator = translator
-                    e5App().installTranslator(self.__translator)
+                    ericApp().installTranslator(self.__translator)
                 else:
                     print("Warning: translation file '{0}' could not be"
                           " loaded.".format(translation))
@@ -217,8 +216,10 @@
         """
         Public method to retrieve the various settings.
         
-        @param key the key of the value to get (string)
-        @return the requested setting
+        @param key key of the value to get
+        @type str
+        @return value of the requested setting
+        @rtype Any
         """
         if key in ["MenuHierarchy"]:
             return json.loads(
@@ -232,8 +233,10 @@
         """
         Public method to store the various settings.
         
-        @param key the key of the setting to be set (string)
-        @param value the value to be set
+        @param key key of the setting to be set
+        @type str
+        @param value value to be set
+        @type Any
         """
         if key in ["MenuHierarchy"]:
             Preferences.Prefs.settings.setValue(
@@ -254,13 +257,15 @@
         """
         Private slot to populate the tools menu with our entry.
         
-        @param name name of the menu (string)
-        @param menu reference to the menu to be populated (QMenu)
+        @param name name of the menu
+        @type str
+        @param menu reference to the menu to be populated
+        @type QMenu
         """
         if name not in ["Tools", "PluginTools"]:
             return
         
-        editor = e5App().getObject("ViewManager").activeWindow()
+        editor = ericApp().getObject("ViewManager").activeWindow()
         
         if name == "Tools":
             if not menu.isEmpty():
@@ -275,7 +280,8 @@
         """
         Private slot called, when a new editor was opened.
         
-        @param editor reference to the new editor (QScintilla.Editor)
+        @param editor reference to the new editor
+        @type Editor
         """
         menu = editor.getMenu("Tools")
         if menu is not None:
@@ -292,7 +298,8 @@
         """
         Private slot called, when an editor was closed.
         
-        @param editor reference to the editor (QScintilla.Editor)
+        @param editor reference to the editor
+        @type Editor
         """
         with contextlib.suppress(KeyError):
             del self.__editors[editor]
@@ -304,9 +311,12 @@
         Private slot called, when the the editor context menu or a submenu is
         about to be shown.
         
-        @param menuName name of the menu to be shown (string)
-        @param menu reference to the menu (QMenu)
+        @param menuName name of the menu to be shown
+        @type str
+        @param menu reference to the menu
+        @type QMenu
         @param editor reference to the editor
+        @type Editor
         """
         if menuName == "Tools":
             if self.__menu.menuAction() not in menu.actions():
@@ -335,35 +345,39 @@
                     if title == '--Separator--':
                         submenu.addSeparator()
                     else:
-                        act = submenu.addAction(title, self.__encloseSelection)
+                        act = submenu.addAction(title)
                         act.setData(encString)
+                submenu.triggered.connect(self.__encloseSelection)
                 self.__menu.addMenu(submenu)
     
-    def __encloseSelection(self):
+    @pyqtSlot(QAction)
+    def __encloseSelection(self, act):
         """
         Private slot to enclose the selection with the selected string.
+        
+        @param act action that triggered
+        @type QAction
         """
-        act = self.sender()
-        if act is None or not isinstance(act, QAction):
+        if act is None:
             return
         
-        editor = e5App().getObject("ViewManager").activeWindow()
+        editor = ericApp().getObject("ViewManager").activeWindow()
         if editor is None:
             return
         
         if not editor.hasSelectedText():
             return
         
-        string = act.data()
-        if not string:
+        encloseString = act.data()
+        if not encloseString:
             return
         
-        if '%s' in string:
-            newText = string % editor.selectedText()
-        elif '{0}' in string:
-            newText = string.format(editor.selectedText())
+        if '%s' in encloseString:
+            newText = encloseString % editor.selectedText()
+        elif '{0}' in encloseString or '{}' in encloseString:
+            newText = encloseString.format(editor.selectedText())
         else:
-            newText = string + editor.selectedText() + string
+            newText = encloseString + editor.selectedText() + encloseString
         editor.beginUndoAction()
         editor.replaceSelectedText(newText)
         editor.endUndoAction()

eric ide

mercurial