Thu, 30 Dec 2021 11:20:01 +0100
Updated copyright for 2022.
# -*- coding: utf-8 -*- # Copyright (c) 2020 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a dialog to select the template type. """ from PyQt6.QtCore import pyqtSlot, QCoreApplication from PyQt6.QtWidgets import QDialog, QDialogButtonBox from .Ui_FormSelectionDialog import Ui_FormSelectionDialog class FormSelectionDialog(QDialog, Ui_FormSelectionDialog): """ Class implementing a dialog to select the template type. """ Templates = { "html5": { "title": QCoreApplication.translate( "FormSelectionDialog", "Standard HTML 5 template"), "template": '''<!DOCTYPE html>\n''' ''' <head>\n''' ''' <title></title>\n''' ''' <style>\n''' ''' </style>\n''' ''' </head>\n''' '''\n''' ''' <body>\n''' ''' </body>\n''' '''</html>\n''', }, "basic_jinja": { "title": QCoreApplication.translate( "FormSelectionDialog", "Basic Jinja template"), "template": '''<!DOCTYPE html>\n''' ''' <head>\n''' ''' {% if title %}\n''' ''' <title>{{ title }}</title>\n''' ''' {% else %}\n''' ''' <title>Title</title>\n''' ''' {% endif %}\n''' ''' </head>\n''' '''\n''' ''' <body>\n''' ''' <h1>Hello, {{ user.username }}!</h1>\n''' ''' </body>\n''' '''</html>\n''', }, "jinja_base": { "title": QCoreApplication.translate( "FormSelectionDialog", "Jinja base template"), "template": '''<!DOCTYPE html>\n''' ''' <head>\n''' ''' {% if title %}\n''' ''' <title>{{ title }}</title>\n''' ''' {% else %}\n''' ''' <title>Title</title>\n''' ''' {% endif %}\n''' ''' </head>\n''' '''\n''' ''' <body>\n''' ''' {% block content %}{% endblock %}\n''' ''' </body>\n''' '''</html>\n''', }, "jinja_extends": { "title": QCoreApplication.translate( "FormSelectionDialog", "Jinja extension template"), "template": '''{% extends "base.html" %}\n''' '''\n''' '''{% block content %}\n''' ''' <h1>{{ user.username }}</h1>\n''' ''' {% for item in items %}\n''' ''' <div><p>{{ item.field1 }}: {{ item.field2 }}''' '''</p></div>\n''' ''' {% endfor %}\n''' '''{% endblock %}\n''' }, } def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ super().__init__(parent) self.setupUi(self) self.buttonBox.button( QDialogButtonBox.StandardButton.Ok).setEnabled(False) self.typeCombo.addItem("") for templateType in FormSelectionDialog.Templates: self.typeCombo.addItem( FormSelectionDialog.Templates[templateType]["title"], templateType) self.typeCombo.setCurrentIndex(0) @pyqtSlot(int) def on_typeCombo_currentIndexChanged(self, index): """ Private slot to act upon a change of the selected template type. @param index selected index @type int """ templateType = self.typeCombo.itemData(index) if templateType: self.preview.setPlainText( FormSelectionDialog.Templates[templateType]["template"]) self.buttonBox.button( QDialogButtonBox.StandardButton.Ok).setEnabled(True) else: self.preview.clear() self.buttonBox.button( QDialogButtonBox.StandardButton.Ok).setEnabled(False) def getTemplateText(self): """ Public method to get the template text. @return text of the template @rtype str """ templateType = self.typeCombo.currentData() return FormSelectionDialog.Templates[templateType]["template"]