src/eric7/Templates/TemplatePropertiesDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2005 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the templates properties dialog.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, Qt, QRegularExpression
11 from PyQt6.QtGui import QRegularExpressionValidator
12 from PyQt6.QtWidgets import QDialog
13
14 from .Ui_TemplatePropertiesDialog import Ui_TemplatePropertiesDialog
15
16 from EricWidgets import EricMessageBox
17
18 import Preferences
19
20
21 class TemplatePropertiesDialog(QDialog, Ui_TemplatePropertiesDialog):
22 """
23 Class implementing the templates properties dialog.
24 """
25 def __init__(self, parent, groupMode=False, itm=None):
26 """
27 Constructor
28
29 @param parent the parent widget (QWidget)
30 @param groupMode flag indicating group mode (boolean)
31 @param itm item (TemplateEntry or TemplateGroup) to
32 read the data from
33 """
34 super().__init__(parent)
35 self.setupUi(self)
36
37 self.templateEdit.setFont(Preferences.getTemplates("EditorFont"))
38
39 if not groupMode:
40 self.nameEdit.setWhatsThis(self.tr(
41 """<b>Template name<b><p>Enter the name of the template."""
42 """ Templates may be autocompleted upon this name."""
43 """ In order to support autocompletion. the template name"""
44 """ must only consist of letters (a-z and A-Z),"""
45 """ digits (0-9) and underscores (_).</p>"""
46 ))
47 self.__nameValidator = QRegularExpressionValidator(
48 QRegularExpression("[a-zA-Z0-9_]+"), self.nameEdit)
49 self.nameEdit.setValidator(self.__nameValidator)
50
51 import QScintilla.Lexers
52 self.languages = [("All", self.tr("All"))]
53 supportedLanguages = QScintilla.Lexers.getSupportedLanguages()
54 languages = sorted(supportedLanguages.keys())
55 for language in languages:
56 self.languages.append((language, supportedLanguages[language][0]))
57
58 self.groupMode = groupMode
59 if groupMode:
60 self.groupLabel.setText(self.tr("Language:"))
61 for lang, langDisp in self.languages:
62 self.groupCombo.addItem(
63 QScintilla.Lexers.getLanguageIcon(lang, False),
64 langDisp)
65 self.templateLabel.setEnabled(False)
66 self.templateEdit.setEnabled(False)
67 self.templateEdit.setPlainText(self.tr("GROUP"))
68 self.helpButton.setEnabled(False)
69 self.descriptionLabel.hide()
70 self.descriptionEdit.hide()
71 else:
72 groups = []
73 for group in parent.getGroupNames():
74 groups.append(group)
75 self.groupCombo.addItems(groups)
76
77 if itm is not None:
78 self.nameEdit.setText(itm.getName())
79 if groupMode:
80 language = itm.getLanguage()
81 for lang, langDisp in self.languages:
82 if language == lang:
83 self.setSelectedGroup(langDisp)
84 break
85 else:
86 self.setSelectedGroup(itm.getGroupName())
87 self.templateEdit.setPlainText(itm.getTemplateText())
88 self.descriptionEdit.setText(itm.getDescription())
89
90 self.nameEdit.selectAll()
91
92 self.__helpDialog = None
93
94 def keyPressEvent(self, ev):
95 """
96 Protected method to handle the user pressing the escape key.
97
98 @param ev key event (QKeyEvent)
99 """
100 if ev.key() == Qt.Key.Key_Escape:
101 res = EricMessageBox.yesNo(
102 self,
103 self.tr("Close dialog"),
104 self.tr("""Do you really want to close the dialog?"""))
105 if not res:
106 self.reject()
107
108 @pyqtSlot()
109 def on_helpButton_clicked(self):
110 """
111 Private slot to show some help.
112 """
113 if self.__helpDialog is None:
114 from EricWidgets.EricSimpleHelpDialog import EricSimpleHelpDialog
115 self.__helpDialog = EricSimpleHelpDialog(
116 title=self.tr("Template Help"),
117 label=self.tr("<b>Template Help</b>"),
118 helpStr=self.tr(
119 """<p>To use variables in a template, you just have to"""
120 """ enclose the variable name with $-characters. When"""
121 """ you use the template, you will then be asked for a"""
122 """ value for this variable.</p>"""
123 """<p>Example template: This is a $VAR$</p>"""
124 """<p>When you use this template you will be prompted"""
125 """ for a value for the variable $VAR$. Any occurrences"""
126 """ of $VAR$ will then be replaced with whatever you've"""
127 """ entered.</p>"""
128 """<p>If you need a single $-character in a template,"""
129 """ which is not used to enclose a variable, type"""
130 """ $$(two dollar characters) instead. They will"""
131 """ automatically be replaced with a single $-character"""
132 """ when you use the template.</p>"""
133 """<p>If you want a variables contents to be treated"""
134 """ specially, the variable name must be followed by a"""
135 """ ':' and one formatting specifier (e.g. $VAR:ml$)."""
136 """ The supported specifiers are:"""
137 """<table>"""
138 """<tr><td>ml</td><td>Specifies a multiline formatting."""
139 """ The first line of the variable contents is prefixed"""
140 """ with the string occurring before the variable on"""
141 """ the same line of the template. All other lines are"""
142 """ prefixed by the same amount of whitespace as the"""
143 """ line containing the variable."""
144 """</td></tr>"""
145 """<tr><td>rl</td><td>Specifies a repeated line"""
146 """ formatting. Each line of the variable contents is"""
147 """ prefixed with the string occurring before the"""
148 """ variable on the same line of the template."""
149 """</td></tr>"""
150 """</table></p>"""
151 """<p>The following predefined variables may be used"""
152 """ in a template:"""
153 """<table>"""
154 """<tr><td>date</td>"""
155 """<td>today's date in ISO format (YYYY-MM-DD)</td></tr>"""
156 """<tr><td>year</td>"""
157 """<td>the current year</td></tr>"""
158 """<tr><td>time</td>"""
159 """<td>current time in ISO format (hh:mm:ss)</td></tr>"""
160 """<tr><td>project_name</td>"""
161 """<td>the name of the project (if any)</td></tr>"""
162 """<tr><td>project_path</td>"""
163 """<td>the path of the project (if any)</td></tr>"""
164 """<tr><td>path_name</td>"""
165 """<td>full path of the current file</td></tr>"""
166 """<tr><td>path_name_rel</td>"""
167 """<td>project relative path of the current file</td>"""
168 """</tr>"""
169 """<tr><td>dir_name</td>"""
170 """<td>full path of the current file's directory</td>"""
171 """</tr>"""
172 """<tr><td>dir_name_rel</td>"""
173 """<td>project relative path of the current file's"""
174 """ directory</td></tr>"""
175 """<tr><td>file_name</td>"""
176 """<td>the current file's name (without directory)"""
177 """</td></tr>"""
178 """<tr><td>base_name</td>"""
179 """<td>like <i>file_name</i>, but without extension"""
180 """</td></tr>"""
181 """<tr><td>ext</td>"""
182 """<td>the extension of the current file</td></tr>"""
183 """<tr><td>cur_select</td>"""
184 """<td>the currently selected text</td></tr>"""
185 """<tr><td>insertion</td>"""
186 """<td>Sets insertion point for cursor after template is"""
187 """ inserted.</td>"""
188 """</tr>"""
189 """<tr><td>select_start</td>"""
190 """<td>Sets span of selected text in template after"""
191 """ template is inserted (used together with"""
192 """ 'select_end').</td></tr>"""
193 """<tr><td>select_end</td>"""
194 """<td>Sets span of selected text in template after"""
195 """ template is inserted (used together with"""
196 """ 'select_start')."""
197 """</td></tr>"""
198 """<tr><td>clipboard</td>"""
199 """<td>the text of the clipboard</td></tr>"""
200 """</table></p>"""
201 """<p>If you want to change the default delimiter to"""
202 """ anything different, please use the configuration"""
203 """ dialog to do so.</p>"""
204 ),
205 parent=self)
206 self.__helpDialog.show()
207
208 def setSelectedGroup(self, name):
209 """
210 Public method to select a group.
211
212 @param name name of the group to be selected (string)
213 """
214 index = self.groupCombo.findText(name)
215 self.groupCombo.setCurrentIndex(index)
216
217 def getData(self):
218 """
219 Public method to get the data entered into the dialog.
220
221 @return a tuple of two strings (name, language), if the dialog is in
222 group mode, and a tuple of four strings (name, description, group
223 name, template) otherwise.
224 """
225 if self.groupMode:
226 return (self.nameEdit.text(),
227 self.languages[self.groupCombo.currentIndex()][0]
228 )
229 else:
230 return (self.nameEdit.text(),
231 self.descriptionEdit.text(),
232 self.groupCombo.currentText(),
233 self.templateEdit.toPlainText()
234 )

eric ide

mercurial