Skip to content

Commit

Permalink
setProjectTranslator
Browse files Browse the repository at this point in the history
...in QgsReadWriteContext creation
implementation of projectTranslator where needed

use of DefaultTranslator to have default translation funtion derived from QgsProjectTranslator

beautify code
  • Loading branch information
signedav committed Aug 7, 2018
1 parent fc018da commit 090db0d
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 23 deletions.
3 changes: 2 additions & 1 deletion python/core/core_auto.sip
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
%Include auto_generated/qgssnappingconfig.sip %Include auto_generated/qgssnappingconfig.sip
%Include auto_generated/qgstaskmanager.sip %Include auto_generated/qgstaskmanager.sip
%Include auto_generated/qgstolerance.sip %Include auto_generated/qgstolerance.sip
%Include auto_generated/qgstranslationcontext.sip
%Include auto_generated/qgstrackedvectorlayertools.sip %Include auto_generated/qgstrackedvectorlayertools.sip
%Include auto_generated/qgstransaction.sip %Include auto_generated/qgstransaction.sip
%Include auto_generated/qgstransactiongroup.sip %Include auto_generated/qgstransactiongroup.sip
Expand All @@ -367,6 +366,7 @@
%Include auto_generated/qgsvectorlayertools.sip %Include auto_generated/qgsvectorlayertools.sip
%Include auto_generated/qgsvectorsimplifymethod.sip %Include auto_generated/qgsvectorsimplifymethod.sip
%Include auto_generated/qgssettings.sip %Include auto_generated/qgssettings.sip
%Include auto_generated/qgsprojecttranslator.sip
%Include auto_generated/annotations/qgsannotation.sip %Include auto_generated/annotations/qgsannotation.sip
%Include auto_generated/annotations/qgsannotationmanager.sip %Include auto_generated/annotations/qgsannotationmanager.sip
%Include auto_generated/annotations/qgshtmlannotation.sip %Include auto_generated/annotations/qgshtmlannotation.sip
Expand Down Expand Up @@ -442,4 +442,5 @@
%Include auto_generated/layertree/qgslayertreeregistrybridge.sip %Include auto_generated/layertree/qgslayertreeregistrybridge.sip
%Include auto_generated/qgsuserprofilemanager.sip %Include auto_generated/qgsuserprofilemanager.sip
%Include auto_generated/symbology/qgsarrowsymbollayer.sip %Include auto_generated/symbology/qgsarrowsymbollayer.sip
%Include auto_generated/qgstranslationcontext.sip
%Include auto_generated/qgsuserprofile.sip %Include auto_generated/qgsuserprofile.sip
19 changes: 9 additions & 10 deletions src/app/qgsprojectproperties.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -831,24 +831,23 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle ); connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle );


//fill ts language checkbox //fill ts language checkbox
//could possibly be taken from QgsOptions //fill ts language checkbox
QString myI18nPath = QgsApplication::i18nPath(); QString i18nPath = QgsApplication::i18nPath();
QDir myDir( myI18nPath, QStringLiteral( "qgis*.qm" ) ); QDir i18Dir( i18nPath, QStringLiteral( "qgis*.qm" ) );
QStringList myFileList = myDir.entryList(); const QStringList qmFileList = i18Dir.entryList();
QStringListIterator myIterator( myFileList ); for ( const QString &qmFile : qmFileList )
while ( myIterator.hasNext() )
{ {
QString myFileName = myIterator.next();

// Ignore the 'en' translation file, already added as 'en_US'. // Ignore the 'en' translation file, already added as 'en_US'.
if ( myFileName.compare( QLatin1String( "qgis_en.qm" ) ) == 0 ) continue; if ( qmFile.compare( QLatin1String( "qgis_en.qm" ) ) == 0 ) continue;


QString l = myFileName.remove( QStringLiteral( "qgis_" ) ).remove( QStringLiteral( ".qm" ) ); QString qmFileName = qmFile;
QString l = qmFileName.remove( QStringLiteral( "qgis_" ) ).remove( QStringLiteral( ".qm" ) );


// QTBUG-57802: eo locale is improperly handled // QTBUG-57802: eo locale is improperly handled
QString displayName = l.startsWith( QLatin1String( "eo" ) ) ? QLocale::languageToString( QLocale::Esperanto ) : QLocale( l ).nativeLanguageName(); QString displayName = l.startsWith( QLatin1String( "eo" ) ) ? QLocale::languageToString( QLocale::Esperanto ) : QLocale( l ).nativeLanguageName();
cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( l ) ), displayName, l ); cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( l ) ), displayName, l );
} }

cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( QStringLiteral( "en_US" ) ) ), QLocale( QStringLiteral( "en_US" ) ).nativeLanguageName(), QStringLiteral( "en_US" ) ); cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( QStringLiteral( "en_US" ) ) ), QLocale( QStringLiteral( "en_US" ) ).nativeLanguageName(), QStringLiteral( "en_US" ) );
cbtsLocale->setCurrentIndex( cbtsLocale->findData( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() ) ); cbtsLocale->setCurrentIndex( cbtsLocale->findData( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() ) );


Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ SET(QGIS_CORE_MOC_HDRS
qgswebpage.h qgswebpage.h
qgswebview.h qgswebview.h
qgssettings.h qgssettings.h
qgsprojecttranslator.h


annotations/qgsannotation.h annotations/qgsannotation.h
annotations/qgsannotationmanager.h annotations/qgsannotationmanager.h
Expand Down
3 changes: 2 additions & 1 deletion src/core/layertree/qgslayertreegroup.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
{ {
QList<QgsLayerTreeGroup *> list; QList<QgsLayerTreeGroup *> list;
Q_FOREACH ( QgsLayerTreeNode *child, mChildren )
for ( QgsLayerTreeNode *child : mChildren )
{ {
if ( QgsLayerTree::isGroup( child ) ) if ( QgsLayerTree::isGroup( child ) )
list << QgsLayerTree::toGroup( child ); list << QgsLayerTree::toGroup( child );
Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreenode.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProj
if ( project ) if ( project )
resolver = project->pathResolver(); resolver = project->pathResolver();
context.setPathResolver( resolver ); context.setPathResolver( resolver );
context.setProjectTranslator( ( QgsProject * )project );


QgsLayerTreeNode *node = readXml( element, context ); QgsLayerTreeNode *node = readXml( element, context );
if ( node ) if ( node )
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgscompositionconverter.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ bool QgsCompositionConverter::readLegendXml( QgsLayoutItemLegend *layoutItem, co
pathResolver = project->pathResolver(); pathResolver = project->pathResolver();
QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( pathResolver ); context.setPathResolver( pathResolver );
context.setProjectTranslator( ( QgsProject * )project );


//composer map: use uuid //composer map: use uuid
QString mapId = itemElem.attribute( QStringLiteral( "map" ), QStringLiteral( "-1" ) ); QString mapId = itemElem.attribute( QStringLiteral( "map" ), QStringLiteral( "-1" ) );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgslayerdefinition.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bool QgsLayerDefinition::loadLayerDefinition( const QString &path, QgsProject *p


QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( QgsPathResolver( path ) ); context.setPathResolver( QgsPathResolver( path ) );
context.setProjectTranslator( project );


return loadLayerDefinition( doc, project, rootGroup, errorMessage, context ); return loadLayerDefinition( doc, project, rootGroup, errorMessage, context );
} }
Expand Down Expand Up @@ -298,6 +299,7 @@ QList<QgsMapLayer *> QgsLayerDefinition::loadLayerDefinitionLayers( const QStrin


QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( QgsPathResolver( qlrfile ) ); context.setPathResolver( QgsPathResolver( qlrfile ) );
//no projecttranslator defined here
return QgsLayerDefinition::loadLayerDefinitionLayers( doc, context ); return QgsLayerDefinition::loadLayerDefinitionLayers( doc, context );
} }


Expand Down
8 changes: 7 additions & 1 deletion src/core/qgsproject.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
{ {
QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( pathResolver() ); context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );
if ( !addLayer( element, brokenNodes, context ) ) if ( !addLayer( element, brokenNodes, context ) )
{ {
returnStatus = false; returnStatus = false;
Expand Down Expand Up @@ -960,6 +961,7 @@ bool QgsProject::read()
} }


QgsReadWriteContext context; QgsReadWriteContext context;
context.setProjectTranslator( this );
if ( !storage->readProject( filename, &inDevice, context ) ) if ( !storage->readProject( filename, &inDevice, context ) )
{ {
QString err = tr( "Unable to open %1" ).arg( filename ); QString err = tr( "Unable to open %1" ).arg( filename );
Expand Down Expand Up @@ -1107,6 +1109,7 @@ bool QgsProject::readProjectFile( const QString &filename )


QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( pathResolver() ); context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );


//crs //crs
QgsCoordinateReferenceSystem projectCrs; QgsCoordinateReferenceSystem projectCrs;
Expand Down Expand Up @@ -1302,7 +1305,7 @@ bool QgsProject::readProjectFile( const QString &filename )
emit ellipsoidChanged( ellipsoid() ); emit ellipsoidChanged( ellipsoid() );


// read the project: used by map canvas and legend // read the project: used by map canvas and legend
emit readProject( *doc ); emit readProject( *doc, context );
emit snappingConfigChanged( mSnappingConfig ); emit snappingConfigChanged( mSnappingConfig );


// if all went well, we're allegedly in pristine state // if all went well, we're allegedly in pristine state
Expand Down Expand Up @@ -1537,6 +1540,7 @@ bool QgsProject::readLayer( const QDomNode &layerNode )
{ {
QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( pathResolver() ); context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );
QList<QDomNode> brokenNodes; QList<QDomNode> brokenNodes;
if ( addLayer( layerNode.toElement(), brokenNodes, context ) ) if ( addLayer( layerNode.toElement(), brokenNodes, context ) )
{ {
Expand Down Expand Up @@ -2151,6 +2155,7 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
QgsReadWriteContext embeddedContext; QgsReadWriteContext embeddedContext;
if ( !useAbsolutePaths ) if ( !useAbsolutePaths )
embeddedContext.setPathResolver( QgsPathResolver( projectFilePath ) ); embeddedContext.setPathResolver( QgsPathResolver( projectFilePath ) );
embeddedContext.setProjectTranslator( this );


QDomElement projectLayersElem = sProjectDocument.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) ); QDomElement projectLayersElem = sProjectDocument.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) );
if ( projectLayersElem.isNull() ) if ( projectLayersElem.isNull() )
Expand Down Expand Up @@ -2207,6 +2212,7 @@ QgsLayerTreeGroup *QgsProject::createEmbeddedGroup( const QString &groupName, co


QgsReadWriteContext context; QgsReadWriteContext context;
context.setPathResolver( pathResolver() ); context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );


// store identify disabled layers of the embedded project // store identify disabled layers of the embedded project
QSet<QString> embeddedIdentifyDisabledLayers; QSet<QString> embeddedIdentifyDisabledLayers;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsproject.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
/** /**
* Emitted when a project is being read. * Emitted when a project is being read.
*/ */
void readProject( const QDomDocument & ); void readProject( const QDomDocument &, QgsReadWriteContext &context );


/** /**
* Emitted when the project is being written. * Emitted when the project is being written.
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsprojecttranslator.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CORE_EXPORT QgsProjectTranslator
* *
* \since QGIS 3.4 * \since QGIS 3.4
*/ */

virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0; virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0;


virtual ~QgsProjectTranslator() = default; virtual ~QgsProjectTranslator() = default;
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsreadwritecontext.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
***************************************************************************/ ***************************************************************************/
#include "qgsreadwritecontext.h" #include "qgsreadwritecontext.h"


QgsReadWriteContext::QgsReadWriteContext()
: mProjectTranslator( &mDefaultTranslator )
{

}

QgsReadWriteContext::~QgsReadWriteContext() QgsReadWriteContext::~QgsReadWriteContext()
{ {
// be sure that categories have been emptied // be sure that categories have been emptied
Expand Down Expand Up @@ -50,9 +56,22 @@ void QgsReadWriteContext::leaveCategory()
mCategories.pop_back(); mCategories.pop_back();
} }


void QgsReadWriteContext::setProjectTranslator( QgsProjectTranslator *projectTranslator )
{
mProjectTranslator = projectTranslator;
}

QList<QgsReadWriteContext::ReadWriteMessage > QgsReadWriteContext::takeMessages() QList<QgsReadWriteContext::ReadWriteMessage > QgsReadWriteContext::takeMessages()
{ {
QList<QgsReadWriteContext::ReadWriteMessage > messages = mMessages; QList<QgsReadWriteContext::ReadWriteMessage > messages = mMessages;
mMessages.clear(); mMessages.clear();
return messages; return messages;
} }

QString QgsReadWriteContext::DefaultTranslator::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const
{
Q_UNUSED( context );
Q_UNUSED( disambiguation );
Q_UNUSED( n );
return sourceText;
}
14 changes: 12 additions & 2 deletions src/core/qgsreadwritecontext.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CORE_EXPORT QgsReadWriteContext
/** /**
* Constructor for QgsReadWriteContext. * Constructor for QgsReadWriteContext.
*/ */
QgsReadWriteContext() = default; QgsReadWriteContext();


~QgsReadWriteContext(); ~QgsReadWriteContext();


Expand Down Expand Up @@ -106,16 +106,26 @@ class CORE_EXPORT QgsReadWriteContext
*/ */
const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; } const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }


void setProjectTranslator( QgsProjectTranslator *projectTranslator );

private: private:

class DefaultTranslator : public QgsProjectTranslator
{
// QgsProjectTranslator interface
public:
QString translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const;
};

//! Pop the last category //! Pop the last category
void leaveCategory(); void leaveCategory();


QgsPathResolver mPathResolver; QgsPathResolver mPathResolver;
QList<ReadWriteMessage> mMessages; QList<ReadWriteMessage> mMessages;
QStringList mCategories = QStringList(); QStringList mCategories = QStringList();
QgsProjectTranslator *mProjectTranslator; QgsProjectTranslator *mProjectTranslator;

friend class QgsReadWriteContextCategoryPopper; friend class QgsReadWriteContextCategoryPopper;
DefaultTranslator mDefaultTranslator;
}; };




Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsrelation.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "qgsproject.h" #include "qgsproject.h"
#include "qgsvectorlayer.h" #include "qgsvectorlayer.h"


QgsRelation QgsRelation::createFromXml( const QDomNode &node ) QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContext &context )
{ {
QDomElement elem = node.toElement(); QDomElement elem = node.toElement();


Expand All @@ -35,7 +35,7 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node )
QString referencingLayerId = elem.attribute( QStringLiteral( "referencingLayer" ) ); QString referencingLayerId = elem.attribute( QStringLiteral( "referencingLayer" ) );
QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) ); QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) );
QString id = elem.attribute( QStringLiteral( "id" ) ); QString id = elem.attribute( QStringLiteral( "id" ) );
QString name = QgsProject::instance()->translate( QStringLiteral( "project:relations" ), elem.attribute( QStringLiteral( "name" ) ) ); QString name = context.projectTranslator()->translate( QStringLiteral( "project:relations" ), elem.attribute( QStringLiteral( "name" ) ) );
QString strength = elem.attribute( QStringLiteral( "strength" ) ); QString strength = elem.attribute( QStringLiteral( "strength" ) );


