1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the font dialog wizard dialog.
8"""
9
10import os
11
12from PyQt5.QtCore import pyqtSlot
13from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QFontDialog
14
15from .Ui_FontDialogWizardDialog import Ui_FontDialogWizardDialog
16
17
18class FontDialogWizardDialog(QDialog, Ui_FontDialogWizardDialog):
19    """
20    Class implementing the font dialog wizard dialog.
21
22    It displays a dialog for entering the parameters
23    for the QFontDialog code generator.
24    """
25    def __init__(self, parent=None):
26        """
27        Constructor
28
29        @param parent parent widget (QWidget)
30        """
31        super().__init__(parent)
32        self.setupUi(self)
33
34        self.bTest = self.buttonBox.addButton(
35            self.tr("Test"), QDialogButtonBox.ButtonRole.ActionRole)
36
37        self.font = None
38
39        msh = self.minimumSizeHint()
40        self.resize(max(self.width(), msh.width()), msh.height())
41
42    def on_buttonBox_clicked(self, button):
43        """
44        Private slot called by a button of the button box clicked.
45
46        @param button button that was clicked (QAbstractButton)
47        """
48        if button == self.bTest:
49            self.on_bTest_clicked()
50
51    @pyqtSlot()
52    def on_bTest_clicked(self):
53        """
54        Private method to test the selected options.
55        """
56        if self.font is None:
57            QFontDialog.getFont()
58        else:
59            QFontDialog.getFont(self.font)
60
61    def on_eVariable_textChanged(self, text):
62        """
63        Private slot to handle the textChanged signal of eVariable.
64
65        @param text the new text (string)
66        """
67        if not text:
68            self.bTest.setEnabled(True)
69        else:
70            self.bTest.setEnabled(False)
71
72    @pyqtSlot()
73    def on_fontButton_clicked(self):
74        """
75        Private slot to handle the button press to select a font via a
76        font selection dialog.
77        """
78        if self.font is None:
79            font, ok = QFontDialog.getFont()
80        else:
81            font, ok = QFontDialog.getFont(self.font)
82        if ok:
83            self.font = font
84        else:
85            self.font = None
86
87    def getCode(self, indLevel, indString):
88        """
89        Public method to get the source code.
90
91        @param indLevel indentation level (int)
92        @param indString string used for indentation (space or tab) (string)
93        @return generated code (string)
94        """
95        # calculate our indentation level and the indentation string
96        il = indLevel + 1
97        istring = il * indString
98        estring = os.linesep + indLevel * indString
99
100        # generate the code
101        resvar = self.eResultVar.text()
102        if not resvar:
103            resvar = "font"
104        title = self.eCaption.text()
105        if self.parentSelf.isChecked():
106            parent = "self"
107        elif self.parentNone.isChecked():
108            parent = "None"
109        elif self.parentOther.isChecked():
110            parent = self.parentEdit.text()
111            if parent == "":
112                parent = "None"
113
114        code = '{0}, ok = QFontDialog.getFont('.format(resvar)
115        if self.eVariable.text() or self.font is not None:
116            if title or parent != "None":
117                code += '{0}{1}'.format(os.linesep, istring)
118            if not self.eVariable.text():
119                if self.font is not None:
120                    code += 'QFont("{0}", {1:d}, {2:d}, {3:d})'.format(
121                        self.font.family(), self.font.pointSize(),
122                        self.font.weight(), self.font.italic())
123            else:
124                code += self.eVariable.text()
125            if title:
126                code += ',{0}{1}{2}'.format(
127                    os.linesep, istring, parent)
128                code += ',{0}{1}self.tr("{2}")'.format(
129                    os.linesep, istring, title)
130            elif parent != "None":
131                code += ',{0}{1}{2}'.format(
132                    os.linesep, istring, parent)
133        code += '){0}'.format(estring)
134
135        return code
136