|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select the template type. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, QCoreApplication |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_FormSelectionDialog import Ui_FormSelectionDialog |
|
14 |
|
15 |
|
16 class FormSelectionDialog(QDialog, Ui_FormSelectionDialog): |
|
17 """ |
|
18 Class implementing a dialog to select the template type. |
|
19 """ |
|
20 Templates = { |
|
21 "html5": { |
|
22 "title": QCoreApplication.translate( |
|
23 "FormSelectionDialog", "Standard HTML 5 template"), |
|
24 "template": '''<!DOCTYPE html>\n''' |
|
25 ''' <head>\n''' |
|
26 ''' <title></title>\n''' |
|
27 ''' <style>\n''' |
|
28 ''' </style>\n''' |
|
29 ''' </head>\n''' |
|
30 '''\n''' |
|
31 ''' <body>\n''' |
|
32 ''' </body>\n''' |
|
33 '''</html>\n''', |
|
34 }, |
|
35 "basic_jinja": { |
|
36 "title": QCoreApplication.translate( |
|
37 "FormSelectionDialog", "Basic Jinja template"), |
|
38 "template": '''<!DOCTYPE html>\n''' |
|
39 ''' <head>\n''' |
|
40 ''' {% if title %}\n''' |
|
41 ''' <title>{{ title }}</title>\n''' |
|
42 ''' {% else %}\n''' |
|
43 ''' <title>Title</title>\n''' |
|
44 ''' {% endif %}\n''' |
|
45 ''' </head>\n''' |
|
46 '''\n''' |
|
47 ''' <body>\n''' |
|
48 ''' <h1>Hello, {{ user.username }}!</h1>\n''' |
|
49 ''' </body>\n''' |
|
50 '''</html>\n''', |
|
51 }, |
|
52 "jinja_base": { |
|
53 "title": QCoreApplication.translate( |
|
54 "FormSelectionDialog", "Jinja base template"), |
|
55 "template": '''<!DOCTYPE html>\n''' |
|
56 ''' <head>\n''' |
|
57 ''' {% if title %}\n''' |
|
58 ''' <title>{{ title }}</title>\n''' |
|
59 ''' {% else %}\n''' |
|
60 ''' <title>Title</title>\n''' |
|
61 ''' {% endif %}\n''' |
|
62 ''' </head>\n''' |
|
63 '''\n''' |
|
64 ''' <body>\n''' |
|
65 ''' {% block content %}{% endblock %}\n''' |
|
66 ''' </body>\n''' |
|
67 '''</html>\n''', |
|
68 }, |
|
69 "jinja_extends": { |
|
70 "title": QCoreApplication.translate( |
|
71 "FormSelectionDialog", "Jinja extension template"), |
|
72 "template": '''{% extends "base.html" %}\n''' |
|
73 '''\n''' |
|
74 '''{% block content %}\n''' |
|
75 ''' <h1>{{ user.username }}</h1>\n''' |
|
76 ''' {% for item in items %}\n''' |
|
77 ''' <div><p>{{ item.field1 }}: {{ item.field2 }}''' |
|
78 '''</p></div>\n''' |
|
79 ''' {% endfor %}\n''' |
|
80 '''{% endblock %}\n''' |
|
81 }, |
|
82 } |
|
83 |
|
84 def __init__(self, parent=None): |
|
85 """ |
|
86 Constructor |
|
87 |
|
88 @param parent reference to the parent widget |
|
89 @type QWidget |
|
90 """ |
|
91 super(FormSelectionDialog, self).__init__(parent) |
|
92 self.setupUi(self) |
|
93 |
|
94 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
95 |
|
96 self.typeCombo.addItem("") |
|
97 for templateType in FormSelectionDialog.Templates: |
|
98 self.typeCombo.addItem( |
|
99 FormSelectionDialog.Templates[templateType]["title"], |
|
100 templateType) |
|
101 self.typeCombo.setCurrentIndex(0) |
|
102 |
|
103 @pyqtSlot(int) |
|
104 def on_typeCombo_currentIndexChanged(self, index): |
|
105 """ |
|
106 Private slot to act upon a change of the selected template type. |
|
107 |
|
108 @param index selected index |
|
109 @type int |
|
110 """ |
|
111 templateType = self.typeCombo.itemData(index) |
|
112 if templateType: |
|
113 self.preview.setPlainText( |
|
114 FormSelectionDialog.Templates[templateType]["template"]) |
|
115 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True) |
|
116 else: |
|
117 self.preview.clear() |
|
118 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
119 |
|
120 def getTemplateText(self): |
|
121 """ |
|
122 Public method to get the template text. |
|
123 |
|
124 @return text of the template |
|
125 @rtype str |
|
126 """ |
|
127 templateType = self.typeCombo.currentData() |
|
128 return FormSelectionDialog.Templates[templateType]["template"] |