[TD]add label translation for Page and Template

This commit is contained in:
wandererfan
2023-08-17 17:26:08 -04:00
committed by WandererFan
parent a8cc561396
commit d69551eccb
14 changed files with 152 additions and 21 deletions

View File

@@ -41,6 +41,7 @@
#include "DrawViewDimension.h"
#include "DrawViewPart.h"
#include "Preferences.h"
#include "DrawUtil.h"
using namespace TechDraw;
@@ -468,6 +469,13 @@ bool DrawPage::AllowPageOverride(void)
return Preferences::getPreferenceGroup("General")->GetBool("AllowPageOverride", true);
}
//! 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 DrawPage::translateLabel(std::string context, std::string baseName, std::string uniqueName)
{
Label.setValue(DrawUtil::translateArbitrary(context, baseName, uniqueName));
}
// Python Drawing feature ---------------------------------------------------------

View File

@@ -106,6 +106,9 @@ public:
bool hasObject(App::DocumentObject* obj);
void translateLabel(std::string context, std::string baseName, std::string uniqueName);
protected:
void onBeforeChange(const App::Property* prop) override;
void onChanged(const App::Property* prop) override;

View File

@@ -28,6 +28,13 @@
<UserDocu>getAllViews() - returns a list of all the views on page including Views inside Collections</UserDocu>
</Documentation>
</Methode>
<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>
<Attribute Name="PageWidth" ReadOnly="true">
<Documentation>
<UserDocu>Returns the width of this page</UserDocu>

View File

@@ -121,6 +121,49 @@ PyObject* DrawPagePy::requestPaint(PyObject* args)
Py_Return;
}
//! replace the current Label with a translated version
PyObject* DrawPagePy::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 DrawPage::translateLabel
DrawPage* page = getDrawPagePtr();
page->translateLabel(context, baseName, uniqueName);
Py_Return;
}
Py::Float DrawPagePy::getPageWidth() const
{
return Py::Float(getDrawPagePtr()->getPageWidth());

View File

@@ -43,7 +43,6 @@
#include "DrawUtil.h"
#include "XMLQuery.h"
using namespace TechDraw;
PROPERTY_SOURCE(TechDraw::DrawSVGTemplate, TechDraw::DrawTemplate)
@@ -250,6 +249,12 @@ std::map<std::string, std::string> DrawSVGTemplate::getEditableTextsFromTemplate
return editables;
}
//! 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 DrawSVGTemplate::translateLabel(std::string context, std::string baseName, std::string uniqueName)
{
Label.setValue(DrawUtil::translateArbitrary(context, baseName, uniqueName));
}
// Python Template feature ---------------------------------------------------------
namespace App {

View File

@@ -57,6 +57,9 @@ public:
QString processTemplate();
void translateLabel(std::string context, std::string baseName, std::string uniqueName);
protected:
void replaceFileIncluded(std::string newTemplateFileName);
std::map<std::string, std::string> getEditableTextsFromTemplate();

View File

@@ -23,5 +23,12 @@
<UserDocu>setEditFieldContent(EditFieldName, NewContent) - sets a specific Editable Text Field to a new value</UserDocu>
</Documentation>
</Methode>
<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>
</PythonExport>
</GenerateModel>

View File

@@ -36,6 +36,49 @@ std::string DrawSVGTemplatePy::representation() const
return std::string("<DrawSVGTemplate object>");
}
//! replace the current Label with a translated version
PyObject* DrawSVGTemplatePy::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 DrawSVGTemplate::translateLabel
auto svgTemplate = getDrawSVGTemplatePtr();
svgTemplate->translateLabel(context, baseName, uniqueName);
Py_Return;
}
PyObject *DrawSVGTemplatePy::getCustomAttributes(const char* ) const
{
return nullptr;

View File

@@ -61,6 +61,7 @@
#include <Base/FileInfo.h>
#include <Base/Parameter.h>
#include <Base/Stream.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Base/Vector3D.h>
@@ -1594,6 +1595,18 @@ void DrawUtil::copyFile(std::string inSpec, std::string outSpec)
}
}
//! static method that provides a translated std::string for objects that are not derived from DrawView
std::string DrawUtil::translateArbitrary(std::string context, std::string baseName, std::string uniqueName)
{
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);
return ssTranslated + suffix;
}
//============================
// various debugging routines.

View File

@@ -252,6 +252,9 @@ public:
std::vector<Base::Vector2d>& intersections);
static void copyFile(std::string inSpec, std::string outSpec);
static std::string translateArbitrary(std::string context, std::string baseName, std::string uniqueName);
//debugging routines
static void dumpVertexes(const char* text, const TopoDS_Shape& s);
static void dumpEdge(const char* label, int i, TopoDS_Edge e);

