ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py

branch
eric7
changeset 55
5390ef66c327
parent 52
c264091162a2
child 60
85d3931419d3
equal deleted inserted replaced
54:7e08cb395fca 55:5390ef66c327
8 """ 8 """
9 9
10 import os 10 import os
11 import datetime 11 import datetime
12 12
13 from PyQt5.QtCore import QObject 13 from PyQt6.QtCore import QObject
14 from PyQt5.QtWidgets import QMenu, QInputDialog, QDialog, QApplication 14 from PyQt6.QtWidgets import QMenu, QInputDialog, QDialog, QApplication
15 15
16 from E5Gui.E5Application import e5App 16 from EricWidgets.EricApplication import ericApp
17 from E5Gui import E5FileDialog, E5MessageBox 17 from EricWidgets import EricFileDialog, EricMessageBox
18 18
19 from .DjangoTagInputDialog import DjangoTagInputDialog 19 from .DjangoTagInputDialog import DjangoTagInputDialog
20 20
21 import Utilities 21 import Utilities
22 22
27 """ 27 """
28 def __init__(self, ui, parent=None): 28 def __init__(self, ui, parent=None):
29 """ 29 """
30 Constructor 30 Constructor
31 31
32 @param ui reference to the user interface object (UI.UserInterface) 32 @param ui reference to the user interface object
33 @param parent reference to the parent object (QObject) 33 @type UserInterface
34 @param parent reference to the parent object
35 @type QObject
34 """ 36 """
35 super().__init__(parent) 37 super().__init__(parent)
36 self.__ui = ui 38 self.__ui = ui
37 39
38 self.__findDialog = None 40 self.__findDialog = None
46 48
47 def initMenus(self, mainMenu): 49 def initMenus(self, mainMenu):
48 """ 50 """
49 Public method to initialize the various menus. 51 Public method to initialize the various menus.
50 52
51 @param mainMenu reference to the main tags menu (QMenu) 53 @param mainMenu reference to the main tags menu
54 @type QMenu
52 """ 55 """
53 mainMenu.addAction(self.tr("Django Template Tags Locator"), 56 mainMenu.addAction(self.tr("Django Template Tags Locator"),
54 self.__findTemplateTag) 57 self.__findTemplateTag)
55 mainMenu.addSeparator() 58 mainMenu.addSeparator()
56 mainMenu.addMenu(self.__initTagsMenu()) 59 mainMenu.addMenu(self.__initTagsMenu())
65 68
66 def __initTagsMenu(self): 69 def __initTagsMenu(self):
67 """ 70 """
68 Private method to initialize the tags menu. 71 Private method to initialize the tags menu.
69 72
70 @return generated menu (QMenu) 73 @return generated menu
74 @rtype QMenu
71 """ 75 """
72 menu = QMenu(self.tr("Tags")) 76 menu = QMenu(self.tr("Tags"))
73 menu.addAction( 77 menu.addAction(
74 self.tr("autoescape - Auto Escape Characters"), 78 self.tr("autoescape - Auto Escape Characters"),
75 lambda: self.__applyTemplate("autoescape")) 79 lambda: self.__applyTemplate("autoescape"))
183 187
184 def __initFiltersMenu(self): 188 def __initFiltersMenu(self):
185 """ 189 """
186 Private method to initialize the filters menu. 190 Private method to initialize the filters menu.
187 191
188 @return generated menu (QMenu) 192 @return generated menu
193 @rtype QMenu
189 """ 194 """
190 mainMenu = QMenu(self.tr("Filters")) 195 mainMenu = QMenu(self.tr("Filters"))
191 196
192 menu = QMenu(self.tr("A-I"), mainMenu) 197 menu = QMenu(self.tr("A-I"), mainMenu)
193 menu.addAction( 198 menu.addAction(
390 395
391 def __initHumanizeMenu(self): 396 def __initHumanizeMenu(self):
392 """ 397 """
393 Private method to initialize the humanize menu. 398 Private method to initialize the humanize menu.
394 399
395 @return generated menu (QMenu) 400 @return generated menu
401 @rtype QMenu
396 """ 402 """
397 menu = QMenu(self.tr("Humanize")) 403 menu = QMenu(self.tr("Humanize"))
398 menu.addAction( 404 menu.addAction(
399 self.tr("Load Package"), 405 self.tr("Load Package"),
400 lambda: self.__applyTemplate("loadhumanize")) 406 lambda: self.__applyTemplate("loadhumanize"))
426 432
427 def __initWebDesignMenu(self): 433 def __initWebDesignMenu(self):
428 """ 434 """
429 Private method to initialize the web design menu. 435 Private method to initialize the web design menu.
430 436
431 @return generated menu (QMenu) 437 @return generated menu
438 @rtype QMenu
432 """ 439 """
433 menu = QMenu(self.tr("Web Design")) 440 menu = QMenu(self.tr("Web Design"))
434 menu.addAction( 441 menu.addAction(
435 self.tr("Load Package"), 442 self.tr("Load Package"),
436 lambda: self.__applyTemplate("loadweb")) 443 lambda: self.__applyTemplate("loadweb"))
444 451
445 def __initStaticMenu(self): 452 def __initStaticMenu(self):
446 """ 453 """
447 Private method to initialize the static menu. 454 Private method to initialize the static menu.
448 455
449 @return generated menu (QMenu) 456 @return generated menu
457 @rtype QMenu
450 """ 458 """
451 menu = QMenu(self.tr("Static")) 459 menu = QMenu(self.tr("Static"))
452 menu.addAction( 460 menu.addAction(
453 self.tr("Load Package"), 461 self.tr("Load Package"),
454 lambda: self.__applyTemplate("loadstatic")) 462 lambda: self.__applyTemplate("loadstatic"))
471 479
472 def __initCommentsMenu(self): 480 def __initCommentsMenu(self):
473 """ 481 """
474 Private method to initialize the comments menu. 482 Private method to initialize the comments menu.
475 483
476 @return generated menu (QMenu) 484 @return generated menu
485 @rtype QMenu
477 """ 486 """
478 menu = QMenu(self.tr("Comment")) 487 menu = QMenu(self.tr("Comment"))
479 menu.addAction( 488 menu.addAction(
480 self.tr("Single Line Comment Selected Text"), 489 self.tr("Single Line Comment Selected Text"),
481 lambda: self.__applyTemplate("singlelinecommentselect")) 490 lambda: self.__applyTemplate("singlelinecommentselect"))
517 526
518 def __initInternationalizationMenu(self): 527 def __initInternationalizationMenu(self):
519 """ 528 """
520 Private method to initialize the static menu. 529 Private method to initialize the static menu.
521 530
522 @return generated menu (QMenu) 531 @return generated menu
532 @rtype QMenu
523 """ 533 """
524 mainMenu = QMenu(self.tr("Internationalization (i18n)")) 534 mainMenu = QMenu(self.tr("Internationalization (i18n)"))
525 mainMenu.addAction( 535 mainMenu.addAction(
526 self.tr("Load Package"), 536 self.tr("Load Package"),
527 lambda: self.__applyTemplate("loadi18n")) 537 lambda: self.__applyTemplate("loadi18n"))
584 594
585 def __initLocalizationMenu(self): 595 def __initLocalizationMenu(self):
586 """ 596 """
587 Private method to initialize the static menu. 597 Private method to initialize the static menu.
588 598
589 @return generated menu (QMenu) 599 @return generated menu
600 @rtype QMenu
590 """ 601 """
591 mainMenu = QMenu(self.tr("Localization (l10n)")) 602 mainMenu = QMenu(self.tr("Localization (l10n)"))
592 mainMenu.addAction( 603 mainMenu.addAction(
593 self.tr("Load Package"), 604 self.tr("Load Package"),
594 lambda: self.__applyTemplate("loadl10n")) 605 lambda: self.__applyTemplate("loadl10n"))
617 628
618 def __initTimezoneMenu(self): 629 def __initTimezoneMenu(self):
619 """ 630 """
620 Private method to initialize the static menu. 631 Private method to initialize the static menu.
621 632
622 @return generated menu (QMenu) 633 @return generated menu
634 @rtype QMenu
623 """ 635 """
624 mainMenu = QMenu(self.tr("Timezone (tz)")) 636 mainMenu = QMenu(self.tr("Timezone (tz)"))
625 mainMenu.addAction( 637 mainMenu.addAction(
626 self.tr("Load Package"), 638 self.tr("Load Package"),
627 lambda: self.__applyTemplate("loadtz")) 639 lambda: self.__applyTemplate("loadtz"))
675 687
676 def __applyTemplate(self, tag): 688 def __applyTemplate(self, tag):
677 """ 689 """
678 Private slot to generate and insert the template text. 690 Private slot to generate and insert the template text.
679 691
680 @param tag name of the tag to insert (string) 692 @param tag name of the tag to insert
681 """ 693 @type str
682 editor = e5App().getObject("ViewManager").activeWindow() 694 """
695 editor = ericApp().getObject("ViewManager").activeWindow()
683 if editor is None: 696 if editor is None:
684 return 697 return
685 698
686 templateText, replace = self.__generateTemplateText( 699 templateText, replace = self.__generateTemplateText(
687 tag, editor.selectedText()) 700 tag, editor.selectedText())
696 709
697 def __generateTemplateText(self, tag, selectedText): 710 def __generateTemplateText(self, tag, selectedText):
698 """ 711 """
699 Private slot to generate the template text. 712 Private slot to generate the template text.
700 713
701 @param tag name of the tag to insert (string) 714 @param tag name of the tag to insert
702 @param selectedText selected text of the current editor (string) 715 @type str
703 @return tuple of generated template text (string), a flag indicating 716 @param selectedText selected text of the current editor
704 to perform a replace operation (boolean) 717 @type str
718 @return tuple of generated template text, a flag indicating to perform
719 a replace operation
720 @rtype tuple of (str, bool)
705 """ 721 """
706 replace = False # safe value 722 replace = False # safe value
707 ok = True 723 ok = True
708 templateText = "" 724 templateText = ""
709 725
805 data[0], data[1], selectedText, data[2])) 821 data[0], data[1], selectedText, data[2]))
806 replace = True 822 replace = True
807 elif tag == "if": 823 elif tag == "if":
808 from .IfTagInputDialog import IfTagInputDialog 824 from .IfTagInputDialog import IfTagInputDialog
809 dlg = IfTagInputDialog() 825 dlg = IfTagInputDialog()
810 if dlg.exec() == QDialog.Accepted: 826 if dlg.exec() == QDialog.DialogCode.Accepted:
811 templateText = dlg.getTag() 827 templateText = dlg.getTag()
812 elif tag == "ifchanged": 828 elif tag == "ifchanged":
813 data, ok = DjangoTagInputDialog.getText( 829 data, ok = DjangoTagInputDialog.getText(
814 None, 830 None,
815 self.tr("Check Variables for Changes"), 831 self.tr("Check Variables for Changes"),
899 [self.tr("Full path to template:")], 915 [self.tr("Full path to template:")],
900 ["/tmp/ssi_template.html"]) # secok 916 ["/tmp/ssi_template.html"]) # secok
901 if ok: 917 if ok:
902 templateText = '{{% ssi "{0}" parsed %}}'.format(data[0]) 918 templateText = '{{% ssi "{0}" parsed %}}'.format(data[0])
903 elif tag == "ssifile": 919 elif tag == "ssifile":
904 ssi = E5FileDialog.getOpenFileName( 920 ssi = EricFileDialog.getOpenFileName(
905 None, 921 None,
906 self.tr("SSI"), 922 self.tr("SSI"),
907 os.path.expanduser("~"), 923 os.path.expanduser("~"),
908 self.tr("All Files (*)")) 924 self.tr("All Files (*)"))
909 if ssi: 925 if ssi:
1305 elif tag == "loadweb": 1321 elif tag == "loadweb":
1306 templateText = '{% load webdesign %}' 1322 templateText = '{% load webdesign %}'
1307 elif tag == "lorem": 1323 elif tag == "lorem":
1308 from .LoremTagInputDialog import LoremTagInputDialog 1324 from .LoremTagInputDialog import LoremTagInputDialog
1309 dlg = LoremTagInputDialog() 1325 dlg = LoremTagInputDialog()
1310 if dlg.exec() == QDialog.Accepted: 1326 if dlg.exec() == QDialog.DialogCode.Accepted:
1311 templateText = "{{% {0} %}}".format(dlg.getTag()) 1327 templateText = "{{% {0} %}}".format(dlg.getTag())
1312 1328
1313 #################################################### 1329 ####################################################
1314 ## Static Template Tags ## 1330 ## Static Template Tags ##
1315 #################################################### 1331 ####################################################
1370 QApplication.clipboard().text().strip()) 1386 QApplication.clipboard().text().strip())
1371 elif tag == "multilinecommentclipboard": 1387 elif tag == "multilinecommentclipboard":
1372 templateText = '{{% comment %}} {0} {{% endcomment %}}'.format( 1388 templateText = '{{% comment %}} {0} {{% endcomment %}}'.format(
1373 QApplication.clipboard().text().strip()) 1389 QApplication.clipboard().text().strip())
1374 elif tag == "multilinecommentfile": 1390 elif tag == "multilinecommentfile":
1375 filename = E5FileDialog.getOpenFileName( 1391 filename = EricFileDialog.getOpenFileName(
1376 None, 1392 None,
1377 self.tr("Comment File"), 1393 self.tr("Comment File"),
1378 Utilities.getHomeDir(), 1394 Utilities.getHomeDir(),
1379 self.tr("All Files (*)")) 1395 self.tr("All Files (*)"))
1380 if filename: 1396 if filename:
1383 comment = f.read() 1399 comment = f.read()
1384 templateText = ( 1400 templateText = (
1385 '{{% comment %}} {0} {{% endcomment %}}'.format( 1401 '{{% comment %}} {0} {{% endcomment %}}'.format(
1386 comment)) 1402 comment))
1387 except (IOError, OSError) as err: 1403 except (IOError, OSError) as err:
1388 E5MessageBox.critical( 1404 EricMessageBox.critical(
1389 None, 1405 None,
1390 self.tr("Comment File"), 1406 self.tr("Comment File"),
1391 self.tr("""<p>The file <b>{0}</b> could not be""" 1407 self.tr("""<p>The file <b>{0}</b> could not be"""
1392 """ read.</p><p>Reason: {1}</p>""").format( 1408 """ read.</p><p>Reason: {1}</p>""").format(
1393 str(err))) 1409 str(err)))

eric ide

mercurial