Templates/TemplateViewer.py

changeset 945
8cd4d08fa9f6
parent 882
34b86be88bf0
child 1002
1151d1ea562a
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
26 from E5XML.TemplatesReader import TemplatesReader 26 from E5XML.TemplatesReader import TemplatesReader
27 from E5XML.TemplatesWriter import TemplatesWriter 27 from E5XML.TemplatesWriter import TemplatesWriter
28 28
29 import UI.PixmapCache 29 import UI.PixmapCache
30 import Utilities 30 import Utilities
31
31 32
32 class TemplateGroup(QTreeWidgetItem): 33 class TemplateGroup(QTreeWidgetItem):
33 """ 34 """
34 Class implementing a template group. 35 Class implementing a template group.
35 """ 36 """
36 def __init__(self, parent, name, language = "All"): 37 def __init__(self, parent, name, language="All"):
37 """ 38 """
38 Constructor 39 Constructor
39 40
40 @param parent parent widget of the template group (QWidget) 41 @param parent parent widget of the template group (QWidget)
41 @param name name of the group (string) 42 @param name name of the group (string)
83 84
84 @return language of the group (string) 85 @return language of the group (string)
85 """ 86 """
86 return self.language 87 return self.language
87 88
88 def addEntry(self, name, description, template, quiet = False): 89 def addEntry(self, name, description, template, quiet=False):
89 """ 90 """
90 Public method to add a template entry to this group. 91 Public method to add a template entry to this group.
91 92
92 @param name name of the entry (string) 93 @param name name of the entry (string)
93 @param description description of the entry to add (string) 94 @param description description of the entry to add (string)
152 except KeyError: 153 except KeyError:
153 return None 154 return None
154 155
155 def getEntryNames(self, beginning): 156 def getEntryNames(self, beginning):
156 """ 157 """
157 Public method to get the names of all entries, who's name starts with the 158 Public method to get the names of all entries, who's name starts with the
158 given string. 159 given string.
159 160
160 @param beginning string denoting the beginning of the template name 161 @param beginning string denoting the beginning of the template name
161 (string) 162 (string)
162 @return list of entry names found (list of strings) 163 @return list of entry names found (list of strings)
173 Public method to retrieve all entries. 174 Public method to retrieve all entries.
174 175
175 @return list of all entries (list of TemplateEntry) 176 @return list of all entries (list of TemplateEntry)
176 """ 177 """
177 return list(self.entries.values()) 178 return list(self.entries.values())
179
178 180
179 class TemplateEntry(QTreeWidgetItem): 181 class TemplateEntry(QTreeWidgetItem):
180 """ 182 """
181 Class immplementing a template entry. 183 Class immplementing a template entry.
182 """ 184 """
275 """ 277 """
276 Public method to get the template text with all variables expanded. 278 Public method to get the template text with all variables expanded.
277 279
278 @param varDict dictionary containing the texts of each variable 280 @param varDict dictionary containing the texts of each variable
279 with the variable name as key. 281 with the variable name as key.
280 @param indent indentation of the line receiving he expanded 282 @param indent indentation of the line receiving he expanded
281 template text (string) 283 template text (string)
282 @return a tuple of the expanded template text (string), the 284 @return a tuple of the expanded template text (string), the
283 number of lines (integer) and the length of the last line (integer) 285 number of lines (integer) and the length of the last line (integer)
284 """ 286 """
285 txt = self.template 287 txt = self.template
315 ind = line.find(var) 317 ind = line.find(var)
316 if ind >= 0: 318 if ind >= 0:
317 format = var[1:-1].split(':', 1)[1] 319 format = var[1:-1].split(':', 1)[1]
318 if format == 'rl': 320 if format == 'rl':
319 prefix = line[:ind] 321 prefix = line[:ind]
320 postfix = line [ind + len(var):] 322 postfix = line[ind + len(var):]
321 for v in val.splitlines(): 323 for v in val.splitlines():
322 t = "{0}{1}{2}{3}{4}".format(t, os.linesep, prefix, v, postfix) 324 t = "{0}{1}{2}{3}{4}".format(t, os.linesep, prefix, v, postfix)
323 elif format == 'ml': 325 elif format == 'ml':
324 indent = line.replace(line.lstrip(), "") 326 indent = line.replace(line.lstrip(), "")
325 prefix = line[:ind] 327 prefix = line[:ind]
361 if not var in self.variables: 363 if not var in self.variables:
362 self.variables.append(var) 364 self.variables.append(var)
363 if var.find(':') >= 0 and not var in self.formatedVariables: 365 if var.find(':') >= 0 and not var in self.formatedVariables:
364 self.formatedVariables.append(var) 366 self.formatedVariables.append(var)
365 367
368
366 class TemplateViewer(QTreeWidget): 369 class TemplateViewer(QTreeWidget):
367 """ 370 """
368 Class implementing the template viewer. 371 Class implementing the template viewer.
369 """ 372 """
370 def __init__(self, parent, viewmanager): 373 def __init__(self, parent, viewmanager):
423 """ 426 """
424 Private method to resort the tree. 427 Private method to resort the tree.
425 """ 428 """
426 self.sortItems(self.sortColumn(), self.header().sortIndicatorOrder()) 429 self.sortItems(self.sortColumn(), self.header().sortIndicatorOrder())
427 430
428 def __templateItemActivated(self, itm = None, col = 0): 431 def __templateItemActivated(self, itm=None, col=0):
429 """ 432 """
430 Private slot to handle the activation of an item. 433 Private slot to handle the activation of an item.
431 434
432 @param itm reference to the activated item (QTreeWidgetItem) 435 @param itm reference to the activated item (QTreeWidgetItem)
433 @param col column the item was activated in (integer) 436 @param col column the item was activated in (integer)
434 """ 437 """
435 itm = self.currentItem() 438 itm = self.currentItem()
678 txt = "'Insertion and selection can not be in template together'" 681 txt = "'Insertion and selection can not be in template together'"
679 682
680 if "i_n_s_e_r_t_i_o_n" in txt: 683 if "i_n_s_e_r_t_i_o_n" in txt:
681 lines = 1 684 lines = 1
682 for aline in txt.splitlines(): 685 for aline in txt.splitlines():
683 count = aline.find("i_n_s_e_r_t_i_o_n") 686 count = aline.find("i_n_s_e_r_t_i_o_n")
684 if count >= 0: 687 if count >= 0:
685 txt = txt.replace("i_n_s_e_r_t_i_o_n", "") 688 txt = txt.replace("i_n_s_e_r_t_i_o_n", "")
686 if lines == 1: 689 if lines == 1:
687 count += index 690 count += index
688 else: 691 else:
695 setselect = False 698 setselect = False
696 if "s_e_l_e_c_t_s_t_a_r_t" in txt and "s_e_l_e_c_t_e_n_d" in txt: 699 if "s_e_l_e_c_t_s_t_a_r_t" in txt and "s_e_l_e_c_t_e_n_d" in txt:
697 setselect = True 700 setselect = True
698 linea = 1 701 linea = 1
699 for aline in txt.splitlines(): 702 for aline in txt.splitlines():
700 posa = aline.find("s_e_l_e_c_t_s_t_a_r_t") 703 posa = aline.find("s_e_l_e_c_t_s_t_a_r_t")
701 if posa >= 0: 704 if posa >= 0:
702 txt = txt.replace("s_e_l_e_c_t_s_t_a_r_t", "") 705 txt = txt.replace("s_e_l_e_c_t_s_t_a_r_t", "")
703 break 706 break
704 else: 707 else:
705 linea += 1 708 linea += 1
706 lineb = 1 709 lineb = 1
707 for aline in txt.splitlines(): 710 for aline in txt.splitlines():
708 posb = aline.find("s_e_l_e_c_t_e_n_d") 711 posb = aline.find("s_e_l_e_c_t_e_n_d")
709 if posb >= 0: 712 if posb >= 0:
710 txt = txt.replace("s_e_l_e_c_t_e_n_d", "") 713 txt = txt.replace("s_e_l_e_c_t_e_n_d", "")
711 break 714 break
712 else: 715 else:
713 lineb += 1 716 lineb += 1
714 717
715 editor.insert(txt) 718 editor.insert(txt)
716 719
717 if setselect: 720 if setselect:
718 editor.setSelection(line + linea - 1, posa, line + lineb - 1, posb) 721 editor.setSelection(line + linea - 1, posa, line + lineb - 1, posb)
732 template = group.getEntry(templateName) 735 template = group.getEntry(templateName)
733 if template is not None: 736 if template is not None:
734 self.applyTemplate(template) 737 self.applyTemplate(template)
735 break 738 break
736 739
737 def addEntry(self, groupName, name, description, template, quiet = False): 740 def addEntry(self, groupName, name, description, template, quiet=False):
738 """ 741 """
739 Public method to add a template entry. 742 Public method to add a template entry.
740 743
741 @param groupName name of the group to add to (string) 744 @param groupName name of the group to add to (string)
742 @param name name of the entry to add (string) 745 @param name name of the entry to add (string)
743 @param description description of the entry to add (string) 746 @param description description of the entry to add (string)
744 @param template template text of the entry (string) 747 @param template template text of the entry (string)
745 @param quiet flag indicating quiet operation (boolean) 748 @param quiet flag indicating quiet operation (boolean)
746 """ 749 """
747 self.groups[groupName].addEntry(name, description, template, quiet = quiet) 750 self.groups[groupName].addEntry(name, description, template, quiet=quiet)
748 self.__resort() 751 self.__resort()
749 752
750 def addGroup(self, name, language = "All"): 753 def addGroup(self, name, language="All"):
751 """ 754 """
752 Public method to add a group. 755 Public method to add a group.
753 756
754 @param name name of the group to be added (string) 757 @param name name of the group to be added (string)
755 @param language programming language for the group (string) 758 @param language programming language for the group (string)
756 """ 759 """
757 if name not in self.groups: 760 if name not in self.groups:
758 self.groups[name] = TemplateGroup(self, name, language) 761 self.groups[name] = TemplateGroup(self, name, language)
759 self.__resort() 762 self.__resort()
760 763
761 def changeGroup(self, oldname, newname, language = "All"): 764 def changeGroup(self, oldname, newname, language="All"):
762 """ 765 """
763 Public method to rename a group. 766 Public method to rename a group.
764 767
765 @param oldname old name of the group (string) 768 @param oldname old name of the group (string)
766 @param newname new name of the group (string) 769 @param newname new name of the group (string)
846 tmpl = self.groups[groupName].getEntry(name) 849 tmpl = self.groups[groupName].getEntry(name)
847 tmpl.setDescription(description) 850 tmpl.setDescription(description)
848 tmpl.setTemplateText(template) 851 tmpl.setTemplateText(template)
849 self.__resort() 852 self.__resort()
850 853
851 def writeTemplates(self, filename = None): 854 def writeTemplates(self, filename=None):
852 """ 855 """
853 Public method to write the templates data to an XML file (.e4c). 856 Public method to write the templates data to an XML file (.e4c).
854 857
855 @param filename name of a templates file to read (string) 858 @param filename name of a templates file to read (string)
856 """ 859 """
866 return 869 return
867 870
868 TemplatesWriter(f, self).writeXML() 871 TemplatesWriter(f, self).writeXML()
869 f.close() 872 f.close()
870 873
871 def readTemplates(self, filename = None): 874 def readTemplates(self, filename=None):
872 """ 875 """
873 Public method to read in the templates file (.e4c) 876 Public method to read in the templates file (.e4c)
874 877
875 @param filename name of a templates file to read (string) 878 @param filename name of a templates file to read (string)
876 """ 879 """
879 if not os.path.exists(filename): 882 if not os.path.exists(filename):
880 return 883 return
881 884
882 f = QFile(filename) 885 f = QFile(filename)
883 if f.open(QIODevice.ReadOnly): 886 if f.open(QIODevice.ReadOnly):
884 reader = TemplatesReader(f, viewer = self) 887 reader = TemplatesReader(f, viewer=self)
885 reader.readXML() 888 reader.readXML()
886 f.close() 889 f.close()
887 else: 890 else:
888 E5MessageBox.critical(self, 891 E5MessageBox.critical(self,
889 self.trUtf8("Read templates"), 892 self.trUtf8("Read templates"),

eric ide

mercurial