Skip to content

Commit

Permalink
ENH: Add translation function for logic classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdiop authored and lassoan committed Aug 31, 2023
1 parent ff5700d commit ad99a2c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Base/QTCore/qMRMLTranslator.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <QCoreApplication>

#include "qMRMLTranslator.h"

std::string qMRMLTranslator::Translate(const char *context, const char *sourceText, const char *disambiguation, int n)
{
return QCoreApplication::translate(context, sourceText, disambiguation, n).toStdString();
}
16 changes: 16 additions & 0 deletions Base/QTCore/qMRMLTranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef qMRMLTranslator_h
#define qMRMLTranslator_h

#include "vtkMRMLTranslator.h"

#include "qSlicerBaseQTCoreExport.h"

class Q_SLICER_BASE_QTCORE_EXPORT qMRMLTranslator: public vtkMRMLTranslator
{
public:

/// Translation function for logic classes
std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) override;
};

#endif
7 changes: 7 additions & 0 deletions Base/QTCore/qSlicerCoreApplication.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "qSlicerModuleFactoryManager.h"
#include "qSlicerModuleManager.h"
#include "qSlicerUtils.h"
#include "qMRMLTranslator.h"

// SlicerLogic includes
#include "vtkDataIOManagerLogic.h"
Expand Down Expand Up @@ -353,6 +354,12 @@ void qSlicerCoreApplicationPrivate::init()
vtkEventBroker::GetInstance()->SetRequestModifiedCallback(modifiedRequestCallback);
}

// Create the translation object used for string translation in logic classes
if (!this->AppLogic->GetTranslatorInstance())
{
this->AppLogic->SetTranslatorInstance(new qMRMLTranslator());
}

// Ensure that temporary folder is writable
{
// QTemporaryFile is deleted automatically when leaving this scope
Expand Down
29 changes: 29 additions & 0 deletions Libs/MRML/Logic/vtkMRMLApplicationLogic.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,32 @@ std::string vtkMRMLApplicationLogic::GetFontsDirectory()
std::string fullPath = vtksys::SystemTools::JoinPath(filesVector);
return fullPath;
}

//----------------------------------------------------------------------------
std::string vtkMRMLApplicationLogic::Translate(const char *context, const char *sourceText, const char *disambiguation, int n)
{
if (vtkMRMLApplicationLogic::TranslatorInstance)
{
return vtkMRMLApplicationLogic::TranslatorInstance->Translate(context, sourceText, disambiguation, n);
}
else
{
return sourceText;
}
}

//----------------------------------------------------------------------------
vtkMRMLTranslator* vtkMRMLApplicationLogic::GetTranslatorInstance()
{
return vtkMRMLApplicationLogic::TranslatorInstance;
}

//----------------------------------------------------------------------------
void vtkMRMLApplicationLogic::SetTranslatorInstance(vtkMRMLTranslator* translatorInstance)
{
if (vtkMRMLApplicationLogic::TranslatorInstance == translatorInstance)
{
return;
}
vtkMRMLApplicationLogic::TranslatorInstance = translatorInstance;
}
13 changes: 13 additions & 0 deletions Libs/MRML/Logic/vtkMRMLApplicationLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "vtkMRMLLogicExport.h"
#include "vtkMRMLSliceCompositeNode.h"

#include "vtkMRMLTranslator.h"

class vtkMRMLColorLogic;
class vtkMRMLModelDisplayNode;
class vtkMRMLSliceNode;
Expand Down Expand Up @@ -289,6 +291,15 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic
/// Font file path is set to the one specified in FontFileName property in this object.
void UseCustomFontFile(vtkTextProperty* textProperty);

/// Translation function for logic classes
std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1);

/// @{
/// Set the object used for string translation in logic classes
static void SetTranslatorInstance(vtkMRMLTranslator* translatorInstance);
static vtkMRMLTranslator* GetTranslatorInstance();
/// @}

protected:

vtkMRMLApplicationLogic();
Expand Down Expand Up @@ -316,6 +327,8 @@ class VTK_MRML_LOGIC_EXPORT vtkMRMLApplicationLogic
class vtkInternal;
vtkInternal* Internal;

static vtkMRMLTranslator* TranslatorInstance;

};


Expand Down
6 changes: 6 additions & 0 deletions Libs/MRML/Logic/vtkMRMLTranslator.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "vtkMRMLTranslator.h"

std::string vtkMRMLTranslator::Translate(const char *vtkNotUsed(context), const char *sourceText, const char *vtkNotUsed(disambiguation), int vtkNotUsed(n))
{
return sourceText;
}
18 changes: 18 additions & 0 deletions Libs/MRML/Logic/vtkMRMLTranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef vtkMRMLTranslator_h
#define vtkMRMLTranslator_h

#include "vtkMRMLLogicExport.h"

// STD includes

#include <string>

class VTK_MRML_LOGIC_EXPORT vtkMRMLTranslator
{
public:

/// Default noop translation function for logic classes
virtual std::string Translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1);
};

#endif

0 comments on commit ad99a2c

Please sign in to comment.