--- a/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Sat Feb 08 18:58:33 2014 +0100 +++ b/ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py Sun Feb 09 18:21:45 2014 +0100 @@ -8,15 +8,18 @@ """ import os +import datetime from PyQt4.QtCore import QObject -from PyQt4.QtGui import QMenu, QInputDialog, QDialog +from PyQt4.QtGui import QMenu, QInputDialog, QDialog, QApplication from E5Gui.E5Application import e5App -from E5Gui import E5FileDialog +from E5Gui import E5FileDialog, E5MessageBox from .DjangoTagInputDialog import DjangoTagInputDialog +import Utilities + class DjangoTagsMenuHandler(QObject): """ @@ -55,6 +58,10 @@ mainMenu.addMenu(self.__initHumanizeMenu()) mainMenu.addMenu(self.__initWebDesignMenu()) mainMenu.addMenu(self.__initStaticMenu()) + mainMenu.addMenu(self.__initCommentsMenu()) + # TODO: add internationalization tags/filters menu + # TODO: add localization tags/filters menu + # TODO: add timezone tags/filters menu def __initTagsMenu(self): """ @@ -180,7 +187,10 @@ @return generated menu (QMenu) """ - menu = QMenu(self.tr("Filters")) + # TODO: subdivide into even parts a-i,j-r,s-z + mainMenu = QMenu(self.tr("Filters")) + + menu = QMenu(self.tr("A-I"), mainMenu) menu.addAction( self.tr("add - Add variable or string"), lambda: self.__applyTemplate("add")) @@ -247,7 +257,9 @@ menu.addAction( self.tr("iriencode - Convert IRI to string"), lambda: self.__applyTemplate("iriencode")) - menu.addSeparator() + mainMenu.addMenu(menu) + + menu = QMenu(self.tr("J-R"), mainMenu) menu.addAction( self.tr("join - Join list"), lambda: self.__applyTemplate("join")) @@ -300,7 +312,9 @@ menu.addAction( self.tr("rjust - Right-align value"), lambda: self.__applyTemplate("rjust")) - menu.addSeparator() + mainMenu.addMenu(menu) + + menu = QMenu(self.tr("S-Z"), mainMenu) menu.addAction( self.tr("safe - Mark as not requiring HTML escaping "), lambda: self.__applyTemplate("safe")) @@ -370,9 +384,10 @@ menu.addAction( self.tr("yesno - Map True, False and None"), lambda: self.__applyTemplate("yesno")) + mainMenu.addMenu(menu) - self.__filtersMenu = menu - return menu + self.__filtersMenu = mainMenu + return mainMenu def __initHumanizeMenu(self): """ @@ -455,6 +470,52 @@ self.__staticMenu = menu return menu + def __initCommentsMenu(self): + """ + Private method to initialize the static menu. + + @return generated menu (QMenu) + """ + menu = QMenu(self.tr("Comment")) + menu.addAction( + self.tr("Single Line Comment Selected Text"), + lambda: self.__applyTemplate("singlelinecommentselect")) + menu.addAction( + self.tr("Multi Line Comment Selected Text"), + lambda: self.__applyTemplate("multilinecommentselect")) + menu.addSeparator() + menu.addAction( + self.tr("Single Line Comment from Input Dialog"), + lambda: self.__applyTemplate("singlelinecommentdialog")) + menu.addAction( + self.tr("Multi Line Comment from Input Dialog"), + lambda: self.__applyTemplate("multilinecommentdialog")) + menu.addSeparator() + menu.addAction( + self.tr("Single Line Comment from Clipboard"), + lambda: self.__applyTemplate("singlelinecommentclipboard")) + menu.addAction( + self.tr("Multi Line Comment from Clipboard"), + lambda: self.__applyTemplate("multilinecommentclipboard")) + menu.addSeparator() + menu.addAction( + self.tr("Multi Line Comment from File"), + lambda: self.__applyTemplate("multilinecommentfile")) + menu.addSeparator() + menu.addAction( + self.tr("Single Line Comment from Date Time Now"), + lambda: self.__applyTemplate("singlelinecommentdatetime")) + menu.addSeparator() + menu.addAction( + self.tr("HTML Comment Out Selected Text"), + lambda: self.__applyTemplate("htmlcomment")) + menu.addAction( + self.tr("MS IE Conditional Comment Selected Text"), + lambda: self.__applyTemplate("iecomment")) + + self.__commentsMenu = menu + return menu + def __findTemplateTag(self): """ Private slot to find a template tag and insert its text. @@ -1136,8 +1197,77 @@ elif tag == "get_media_prefix": templateText = '{% get_media_prefix %}' - # TODO: add comment functions - # TODO: add internationalization tags + #################################################### + ## Comments ## + #################################################### + + elif tag == "singlelinecommentselect": + templateText = '{{# {0} #}}'.format(selectedText) + replace = True + elif tag == "multilinecommentselect": + templateText = '{{% comment %}} {0} {{% endcomment }}'.format( + selectedText) + replace = True + elif tag == "singlelinecommentdialog": + data, ok = DjangoTagInputDialog.getText( + None, + self.tr("Single Line Comment"), + [self.tr("Enter comment:")], + [""]) + if ok: + templateText = '{{# {0} #}}'.format(data[0]) + elif tag == "multilinecommentdialog": + from .MultiLineInputDialog import MultiLineInputDialog + comment, ok = MultiLineInputDialog.getText( + None, self.tr("Multi Line Comment"), + self.tr("Enter comment:"), "") + if ok: + templateText = '{{% comment %}} {0} {{% endcomment }}'.format( + comment) + elif tag == "singlelinecommentclipboard": + templateText = '{{# {0} #}}'.format( + QApplication.clipboard().text().strip()) + elif tag == "multilinecommentclipboard": + templateText = '{{% comment %}} {0} {{% endcomment }}'.format( + QApplication.clipboard().text().strip()) + elif tag == "multilinecommentfile": + filename = E5FileDialog.getOpenFileName( + None, + self.tr("Comment File"), + Utilities.getHomeDir(), + self.tr("All Files (*)")) + if filename: + try: + f = open(filename, "r", encoding="utf-8") + comment = f.read() + f.close() + templateText = ( + '{{% comment %}} {0} {{% endcomment }}'.format( + comment)) + except (IOError, OSError) as err: + E5MessageBox.critical( + None, + self.tr("Comment File"), + self.tr("""<p>The file <b>{0}</b> could not be""" + """ read.</p><p>Reason: {1}</p>""").format( + str(err))) + elif tag == "singlelinecommentdatetime": + templateText = '{{# {0} by {1} #}}'.format( + datetime.datetime.now().isoformat().split(), + Utilities.getUserName()) + elif tag == "htmlcomment": + templateText = '<!-- {0} -->'.format(selectedText) + replace = True + elif tag == "iecomment": + from .IeCommentDialog import IeCommentDialog + tag, ok = IeCommentDialog.getTag(selectedText) + if ok: + templateText = '<!--{0}-->'.format(tag) + replace = True + + # TODO: add internationalization tags/filters + # TODO: add localization tags/filters + # TODO: add timezone tags/filters #################################################### ## Fallback: return just the tag name ## ####################################################