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