From f4180658dc193c97f907f8d68487780e376e9602 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Wed, 16 Aug 2023 11:40:42 -0400 Subject: [PATCH] [TD]Add method to translate View labels --- src/Mod/TechDraw/App/DrawView.cpp | 16 ++++++++++ src/Mod/TechDraw/App/DrawView.h | 3 ++ src/Mod/TechDraw/App/DrawViewPy.xml | 9 +++++- src/Mod/TechDraw/App/DrawViewPyImp.cpp | 44 +++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawView.cpp b/src/Mod/TechDraw/App/DrawView.cpp index 3e2b8a1b94..f179dee5ce 100644 --- a/src/Mod/TechDraw/App/DrawView.cpp +++ b/src/Mod/TechDraw/App/DrawView.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include // generated from DrawViewPy.xml #include "DrawView.h" @@ -594,6 +595,21 @@ void DrawView::showProgressMessage(std::string featureName, std::string text) } } +//! get a translated label string from the context (ex TaskActiveView), the base name (ex ActiveView) and +//! the unique name within the document (ex ActiveView001), and use it to update the Label property. +void DrawView::translateLabel(std::string context, std::string baseName, std::string uniqueName) +{ +// Base::Console().Message("DV::getTranslatedLabel - context: %s baseName: %s uniqueName: %s\n", +// context.c_str(), baseName.c_str(), uniqueName.c_str()); + std::string suffix(""); + if (uniqueName.length() > baseName.length()) { + suffix = uniqueName.substr(baseName.length(), uniqueName.length() - baseName.length()); + } + QString qTranslated = qApp->translate(context.c_str(), baseName.c_str()); + std::string ssTranslated = Base::Tools::toStdString(qTranslated); + Label.setValue(ssTranslated + suffix); +} + PyObject *DrawView::getPyObject(void) { if (PythonObject.is(Py::_None())) { diff --git a/src/Mod/TechDraw/App/DrawView.h b/src/Mod/TechDraw/App/DrawView.h index 537e8dfc38..c5728679ed 100644 --- a/src/Mod/TechDraw/App/DrawView.h +++ b/src/Mod/TechDraw/App/DrawView.h @@ -113,6 +113,9 @@ public: void overrideKeepUpdated(bool s) { m_overrideKeepUpdated = s; } bool overrideKeepUpdated(void) { return m_overrideKeepUpdated; } + void translateLabel(std::string context, std::string objectName, std::string uniqueName); + + protected: void onChanged(const App::Property* prop) override; virtual void validateScale(); diff --git a/src/Mod/TechDraw/App/DrawViewPy.xml b/src/Mod/TechDraw/App/DrawViewPy.xml index 750819f6b4..7bc1d48a31 100644 --- a/src/Mod/TechDraw/App/DrawViewPy.xml +++ b/src/Mod/TechDraw/App/DrawViewPy.xml @@ -13,6 +13,13 @@ Feature for creating and manipulating Technical Drawing Views - + + + translateLabel(translationContext, objectBaseName, objectUniqueName). + No return value. Replace the current label with a translated version where possible. + + + + diff --git a/src/Mod/TechDraw/App/DrawViewPyImp.cpp b/src/Mod/TechDraw/App/DrawViewPyImp.cpp index 1d2025f532..9d43c4044f 100644 --- a/src/Mod/TechDraw/App/DrawViewPyImp.cpp +++ b/src/Mod/TechDraw/App/DrawViewPyImp.cpp @@ -27,7 +27,6 @@ #include #include - using namespace TechDraw; // returns a string which represents the object e.g. when printed in python @@ -36,6 +35,49 @@ std::string DrawViewPy::representation(void) const return std::string(""); } +//! replace the current Label with a translated version +PyObject* DrawViewPy::translateLabel(PyObject *args) +{ + PyObject* pyContext; + PyObject* pyBaseName; + PyObject* pyUniqueName; + std::string context; + std::string baseName; + std::string uniqueName; + + if (!PyArg_ParseTuple(args, "OOO", &pyContext, &pyBaseName, &pyUniqueName)) { + throw Py::TypeError("Could not translate label - bad parameters."); + } + + Py_ssize_t size = 0; + const char* cContext = PyUnicode_AsUTF8AndSize(pyContext, &size); + if (cContext) { + context = std::string(cContext, size); + } else { + throw Py::TypeError("Could not translate label - context not available."); + } + + const char* cBaseName = PyUnicode_AsUTF8AndSize(pyBaseName, &size); + if (cBaseName) { + baseName = std::string(cBaseName, size); + } else { + throw Py::TypeError("Could not translate label - base name not available."); + } + + const char* cUniqueName = PyUnicode_AsUTF8AndSize(pyUniqueName, &size); + if (cUniqueName) { + uniqueName = std::string(cUniqueName, size); + } else { + throw Py::TypeError("Could not translate label - unique name not available."); + } + + // we have the 3 parameters we need for DrawView::translateLabel + DrawView* dv = getDrawViewPtr(); + dv->translateLabel(context, baseName, uniqueName); + + Py_Return; +} + PyObject *DrawViewPy::getCustomAttributes(const char* /*attr*/) const {