const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers(); const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers();
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsrelation.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@


#include "qgis_core.h" #include "qgis_core.h"
#include "qgsfields.h" #include "qgsfields.h"
#include "qgsreadwritecontext.h"


#include "qgis.h" #include "qgis.h"


Expand Down Expand Up @@ -100,7 +101,7 @@ class CORE_EXPORT QgsRelation
* *
* \returns A relation * \returns A relation
*/ */
static QgsRelation createFromXml( const QDomNode &node ); static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context );


/** /**
* Writes a relation to an XML structure. Used for saving .qgs projects * Writes a relation to an XML structure. Used for saving .qgs projects
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsrelationmanager.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ QList<QgsRelation> QgsRelationManager::referencedRelations( QgsVectorLayer *laye
return relations; return relations;
} }


void QgsRelationManager::readProject( const QDomDocument &doc ) void QgsRelationManager::readProject( const QDomDocument &doc, QgsReadWriteContext &context )
{ {
mRelations.clear(); mRelations.clear();


Expand All @@ -165,7 +165,7 @@ void QgsRelationManager::readProject( const QDomDocument &doc )
int relCount = relationNodes.count(); int relCount = relationNodes.count();
for ( int i = 0; i < relCount; ++i ) for ( int i = 0; i < relCount; ++i )
{ {
addRelation( QgsRelation::createFromXml( relationNodes.at( i ) ) ); addRelation( QgsRelation::createFromXml( relationNodes.at( i ), context ) );
} }
} }
else else
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrelationmanager.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CORE_EXPORT QgsRelationManager : public QObject
void changed(); void changed();


private slots: private slots:
void readProject( const QDomDocument &doc ); void readProject( const QDomDocument &doc, QgsReadWriteContext &context );
void writeProject( QDomDocument &doc ); void writeProject( QDomDocument &doc );
void layersRemoved( const QStringList &layers ); void layersRemoved( const QStringList &layers );


Expand Down
2 changes: 1 addition & 1 deletion src/core/qgstranslationcontext.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void QgsTranslationContext::writeTsFile( const QString &locale )
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() ); tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );
doc.appendChild( tsElement ); doc.appendChild( tsElement );


for ( TranslatableObject translatableObject : mTranslatableObjects ) for ( const TranslatableObject &translatableObject : mTranslatableObjects )
{ {
QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) ); QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
tsElement.appendChild( contextElement ); tsElement.appendChild( contextElement );
Expand Down

0 comments on commit 090db0d

Please sign in to comment.