eric7/Project/TranslationPropertiesDialog.py

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

eric ide

mercurial