eric6/Templates/TemplateViewer.py

branch
jsonfiles
changeset 8018
641c7c312f26
parent 8013
c3bd65c330ed
child 8021
a8ba35ce81ad
equal deleted inserted replaced
8017:7ec108389fde 8018:641c7c312f26
21 21
22 import Preferences 22 import Preferences
23 23
24 import UI.PixmapCache 24 import UI.PixmapCache
25 import Utilities 25 import Utilities
26
27 from .TemplatesFile import TemplatesFile
26 28
27 29
28 class TemplateGroup(QTreeWidgetItem): 30 class TemplateGroup(QTreeWidgetItem):
29 """ 31 """
30 Class implementing a template group. 32 Class implementing a template group.
430 self.tr("Configure..."), self.__configure) 432 self.tr("Configure..."), self.__configure)
431 433
432 self.__activating = False 434 self.__activating = False
433 self.__dirty = False 435 self.__dirty = False
434 436
437 self.__templatesFile = TemplatesFile(self)
438
435 self.setContextMenuPolicy(Qt.CustomContextMenu) 439 self.setContextMenuPolicy(Qt.CustomContextMenu)
436 self.customContextMenuRequested.connect(self.__showContextMenu) 440 self.customContextMenuRequested.connect(self.__showContextMenu)
437 self.itemActivated.connect(self.__templateItemActivated) 441 self.itemActivated.connect(self.__templateItemActivated)
438 442
439 self.setWindowIcon(UI.PixmapCache.getIcon("eric")) 443 self.setWindowIcon(UI.PixmapCache.getIcon("eric"))
561 """ 565 """
562 fn = E5FileDialog.getOpenFileName( 566 fn = E5FileDialog.getOpenFileName(
563 self, 567 self,
564 self.tr("Import Templates"), 568 self.tr("Import Templates"),
565 "", 569 "",
566 self.tr("Templates Files (*.e4c);; All Files (*)")) 570 self.tr("Templates Files (*.ecj);;"
571 "XML Templates Files (*.e4c);;"
572 "All Files (*)"))
567 573
568 if fn: 574 if fn:
569 self.readTemplates(fn) 575 self.readTemplates(fn)
570 self.__dirty = True 576 self.__dirty = True
571 577
575 """ 581 """
576 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 582 fn, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
577 self, 583 self,
578 self.tr("Export Templates"), 584 self.tr("Export Templates"),
579 "", 585 "",
580 self.tr("Templates Files (*.e4c);; All Files (*)"), 586 self.tr("Templates Files (*.ecj);;"
587 "XML Templates Files (*.e4c);;"
588 "All Files (*)"),
581 "", 589 "",
582 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) 590 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
583 591
584 if fn: 592 if fn:
585 ext = QFileInfo(fn).suffix() 593 ext = QFileInfo(fn).suffix()
947 tmpl = self.groups[groupName].getEntry(name) 955 tmpl = self.groups[groupName].getEntry(name)
948 tmpl.setDescription(description) 956 tmpl.setDescription(description)
949 tmpl.setTemplateText(template) 957 tmpl.setTemplateText(template)
950 self.__resort() 958 self.__resort()
951 959
952 # TODO: do the JSON templates
953 def writeTemplates(self, filename=None): 960 def writeTemplates(self, filename=None):
954 """ 961 """
955 Public method to write the templates data to an XML file (.e4c). 962 Public method to write the templates data to a JSON file (.ecj).
956 963
957 @param filename name of a templates file to read (string) 964 @param filename name of a templates file to write
958 @return flag indicating success (boolean) 965 @type str
966 @return flag indicating success
967 @rtype bool
959 """ 968 """
960 if filename is None: 969 if filename is None:
961 filename = os.path.join( 970 filename = os.path.join(
962 Utilities.getConfigDir(), "eric6templates.e4c") 971 Utilities.getConfigDir(), "eric6templates.ecj")
963 f = QFile(filename) 972 if filename.endswith(".ecj"):
964 ok = f.open(QIODevice.WriteOnly) 973 # new JSON based file
965 if not ok: 974 res = self.__templatesFile.writeFile(filename)
966 E5MessageBox.critical( 975 else:
967 self, 976 # old XML based file
968 self.tr("Save templates"), 977 f = QFile(filename)
969 self.tr( 978 ok = f.open(QIODevice.WriteOnly)
970 "<p>The templates file <b>{0}</b> could not be" 979 if not ok:
971 " written.</p>") 980 E5MessageBox.critical(
972 .format(filename)) 981 self,
973 return False 982 self.tr("Save Templates"),
974 983 self.tr(
975 from E5XML.TemplatesWriter import TemplatesWriter 984 "<p>The templates file <b>{0}</b> could not be"
976 TemplatesWriter(f, self).writeXML() 985 " written.</p>")
977 f.close() 986 .format(filename))
978 987 res = False
979 return True 988 else:
980 989 from E5XML.TemplatesWriter import TemplatesWriter
981 # TODO: do the JSON templates 990 TemplatesWriter(f, self).writeXML()
991 f.close()
992 res = True
993
994 return res
995
982 def readTemplates(self, filename=None): 996 def readTemplates(self, filename=None):
983 """ 997 """
984 Public method to read in the templates file (.e4c). 998 Public method to read in the templates file (.e4c).
985 999
986 @param filename name of a templates file to read (string) 1000 @param filename name of a templates file to read
1001 @type str
987 """ 1002 """
988 if filename is None: 1003 if filename is None:
1004 # new JSON based file first
989 filename = os.path.join( 1005 filename = os.path.join(
990 Utilities.getConfigDir(), "eric6templates.e4c") 1006 Utilities.getConfigDir(), "eric6templates.ecj")
991 if not os.path.exists(filename): 1007 if not os.path.exists(filename):
992 return 1008 # old XML based file second
993 1009 filename = os.path.join(
994 f = QFile(filename) 1010 Utilities.getConfigDir(), "eric6templates.e4c")
995 if f.open(QIODevice.ReadOnly): 1011 if not os.path.exists(filename):
996 from E5XML.TemplatesReader import TemplatesReader 1012 return
997 reader = TemplatesReader(f, viewer=self) 1013
998 reader.readXML() 1014 if filename.endswith(".ecj"):
999 f.close() 1015 self.__templatesFile.readFile(filename)
1000 else: 1016 else:
1001 E5MessageBox.critical( 1017 f = QFile(filename)
1002 self, 1018 if f.open(QIODevice.ReadOnly):
1003 self.tr("Read templates"), 1019 from E5XML.TemplatesReader import TemplatesReader
1004 self.tr( 1020 reader = TemplatesReader(f, viewer=self)
1005 "<p>The templates file <b>{0}</b> could not be read.</p>") 1021 reader.readXML()
1006 .format(filename)) 1022 f.close()
1023 else:
1024 E5MessageBox.critical(
1025 self,
1026 self.tr("Read Templates"),
1027 self.tr(
1028 "<p>The templates file <b>{0}</b> could not be read."
1029 "</p>")
1030 .format(filename))
1007 1031
1008 def __configure(self): 1032 def __configure(self):
1009 """ 1033 """
1010 Private method to open the configuration dialog. 1034 Private method to open the configuration dialog.
1011 """ 1035 """

eric ide

mercurial