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
Expand Up @@ -352,7 +352,6 @@
%Include auto_generated/qgssnappingconfig.sip
%Include auto_generated/qgstaskmanager.sip
%Include auto_generated/qgstolerance.sip
%Include auto_generated/qgstranslationcontext.sip
%Include auto_generated/qgstrackedvectorlayertools.sip
%Include auto_generated/qgstransaction.sip
%Include auto_generated/qgstransactiongroup.sip
Expand All @@ -367,6 +366,7 @@
%Include auto_generated/qgsvectorlayertools.sip
%Include auto_generated/qgsvectorsimplifymethod.sip
%Include auto_generated/qgssettings.sip
%Include auto_generated/qgsprojecttranslator.sip
%Include auto_generated/annotations/qgsannotation.sip
%Include auto_generated/annotations/qgsannotationmanager.sip
%Include auto_generated/annotations/qgshtmlannotation.sip
Expand Down Expand Up @@ -442,4 +442,5 @@
%Include auto_generated/layertree/qgslayertreeregistrybridge.sip
%Include auto_generated/qgsuserprofilemanager.sip
%Include auto_generated/symbology/qgsarrowsymbollayer.sip
%Include auto_generated/qgstranslationcontext.sip
%Include auto_generated/qgsuserprofile.sip
19 changes: 9 additions & 10 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -831,24 +831,23 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle );

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

// 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
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( QStringLiteral( "en_US" ) ) ), QLocale( QStringLiteral( "en_US" ) ).nativeLanguageName(), QStringLiteral( "en_US" ) );
cbtsLocale->setCurrentIndex( cbtsLocale->findData( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() ) );

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

annotations/qgsannotation.h
annotations/qgsannotationmanager.h
Expand Down
3 changes: 2 additions & 1 deletion src/core/layertree/qgslayertreegroup.cpp
Expand Up @@ -255,7 +255,8 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
{
QList<QgsLayerTreeGroup *> list;
Q_FOREACH ( QgsLayerTreeNode *child, mChildren )

for ( QgsLayerTreeNode *child : mChildren )
{
if ( QgsLayerTree::isGroup( child ) )
list << QgsLayerTree::toGroup( child );
Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreenode.cpp
Expand Up @@ -65,6 +65,7 @@ QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProj
if ( project )
resolver = project->pathResolver();
context.setPathResolver( resolver );
context.setProjectTranslator( ( QgsProject * )project );

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

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

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

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

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

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

QgsReadWriteContext context;
context.setProjectTranslator( this );
if ( !storage->readProject( filename, &inDevice, context ) )
{
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;
context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );

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

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

// 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;
context.setPathResolver( pathResolver() );
context.setProjectTranslator( this );
QList<QDomNode> brokenNodes;
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;
if ( !useAbsolutePaths )
embeddedContext.setPathResolver( QgsPathResolver( projectFilePath ) );
embeddedContext.setProjectTranslator( this );

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

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

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

/**
* Emitted when the project is being written.
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsprojecttranslator.h
Expand Up @@ -37,6 +37,7 @@ class CORE_EXPORT QgsProjectTranslator
*
* \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;
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsreadwritecontext.cpp
Expand Up @@ -14,6 +14,12 @@
***************************************************************************/
#include "qgsreadwritecontext.h"

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

}

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

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

QList<QgsReadWriteContext::ReadWriteMessage > QgsReadWriteContext::takeMessages()
{
QList<QgsReadWriteContext::ReadWriteMessage > messages = mMessages;
mMessages.clear();
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
Expand Up @@ -65,7 +65,7 @@ class CORE_EXPORT QgsReadWriteContext
/**
* Constructor for QgsReadWriteContext.
*/
QgsReadWriteContext() = default;
QgsReadWriteContext();

~QgsReadWriteContext();

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

void setProjectTranslator( QgsProjectTranslator *projectTranslator );

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
void leaveCategory();

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

friend class QgsReadWriteContextCategoryPopper;
DefaultTranslator mDefaultTranslator;
};


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

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

Expand All @@ -35,7 +35,7 @@ QgsRelation QgsRelation::createFromXml( const QDomNode &node )
QString referencingLayerId = elem.attribute( QStringLiteral( "referencingLayer" ) );
QString referencedLayerId = elem.attribute( QStringLiteral( "referencedLayer" ) );
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" ) );

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

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

#include "qgis.h"

Expand Down Expand Up @@ -100,7 +101,7 @@ class CORE_EXPORT QgsRelation
*
* \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
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsrelationmanager.cpp
Expand Up @@ -153,7 +153,7 @@ QList<QgsRelation> QgsRelationManager::referencedRelations( QgsVectorLayer *laye
return relations;
}

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

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

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

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

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

0 comments on commit 090db0d

Please sign in to comment.