--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ProjectFlask/FlaskBabelExtension/PyBabelConfigDialog.py Sat Nov 21 20:37:54 2020 +0100 @@ -0,0 +1,169 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a dialog to edit the PyBabel configuration. +""" + +import os + +from PyQt5.QtCore import pyqtSlot, Qt +from PyQt5.QtWidgets import QDialog, QDialogButtonBox + +from E5Gui.E5PathPicker import E5PathPickerModes +from E5Gui.E5Application import e5App + +from .Ui_PyBabelConfigDialog import Ui_PyBabelConfigDialog + + +class PyBabelConfigDialog(QDialog, Ui_PyBabelConfigDialog): + """ + Class implementing a dialog to edit the PyBabel configuration. + """ + def __init__(self, configuration, parent=None): + """ + Constructor + + @param configuration current pybabel configuration + @type dict + @param parent reference to the parent widget + @type QWidget + """ + super(PyBabelConfigDialog, self).__init__(parent) + self.setupUi(self) + + self.__e5project = e5App().getObject("Project") + + self.configFilePicker.setMode( + E5PathPickerModes.SaveFileEnsureExtensionMode) + self.configFilePicker.setFilters(self.tr( + "Configuration Files (*.cfg);;" + "All Files (*)" + )) + self.configFilePicker.setDefaultDirectory( + self.__e5project.getProjectPath()) + + self.translationsDirectoryPicker.setMode( + E5PathPickerModes.DirectoryMode) + self.translationsDirectoryPicker.setDefaultDirectory( + self.__e5project.getProjectPath()) + + self.catalogFilePicker.setMode( + E5PathPickerModes.SaveFileEnsureExtensionMode) + self.catalogFilePicker.setFilters(self.tr( + "Message Catalog Files (*.pot);;" + "All Files (*)" + )) + self.catalogFilePicker.setDefaultDirectory( + self.__e5project.getProjectPath()) + + self.configFilePicker.setFocus(Qt.OtherFocusReason) + + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) + + if "configFile" in configuration: + self.configFilePicker.setText( + self.__e5project.getAbsoluteUniversalPath( + configuration["configFile"])) + if "translationsDirectory" in configuration: + self.translationsDirectoryPicker.setText( + self.__e5project.getAbsoluteUniversalPath( + configuration["translationsDirectory"])) + if "domain" in configuration: + self.domainEdit.setText(configuration["domain"]) + if "catalogFile" in configuration: + self.catalogFilePicker.setText( + self.__e5project.getAbsoluteUniversalPath( + configuration["catalogFile"])) + if "markersList" in configuration: + self.markersEdit.setText(" ".join(configuration["markersList"])) + + msh = self.minimumSizeHint() + self.resize(max(self.width(), msh.width()), msh.height()) + + def getConfiguration(self): + """ + Public method to get the entered configuration data. + + @return pybabel configuration + @rtype dict + """ + configuration = { + "configFile": self.__e5project.getRelativeUniversalPath( + self.configFilePicker.text()), + "translationsDirectory": self.__e5project.getRelativeUniversalPath( + self.translationsDirectoryPicker.text()), + } + + domain = self.domainEdit.text() + if domain: + configuration["domain"] = domain + else: + configuration["domain"] = "messages" + + catalogFile = self.catalogFilePicker.text() + if not catalogFile: + # use a default name made of translations dir and domain + catalogFile = os.path.join( + configuration["translationsDirectory"], + "{0}.pot".format(configuration["domain"])) + configuration["catalogFile"] = ( + self.__e5project.getRelativeUniversalPath(catalogFile) + ) + + if self.markersEdit.text(): + configuration["markersList"] = self.markersEdit.text().split() + + return configuration + + def __updateOK(self): + """ + Private method to update the status of the OK button. + """ + enable = ( + bool(self.configFilePicker.text()) and + bool(self.translationsDirectoryPicker.text()) + ) + self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enable) + + def __updateCatalogPicker(self): + """ + Private method to update the contents of the catalog picker. + """ + translationsDirectory = self.translationsDirectoryPicker.text() + domain = self.domainEdit.text() + self.catalogFilePicker.setText(os.path.join( + translationsDirectory, "{0}.pot".format(domain))) + + @pyqtSlot(str) + def on_configFilePicker_textChanged(self, txt): + """ + Private slot to handle a change of the configuration file name. + + @param txt configuration file name + @type str + """ + self.__updateOK() + + @pyqtSlot(str) + def on_translationsDirectoryPicker_textChanged(self, txt): + """ + Private slot to handle a change of the catalog file name. + + @param txt configuration file name + @type str + """ + self.__updateOK() + self.__updateCatalogPicker() + + @pyqtSlot(str) + def on_domainEdit_textChanged(self, txt): + """ + Private slot to handle a change of the translations domain. + + @param txt entered translations domain + @type str + """ + self.__updateCatalogPicker()