|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the font dialog wizard dialog. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt6.QtCore import pyqtSlot |
|
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QFontDialog |
|
14 |
|
15 from .Ui_FontDialogWizardDialog import Ui_FontDialogWizardDialog |
|
16 |
|
17 |
|
18 class 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 FontWeight2Code = { |
|
26 100: "Thin", |
|
27 200: "ExtraLight", |
|
28 300: "Light", |
|
29 400: "Normal", |
|
30 500: "Medium", |
|
31 600: "DemiBold", |
|
32 700: "Bold", |
|
33 800: "ExtraBold", |
|
34 900: "Black", |
|
35 } |
|
36 |
|
37 def __init__(self, parent=None): |
|
38 """ |
|
39 Constructor |
|
40 |
|
41 @param parent parent widget (QWidget) |
|
42 """ |
|
43 super().__init__(parent) |
|
44 self.setupUi(self) |
|
45 |
|
46 self.bTest = self.buttonBox.addButton( |
|
47 self.tr("Test"), QDialogButtonBox.ButtonRole.ActionRole) |
|
48 |
|
49 self.font = None |
|
50 self.fontOptions = { |
|
51 "noNativeDialog": False, |
|
52 "scalableFonts": False, |
|
53 "nonScalableFonts": False, |
|
54 "monospacedFonts": False, |
|
55 "proportionalFonts": False, |
|
56 } |
|
57 |
|
58 msh = self.minimumSizeHint() |
|
59 self.resize(max(self.width(), msh.width()), msh.height()) |
|
60 |
|
61 def on_buttonBox_clicked(self, button): |
|
62 """ |
|
63 Private slot called by a button of the button box clicked. |
|
64 |
|
65 @param button button that was clicked (QAbstractButton) |
|
66 """ |
|
67 if button == self.bTest: |
|
68 self.on_bTest_clicked() |
|
69 |
|
70 @pyqtSlot() |
|
71 def on_bTest_clicked(self): |
|
72 """ |
|
73 Private method to test the selected options. |
|
74 """ |
|
75 if self.font is None: |
|
76 QFontDialog.getFont() |
|
77 else: |
|
78 options = QFontDialog.FontDialogOption(0) |
|
79 if any(self.fontOptions.values()): |
|
80 if self.fontOptions["noNativeDialog"]: |
|
81 options |= QFontDialog.FontDialogOption.DontUseNativeDialog |
|
82 if self.fontOptions["scalableFonts"]: |
|
83 options |= QFontDialog.FontDialogOption.ScalableFonts |
|
84 if self.fontOptions["nonScalableFonts"]: |
|
85 options |= QFontDialog.FontDialogOption.NonScalableFonts |
|
86 if self.fontOptions["monospacedFonts"]: |
|
87 options |= QFontDialog.FontDialogOption.MonospacedFonts |
|
88 if self.fontOptions["proportionalFonts"]: |
|
89 options |= QFontDialog.FontDialogOption.ProportionalFonts |
|
90 QFontDialog.getFont( |
|
91 self.font, |
|
92 self, |
|
93 self.eCaption.text(), |
|
94 options |
|
95 ) |
|
96 |
|
97 def on_eVariable_textChanged(self, text): |
|
98 """ |
|
99 Private slot to handle the textChanged signal of eVariable. |
|
100 |
|
101 @param text the new text (string) |
|
102 """ |
|
103 if not text: |
|
104 self.bTest.setEnabled(True) |
|
105 else: |
|
106 self.bTest.setEnabled(False) |
|
107 |
|
108 @pyqtSlot() |
|
109 def on_fontButton_clicked(self): |
|
110 """ |
|
111 Private slot to handle the button press to select a font via a |
|
112 font selection dialog. |
|
113 """ |
|
114 if self.font is None: |
|
115 font, ok = QFontDialog.getFont() |
|
116 else: |
|
117 font, ok = QFontDialog.getFont(self.font) |
|
118 if ok: |
|
119 self.font = font |
|
120 else: |
|
121 self.font = None |
|
122 |
|
123 @pyqtSlot() |
|
124 def on_optionsButton_clicked(self): |
|
125 """ |
|
126 Private slot to handle the selection of font dialog options. |
|
127 """ |
|
128 from .FontDialogOptionsDialog import FontDialogOptionsDialog |
|
129 dlg = FontDialogOptionsDialog(self.fontOptions, self) |
|
130 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
131 self.fontOptions = dlg.getOptions() |
|
132 |
|
133 def getCode(self, indLevel, indString): |
|
134 """ |
|
135 Public method to get the source code. |
|
136 |
|
137 @param indLevel indentation level (int) |
|
138 @param indString string used for indentation (space or tab) (string) |
|
139 @return generated code (string) |
|
140 """ |
|
141 # calculate our indentation level and the indentation string |
|
142 il = indLevel + 1 |
|
143 istring = il * indString |
|
144 estring = os.linesep + indLevel * indString |
|
145 |
|
146 # generate the code |
|
147 resvar = self.eResultVar.text() |
|
148 if not resvar: |
|
149 resvar = "font" |
|
150 title = self.eCaption.text() |
|
151 if self.parentSelf.isChecked(): |
|
152 parent = "self" |
|
153 elif self.parentNone.isChecked(): |
|
154 parent = "None" |
|
155 elif self.parentOther.isChecked(): |
|
156 parent = self.parentEdit.text() |
|
157 if parent == "": |
|
158 parent = "None" |
|
159 |
|
160 code = '{0}, ok = QFontDialog.getFont('.format(resvar) |
|
161 if self.eVariable.text() or self.font is not None: |
|
162 if title or parent != "None": |
|
163 code += '{0}{1}'.format(os.linesep, istring) |
|
164 if not self.eVariable.text(): |
|
165 if self.font is not None: |
|
166 code += ( |
|
167 'QFont(["{0}"], {1:d}, QFont.Weight.{2}, {3})' |
|
168 .format( |
|
169 self.font.family(), |
|
170 self.font.pointSize(), |
|
171 FontDialogWizardDialog.FontWeight2Code[ |
|
172 self.font.weight()], |
|
173 "True" if self.font.italic() else "False") |
|
174 ) |
|
175 else: |
|
176 code += self.eVariable.text() |
|
177 if title: |
|
178 code += ',{0}{1}{2}'.format( |
|
179 os.linesep, istring, parent) |
|
180 code += ',{0}{1}self.tr("{2}")'.format( |
|
181 os.linesep, istring, title) |
|
182 elif parent != "None": |
|
183 code += ',{0}{1}{2}'.format( |
|
184 os.linesep, istring, parent) |
|
185 if any(self.fontOptions.values()): |
|
186 options = [] |
|
187 if self.fontOptions["noNativeDialog"]: |
|
188 options.append( |
|
189 "QFontDialog.FontDialogOption.DontUseNativeDialog") |
|
190 if self.fontOptions["scalableFonts"]: |
|
191 options.append( |
|
192 "QFontDialog.FontDialogOption.ScalableFonts") |
|
193 if self.fontOptions["nonScalableFonts"]: |
|
194 options.append( |
|
195 "QFontDialog.FontDialogOption.NonScalableFonts") |
|
196 if self.fontOptions["monospacedFonts"]: |
|
197 options.append( |
|
198 "QFontDialog.FontDialogOption.MonospacedFonts") |
|
199 if self.fontOptions["proportionalFonts"]: |
|
200 options.append( |
|
201 "QFontDialog.FontDialogOption.ProportionalFonts") |
|
202 fontOptionsString = ( |
|
203 ' |{0}{1}'.format(os.linesep, istring).join(options) |
|
204 ) |
|
205 code += ',{0}{1}{2}'.format( |
|
206 os.linesep, istring, fontOptionsString) |
|
207 code += '{0}){0}'.format(estring) |
|
208 |
|
209 return code |