|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Translations Properties dialog. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import * |
|
13 from PyQt4.QtGui import * |
|
14 |
|
15 from E4Gui.E4Completers import E4FileCompleter, E4DirCompleter |
|
16 |
|
17 from Ui_TranslationPropertiesDialog import Ui_TranslationPropertiesDialog |
|
18 |
|
19 import Utilities |
|
20 |
|
21 class TranslationPropertiesDialog(QDialog, Ui_TranslationPropertiesDialog): |
|
22 """ |
|
23 Class implementing the Translations Properties dialog. |
|
24 """ |
|
25 def __init__(self, project, new, parent): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param project reference to the project object |
|
30 @param new flag indicating the generation of a new project |
|
31 @param parent parent widget of this dialog (QWidget) |
|
32 """ |
|
33 QDialog.__init__(self, parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.project = project |
|
37 self.parent = parent |
|
38 |
|
39 self.transPatternCompleter = E4FileCompleter(self.transPatternEdit) |
|
40 self.transBinPathCompleter = E4DirCompleter(self.transBinPathEdit) |
|
41 self.exceptionCompleter = E4FileCompleter(self.exceptionEdit) |
|
42 |
|
43 self.initFilters() |
|
44 if not new: |
|
45 self.initDialog() |
|
46 |
|
47 def initFilters(self): |
|
48 """ |
|
49 Public method to initialize the filters. |
|
50 """ |
|
51 patterns = { |
|
52 "SOURCES" : [], |
|
53 "FORMS" : [], |
|
54 } |
|
55 for pattern, filetype in self.project.pdata["FILETYPES"].items(): |
|
56 if filetype in patterns: |
|
57 patterns[filetype].append(pattern) |
|
58 self.filters = self.trUtf8("Source Files ({0});;")\ |
|
59 .format(" ".join(patterns["SOURCES"])) |
|
60 if self.parent.getProjectType() in ["Qt4", "E4Plugin", "PySide"]: |
|
61 self.filters += self.trUtf8("Forms Files ({0});;")\ |
|
62 .format(" ".join(patterns["FORMS"])) |
|
63 self.filters += self.trUtf8("All Files (*)") |
|
64 |
|
65 def initDialog(self): |
|
66 """ |
|
67 Public method to initialize the dialogs data. |
|
68 """ |
|
69 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
|
70 try: |
|
71 self.transPatternEdit.setText(os.path.join(\ |
|
72 self.project.ppath, self.project.pdata["TRANSLATIONPATTERN"][0])) |
|
73 except IndexError: |
|
74 pass |
|
75 try: |
|
76 self.transBinPathEdit.setText(os.path.join(\ |
|
77 self.project.ppath, self.project.pdata["TRANSLATIONSBINPATH"][0])) |
|
78 except IndexError: |
|
79 pass |
|
80 self.exceptionsList.clear() |
|
81 for texcept in self.project.pdata["TRANSLATIONEXCEPTIONS"]: |
|
82 if texcept: |
|
83 self.exceptionsList.addItem(texcept) |
|
84 |
|
85 @pyqtSlot() |
|
86 def on_transPatternButton_clicked(self): |
|
87 """ |
|
88 Private slot to display a file selection dialog. |
|
89 """ |
|
90 tp = self.transPatternEdit.text() |
|
91 if "%language%" in tp: |
|
92 tp = tp.split("%language%")[0] |
|
93 tsfile = QFileDialog.getOpenFileName(\ |
|
94 self, |
|
95 self.trUtf8("Select translation file"), |
|
96 tp, |
|
97 "") |
|
98 |
|
99 if tsfile: |
|
100 self.transPatternEdit.setText(Utilities.toNativeSeparators(tsfile)) |
|
101 |
|
102 @pyqtSlot(str) |
|
103 def on_transPatternEdit_textChanged(self, txt): |
|
104 """ |
|
105 Private slot to check the translation pattern for correctness. |
|
106 |
|
107 @param txt text of the transPatternEdit lineedit (string) |
|
108 """ |
|
109 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
110 "%language%" in txt) |
|
111 |
|
112 @pyqtSlot() |
|
113 def on_transBinPathButton_clicked(self): |
|
114 """ |
|
115 Private slot to display a directory selection dialog. |
|
116 """ |
|
117 directory = QFileDialog.getExistingDirectory(\ |
|
118 self, |
|
119 self.trUtf8("Select directory for binary translations"), |
|
120 self.transBinPathEdit.text(), |
|
121 QFileDialog.Options(QFileDialog.Option(0))) |
|
122 |
|
123 if directory: |
|
124 self.transBinPathEdit.setText(Utilities.toNativeSeparators(directory)) |
|
125 |
|
126 @pyqtSlot() |
|
127 def on_deleteExceptionButton_clicked(self): |
|
128 """ |
|
129 Private slot to delete the currently selected entry of the listwidget. |
|
130 """ |
|
131 row = self.exceptionsList.currentRow() |
|
132 itm = self.exceptionsList.takeItem(row) |
|
133 del itm |
|
134 row = self.exceptionsList.currentRow() |
|
135 self.on_exceptionsList_currentRowChanged(row) |
|
136 |
|
137 @pyqtSlot() |
|
138 def on_addExceptionButton_clicked(self): |
|
139 """ |
|
140 Private slot to add the shown exception to the listwidget. |
|
141 """ |
|
142 if self.project.ppath == '': |
|
143 ppath = self.parent.getPPath() |
|
144 else: |
|
145 ppath = self.project.ppath |
|
146 texcept = self.exceptionEdit.text() |
|
147 texcept = texcept.replace(ppath + os.sep, "") |
|
148 if texcept.endswith(os.sep): |
|
149 texcept = texcept[:-1] |
|
150 if texcept: |
|
151 QListWidgetItem(texcept, self.exceptionsList) |
|
152 self.exceptionEdit.clear() |
|
153 row = self.exceptionsList.currentRow() |
|
154 self.on_exceptionsList_currentRowChanged(row) |
|
155 |
|
156 @pyqtSlot() |
|
157 def on_exceptFileButton_clicked(self): |
|
158 """ |
|
159 Private slot to select a file to exempt from translation. |
|
160 """ |
|
161 texcept = QFileDialog.getOpenFileName(\ |
|
162 self, |
|
163 self.trUtf8("Exempt file from translation"), |
|
164 self.project.ppath, |
|
165 self.filters) |
|
166 if texcept: |
|
167 self.exceptionEdit.setText(Utilities.toNativeSeparators(texcept)) |
|
168 |
|
169 @pyqtSlot() |
|
170 def on_exceptDirButton_clicked(self): |
|
171 """ |
|
172 Private slot to select a file to exempt from translation. |
|
173 """ |
|
174 texcept = QFileDialog.getExistingDirectory(\ |
|
175 self, |
|
176 self.trUtf8("Exempt directory from translation"), |
|
177 self.project.ppath, |
|
178 QFileDialog.Options(QFileDialog.ShowDirsOnly)) |
|
179 if texcept: |
|
180 self.exceptionEdit.setText(Utilities.toNativeSeparators(texcept)) |
|
181 |
|
182 def on_exceptionsList_currentRowChanged(self, row): |
|
183 """ |
|
184 Private slot to handle the currentRowChanged signal of the exceptions list. |
|
185 |
|
186 @param row the current row (integer) |
|
187 """ |
|
188 if row == -1: |
|
189 self.deleteExceptionButton.setEnabled(False) |
|
190 else: |
|
191 self.deleteExceptionButton.setEnabled(True) |
|
192 |
|
193 def on_exceptionEdit_textChanged(self, txt): |
|
194 """ |
|
195 Private slot to handle the textChanged signal of the exception edit. |
|
196 |
|
197 @param txt the text of the exception edit (string) |
|
198 """ |
|
199 self.addExceptionButton.setEnabled(txt != "") |
|
200 |
|
201 def storeData(self): |
|
202 """ |
|
203 Public method to store the entered/modified data. |
|
204 """ |
|
205 tp = Utilities.toNativeSeparators(self.transPatternEdit.text()) |
|
206 if tp: |
|
207 tp = tp.replace(self.project.ppath + os.sep, "") |
|
208 self.project.pdata["TRANSLATIONPATTERN"] = [tp] |
|
209 self.project.translationsRoot = tp.split("%language%")[0] |
|
210 else: |
|
211 self.project.pdata["TRANSLATIONPATTERN"] = [] |
|
212 tp = Utilities.toNativeSeparators(self.transBinPathEdit.text()) |
|
213 if tp: |
|
214 tp = tp.replace(self.project.ppath + os.sep, "") |
|
215 self.project.pdata["TRANSLATIONSBINPATH"] = [tp] |
|
216 else: |
|
217 self.project.pdata["TRANSLATIONSBINPATH"] = [] |
|
218 exceptList = [] |
|
219 for i in range(self.exceptionsList.count()): |
|
220 exceptList.append(self.exceptionsList.item(i).text()) |
|
221 self.project.pdata["TRANSLATIONEXCEPTIONS"] = exceptList[:] |