Editor: added a context menu to select spell check language.

Thu, 24 Oct 2019 19:30:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 24 Oct 2019 19:30:32 +0200
changeset 7310
460a332dcc56
parent 7309
5a434813eef3
child 7311
ead752e32ffa

Editor: added a context menu to select spell check language.

eric6/QScintilla/Editor.py file | annotate | diff | comparison | revisions
--- a/eric6/QScintilla/Editor.py	Thu Oct 24 19:30:01 2019 +0200
+++ b/eric6/QScintilla/Editor.py	Thu Oct 24 19:30:32 2019 +0200
@@ -29,6 +29,7 @@
 
 from .QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION
 from .EditorMarkerMap import EditorMarkerMap
+from .SpellChecker import SpellChecker
 
 import Preferences
 import Utilities
@@ -82,6 +83,8 @@
         string is passed as a parameter.
     @signal encodingChanged(str) emitted when the editors encoding was set. The
             encoding name is passed as a parameter.
+    @signal spellLanguageChanged(str) emitted when the editor spell check
+            language was set. The language is passed as a parameter.
     @signal lastEditPositionAvailable() emitted when a last edit position is
         available
     @signal refreshed() emitted to signal a refresh of the editor contents
@@ -110,6 +113,7 @@
     languageChanged = pyqtSignal(str)
     eolChanged = pyqtSignal(str)
     encodingChanged = pyqtSignal(str)
+    spellLanguageChanged = pyqtSignal(str)
     lastEditPositionAvailable = pyqtSignal()
     refreshed = pyqtSignal()
     settingsRead = pyqtSignal()
@@ -480,10 +484,13 @@
         if editor is None:
             self.__checkLanguage()
             self.__checkEncoding()
+            self.__checkSpellLanguage()
         else:
             # it's a clone
             self.__languageChanged(editor.apiLanguage, propagate=False)
             self.__encodingChanged(editor.encoding, propagate=False)
+            self.__spellLanguageChanged(editor.getSpellingLanguage(),
+                                        propagate=False)
         
         self.setAcceptDrops(True)
         
@@ -592,6 +599,7 @@
         editor.languageChanged.connect(self.languageChanged)
         editor.eolChanged.connect(self.__eolChanged)
         editor.encodingChanged.connect(self.__encodingChanged)
+        editor.spellLanguageChanged.connect(self.__spellLanguageChanged)
         
     def removeClone(self, editor):
         """
@@ -605,6 +613,7 @@
             editor.languageChanged.disconnect(self.languageChanged)
             editor.eolChanged.disconnect(self.__eolChanged)
             editor.encodingChanged.disconnect(self.__encodingChanged)
+            editor.spellLanguageChanged.disconnect(self.__spellLanguageChanged)
             self.__clones.remove(editor)
         
     def isClone(self, editor):
@@ -763,6 +772,8 @@
         self.__menus["Eol"] = self.eolMenu
         self.encodingsMenu = self.__initContextMenuEncodings()
         self.__menus["Encodings"] = self.encodingsMenu
+        self.spellLanguagesMenu = self.__initContextMenuSpellLanguages()
+        self.__menus["SpellLanguages"] = self.spellLanguagesMenu
         
         self.menuActs["Undo"] = self.menu.addAction(
             UI.PixmapCache.getIcon("editUndo.png"),
@@ -825,6 +836,8 @@
         self.menuActs["SpellCheckRemove"] = self.menu.addAction(
             self.tr("Remove from dictionary"),
             self.__removeFromSpellingDictionary)
+        self.menuActs["SpellCheckLanguages"] = self.menu.addMenu(
+            self.spellLanguagesMenu)
         self.menu.addSeparator()
         self.menu.addAction(
             self.tr('Shorten empty lines'), self.shortenEmptyLines)
@@ -1142,7 +1155,39 @@
         menu.aboutToShow.connect(self.__showContextMenuEol)
         
         return menu
-        
+    
+    def __initContextMenuSpellLanguages(self):
+        """
+        Private method to setup the spell checking languages context sub menu.
+        
+        @return reference to the generated menu
+        @rtype QMenu
+        """
+        self.supportedSpellLanguages = {}
+        
+        menu = QMenu(self.tr("Spell Check Languages"))
+        
+        self.spellLanguagesActGrp = QActionGroup(self)
+        
+        self.noSpellLanguageAct = menu.addAction(
+            self.tr("No Language"))
+        self.noSpellLanguageAct.setCheckable(True)
+        self.noSpellLanguageAct.setData("")
+        self.spellLanguagesActGrp.addAction(self.noSpellLanguageAct)
+        menu.addSeparator()
+        
+        for language in sorted(SpellChecker.getAvailableLanguages()):
+            act = menu.addAction(language)
+            act.setCheckable(True)
+            act.setData(language)
+            self.supportedSpellLanguages[language] = act
+            self.spellLanguagesActGrp.addAction(act)
+        
+        menu.triggered.connect(self.__spellLanguagesMenuTriggered)
+        menu.aboutToShow.connect(self.__showContextMenuSpellLanguages)
+        
+        return menu
+    
     def __initContextMenuExporters(self):
         """
         Private method used to setup the Exporters context sub menu.
