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