eric6/Project/TranslationPropertiesDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2006 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Translations Properties 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 QListWidgetItem, QDialog, QDialogButtonBox
16
17 from E5Gui.E5Completers import E5FileCompleter
18 from E5Gui import E5FileDialog
19 from E5Gui.E5PathPicker import E5PathPickerModes
20
21 from .Ui_TranslationPropertiesDialog import Ui_TranslationPropertiesDialog
22
23 import Utilities
24
25
26 class TranslationPropertiesDialog(QDialog, Ui_TranslationPropertiesDialog):
27 """
28 Class implementing the Translations Properties dialog.
29 """
30 def __init__(self, project, new, parent):
31 """
32 Constructor
33
34 @param project reference to the project object
35 @param new flag indicating the generation of a new project
36 @param parent parent widget of this dialog (QWidget)
37 """
38 super(TranslationPropertiesDialog, self).__init__(parent)
39 self.setupUi(self)
40
41 self.transPatternPicker.setMode(E5PathPickerModes.SaveFileMode)
42 self.transPatternPicker.setDefaultDirectory(project.ppath)
43 self.transBinPathPicker.setMode(E5PathPickerModes.DirectoryMode)
44 self.transBinPathPicker.setDefaultDirectory(project.ppath)
45
46 self.project = project
47 self.parent = parent
48
49 self.exceptionCompleter = E5FileCompleter(self.exceptionEdit)
50
51 self.initFilters()
52 if not new:
53 self.initDialog()
54
55 def initFilters(self):
56 """
57 Public method to initialize the filters.
58 """
59 patterns = {
60 "SOURCES": [],
61 "FORMS": [],
62 }
63 for pattern, filetype in list(self.project.pdata["FILETYPES"].items()):
64 if filetype in patterns:
65 patterns[filetype].append(pattern)
66 self.filters = self.tr("Source Files ({0});;")\
67 .format(" ".join(patterns["SOURCES"]))
68 self.filters += self.tr("Forms Files ({0});;")\
69 .format(" ".join(patterns["FORMS"]))
70 self.filters += self.tr("All Files (*)")
71
72 def initDialog(self):
73 """
74 Public method to initialize the dialogs data.
75 """
76 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
77 self.transPatternPicker.setText(
78 self.project.pdata["TRANSLATIONPATTERN"])
79 self.transBinPathPicker.setText(
80 self.project.pdata["TRANSLATIONSBINPATH"])
81 self.exceptionsList.clear()
82 for texcept in self.project.pdata["TRANSLATIONEXCEPTIONS"]:
83 if texcept:
84 self.exceptionsList.addItem(texcept)
85
86 @pyqtSlot(str)
87 def on_transPatternPicker_pathSelected(self, path):
88 """
89 Private slot handling the selection of a translation path.
90
91 @param path selected path
92 @type str
93 """
94 self.transPatternPicker.setText(self.project.getRelativePath(path))
95
96 @pyqtSlot(str)
97 def on_transPatternPicker_textChanged(self, txt):
98 """
99 Private slot to check the translation pattern for correctness.
100
101 @param txt text of the transPatternPicker line edit (string)
102 """
103 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
104 "%language%" in txt)
105
106 @pyqtSlot(str)
107 def on_transBinPathPicker_pathSelected(self, path):
108 """
109 Private slot handling the selection of a binary translations path.
110
111 @param path selected path
112 @type str
113 """
114 self.transBinPathPicker.setText(self.project.getRelativePath(path))
115
116 @pyqtSlot()
117 def on_deleteExceptionButton_clicked(self):
118 """
119 Private slot to delete the currently selected entry of the listwidget.
120 """
121 row = self.exceptionsList.currentRow()
122 itm = self.exceptionsList.takeItem(row)
123 del itm
124 row = self.exceptionsList.currentRow()
125 self.on_exceptionsList_currentRowChanged(row)
126
127 @pyqtSlot()
128 def on_addExceptionButton_clicked(self):
129 """
130 Private slot to add the shown exception to the listwidget.
131 """
132 texcept = self.exceptionEdit.text()
133 if self.project.ppath == '':
134 texcept = texcept.replace(self.parent.getPPath() + os.sep, "")
135 else:
136 texcept = self.project.getRelativePath(texcept)
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