Skip to content

Commit

Permalink
QgsProjectTranslator
Browse files Browse the repository at this point in the history
implementation of abstract class QgsProjectTranslator
  • Loading branch information
signedav committed Aug 7, 2018
1 parent 1bccedd commit fc018da
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ SET(QGIS_CORE_MOC_HDRS
qgstaskmanager.h
qgstolerance.h
qgstracer.h
qgstranslationcontext.h
qgstrackedvectorlayertools.h
qgstransaction.h
qgstransactiongroup.h
Expand Down
2 changes: 1 addition & 1 deletion src/core/layertree/qgslayertreegroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::readXml( QDomElement &element, const QgsRe
if ( element.tagName() != QLatin1String( "layer-tree-group" ) )
return nullptr;

QString name = QgsProject::instance()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
QString name = context.projectTranslator()->translate( QStringLiteral( "project:layergroups" ), element.attribute( QStringLiteral( "name" ) ) );
bool isExpanded = ( element.attribute( QStringLiteral( "expanded" ), QStringLiteral( "1" ) ) == QLatin1String( "1" ) );
bool checked = QgsLayerTreeUtils::checkStateFromXml( element.attribute( QStringLiteral( "checked" ) ) ) != Qt::Unchecked;
bool isMutuallyExclusive = element.attribute( QStringLiteral( "mutually-exclusive" ), QStringLiteral( "0" ) ) == QLatin1String( "1" );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
mne = mnl.toElement();

//name can be translated
setName( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );
setName( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );

//short name
QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,7 @@ void QgsProject::generateTsFile( const QString &locale )
translationContext.writeTsFile( locale );
}

QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const
{
if ( !mTranslator )
{
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "qgsprojectmetadata.h"
#include "qgstranslationcontext.h"
#include "qgsvectorlayer.h"
#include "qgsprojecttranslator.h"

class QFileInfo;
class QDomDocument;
Expand Down Expand Up @@ -85,7 +86,7 @@ class QgsAuxiliaryStorage;
*/

class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator, public QgsProjectTranslator
{
Q_OBJECT
Q_PROPERTY( QStringList nonIdentifiableLayers READ nonIdentifiableLayers WRITE setNonIdentifiableLayers NOTIFY nonIdentifiableLayersChanged )
Expand Down Expand Up @@ -980,7 +981,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* \param disambiguation it's the disambiguation
* \param n if -1 uses the appropriate form
*/
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 );
QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const override;

signals:

Expand Down
45 changes: 45 additions & 0 deletions src/core/qgsprojecttranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************************
qgsprojecttranslator.h
---------------------
begin : 24.7.2018
copyright : (C) 2018 by david signer
email : david at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROJECTTRANSLATOR_H
#define QGSPROJECTTRANSLATOR_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgis.h"
#include <QString>

/**
* \ingroup core
* This abstract class is to call translate() for project data from wherever QgsReadWriteContext is available.
*
* \since QGIS 3.4
*/

class CORE_EXPORT QgsProjectTranslator
{
public:

/**
* This method needs to be reimplemented in all classes which implement this interface
*
* \since QGIS 3.4
*/
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0;

virtual ~QgsProjectTranslator() = default;
};

#endif // QGSPROJECTTRANSLATOR_H
7 changes: 7 additions & 0 deletions src/core/qgsreadwritecontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "qgspathresolver.h"
#include "qgis.h"
#include "qgsprojecttranslator.h"

class QgsReadWriteContextCategoryPopper;

Expand Down Expand Up @@ -99,6 +100,11 @@ class CORE_EXPORT QgsReadWriteContext
*/
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();

/**
* Returns the project translator
* \since QGIS 3.2
*/
const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }

private:
//! Pop the last category
Expand All @@ -107,6 +113,7 @@ class CORE_EXPORT QgsReadWriteContext
QgsPathResolver mPathResolver;
QList<ReadWriteMessage> mMessages;
QStringList mCategories = QStringList();
QgsProjectTranslator *mProjectTranslator;

friend class QgsReadWriteContextCategoryPopper;
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,13 +1907,13 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
if ( !aliasElem.attribute( QStringLiteral( "name" ) ).isEmpty() )
{
//if it has alias
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), aliasElem.attribute( QStringLiteral( "name" ) ) );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + aliasElem.attribute( QStringLiteral( "name" ) ), 1 );
}
else
{
//if it has no alias, it should be the fields translation
alias = QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
alias = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ), field );
QgsDebugMsgLevel( "context" + QStringLiteral( "project:layers:%1:fieldaliases" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text() ) + " source " + field, 1 );
//if it gets the exact field value, there has been no translation (or not even translation loaded) - so no alias should be generated;
if ( alias == aliasElem.attribute( QStringLiteral( "field" ) ) )
Expand Down

0 comments on commit fc018da

Please sign in to comment.