Continued implementing a format button bar and provider classes for various markup languages.

Fri, 06 Jan 2017 20:00:52 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 06 Jan 2017 20:00:52 +0100
changeset 5402
ce21a78a5fcf
parent 5401
dbbbd94aec0b
child 5403
d6b43ecf2488

Continued implementing a format button bar and provider classes for various markup languages.

QScintilla/EditorButtonsWidget.py file | annotate | diff | comparison | revisions
QScintilla/MarkupProviders/HtmlProvider.py file | annotate | diff | comparison | revisions
QScintilla/MarkupProviders/MarkdownProvider.py file | annotate | diff | comparison | revisions
QScintilla/MarkupProviders/MarkupBase.py file | annotate | diff | comparison | revisions
QScintilla/MarkupProviders/RestructuredTextProvider.py file | annotate | diff | comparison | revisions
--- a/QScintilla/EditorButtonsWidget.py	Fri Jan 06 18:28:14 2017 +0100
+++ b/QScintilla/EditorButtonsWidget.py	Fri Jan 06 20:00:52 2017 +0100
@@ -11,7 +11,7 @@
 from __future__ import unicode_literals
 
 from PyQt5.QtCore import pyqtSlot
-from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame
+from PyQt5.QtWidgets import QWidget, QVBoxLayout, QToolButton, QFrame, QMenu
 
 import UI.PixmapCache
 
@@ -60,6 +60,7 @@
         """
         self.__buttons = {}
         self.__separators = []
+        self.__headerMenu = QMenu()
         
         self.__addButton("bold", "formatTextBold.png")
         self.__addButton("italic", "formatTextItalic.png")
@@ -70,6 +71,11 @@
         self.__addButton("header3", "formatTextHeader3.png")
         button = self.__addButton("header", "formatTextHeader.png")
         button.setPopupMode(QToolButton.InstantPopup)
+        button.setMenu(self.__headerMenu)
+        self.__addSeparator()
+        self.__addButton("code", "formatTextInlineCode.png")
+        
+        self.__headerMenu.triggered.connect(self.__headerMenuTriggered)
     
     def __addButton(self, format, iconName):
         """
@@ -119,12 +125,22 @@
             self.__buttons["italic"].setEnabled(self.__provider.hasItalic())
             self.__buttons["strikethrough"].setEnabled(
                 self.__provider.hasStrikethrough())
+            
             headerLevels = self.__provider.headerLevels()
             self.__buttons["header1"].setEnabled(headerLevels >= 1)
             self.__buttons["header2"].setEnabled(headerLevels >= 2)
             self.__buttons["header3"].setEnabled(headerLevels >= 3)
             self.__buttons["header"].setEnabled(headerLevels > 3)
-            # TODO: create header button menu
+            self.__headerMenu.clear()
+            for level in range(1, headerLevels + 1):
+                act = self.__headerMenu.addAction(
+                    self.tr("Level {0}").format(level))
+                act.setData("header{0}".format(level))
+            
+            self.__buttons["code"].setEnabled(self.__provider.hasCode())
+            
+            # TODO: make this configurable
+            self.setVisible(self.__provider.kind() != "none")
     
     def __formatClicked(self, format):
         """
@@ -144,5 +160,16 @@
                 level = int(format[-1])
                 self.__provider.header(self.__editor, level)
             except ValueError:
-                # TODO: implement this
                 pass
+        elif format == "code":
+            self.__provider.code(self.__editor)
+    
+    def __headerMenuTriggered(self, act):
+        """
+        Private method handling the selection of a header menu entry.
+        
+        @param act action of the headers menu that was triggered
+        @type QAction
+        """
+        format = act.data()
+        self.__formatClicked(format)
--- a/QScintilla/MarkupProviders/HtmlProvider.py	Fri Jan 06 18:28:14 2017 +0100
+++ b/QScintilla/MarkupProviders/HtmlProvider.py	Fri Jan 06 20:00:52 2017 +0100
@@ -40,6 +40,15 @@
         """
         return True
     
+    def bold(self, editor):
+        """
+        Public method to generate bold text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("b", editor)
+    
     def hasItalic(self):
         """
         Public method to indicate the availability of italic markup.
@@ -49,6 +58,15 @@
         """
         return True
     
+    def italic(self, editor):
+        """
+        Public method to generate italic text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("i", editor)
+    
     def hasStrikethrough(self):
         """
         Public method to indicate the availability of strikethrough markup.
@@ -58,6 +76,15 @@
         """
         return True
     
+    def strikethrough(self, editor):
+        """
+        Public method to generate strikethrough text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("del", editor)
+    
     def headerLevels(self):
         """
         Public method to determine the available header levels.
@@ -67,33 +94,6 @@
         """
         return 6
     
-    def bold(self, editor):
-        """
-        Public method to generate bold text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("b", editor)
-    
-    def italic(self, editor):
-        """
-        Public method to generate italic text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("i", editor)
-    
-    def strikethrough(self, editor):
-        """
-        Public method to generate strikethrough text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("del", editor)
-    
     def header(self, editor, level):
         """
         Public method to generate a header.
@@ -106,6 +106,24 @@
         if level <= 6:
             self.__insertMarkup("h{0}".format(level), editor)
     
+    def hasCode(self):
+        """
+        Public method to indicate the availability of inline code markup.
+        
+        @return flag indicating the availability of inline code markup
+        @rtype bool
+        """
+        return True
+    
+    def code(self, editor):
+        """
+        Public method to generate inline code text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("code", editor)
+    
     def __insertMarkup(self, markup, editor):
         """
         Private method to insert the specified markup.
--- a/QScintilla/MarkupProviders/MarkdownProvider.py	Fri Jan 06 18:28:14 2017 +0100
+++ b/QScintilla/MarkupProviders/MarkdownProvider.py	Fri Jan 06 20:00:52 2017 +0100
@@ -40,6 +40,15 @@
         """
         return True
     