@@ -1613,7 +1658,52 @@
             eol = self.getLineSeparator()
             self.eolChanged.emit(eol)
             self.inEolChanged = False
-        
+    
+    def __showContextMenuSpellLanguages(self):
+        """
+        Private slot handling the aboutToShow signal of the spell check
+        languages context menu.
+        """
+        self.showMenu.emit("SpellLanguage", self.spellLanguagesMenu, self)
+    
+    def __spellLanguagesMenuTriggered(self, act):
+        """
+        Private method to handle the selection of a spell check language.
+        
+        @param act reference to the action that was triggered
+        @type QAction
+        """
+        language = act.data()
+        self.__setSpellingLanguage(language)
+        self.spellLanguageChanged.emit(language)
+    
+    def __checkSpellLanguage(self):
+        """
+        Private slot to check the selected spell check language action.
+        """
+        language = self.getSpellingLanguage()
+        try:
+            self.supportedSpellLanguages[language].setChecked(True)
+        except (AttributeError, KeyError):
+            pass
+    
+    def __spellLanguageChanged(self, language, propagate=True):
+        """
+        Private slot to handle a change of the spell check language.
+        
+        @param language new spell check language
+        @type str
+        @param propagate flag indicating to propagate the change
+        @type bool
+        """
+        self.__setSpellingLanguage(language)
+        self.__checkSpellLanguage(language)
+        
+        if not self.__inSpellLanguageChanged and propagate:
+            self.__inSpellLanguageChanged = True
+            self.spellLanguageChanged.emit(language)
+            self.__inSpellLanguageChanged = False
+    
     def __bindLexer(self, filename, pyname=""):
         """
         Private slot to set the correct lexer depending on language.
@@ -5367,13 +5457,13 @@
             self.menuActs["codeInfo"].setEnabled(
                 self.vm.isEditorInfoSupported(self.getLanguage()))
         
-        from .SpellChecker import SpellChecker
         spellingAvailable = SpellChecker.isAvailable()
         self.menuActs["SpellCheck"].setEnabled(spellingAvailable)
         self.menuActs["SpellCheckSelection"].setEnabled(
             spellingAvailable and self.hasSelectedText())
         self.menuActs["SpellCheckRemove"].setEnabled(
             spellingAvailable and self.spellingMenuPos >= 0)
+        self.menuActs["SpellCheckLanguages"].setEnabled(spellingAvailable)
         
         if self.menuActs["OpenRejections"]:
             if self.fileName:
@@ -7564,6 +7654,18 @@
     ## Spell checking related methods
     #######################################################################
     
+    def getSpellingLanguage(self):
+        """
+        Public method to get the current spelling language.
+        
+        @return current spelling language
+        @rtype str
+        """
+        if self.spell:
+            return self.spell.getLanguage()
+        
+        return ""
+    
     def __setSpellingLanguage(self, language, pwl="", pel=""):
         """
         Private slot to set the spell checking language.
@@ -7584,7 +7686,6 @@
             self.__spellCheckStringsOnly = Preferences.getEditor(
                 "SpellCheckStringsOnly")
             if self.spell is None:
-                from .SpellChecker import SpellChecker
                 self.spell = SpellChecker(self, self.spellingIndicator,
                                           checkRegion=self.isSpellCheckRegion)
             self.setSpellingForProject()

eric ide

mercurial