6 """ |
6 """ |
7 Module implementing the Django tags menu handler. |
7 Module implementing the Django tags menu handler. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
|
11 import datetime |
11 |
12 |
12 from PyQt4.QtCore import QObject |
13 from PyQt4.QtCore import QObject |
13 from PyQt4.QtGui import QMenu, QInputDialog, QDialog |
14 from PyQt4.QtGui import QMenu, QInputDialog, QDialog, QApplication |
14 |
15 |
15 from E5Gui.E5Application import e5App |
16 from E5Gui.E5Application import e5App |
16 from E5Gui import E5FileDialog |
17 from E5Gui import E5FileDialog, E5MessageBox |
17 |
18 |
18 from .DjangoTagInputDialog import DjangoTagInputDialog |
19 from .DjangoTagInputDialog import DjangoTagInputDialog |
|
20 |
|
21 import Utilities |
19 |
22 |
20 |
23 |
21 class DjangoTagsMenuHandler(QObject): |
24 class DjangoTagsMenuHandler(QObject): |
22 """ |
25 """ |
23 Class implementing the Django tags menu handler. |
26 Class implementing the Django tags menu handler. |
53 mainMenu.addMenu(self.__initTagsMenu()) |
56 mainMenu.addMenu(self.__initTagsMenu()) |
54 mainMenu.addMenu(self.__initFiltersMenu()) |
57 mainMenu.addMenu(self.__initFiltersMenu()) |
55 mainMenu.addMenu(self.__initHumanizeMenu()) |
58 mainMenu.addMenu(self.__initHumanizeMenu()) |
56 mainMenu.addMenu(self.__initWebDesignMenu()) |
59 mainMenu.addMenu(self.__initWebDesignMenu()) |
57 mainMenu.addMenu(self.__initStaticMenu()) |
60 mainMenu.addMenu(self.__initStaticMenu()) |
|
61 mainMenu.addMenu(self.__initCommentsMenu()) |
|
62 # TODO: add internationalization tags/filters menu |
|
63 # TODO: add localization tags/filters menu |
|
64 # TODO: add timezone tags/filters menu |
58 |
65 |
59 def __initTagsMenu(self): |
66 def __initTagsMenu(self): |
60 """ |
67 """ |
61 Private method to initialize the tags menu. |
68 Private method to initialize the tags menu. |
62 |
69 |
178 """ |
185 """ |
179 Private method to initialize the filters menu. |
186 Private method to initialize the filters menu. |
180 |
187 |
181 @return generated menu (QMenu) |
188 @return generated menu (QMenu) |
182 """ |
189 """ |
183 menu = QMenu(self.tr("Filters")) |
190 # TODO: subdivide into even parts a-i,j-r,s-z |
|
191 mainMenu = QMenu(self.tr("Filters")) |
|
192 |
|
193 menu = QMenu(self.tr("A-I"), mainMenu) |
184 menu.addAction( |
194 menu.addAction( |
185 self.tr("add - Add variable or string"), |
195 self.tr("add - Add variable or string"), |
186 lambda: self.__applyTemplate("add")) |
196 lambda: self.__applyTemplate("add")) |
187 menu.addAction( |
197 menu.addAction( |
188 self.tr("addslashes - Add slashes before quotes"), |
198 self.tr("addslashes - Add slashes before quotes"), |
245 lambda: self.__applyTemplate("get_digit")) |
255 lambda: self.__applyTemplate("get_digit")) |
246 menu.addSeparator() |
256 menu.addSeparator() |
247 menu.addAction( |
257 menu.addAction( |
248 self.tr("iriencode - Convert IRI to string"), |
258 self.tr("iriencode - Convert IRI to string"), |
249 lambda: self.__applyTemplate("iriencode")) |
259 lambda: self.__applyTemplate("iriencode")) |
250 menu.addSeparator() |
260 mainMenu.addMenu(menu) |
|
261 |
|
262 menu = QMenu(self.tr("J-R"), mainMenu) |
251 menu.addAction( |
263 menu.addAction( |
252 self.tr("join - Join list"), |
264 self.tr("join - Join list"), |
253 lambda: self.__applyTemplate("join")) |
265 lambda: self.__applyTemplate("join")) |
254 menu.addSeparator() |
266 menu.addSeparator() |
255 menu.addAction( |
267 menu.addAction( |
298 self.tr("removetags - Remove HTML tags"), |
310 self.tr("removetags - Remove HTML tags"), |
299 lambda: self.__applyTemplate("removetags")) |
311 lambda: self.__applyTemplate("removetags")) |
300 menu.addAction( |
312 menu.addAction( |
301 self.tr("rjust - Right-align value"), |
313 self.tr("rjust - Right-align value"), |
302 lambda: self.__applyTemplate("rjust")) |
314 lambda: self.__applyTemplate("rjust")) |
303 menu.addSeparator() |
315 mainMenu.addMenu(menu) |
|
316 |
|
317 menu = QMenu(self.tr("S-Z"), mainMenu) |
304 menu.addAction( |
318 menu.addAction( |
305 self.tr("safe - Mark as not requiring HTML escaping "), |
319 self.tr("safe - Mark as not requiring HTML escaping "), |
306 lambda: self.__applyTemplate("safe")) |
320 lambda: self.__applyTemplate("safe")) |
307 menu.addAction( |
321 menu.addAction( |
308 self.tr("safeseq - Mark as a safe sequence"), |
322 self.tr("safeseq - Mark as a safe sequence"), |
368 lambda: self.__applyTemplate("wordwrap")) |
382 lambda: self.__applyTemplate("wordwrap")) |
369 menu.addSeparator() |
383 menu.addSeparator() |
370 menu.addAction( |
384 menu.addAction( |
371 self.tr("yesno - Map True, False and None"), |
385 self.tr("yesno - Map True, False and None"), |
372 lambda: self.__applyTemplate("yesno")) |
386 lambda: self.__applyTemplate("yesno")) |
373 |
387 mainMenu.addMenu(menu) |
374 self.__filtersMenu = menu |
388 |
375 return menu |
389 self.__filtersMenu = mainMenu |
|
390 return mainMenu |
376 |
391 |
377 def __initHumanizeMenu(self): |
392 def __initHumanizeMenu(self): |
378 """ |
393 """ |
379 Private method to initialize the humanize menu. |
394 Private method to initialize the humanize menu. |
380 |
395 |
451 menu.addAction( |
466 menu.addAction( |
452 self.tr("get_media_prefix - Insert media URL"), |
467 self.tr("get_media_prefix - Insert media URL"), |
453 lambda: self.__applyTemplate("get_media_prefix")) |
468 lambda: self.__applyTemplate("get_media_prefix")) |
454 |
469 |
455 self.__staticMenu = menu |
470 self.__staticMenu = menu |
|
471 return menu |
|
472 |
|
473 def __initCommentsMenu(self): |
|
474 """ |
|
475 Private method to initialize the static menu. |
|
476 |
|
477 @return generated menu (QMenu) |
|
478 """ |
|
479 menu = QMenu(self.tr("Comment")) |
|
480 menu.addAction( |
|
481 self.tr("Single Line Comment Selected Text"), |
|
482 lambda: self.__applyTemplate("singlelinecommentselect")) |
|
483 menu.addAction( |
|
484 self.tr("Multi Line Comment Selected Text"), |
|
485 lambda: self.__applyTemplate("multilinecommentselect")) |
|
486 menu.addSeparator() |
|
487 menu.addAction( |
|
488 self.tr("Single Line Comment from Input Dialog"), |
|
489 lambda: self.__applyTemplate("singlelinecommentdialog")) |
|
490 menu.addAction( |
|
491 self.tr("Multi Line Comment from Input Dialog"), |
|
492 lambda: self.__applyTemplate("multilinecommentdialog")) |
|
493 menu.addSeparator() |
|
494 menu.addAction( |
|
495 self.tr("Single Line Comment from Clipboard"), |
|
496 lambda: self.__applyTemplate("singlelinecommentclipboard")) |
|
497 menu.addAction( |
|
498 self.tr("Multi Line Comment from Clipboard"), |
|
499 lambda: self.__applyTemplate("multilinecommentclipboard")) |
|
500 menu.addSeparator() |
|
501 menu.addAction( |
|
502 self.tr("Multi Line Comment from File"), |
|
503 lambda: self.__applyTemplate("multilinecommentfile")) |
|
504 menu.addSeparator() |
|
505 menu.addAction( |
|
506 self.tr("Single Line Comment from Date Time Now"), |
|
507 lambda: self.__applyTemplate("singlelinecommentdatetime")) |
|
508 menu.addSeparator() |
|
509 menu.addAction( |
|
510 self.tr("HTML Comment Out Selected Text"), |
|
511 lambda: self.__applyTemplate("htmlcomment")) |
|
512 menu.addAction( |
|
513 self.tr("MS IE Conditional Comment Selected Text"), |
|
514 lambda: self.__applyTemplate("iecomment")) |
|
515 |
|
516 self.__commentsMenu = menu |
456 return menu |
517 return menu |
457 |
518 |
458 def __findTemplateTag(self): |
519 def __findTemplateTag(self): |
459 """ |
520 """ |
460 Private slot to find a template tag and insert its text. |
521 Private slot to find a template tag and insert its text. |
1134 elif tag == "get_static_prefix": |
1195 elif tag == "get_static_prefix": |
1135 templateText = '{% get_static_prefix %}' |
1196 templateText = '{% get_static_prefix %}' |
1136 elif tag == "get_media_prefix": |
1197 elif tag == "get_media_prefix": |
1137 templateText = '{% get_media_prefix %}' |
1198 templateText = '{% get_media_prefix %}' |
1138 |
1199 |
1139 # TODO: add comment functions |
1200 #################################################### |
1140 # TODO: add internationalization tags |
1201 ## Comments ## |
|
1202 #################################################### |
|
1203 |
|
1204 elif tag == "singlelinecommentselect": |
|
1205 templateText = '{{# {0} #}}'.format(selectedText) |
|
1206 replace = True |
|
1207 elif tag == "multilinecommentselect": |
|
1208 templateText = '{{% comment %}} {0} {{% endcomment }}'.format( |
|
1209 selectedText) |
|
1210 replace = True |
|
1211 elif tag == "singlelinecommentdialog": |
|
1212 data, ok = DjangoTagInputDialog.getText( |
|
1213 None, |
|
1214 self.tr("Single Line Comment"), |
|
1215 [self.tr("Enter comment:")], |
|
1216 [""]) |
|
1217 if ok: |
|
1218 templateText = '{{# {0} #}}'.format(data[0]) |
|
1219 elif tag == "multilinecommentdialog": |
|
1220 from .MultiLineInputDialog import MultiLineInputDialog |
|
1221 comment, ok = MultiLineInputDialog.getText( |
|
1222 None, self.tr("Multi Line Comment"), |
|
1223 self.tr("Enter comment:"), "") |
|
1224 if ok: |
|
1225 templateText = '{{% comment %}} {0} {{% endcomment }}'.format( |
|
1226 comment) |
|
1227 elif tag == "singlelinecommentclipboard": |
|
1228 templateText = '{{# {0} #}}'.format( |
|
1229 QApplication.clipboard().text().strip()) |
|
1230 elif tag == "multilinecommentclipboard": |
|
1231 templateText = '{{% comment %}} {0} {{% endcomment }}'.format( |
|
1232 QApplication.clipboard().text().strip()) |
|
1233 elif tag == "multilinecommentfile": |
|
1234 filename = E5FileDialog.getOpenFileName( |
|
1235 None, |
|
1236 self.tr("Comment File"), |
|
1237 Utilities.getHomeDir(), |
|
1238 self.tr("All Files (*)")) |
|
1239 if filename: |
|
1240 try: |
|
1241 f = open(filename, "r", encoding="utf-8") |
|
1242 comment = f.read() |
|
1243 f.close() |
|
1244 templateText = ( |
|
1245 '{{% comment %}} {0} {{% endcomment }}'.format( |
|
1246 comment)) |
|
1247 except (IOError, OSError) as err: |
|
1248 E5MessageBox.critical( |
|
1249 None, |
|
1250 self.tr("Comment File"), |
|
1251 self.tr("""<p>The file <b>{0}</b> could not be""" |
|
1252 """ read.</p><p>Reason: {1}</p>""").format( |
|
1253 str(err))) |
|
1254 elif tag == "singlelinecommentdatetime": |
|
1255 templateText = '{{# {0} by {1} #}}'.format( |
|
1256 datetime.datetime.now().isoformat().split(), |
|
1257 Utilities.getUserName()) |
|
1258 elif tag == "htmlcomment": |
|
1259 templateText = '<!-- {0} -->'.format(selectedText) |
|
1260 replace = True |
|
1261 elif tag == "iecomment": |
|
1262 from .IeCommentDialog import IeCommentDialog |
|
1263 tag, ok = IeCommentDialog.getTag(selectedText) |
|
1264 if ok: |
|
1265 templateText = '<!--{0}-->'.format(tag) |
|
1266 replace = True |
|
1267 |
|
1268 # TODO: add internationalization tags/filters |
|
1269 # TODO: add localization tags/filters |
|
1270 # TODO: add timezone tags/filters |
1141 #################################################### |
1271 #################################################### |
1142 ## Fallback: return just the tag name ## |
1272 ## Fallback: return just the tag name ## |
1143 #################################################### |
1273 #################################################### |
1144 |
1274 |
1145 else: |
1275 else: |