View File

@@ -45,12 +45,15 @@
using namespace TechDraw;
using DU = DrawUtil;
//===========================================================================
// DrawView
//===========================================================================
#if 0 // needed for Qt's lupdate utility
QT_TRANSLATE_NOOP("DrawPage", "Page");
QT_TRANSLATE_NOOP("DrawSVGTemplate", "Template");
QT_TRANSLATE_NOOP("DrawView", "View");
QT_TRANSLATE_NOOP("DrawViewPart", "View");
QT_TRANSLATE_NOOP("DrawViewSection", "Section");
@@ -622,22 +625,10 @@ void DrawView::translateLabel(std::string context, std::string baseName, std::st
// Base::Console().Message("DV::translateLabel - context: %s baseName: %s uniqueName: %s\n",
// context.c_str(), baseName.c_str(), uniqueName.c_str());
Label.setValue(translateArbitrary(context, baseName, uniqueName));
Label.setValue(DU::translateArbitrary(context, baseName, uniqueName));
// Base::Console().Message("DV::translateLabel - new label: %s\n", Label.getValue());
}
//! static method that provides a translated std::string for objects that are not derived from DrawView
std::string DrawView::translateArbitrary(std::string context, std::string baseName, std::string uniqueName)
{
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);
return ssTranslated + suffix;
}
PyObject *DrawView::getPyObject(void)
{
if (PythonObject.is(Py::_None())) {

View File

@@ -114,9 +114,6 @@ public:
bool overrideKeepUpdated(void) { return m_overrideKeepUpdated; }
void translateLabel(std::string context, std::string baseName, std::string uniqueName);
static std::string translateArbitrary(std::string context, std::string baseName, std::string uniqueName);
protected:
void onChanged(const App::Property* prop) override;

View File

@@ -30,8 +30,7 @@
#include "DrawWeldSymbolPy.h" // generated from DrawWeldSymbolPy.xml
#include "DrawLeaderLine.h"
#include "DrawTileWeld.h"
#include "DrawView.h" // for translateArbitrary
#include "DrawUtil.h"
using namespace TechDraw;
@@ -80,7 +79,7 @@ void DrawWeldSymbol::onSettingDocument()
auto tile1Obj( doc->addObject( "TechDraw::DrawTileWeld", tileName1.c_str() ) );
DrawTileWeld* tile1 = dynamic_cast<DrawTileWeld*>(tile1Obj);
if (tile1) {
tile1->Label.setValue(DrawView::translateArbitrary("DrawTileWeld", "TileWeld", tileName1));
tile1->Label.setValue(DrawUtil::translateArbitrary("DrawTileWeld", "TileWeld", tileName1));
tile1->TileParent.setValue(this);
}
@@ -88,7 +87,7 @@ void DrawWeldSymbol::onSettingDocument()
auto tile2Obj( doc->addObject( "TechDraw::DrawTileWeld", tileName2.c_str() ) );
DrawTileWeld* tile2 = dynamic_cast<DrawTileWeld*>(tile2Obj);
if (tile2) {
tile2->Label.setValue(DrawView::translateArbitrary("DrawTileWeld", "TileWeld", tileName2));
tile2->Label.setValue(DrawUtil::translateArbitrary("DrawTileWeld", "TileWeld", tileName2));
tile2->TileParent.setValue(this);
tile2->TileRow.setValue(-1); //other side is row -1
}

View File

@@ -116,8 +116,13 @@ void CmdTechDrawPageDefault::activated(int iMsg)
openCommand(QT_TRANSLATE_NOOP("Command", "Drawing create page"));
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawPage', '%s')",
PageName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawPage', 'Page', '%s')",
PageName.c_str(), PageName.c_str());
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawSVGTemplate', '%s')",
TemplateName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawSVGTemplate', 'Template', '%s')",
TemplateName.c_str(), TemplateName.c_str());
doCommand(Doc, "App.activeDocument().%s.Template = '%s'", TemplateName.c_str(),
templateFileName.toStdString().c_str());
@@ -187,10 +192,14 @@ void CmdTechDrawPageTemplate::activated(int iMsg)
openCommand(QT_TRANSLATE_NOOP("Command", "Drawing create page"));
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawPage', '%s')",
PageName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawPage', 'Page', '%s')",
PageName.c_str(), PageName.c_str());
// Create the Template Object to attach to the page
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawSVGTemplate', '%s')",
TemplateName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawSVGTemplate', 'Template', '%s')",
TemplateName.c_str(), TemplateName.c_str());
//why is "Template" property set twice? -wf
// once to set DrawSVGTemplate.Template to OS template file name