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