From b2f0bf0e6710216dbd9c76f56698c7432e24a032 Mon Sep 17 00:00:00 2001 From: sliptonic Date: Sat, 22 Jan 2022 15:58:28 -0600 Subject: [PATCH] ToolController Translation cleanup --- .../Path/PathScripts/PathToolController.py | 96 +++++++++++-------- .../Path/PathScripts/PathToolControllerGui.py | 81 ++++++++++++---- 2 files changed, 123 insertions(+), 54 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathToolController.py b/src/Mod/Path/PathScripts/PathToolController.py index a227de9bf7..5704fcfb87 100644 --- a/src/Mod/Path/PathScripts/PathToolController.py +++ b/src/Mod/Path/PathScripts/PathToolController.py @@ -22,21 +22,21 @@ """Tool Controller defines tool, spindle speed and feed rates for Path Operations""" +from PySide.QtCore import QT_TRANSLATE_NOOP import FreeCAD import Path import PathScripts.PathLog as PathLog import PathScripts.PathPreferences as PathPreferences import PathScripts.PathToolBit as PathToolBit -from PySide import QtCore -# PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule()) -# PathLog.trackModule(PathLog.thisModule()) +if False: + PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule()) + PathLog.trackModule(PathLog.thisModule()) +else: + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) - -# Qt translation handling -def translate(context, text, disambig=None): - return QtCore.QCoreApplication.translate(context, text, disambig) +translate = FreeCAD.Qt.translate class ToolControllerTemplate: @@ -66,63 +66,88 @@ class ToolController: "App::PropertyIntegerConstraint", "ToolNumber", "Tool", - QtCore.QT_TRANSLATE_NOOP("PathToolController", "The active tool"), + QT_TRANSLATE_NOOP("App::Property", "The active tool"), ) obj.ToolNumber = (0, 0, 10000, 1) obj.addProperty( "App::PropertyFloat", "SpindleSpeed", "Tool", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "The speed of the cutting spindle in RPM" + QT_TRANSLATE_NOOP( + "App::Property", "The speed of the cutting spindle in RPM" ), ) obj.addProperty( "App::PropertyEnumeration", "SpindleDir", "Tool", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "Direction of spindle rotation" - ), + QT_TRANSLATE_NOOP("App::Property", "Direction of spindle rotation"), ) - obj.SpindleDir = ["Forward", "Reverse"] obj.addProperty( "App::PropertySpeed", "VertFeed", "Feed", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "Feed rate for vertical moves in Z" - ), + QT_TRANSLATE_NOOP("App::Property", "Feed rate for vertical moves in Z"), ) obj.addProperty( "App::PropertySpeed", "HorizFeed", "Feed", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "Feed rate for horizontal moves" - ), + QT_TRANSLATE_NOOP("App::Property", "Feed rate for horizontal moves"), ) obj.addProperty( "App::PropertySpeed", "VertRapid", "Rapid", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "Rapid rate for vertical moves in Z" - ), + QT_TRANSLATE_NOOP("App::Property", "Rapid rate for vertical moves in Z"), ) obj.addProperty( "App::PropertySpeed", "HorizRapid", "Rapid", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "Rapid rate for horizontal moves" - ), + QT_TRANSLATE_NOOP("App::Property", "Rapid rate for horizontal moves"), ) obj.setEditorMode("Placement", 2) + for n in self.propertyEnumerations(): + setattr(obj, n[0], n[1]) + if createTool: self.ensureUseLegacyTool(obj, legacyTool) + @classmethod + def propertyEnumerations(self, dataType="data"): + """helixOpPropertyEnumerations(dataType="data")... return property enumeration lists of specified dataType. + Args: + dataType = 'data', 'raw', 'translated' + Notes: + 'data' is list of internal string literals used in code + 'raw' is list of (translated_text, data_string) tuples + 'translated' is list of translated string literals + """ + + # Enumeration lists for App::PropertyEnumeration properties + enums = { + "SpindleDir": [ + (translate("Path_ToolController", "Forward"), "Forward"), + (translate("Path_ToolController", "Reverse"), "Reverse"), + ], # this is the direction that the profile runs + } + + if dataType == "raw": + return enums + + data = list() + idx = 0 if dataType == "translated" else 1 + + PathLog.debug(enums) + + for k, v in enumerate(enums): + data.append((v, [tup[idx] for tup in enums[v]])) + PathLog.debug(data) + + return data + def onDocumentRestored(self, obj): obj.setEditorMode("Placement", 2) @@ -192,18 +217,13 @@ class ToolController: ) else: PathLog.error( - translate( - "PathToolController", - "Unsupported PathToolController template version %s", + "Unsupported PathToolController template version {}".format( + template.get(ToolControllerTemplate.Version) ) - % template.get(ToolControllerTemplate.Version) ) else: PathLog.error( - translate( - "PathToolController", - "PathToolController template has no version - corrupted template file?", - ) + "PathToolController template has no version - corrupted template file?" ) def templateAttrs(self, obj): @@ -290,8 +310,8 @@ class ToolController: "Path::PropertyTool", "Tool", "Base", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "The tool used by this controller" + QT_TRANSLATE_NOOP( + "App::Property", "The tool used by this controller" ), ) else: @@ -299,8 +319,8 @@ class ToolController: "App::PropertyLink", "Tool", "Base", - QtCore.QT_TRANSLATE_NOOP( - "PathToolController", "The tool used by this controller" + QT_TRANSLATE_NOOP( + "App::Property", "The tool used by this controller" ), ) diff --git a/src/Mod/Path/PathScripts/PathToolControllerGui.py b/src/Mod/Path/PathScripts/PathToolControllerGui.py index 3b5b4d70d3..91595b9168 100644 --- a/src/Mod/Path/PathScripts/PathToolControllerGui.py +++ b/src/Mod/Path/PathScripts/PathToolControllerGui.py @@ -20,6 +20,8 @@ # * * # *************************************************************************** +from PySide import QtCore, QtGui +from PySide.QtCore import QT_TRANSLATE_NOOP import FreeCAD import FreeCADGui import PathGui as PGui # ensure Path/Gui/Resources are loaded @@ -29,8 +31,7 @@ import PathScripts.PathLog as PathLog import PathScripts.PathToolBitGui as PathToolBitGui import PathScripts.PathToolEdit as PathToolEdit import PathScripts.PathUtil as PathUtil - -from PySide import QtCore, QtGui +import PathScripts.PathToolController as PathToolController # lazily loaded modules from lazy_loader.lazy_loader import LazyLoader @@ -38,9 +39,13 @@ from lazy_loader.lazy_loader import LazyLoader Part = LazyLoader("Part", globals(), "Part") -# Qt translation handling -def translate(context, text, disambig=None): - return QtCore.QCoreApplication.translate(context, text, disambig) +if False: + PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule()) + PathLog.trackModule(PathLog.thisModule()) +else: + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) + +translate = FreeCAD.Qt.translate class ViewProvider: @@ -144,12 +149,10 @@ class CommandPathToolController(object): def GetResources(self): return { "Pixmap": "Path_LengthOffset", - "MenuText": QtCore.QT_TRANSLATE_NOOP( + "MenuText": QT_TRANSLATE_NOOP( "Path_ToolController", "Add Tool Controller to the Job" ), - "ToolTip": QtCore.QT_TRANSLATE_NOOP( - "Path_ToolController", "Add Tool Controller" - ), + "ToolTip": QT_TRANSLATE_NOOP("Path_ToolController", "Add Tool Controller"), } def selectedJob(self): @@ -190,6 +193,12 @@ class ToolControllerEditor(object): self.form.buttonBox.hide() self.obj = obj + comboToPropertyMap = [("spindleDirection", "SpindleDir")] + enumTups = PathToolController.ToolController.propertyEnumerations( + dataType="raw" + ) + + self.populateCombobox(self.form, enumTups, comboToPropertyMap) self.vertFeed = PathGui.QuantitySpinBox(self.form.vertFeed, obj, "VertFeed") self.horizFeed = PathGui.QuantitySpinBox(self.form.horizFeed, obj, "HorizFeed") self.vertRapid = PathGui.QuantitySpinBox(self.form.vertRapid, obj, "VertRapid") @@ -204,6 +213,43 @@ class ToolControllerEditor(object): self.form.toolBox.widget(1).hide() self.form.toolBox.removeItem(1) + def selectInComboBox(self, name, combo): + """selectInComboBox(name, combo) ... + helper function to select a specific value in a combo box.""" + blocker = QtCore.QSignalBlocker(combo) + index = combo.currentIndex() # Save initial index + + # Search using currentData and return if found + newindex = combo.findData(name) + if newindex >= 0: + combo.setCurrentIndex(newindex) + return + + # if not found, search using current text + newindex = combo.findText(name, QtCore.Qt.MatchFixedString) + if newindex >= 0: + combo.setCurrentIndex(newindex) + return + + # not found, return unchanged + combo.setCurrentIndex(index) + return + + def populateCombobox(self, form, enumTups, comboBoxesPropertyMap): + """fillComboboxes(form, comboBoxesPropertyMap) ... populate comboboxes with translated enumerations + ** comboBoxesPropertyMap will be unnecessary if UI files use strict combobox naming protocol. + Args: + form = UI form + enumTups = list of (translated_text, data_string) tuples + comboBoxesPropertyMap = list of (translated_text, data_string) tuples + """ + # Load appropriate enumerations in each combobox + for cb, prop in comboBoxesPropertyMap: + box = getattr(form, cb) # Get the combobox + box.clear() # clear the combobox + for text, data in enumTups[prop]: # load enumerations + box.addItem(text, data) + def updateUi(self): tc = self.obj self.form.tcName.setText(tc.Label) @@ -213,11 +259,14 @@ class ToolControllerEditor(object): self.vertFeed.updateSpinBox() self.vertRapid.updateSpinBox() self.form.spindleSpeed.setValue(tc.SpindleSpeed) - index = self.form.spindleDirection.findText( - tc.SpindleDir, QtCore.Qt.MatchFixedString - ) - if index >= 0: - self.form.spindleDirection.setCurrentIndex(index) + + self.selectInComboBox(tc.SpindleDir, self.form.spindleDirection) + + # index = self.form.spindleDirection.findText( + # tc.SpindleDir, QtCore.Qt.MatchFixedString + # ) + # if index >= 0: + # self.form.spindleDirection.setCurrentIndex(index) if self.editor: self.editor.updateUI() @@ -232,14 +281,14 @@ class ToolControllerEditor(object): self.horizRapid.updateProperty() self.vertRapid.updateProperty() tc.SpindleSpeed = self.form.spindleSpeed.value() - tc.SpindleDir = self.form.spindleDirection.currentText() + tc.SpindleDir = self.form.spindleDirection.currentData() if self.editor: self.editor.updateTool() tc.Tool = self.editor.tool except Exception as e: - PathLog.error(translate("PathToolController", "Error updating TC: %s") % e) + PathLog.error("Error updating TC: {}".format(e)) def refresh(self): self.form.blockSignals(True)