11 import datetime |
11 import datetime |
12 import os |
12 import os |
13 import re |
13 import re |
14 |
14 |
15 from PyQt5.QtCore import QFile, QFileInfo, QIODevice, Qt, QCoreApplication |
15 from PyQt5.QtCore import QFile, QFileInfo, QIODevice, Qt, QCoreApplication |
16 from PyQt5.QtWidgets import QTreeWidget, QDialog, QApplication, QMenu, \ |
16 from PyQt5.QtWidgets import ( |
17 QTreeWidgetItem |
17 QTreeWidget, QDialog, QApplication, QMenu, QTreeWidgetItem |
|
18 ) |
18 |
19 |
19 from E5Gui.E5Application import e5App |
20 from E5Gui.E5Application import e5App |
20 from E5Gui import E5MessageBox, E5FileDialog |
21 from E5Gui import E5MessageBox, E5FileDialog |
21 |
22 |
22 import Preferences |
23 import Preferences |
103 .format(self.name, name)) |
104 .format(self.name, name)) |
104 return |
105 return |
105 |
106 |
106 self.entries[name] = TemplateEntry(self, name, description, template) |
107 self.entries[name] = TemplateEntry(self, name, description, template) |
107 |
108 |
108 if Preferences.getTemplates("AutoOpenGroups") and \ |
109 if ( |
109 not self.isExpanded(): |
110 Preferences.getTemplates("AutoOpenGroups") and |
|
111 not self.isExpanded() |
|
112 ): |
110 self.setExpanded(True) |
113 self.setExpanded(True) |
111 |
114 |
112 def removeEntry(self, name): |
115 def removeEntry(self, name): |
113 """ |
116 """ |
114 Public method to remove a template entry from this group. |
117 Public method to remove a template entry from this group. |
119 index = self.indexOfChild(self.entries[name]) |
122 index = self.indexOfChild(self.entries[name]) |
120 self.takeChild(index) |
123 self.takeChild(index) |
121 del self.entries[name] |
124 del self.entries[name] |
122 |
125 |
123 if len(self.entries) == 0: |
126 if len(self.entries) == 0: |
124 if Preferences.getTemplates("AutoOpenGroups") and \ |
127 if ( |
125 self.isExpanded(): |
128 Preferences.getTemplates("AutoOpenGroups") and |
|
129 self.isExpanded() |
|
130 ): |
126 self.setExpanded(False) |
131 self.setExpanded(False) |
127 |
132 |
128 def removeAllEntries(self): |
133 def removeAllEntries(self): |
129 """ |
134 """ |
130 Public method to remove all template entries of this group. |
135 Public method to remove all template entries of this group. |
353 def __extractVariables(self): |
358 def __extractVariables(self): |
354 """ |
359 """ |
355 Private method to retrieve the list of variables. |
360 Private method to retrieve the list of variables. |
356 """ |
361 """ |
357 sepchar = Preferences.getTemplates("SeparatorChar") |
362 sepchar = Preferences.getTemplates("SeparatorChar") |
358 variablesPattern = \ |
363 variablesPattern = re.compile( |
359 re.compile( |
364 r"""\{0}[a-zA-Z][a-zA-Z0-9_]*(?::(?:ml|rl))?\{1}""".format( |
360 r"""\{0}[a-zA-Z][a-zA-Z0-9_]*(?::(?:ml|rl))?\{1}""".format( |
365 sepchar, sepchar) |
361 sepchar, sepchar)) |
366 ) |
362 variables = variablesPattern.findall(self.template) |
367 variables = variablesPattern.findall(self.template) |
363 self.variables = [] |
368 self.variables = [] |
364 self.formatedVariables = [] |
369 self.formatedVariables = [] |
365 for var in variables: |
370 for var in variables: |
366 if var not in self.variables: |
371 if var not in self.variables: |
662 keyfmt.format('file_name'): file_name, |
667 keyfmt.format('file_name'): file_name, |
663 keyfmt.format('base_name'): base_name, |
668 keyfmt.format('base_name'): base_name, |
664 keyfmt.format('ext'): ext |
669 keyfmt.format('ext'): ext |
665 }) |
670 }) |
666 |
671 |
667 varValues[keyfmt.format('clipboard:ml')] = \ |
672 varValues[keyfmt.format('clipboard:ml')] = ( |
668 QApplication.clipboard().text() |
673 QApplication.clipboard().text() |
669 varValues[keyfmt.format('clipboard')] = \ |
674 ) |
670 QApplication.clipboard().text() |
675 varValues[keyfmt.format('clipboard')] = QApplication.clipboard().text() |
671 |
676 |
672 if editor.hasSelectedText(): |
677 if editor.hasSelectedText(): |
673 varValues[keyfmt.format('cur_select:ml')] = editor.selectedText() |
678 varValues[keyfmt.format('cur_select:ml')] = editor.selectedText() |
674 varValues[keyfmt.format('cur_select')] = editor.selectedText() |
679 varValues[keyfmt.format('cur_select')] = editor.selectedText() |
675 else: |
680 else: |
703 if v in variables: |
708 if v in variables: |
704 variables.remove(v) |
709 variables.remove(v) |
705 |
710 |
706 if variables: |
711 if variables: |
707 if Preferences.getTemplates("SingleDialog"): |
712 if Preferences.getTemplates("SingleDialog"): |
708 from .TemplateMultipleVariablesDialog import \ |
713 from .TemplateMultipleVariablesDialog import ( |
709 TemplateMultipleVariablesDialog |
714 TemplateMultipleVariablesDialog |
|
715 ) |
710 dlg = TemplateMultipleVariablesDialog(variables, self) |
716 dlg = TemplateMultipleVariablesDialog(variables, self) |
711 if dlg.exec_() == QDialog.Accepted: |
717 if dlg.exec_() == QDialog.Accepted: |
712 varValues.update(dlg.getVariables()) |
718 varValues.update(dlg.getVariables()) |
713 ok = True |
719 ok = True |
714 else: |
720 else: |
715 from .TemplateSingleVariableDialog import \ |
721 from .TemplateSingleVariableDialog import ( |
716 TemplateSingleVariableDialog |
722 TemplateSingleVariableDialog |
|
723 ) |
717 for var in variables: |
724 for var in variables: |
718 dlg = TemplateSingleVariableDialog(var, self) |
725 dlg = TemplateSingleVariableDialog(var, self) |
719 if dlg.exec_() == QDialog.Accepted: |
726 if dlg.exec_() == QDialog.Accepted: |
720 varValues[var] = dlg.getVariable() |
727 varValues[var] = dlg.getVariable() |
721 else: |
728 else: |
724 ok = True |
731 ok = True |
725 else: |
732 else: |
726 ok = True |
733 ok = True |
727 |
734 |
728 if ok: |
735 if ok: |
729 line = editor.text(editor.getCursorPosition()[0])\ |
736 line = editor.text( |
730 .replace(os.linesep, "") |
737 editor.getCursorPosition()[0]).replace(os.linesep, "") |
731 indent = line.replace(line.lstrip(), "") |
738 indent = line.replace(line.lstrip(), "") |
732 txt, lines, count = itm.getExpandedText(varValues, indent) |
739 txt, lines, count = itm.getExpandedText(varValues, indent) |
733 # It should be done in this way to allow undo |
740 # It should be done in this way to allow undo |
734 editor.beginUndoAction() |
741 editor.beginUndoAction() |
735 if editor.hasSelectedText(): |
742 if editor.hasSelectedText(): |
743 else: |
750 else: |
744 if len(indent) > 0: |
751 if len(indent) > 0: |
745 count += len(indent) |
752 count += len(indent) |
746 |
753 |
747 if "i_n_s_e_r_t_i_o_n" in txt and "s_e_l_e_c_t" in txt: |
754 if "i_n_s_e_r_t_i_o_n" in txt and "s_e_l_e_c_t" in txt: |
748 txt = "'Insertion and selection can not be in" \ |
755 txt = ( |
|
756 "'Insertion and selection can not be in" |
749 " template together'" |
757 " template together'" |
|
758 ) |
750 |
759 |
751 if "i_n_s_e_r_t_i_o_n" in txt: |
760 if "i_n_s_e_r_t_i_o_n" in txt: |
752 lines = 1 |
761 lines = 1 |
753 for aline in txt.splitlines(): |
762 for aline in txt.splitlines(): |
754 count = aline.find("i_n_s_e_r_t_i_o_n") |
763 count = aline.find("i_n_s_e_r_t_i_o_n") |