Skip to content
Permalink
Browse files

setProjectTranslator

...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 Jul 30, 2018
1 parent fc018da commit 090db0d86bec044c261c2c3d63f6696d349a048a
@@ -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
@@ -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
@@ -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
@@ -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() ) );

@@ -657,6 +657,7 @@ SET(QGIS_CORE_MOC_HDRS
qgswebpage.h
qgswebview.h
qgssettings.h
qgsprojecttranslator.h

annotations/qgsannotation.h
annotations/qgsannotationmanager.h
@@ -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 );
@@ -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 )
@@ -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" ) );
@@ -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 );
}
@@ -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 );
}

@@ -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;
@@ -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 );
@@ -1107,6 +1109,7 @@ bool QgsProject::readProjectFile( const QString &filename )

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

//crs
QgsCoordinateReferenceSystem projectCrs;
@@ -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
@@ -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 ) )
{
@@ -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() )
@@ -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;
@@ -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.
@@ -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;
@@ -14,6 +14,12 @@
***************************************************************************/
#include "qgsreadwritecontext.h"

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

}

QgsReadWriteContext::~QgsReadWriteContext()
{
// be sure that categories have been emptied
@@ -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;
}
@@ -65,7 +65,7 @@ class CORE_EXPORT QgsReadWriteContext
/**
* Constructor for QgsReadWriteContext.
*/
QgsReadWriteContext() = default;
QgsReadWriteContext();

~QgsReadWriteContext();

@@ -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;
};


@@ -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();

@@ -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();
@@ -22,6 +22,7 @@

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

#include "qgis.h"

@@ -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
@@ -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();

@@ -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
@@ -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 );

@@ -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 );

0 comments on commit 090db0d

Please sign in to comment.
You can’t perform that action at this time.