Tue, 10 Dec 2024 15:48:59 +0100
Updated copyright for 2025.
# -*- coding: utf-8 -*- # Copyright (c) 2020 - 2025 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a dialog to edit the flask-babel configuration. """ import os from PyQt6.QtCore import Qt, pyqtSlot from PyQt6.QtWidgets import QDialog, QDialogButtonBox from eric7.EricWidgets.EricApplication import ericApp from eric7.EricWidgets.EricPathPicker import EricPathPickerModes from .Ui_PyBabelConfigDialog import Ui_PyBabelConfigDialog class PyBabelConfigDialog(QDialog, Ui_PyBabelConfigDialog): """ Class implementing a dialog to edit the flask-babel 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().__init__(parent) self.setupUi(self) self.__ericProject = ericApp().getObject("Project") self.configFilePicker.setMode( EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE ) self.configFilePicker.setFilters( self.tr("Configuration Files (*.cfg);;All Files (*)") ) self.configFilePicker.setDefaultDirectory(self.__ericProject.getProjectPath()) self.translationsDirectoryPicker.setMode(EricPathPickerModes.DIRECTORY_MODE) self.translationsDirectoryPicker.setDefaultDirectory( self.__ericProject.getProjectPath() ) self.catalogFilePicker.setMode( EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE ) self.catalogFilePicker.setFilters( self.tr("Message Catalog Files (*.pot);;All Files (*)") ) self.catalogFilePicker.setDefaultDirectory(self.__ericProject.getProjectPath()) self.configFilePicker.setFocus(Qt.FocusReason.OtherFocusReason) self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) if "configFile" in configuration: self.configFilePicker.setText( self.__ericProject.getAbsoluteUniversalPath(configuration["configFile"]) ) if "translationsDirectory" in configuration: self.translationsDirectoryPicker.setText( self.__ericProject.getAbsoluteUniversalPath( configuration["translationsDirectory"] ) ) if "domain" in configuration: self.domainEdit.setText(configuration["domain"]) if "catalogFile" in configuration: self.catalogFilePicker.setText( self.__ericProject.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.__ericProject.getRelativeUniversalPath( self.configFilePicker.text() ), "translationsDirectory": self.__ericProject.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.__ericProject.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.StandardButton.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): # noqa: U100 """ 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): # noqa: U100 """ 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): # noqa: U100 """ Private slot to handle a change of the translations domain. @param txt entered translations domain @type str """ self.__updateCatalogPicker()