ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py

changeset 3
6d10c1249cb8
parent 2
4be31b0908c7
child 4
ba04ed0b14a1
--- a/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py	Sun Feb 02 20:02:23 2014 +0100
+++ b/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py	Mon Feb 03 19:54:07 2014 +0100
@@ -8,9 +8,11 @@
 """
 
 from PyQt4.QtCore import QObject
+from PyQt4.QtGui import QMenu, QInputDialog, QLineEdit
 
 from E5Gui.E5Application import e5App
 
+
 class DjangoTagsMenuHandler(QObject):
     """
     Class implementing the Django tags menu handler.
@@ -27,6 +29,13 @@
         
         self.__findDialog = None
     
+    def closeAllWindows(self):
+        """
+        Public method to close all dialogs managed by the handler.
+        """
+        if self.__findDialog:
+            self.__findDialog.close()
+    
     def initMenus(self, mainMenu):
         """
         Public method to initialize the various menus.
@@ -35,6 +44,42 @@
         """
         mainMenu.addAction(self.tr("Django Template Tags Locator"),
                            self.__findTemplateTag)
+        mainMenu.addSeparator()
+        mainMenu.addMenu(self.__initTagsMenu())
+    
+    def __initTagsMenu(self):
+        """
+        Private method to initialize the tags menu.
+        
+        @return generated menu (QMenu)
+        """
+        menu = QMenu(self.tr("Tags"))
+        menu.addAction(self.tr("autoescape - Auto Escape Characters"),
+                       lambda: self.__applyTemplate("autoescape"))
+        menu.addSeparator()
+        menu.addAction(self.tr("block - Named Block"),
+                       lambda: self.__applyTemplate("block"))
+        menu.addSeparator()
+        menu.addAction(self.tr("comment - Multiline Comment"),
+                       lambda: self.__applyTemplate("comment"))
+        menu.addAction(
+            self.tr( "csrf_token - Cross Site Request Forgery Token"),
+            lambda: self.__applyTemplate("csrf_token"))
+        menu.addAction(self.tr("cycle - Cycle variables each time used"),
+                       lambda: self.__applyTemplate("cycle"))
+        menu.addSeparator()
+        menu.addAction(self.tr("debug - Output Debug Information"),
+                       lambda: self.__applyTemplate("debug"))
+        menu.addSeparator()
+        menu.addAction(
+            self.tr("extends - Extend a template with variable contents"),
+            lambda: self.__applyTemplate("extendsvariable"))
+        menu.addAction(
+            self.tr("extends - Extend a template with file"),
+            lambda: self.__applyTemplate("extendsfile"))
+        
+        self.__tagsMenu = menu
+        return menu
     
     def __findTemplateTag(self):
         """
@@ -42,9 +87,11 @@
         """
         if self.__findDialog is None:
             from .FindTemplateTagDialog import FindTemplateTagDialog
-            self.__findDialog = FindTemplateTagDialog(self.__ui)
+            self.__findDialog = FindTemplateTagDialog()
             self.__findDialog.tag.connect(self.__applyTemplate)
         self.__findDialog.show()
+        self.__findDialog.raise_()
+        self.__findDialog.activateWindow()
     
     def __applyTemplate(self, tag):
         """
@@ -56,17 +103,88 @@
         if editor is None:
             return
         
-        templateText = self.__generateTemplateText(tag)
+        templateText, replace = self.__generateTemplateText(
+            tag, editor.selectedText())
         
         editor.beginUndoAction()
-        editor.replaceSelectedText(templateText)
+        if replace:
+            editor.replaceSelectedText(templateText)
+        else:
+            editor.insert(templateText)
         editor.endUndoAction()
     
-    def __generateTemplateText(self, tag):
+    def __generateTemplateText(self, tag, selectedText):
         """
         Private slot to generate the template text.
         
         @param tag name of the tag to insert (string)
+        @param selectedText selected text of the current editor (string)
+        @return tuple of generated template text (string) and a flag indicating
+            to perform a replace operation (boolean)
         """
-        # TODO: implement the tag generation logic
-        return "needs real tags"
+        # TODO: complete the tag generation logic
+        replace = False         # safe value
+        
+        ####################################################
+        ## Template Tags                                  ##
+        ####################################################
+        
+        if tag == "autoescape":
+            templateText = ("{% autoescape on %} " +
+                            selectedText +
+                            " {% endautoescape %}")
+            replace = True
+        elif tag == "block":
+            blockName, ok = QInputDialog.getText(
+                None,
+                self.tr("Named Block"),
+                self.tr("Enter block name:"),
+                QLineEdit.Normal,
+                "block_name")
+            templateText = ("{% block " +
+                            blockName.strip() +
+                            " %} " +
+                            selectedText +
+                            " {% endblock %}")
+            replace = True
+        elif tag == "comment":
+            templateText = ("{% comment %} " +
+                            selectedText +
+                            " {% endcomment %}")
+            replace = True
+        elif tag == "csrf_token":
+            templateText = "{% csrf_token %}"
+        elif tag == "cycle":
+            cycleItems, ok = QInputDialog.getText(
+                None,
+                self.tr("Cycle Variables"),
+                self.tr("Enter items to cycle separated by space"),
+                QLineEdit.Normal,
+                "item1 item2 item3")
+            templateText = ("{% cycle " + cycleItems.strip() + " %} ")
+        elif tag == "debug":
+            templateText = "{% debug %}"
+        elif tag == "extendsvariable":
+            extends, ok = QInputDialog.getText(
+                None,
+                self.tr("Extends Variable"),
+                self.tr("Enter variable name:"),
+                QLineEdit.Normal,
+                "variable")
+            templateText = '{% extends ' + extends.strip() + ' %} '
+        elif tag == "extendsfile":
+            extends, ok = QInputDialog.getText(
+                None,
+                self.tr("Extends Parent"),
+                self.tr("Enter parent file name:"),
+                QLineEdit.Normal,
+                "base.html")
+            templateText = '{% extends "' + extends.strip() + '" %} '
+        
+        ####################################################
+        ## Fallback: return just the tag name             ##
+        ####################################################
+        
+        else:
+            templateText = tag
+        return templateText, replace

eric ide

mercurial