|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit the PyBabel configuration. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot, Qt |
|
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
14 |
|
15 from E5Gui.E5PathPicker import E5PathPickerModes |
|
16 from E5Gui.E5Application import e5App |
|
17 |
|
18 from .Ui_PyBabelConfigDialog import Ui_PyBabelConfigDialog |
|
19 |
|
20 |
|
21 class PyBabelConfigDialog(QDialog, Ui_PyBabelConfigDialog): |
|
22 """ |
|
23 Class implementing a dialog to edit the PyBabel configuration. |
|
24 """ |
|
25 def __init__(self, configuration, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param configuration current pybabel configuration |
|
30 @type dict |
|
31 @param parent reference to the parent widget |
|
32 @type QWidget |
|
33 """ |
|
34 super(PyBabelConfigDialog, self).__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 self.__e5project = e5App().getObject("Project") |
|
38 |
|
39 self.configFilePicker.setMode( |
|
40 E5PathPickerModes.SaveFileEnsureExtensionMode) |
|
41 self.configFilePicker.setFilters(self.tr( |
|
42 "Configuration Files (*.cfg);;" |
|
43 "All Files (*)" |
|
44 )) |
|
45 self.configFilePicker.setDefaultDirectory( |
|
46 self.__e5project.getProjectPath()) |
|
47 |
|
48 self.translationsDirectoryPicker.setMode( |
|
49 E5PathPickerModes.DirectoryMode) |
|
50 self.translationsDirectoryPicker.setDefaultDirectory( |
|
51 self.__e5project.getProjectPath()) |
|
52 |
|
53 self.catalogFilePicker.setMode( |
|
54 E5PathPickerModes.SaveFileEnsureExtensionMode) |
|
55 self.catalogFilePicker.setFilters(self.tr( |
|
56 "Message Catalog Files (*.pot);;" |
|
57 "All Files (*)" |
|
58 )) |
|
59 self.catalogFilePicker.setDefaultDirectory( |
|
60 self.__e5project.getProjectPath()) |
|
61 |
|
62 self.configFilePicker.setFocus(Qt.OtherFocusReason) |
|
63 |
|
64 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
65 |
|
66 if "configFile" in configuration: |
|
67 self.configFilePicker.setText( |
|
68 self.__e5project.getAbsoluteUniversalPath( |
|
69 configuration["configFile"])) |
|
70 if "translationsDirectory" in configuration: |
|
71 self.translationsDirectoryPicker.setText( |
|
72 self.__e5project.getAbsoluteUniversalPath( |
|
73 configuration["translationsDirectory"])) |
|
74 if "domain" in configuration: |
|
75 self.domainEdit.setText(configuration["domain"]) |
|
76 if "catalogFile" in configuration: |
|
77 self.catalogFilePicker.setText( |
|
78 self.__e5project.getAbsoluteUniversalPath( |
|
79 configuration["catalogFile"])) |
|
80 if "markersList" in configuration: |
|
81 self.markersEdit.setText(" ".join(configuration["markersList"])) |
|
82 |
|
83 msh = self.minimumSizeHint() |
|
84 self.resize(max(self.width(), msh.width()), msh.height()) |
|
85 |
|
86 def getConfiguration(self): |
|
87 """ |
|
88 Public method to get the entered configuration data. |
|
89 |
|
90 @return pybabel configuration |
|
91 @rtype dict |
|
92 """ |
|
93 configuration = { |
|
94 "configFile": self.__e5project.getRelativeUniversalPath( |
|
95 self.configFilePicker.text()), |
|
96 "translationsDirectory": self.__e5project.getRelativeUniversalPath( |
|
97 self.translationsDirectoryPicker.text()), |
|
98 } |
|
99 |
|
100 domain = self.domainEdit.text() |
|
101 if domain: |
|
102 configuration["domain"] = domain |
|
103 else: |
|
104 configuration["domain"] = "messages" |
|
105 |
|
106 catalogFile = self.catalogFilePicker.text() |
|
107 if not catalogFile: |
|
108 # use a default name made of translations dir and domain |
|
109 catalogFile = os.path.join( |
|
110 configuration["translationsDirectory"], |
|
111 "{0}.pot".format(configuration["domain"])) |
|
112 configuration["catalogFile"] = ( |
|
113 self.__e5project.getRelativeUniversalPath(catalogFile) |
|
114 ) |
|
115 |
|
116 if self.markersEdit.text(): |
|
117 configuration["markersList"] = self.markersEdit.text().split() |
|
118 |
|
119 return configuration |
|
120 |
|
121 def __updateOK(self): |
|
122 """ |
|
123 Private method to update the status of the OK button. |
|
124 """ |
|
125 enable = ( |
|
126 bool(self.configFilePicker.text()) and |
|
127 bool(self.translationsDirectoryPicker.text()) |
|
128 ) |
|
129 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable) |
|
130 |
|
131 def __updateCatalogPicker(self): |
|
132 """ |
|
133 Private method to update the contents of the catalog picker. |
|
134 """ |
|
135 translationsDirectory = self.translationsDirectoryPicker.text() |
|
136 domain = self.domainEdit.text() |
|
137 self.catalogFilePicker.setText(os.path.join( |
|
138 translationsDirectory, "{0}.pot".format(domain))) |
|
139 |
|
140 @pyqtSlot(str) |
|
141 def on_configFilePicker_textChanged(self, txt): |
|
142 """ |
|
143 Private slot to handle a change of the configuration file name. |
|
144 |
|
145 @param txt configuration file name |
|
146 @type str |
|
147 """ |
|
148 self.__updateOK() |
|
149 |
|
150 @pyqtSlot(str) |
|
151 def on_translationsDirectoryPicker_textChanged(self, txt): |
|
152 """ |
|
153 Private slot to handle a change of the catalog file name. |
|
154 |
|
155 @param txt configuration file name |
|
156 @type str |
|
157 """ |
|
158 self.__updateOK() |
|
159 self.__updateCatalogPicker() |
|
160 |
|
161 @pyqtSlot(str) |
|
162 def on_domainEdit_textChanged(self, txt): |
|
163 """ |
|
164 Private slot to handle a change of the translations domain. |
|
165 |
|
166 @param txt entered translations domain |
|
167 @type str |
|
168 """ |
|
169 self.__updateCatalogPicker() |