Mon, 03 Feb 2014 19:54:07 +0100
Implemented template tags 'a' to 'e'.
# -*- coding: utf-8 -*- # Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the Django tags menu handler. """ 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. """ def __init__(self, ui, parent=None): """ Constructor @param ui reference to the user interface object (UI.UserInterface) @param parent reference to the parent object (QObject) """ super(DjangoTagsMenuHandler, self).__init__(parent) self.__ui = ui 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. @param mainMenu reference to the main tags menu (QMenu) """ 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): """ Private slot to find a template tag and insert its text. """ if self.__findDialog is None: from .FindTemplateTagDialog import FindTemplateTagDialog self.__findDialog = FindTemplateTagDialog() self.__findDialog.tag.connect(self.__applyTemplate) self.__findDialog.show() self.__findDialog.raise_() self.__findDialog.activateWindow() def __applyTemplate(self, tag): """ Private slot to generate and insert the template text. @param tag name of the tag to insert (string) """ editor = e5App().getObject("ViewManager").activeWindow() if editor is None: return templateText, replace = self.__generateTemplateText( tag, editor.selectedText()) editor.beginUndoAction() if replace: editor.replaceSelectedText(templateText) else: editor.insert(templateText) editor.endUndoAction() 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: 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