+    def bold(self, editor):
+        """
+        Public method to generate bold text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("**", editor)
+    
     def hasItalic(self):
         """
         Public method to indicate the availability of italic markup.
@@ -49,6 +58,15 @@
         """
         return True
     
+    def italic(self, editor):
+        """
+        Public method to generate italic text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("_", editor)
+    
     def hasStrikethrough(self):
         """
         Public method to indicate the availability of strikethrough markup.
@@ -58,6 +76,15 @@
         """
         return True
     
+    def strikethrough(self, editor):
+        """
+        Public method to generate strikethrough text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("~~", editor)
+    
     def headerLevels(self):
         """
         Public method to determine the available header levels.
@@ -67,33 +94,6 @@
         """
         return 6
     
-    def bold(self, editor):
-        """
-        Public method to generate bold text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("**", editor)
-    
-    def italic(self, editor):
-        """
-        Public method to generate italic text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("_", editor)
-    
-    def strikethrough(self, editor):
-        """
-        Public method to generate strikethrough text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("~~", editor)
-    
     def header(self, editor, level):
         """
         Public method to generate a header.
@@ -112,6 +112,24 @@
         editor.setCursorPosition(cline, level + 1)
         editor.endUndoAction()
     
+    def hasCode(self):
+        """
+        Public method to indicate the availability of inline code markup.
+        
+        @return flag indicating the availability of inline code markup
+        @rtype bool
+        """
+        return True
+    
+    def code(self, editor):
+        """
+        Public method to generate inline code text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("`", editor)
+    
     def __insertMarkup(self, markup, editor):
         """
         Private method to insert the specified markup.
--- a/QScintilla/MarkupProviders/MarkupBase.py	Fri Jan 06 18:28:14 2017 +0100
+++ b/QScintilla/MarkupProviders/MarkupBase.py	Fri Jan 06 20:00:52 2017 +0100
@@ -42,6 +42,15 @@
         """
         return False
     
+    def bold(self, editor):
+        """
+        Public method to generate bold text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        pass
+    
     def hasItalic(self):
         """
         Public method to indicate the availability of italic markup.
@@ -51,6 +60,15 @@
         """
         return False
     
+    def italic(self, editor):
+        """
+        Public method to generate italic text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        pass
+    
     def hasStrikethrough(self):
         """
         Public method to indicate the availability of strikethrough markup.
@@ -60,6 +78,15 @@
         """
         return False
     
+    def strikethrough(self, editor):
+        """
+        Public method to generate strikethrough text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        pass
+    
     def headerLevels(self):
         """
         Public method to determine the available header levels.
@@ -69,33 +96,6 @@
         """
         return 0
     
-    def bold(self, editor):
-        """
-        Public method to generate bold text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        pass
-    
-    def italic(self, editor):
-        """
-        Public method to generate italic text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        pass
-    
-    def strikethrough(self, editor):
-        """
-        Public method to generate strikethrough text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        pass
-    
     def header(self, editor, level):
         """
         Public method to generate a header.
@@ -106,3 +106,21 @@
         @type int
         """
         pass
+    
+    def hasCode(self):
+        """
+        Public method to indicate the availability of inline code markup.
+        
+        @return flag indicating the availability of inline code markup
+        @rtype bool
+        """
+        return False
+    
+    def code(self, editor):
+        """
+        Public method to generate inline code text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        pass
--- a/QScintilla/MarkupProviders/RestructuredTextProvider.py	Fri Jan 06 18:28:14 2017 +0100
+++ b/QScintilla/MarkupProviders/RestructuredTextProvider.py	Fri Jan 06 20:00:52 2017 +0100
@@ -42,6 +42,15 @@
         """
         return True
     
+    def bold(self, editor):
+        """
+        Public method to generate bold text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("**", editor)
+    
     def hasItalic(self):
         """
         Public method to indicate the availability of italic markup.
@@ -51,6 +60,15 @@
         """
         return True
     
+    def italic(self, editor):
+        """
+        Public method to generate italic text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("*", editor)
+    
     def headerLevels(self):
         """
         Public method to determine the available header levels.
@@ -60,24 +78,6 @@
         """
         return len(self.__headerChars)
     
-    def bold(self, editor):
-        """
-        Public method to generate bold text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("**", editor)
-    
-    def italic(self, editor):
-        """
-        Public method to generate italic text.
-        
-        @param editor reference to the editor to work on
-        @type Editor
-        """
-        self.__insertMarkup("*", editor)
-    
     def header(self, editor, level):
         """
         Public method to generate a header.
@@ -102,6 +102,24 @@
         editor.setCursorPosition(cline + 2, 0)
         editor.endUndoAction()
     
+    def hasCode(self):
+        """
+        Public method to indicate the availability of inline code markup.
+        
+        @return flag indicating the availability of inline code markup
+        @rtype bool
+        """
+        return True
+    
+    def code(self, editor):
+        """
+        Public method to generate inline code text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        self.__insertMarkup("``", editor)
+    
     def __insertMarkup(self, markup, editor):
         """
         Private method to insert the specified markup.

eric ide

mercurial