[TD]Add method to translate View labels

This commit is contained in:
wandererfan
2023-08-16 11:40:42 -04:00
committed by WandererFan
parent 94426e1f54
commit bb08f70d04
4 changed files with 70 additions and 2 deletions

View File

@@ -32,6 +32,7 @@
#include <App/Application.h>
#include <App/Document.h>
#include <Base/Reader.h>
#include <Base/Tools.h>
#include <Mod/TechDraw/App/DrawViewPy.h> // 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())) {

View File

@@ -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();

View File

@@ -13,6 +13,13 @@
<Author Licence="LGPL" Name="WandererFan" EMail="wandererfan@gmail.com" />
<UserDocu>Feature for creating and manipulating Technical Drawing Views</UserDocu>
</Documentation>
<CustomAttributes />
<Methode Name="translateLabel">
<Documentation>
<UserDocu>translateLabel(translationContext, objectBaseName, objectUniqueName).
No return value. Replace the current label with a translated version where possible.
</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>

View File

@@ -27,7 +27,6 @@
#include <Mod/TechDraw/App/DrawViewPy.h>
#include <Mod/TechDraw/App/DrawViewPy.cpp>
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("<DrawView object>");
}
//! 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
{