PluginColorString.py

branch
eric7
changeset 48
e35a2acef5b8
parent 45
1ba3620cae56
child 51
c02cbedf626d
--- a/PluginColorString.py	Fri May 28 18:27:40 2021 +0200
+++ b/PluginColorString.py	Fri May 28 18:35:49 2021 +0200
@@ -10,19 +10,19 @@
 import contextlib
 import os
 
-from PyQt5.QtCore import QObject, QTranslator
-from PyQt5.QtGui import QColor
-from PyQt5.QtWidgets import QColorDialog, QMenu, QDialog
+from PyQt6.QtCore import QObject, QTranslator
+from PyQt6.QtGui import QColor
+from PyQt6.QtWidgets import QColorDialog, QMenu, QDialog
 
-from E5Gui.E5Application import e5App
-from E5Gui import E5MessageBox
+from EricWidgets.EricApplication import ericApp
+from EricWidgets import EricMessageBox
 
 # Start-Of-Header
 name = "Color String Plug-in"
 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
 autoactivate = True
 deactivateable = True
-version = "3.2.0"
+version = "1.0.0"
 className = "ColorStringPlugin"
 packageName = "ColorString"
 shortDescription = "Insert color as string"
@@ -47,7 +47,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
@@ -64,7 +65,8 @@
         """
         Public method to activate this plugin.
         
-        @return tuple of None and activation status (boolean)
+        @return tuple of None and activation status
+        @rtype tuple of (None, bool)
         """
         global error
         error = ""     # clear previous error
@@ -79,12 +81,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
@@ -101,9 +103,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():
@@ -128,7 +130,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))
@@ -148,13 +150,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():
@@ -168,7 +172,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:
@@ -185,7 +190,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]
@@ -197,9 +203,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" and
@@ -217,45 +226,38 @@
         """
         Private method to check, if a given text is a hex string.
         
-        @param text text to check (string)
-        @return flag indicating a hex string (boolean)
+        @param text text to check
+        @type str
+        @return flag indicating a hex string
+        @rtype bool
         """
-        return all(map(lambda c: c in "0123456789abcdefABCDEF", text))
+        return all(c in "0123456789abcdefABCDEF" for c in text)
     
     def __isValidColor(self, name):
         """
         Private method to check for a valid color name.
         
-        @param name color name to check (string)
-        @return flag indicating a valid color name (boolean)
+        @param name color name to check
+        @type str
+        @return flag indicating a valid color name
+        @rtype bool
         """
-        try:
-            if self.__isHexString(name) and len(name) in [3, 6, 9, 12]:
-                return True
-            return QColor.isValidColor(name)
-        except AttributeError:
-            if name.startswith("#"):
-                if len(name) not in [4, 7, 10, 13]:
-                    return False
-                hexCheckStr = name[1:]
-                return self.__isHexString(hexCheckStr)
-            else:
-                if self.__isHexString(name) and len(name) in [3, 6, 9, 12]:
-                    return True
-                return name in QColor.colorNames()
+        if self.__isHexString(name) and len(name) in (3, 6, 8, 9, 12):
+            return True
+        return QColor.isValidColor(name)
     
     def __selectHexColor(self):
         """
         Private slot implementing the hex color string selection.
         """
-        editor = e5App().getObject("ViewManager").activeWindow()
+        editor = ericApp().getObject("ViewManager").activeWindow()
         if editor is None:
             return
         
         if editor.hasSelectedText():
             currColor = editor.selectedText()
             if not self.__isValidColor(currColor):
-                E5MessageBox.critical(
+                EricMessageBox.critical(
                     self.__ui,
                     self.tr("Color String"),
                     self.tr(
@@ -278,9 +280,14 @@
             initColor = QColor()
         
         color = QColorDialog.getColor(
-            initColor, self.__ui, self.tr("Color String"))
+            initColor, self.__ui, self.tr("Color String"),
+            QColorDialog.ColorDialogOption.ShowAlphaChannel)
         if color.isValid():
-            colorStr = color.name()
+            if color.alpha() == 255:
+                nameFormat = QColor.NameFormat.HexRgb
+            else:
+                nameFormat = QColor.NameFormat.HexArgb
+            colorStr = color.name(nameFormat)
             if not withHash:
                 colorStr = colorStr[1:]
             editor.beginUndoAction()
@@ -296,14 +303,14 @@
         """
         Private slot implementing the named color string selection.
         """
-        editor = e5App().getObject("ViewManager").activeWindow()
+        editor = ericApp().getObject("ViewManager").activeWindow()
         if editor is None:
             return
         
         if editor.hasSelectedText():
             currColor = editor.selectedText()
             if currColor not in QColor.colorNames():
-                E5MessageBox.critical(
+                EricMessageBox.critical(
                     self.__ui,
                     self.tr("Color String"),
                     self.tr(
@@ -316,7 +323,7 @@
         
         from ColorString.ColorSelectionDialog import ColorSelectionDialog
         dlg = ColorSelectionDialog(currColor, self.__ui)
-        if dlg.exec() == QDialog.Accepted:
+        if dlg.exec() == QDialog.DialogCode.Accepted:
             colorStr = dlg.getColor()
             editor.beginUndoAction()
             if editor.hasSelectedText():
@@ -331,7 +338,7 @@
         """
         Private slot implementing the RGBA color string selection.
         """
-        editor = e5App().getObject("ViewManager").activeWindow()
+        editor = ericApp().getObject("ViewManager").activeWindow()
         if editor is None:
             return
         
@@ -339,12 +346,12 @@
             currColor = editor.selectedText()
             valid, rgba = self.__isValidRgbaColor(currColor)
             if not valid:
-                E5MessageBox.critical(
+                EricMessageBox.critical(
                     self.__ui,
                     self.tr("Color String"),
                     self.tr(
                         """<p>The selected string <b>{0}</b> is not a"""
-                        """ valid color string. Aborting!</p>""")
+                        """ valid RGBA color string. Aborting!</p>""")
                     .format(currColor))
                 return
             initColor = QColor(*rgba)
@@ -353,7 +360,7 @@
         
         color = QColorDialog.getColor(
             initColor, self.__ui, self.tr("Color String"),
-            QColorDialog.ShowAlphaChannel)
+            QColorDialog.ColorDialogOption.ShowAlphaChannel)
         if color.isValid():
             rgba = color.getRgb()
             if rgba[-1] == 255:
@@ -373,9 +380,11 @@
         """
         Private method to check for a valid RGBA color.
         
-        @param color color string to check (string)
+        @param color color string to check
+        @type str
         @return flag indicating a valid RGBA color (boolean) and a list with
             the RGBA components of the color (three or four integers)
+        @rtype tuple of (bool, [int, int, int]) or (bool, [int, int, int, int])
         """
         rgba = []
         

eric ide

mercurial