Skip to content

Commit 090db0d

Browse files
committedAug 7, 2018
setProjectTranslator
...in QgsReadWriteContext creation implementation of projectTranslator where needed use of DefaultTranslator to have default translation funtion derived from QgsProjectTranslator beautify code
1 parent fc018da commit 090db0d

17 files changed

+66
-23
lines changed
 

‎python/core/core_auto.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@
352352
%Include auto_generated/qgssnappingconfig.sip
353353
%Include auto_generated/qgstaskmanager.sip
354354
%Include auto_generated/qgstolerance.sip
355-
%Include auto_generated/qgstranslationcontext.sip
356355
%Include auto_generated/qgstrackedvectorlayertools.sip
357356
%Include auto_generated/qgstransaction.sip
358357
%Include auto_generated/qgstransactiongroup.sip
@@ -367,6 +366,7 @@
367366
%Include auto_generated/qgsvectorlayertools.sip
368367
%Include auto_generated/qgsvectorsimplifymethod.sip
369368
%Include auto_generated/qgssettings.sip
369+
%Include auto_generated/qgsprojecttranslator.sip
370370
%Include auto_generated/annotations/qgsannotation.sip
371371
%Include auto_generated/annotations/qgsannotationmanager.sip
372372
%Include auto_generated/annotations/qgshtmlannotation.sip
@@ -442,4 +442,5 @@
442442
%Include auto_generated/layertree/qgslayertreeregistrybridge.sip
443443
%Include auto_generated/qgsuserprofilemanager.sip
444444
%Include auto_generated/symbology/qgsarrowsymbollayer.sip
445+
%Include auto_generated/qgstranslationcontext.sip
445446
%Include auto_generated/qgsuserprofile.sip

‎src/app/qgsprojectproperties.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -831,24 +831,23 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
831831
connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle );
832832

833833
//fill ts language checkbox
834-
//could possibly be taken from QgsOptions
835-
QString myI18nPath = QgsApplication::i18nPath();
836-
QDir myDir( myI18nPath, QStringLiteral( "qgis*.qm" ) );
837-
QStringList myFileList = myDir.entryList();
838-
QStringListIterator myIterator( myFileList );
839-
while ( myIterator.hasNext() )
834+
//fill ts language checkbox
835+
QString i18nPath = QgsApplication::i18nPath();
836+
QDir i18Dir( i18nPath, QStringLiteral( "qgis*.qm" ) );
837+
const QStringList qmFileList = i18Dir.entryList();
838+
for ( const QString &qmFile : qmFileList )
840839
{
841-
QString myFileName = myIterator.next();
842-
843840
// Ignore the 'en' translation file, already added as 'en_US'.
844-
if ( myFileName.compare( QLatin1String( "qgis_en.qm" ) ) == 0 ) continue;
841+
if ( qmFile.compare( QLatin1String( "qgis_en.qm" ) ) == 0 ) continue;
845842

846-
QString l = myFileName.remove( QStringLiteral( "qgis_" ) ).remove( QStringLiteral( ".qm" ) );
843+
QString qmFileName = qmFile;
844+
QString l = qmFileName.remove( QStringLiteral( "qgis_" ) ).remove( QStringLiteral( ".qm" ) );
847845

848846
// QTBUG-57802: eo locale is improperly handled
849847
QString displayName = l.startsWith( QLatin1String( "eo" ) ) ? QLocale::languageToString( QLocale::Esperanto ) : QLocale( l ).nativeLanguageName();
850848
cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( l ) ), displayName, l );
851849
}
850+
852851
cbtsLocale->addItem( QIcon( QString( ":/images/flags/%1.svg" ).arg( QStringLiteral( "en_US" ) ) ), QLocale( QStringLiteral( "en_US" ) ).nativeLanguageName(), QStringLiteral( "en_US" ) );
853852
cbtsLocale->setCurrentIndex( cbtsLocale->findData( settings.value( QStringLiteral( "locale/userLocale" ), QString() ).toString() ) );
854853

‎src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ SET(QGIS_CORE_MOC_HDRS
657657
qgswebpage.h
658658
qgswebview.h
659659
qgssettings.h
660+
qgsprojecttranslator.h
660661

661662
annotations/qgsannotation.h
662663
annotations/qgsannotationmanager.h

‎src/core/layertree/qgslayertreegroup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ QgsLayerTreeGroup *QgsLayerTreeGroup::findGroup( const QString &name )
255255
QList<QgsLayerTreeGroup *> QgsLayerTreeGroup::findGroups() const
256256
{
257257
QList<QgsLayerTreeGroup *> list;
258-
Q_FOREACH ( QgsLayerTreeNode *child, mChildren )
258+
259+
for ( QgsLayerTreeNode *child : mChildren )
259260
{
260261
if ( QgsLayerTree::isGroup( child ) )
261262
list << QgsLayerTree::toGroup( child );

‎src/core/layertree/qgslayertreenode.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProj
6565
if ( project )
6666
resolver = project->pathResolver();
6767
context.setPathResolver( resolver );
68+
context.setProjectTranslator( ( QgsProject * )project );
6869

6970
QgsLayerTreeNode *node = readXml( element, context );
7071
if ( node )

‎src/core/layout/qgscompositionconverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ bool QgsCompositionConverter::readLegendXml( QgsLayoutItemLegend *layoutItem, co
11931193
pathResolver = project->pathResolver();
11941194
QgsReadWriteContext context;
11951195
context.setPathResolver( pathResolver );
1196+
context.setProjectTranslator( ( QgsProject * )project );
11961197

11971198
//composer map: use uuid
11981199
QString mapId = itemElem.attribute( QStringLiteral( "map" ), QStringLiteral( "-1" ) );

‎src/core/qgslayerdefinition.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool QgsLayerDefinition::loadLayerDefinition( const QString &path, QgsProject *p
5151

5252
QgsReadWriteContext context;
5353
context.setPathResolver( QgsPathResolver( path ) );
54+
context.setProjectTranslator( project );
5455

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

299300
QgsReadWriteContext context;
300301
context.setPathResolver( QgsPathResolver( qlrfile ) );
302+
//no projecttranslator defined here
301303
return QgsLayerDefinition::loadLayerDefinitionLayers( doc, context );
302304
}
303305

‎src/core/qgsproject.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
861861
{
862862
QgsReadWriteContext context;
863863
context.setPathResolver( pathResolver() );
864+
context.setProjectTranslator( this );
864865
if ( !addLayer( element, brokenNodes, context ) )
865866
{
866867
returnStatus = false;
@@ -960,6 +961,7 @@ bool QgsProject::read()
960961
}
961962

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

11081110
QgsReadWriteContext context;
11091111
context.setPathResolver( pathResolver() );
1112+
context.setProjectTranslator( this );
11101113

11111114
//crs
11121115
QgsCoordinateReferenceSystem projectCrs;
@@ -1302,7 +1305,7 @@ bool QgsProject::readProjectFile( const QString &filename )
13021305
emit ellipsoidChanged( ellipsoid() );
13031306

13041307
// read the project: used by map canvas and legend
1305-
emit readProject( *doc );
1308+
emit readProject( *doc, context );
13061309
emit snappingConfigChanged( mSnappingConfig );
13071310

13081311
// if all went well, we're allegedly in pristine state
@@ -1537,6 +1540,7 @@ bool QgsProject::readLayer( const QDomNode &layerNode )
15371540
{
15381541
QgsReadWriteContext context;
15391542
context.setPathResolver( pathResolver() );
1543+
context.setProjectTranslator( this );
15401544
QList<QDomNode> brokenNodes;
15411545
if ( addLayer( layerNode.toElement(), brokenNodes, context ) )
15421546
{
@@ -2151,6 +2155,7 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
21512155
QgsReadWriteContext embeddedContext;
21522156
if ( !useAbsolutePaths )
21532157
embeddedContext.setPathResolver( QgsPathResolver( projectFilePath ) );
2158+
embeddedContext.setProjectTranslator( this );
21542159

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

22082213
QgsReadWriteContext context;
22092214
context.setPathResolver( pathResolver() );
2215+
context.setProjectTranslator( this );
22102216

22112217
// store identify disabled layers of the embedded project
22122218
QSet<QString> embeddedIdentifyDisabledLayers;

‎src/core/qgsproject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
997997
/**
998998
* Emitted when a project is being read.
999999
*/
1000-
void readProject( const QDomDocument & );
1000+
void readProject( const QDomDocument &, QgsReadWriteContext &context );
10011001

10021002
/**
10031003
* Emitted when the project is being written.

‎src/core/qgsprojecttranslator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CORE_EXPORT QgsProjectTranslator
3737
*
3838
* \since QGIS 3.4
3939
*/
40+
4041
virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = nullptr, int n = -1 ) const = 0;
4142

4243
virtual ~QgsProjectTranslator() = default;

‎src/core/qgsreadwritecontext.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
***************************************************************************/
1515
#include "qgsreadwritecontext.h"
1616

17+
QgsReadWriteContext::QgsReadWriteContext()
18+
: mProjectTranslator( &mDefaultTranslator )
19+
{
20+
21+
}
22+
1723
QgsReadWriteContext::~QgsReadWriteContext()
1824
{
1925
// be sure that categories have been emptied
@@ -50,9 +56,22 @@ void QgsReadWriteContext::leaveCategory()
5056
mCategories.pop_back();
5157
}
5258

59+
void QgsReadWriteContext::setProjectTranslator( QgsProjectTranslator *projectTranslator )
60+
{
61+
mProjectTranslator = projectTranslator;
62+
}
63+
5364
QList<QgsReadWriteContext::ReadWriteMessage > QgsReadWriteContext::takeMessages()
5465
{
5566
QList<QgsReadWriteContext::ReadWriteMessage > messages = mMessages;
5667
mMessages.clear();
5768
return messages;
5869
}
70+
71+
QString QgsReadWriteContext::DefaultTranslator::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const
72+
{
73+
Q_UNUSED( context );
74+
Q_UNUSED( disambiguation );
75+
Q_UNUSED( n );
76+
return sourceText;
77+
}

‎src/core/qgsreadwritecontext.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CORE_EXPORT QgsReadWriteContext
6565
/**
6666
* Constructor for QgsReadWriteContext.
6767
*/
68-
QgsReadWriteContext() = default;
68+
QgsReadWriteContext();
6969

7070
~QgsReadWriteContext();
7171

@@ -106,16 +106,26 @@ class CORE_EXPORT QgsReadWriteContext
106106
*/
107107
const QgsProjectTranslator *projectTranslator( ) const { return mProjectTranslator; }
108108

109+
void setProjectTranslator( QgsProjectTranslator *projectTranslator );
110+
109111
private:
112+
113+
class DefaultTranslator : public QgsProjectTranslator
114+
{
115+
// QgsProjectTranslator interface
116+
public:
117+
QString translate( const QString &context, const QString &sourceText, const char *disambiguation, int n ) const;
118+
};
119+
110120
//! Pop the last category
111121
void leaveCategory();
112122

113123
QgsPathResolver mPathResolver;
114124
QList<ReadWriteMessage> mMessages;
115125
QStringList mCategories = QStringList();
116126
QgsProjectTranslator *mProjectTranslator;
117-
118127
friend class QgsReadWriteContextCategoryPopper;
128+
DefaultTranslator mDefaultTranslator;
119129
};
120130

121131

‎src/core/qgsrelation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "qgsproject.h"
2222
#include "qgsvectorlayer.h"
2323

24-
QgsRelation QgsRelation::createFromXml( const QDomNode &node )
24+
QgsRelation QgsRelation::createFromXml( const QDomNode &node, QgsReadWriteContext &context )
2525
{
2626
QDomElement elem = node.toElement();
2727

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

4141
const QMap<QString, QgsMapLayer *> &mapLayers = QgsProject::instance()->mapLayers();

‎src/core/qgsrelation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "qgis_core.h"
2424
#include "qgsfields.h"
25+
#include "qgsreadwritecontext.h"
2526

2627
#include "qgis.h"
2728

@@ -100,7 +101,7 @@ class CORE_EXPORT QgsRelation
100101
*
101102
* \returns A relation
102103
*/
103-
static QgsRelation createFromXml( const QDomNode &node );
104+
static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context );
104105

105106
/**
106107
* Writes a relation to an XML structure. Used for saving .qgs projects

‎src/core/qgsrelationmanager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ QList<QgsRelation> QgsRelationManager::referencedRelations( QgsVectorLayer *laye
153153
return relations;
154154
}
155155

156-
void QgsRelationManager::readProject( const QDomDocument &doc )
156+
void QgsRelationManager::readProject( const QDomDocument &doc, QgsReadWriteContext &context )
157157
{
158158
mRelations.clear();
159159

@@ -165,7 +165,7 @@ void QgsRelationManager::readProject( const QDomDocument &doc )
165165
int relCount = relationNodes.count();
166166
for ( int i = 0; i < relCount; ++i )
167167
{
168-
addRelation( QgsRelation::createFromXml( relationNodes.at( i ) ) );
168+
addRelation( QgsRelation::createFromXml( relationNodes.at( i ), context ) );
169169
}
170170
}
171171
else

‎src/core/qgsrelationmanager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class CORE_EXPORT QgsRelationManager : public QObject
142142
void changed();
143143

144144
private slots:
145-
void readProject( const QDomDocument &doc );
145+
void readProject( const QDomDocument &doc, QgsReadWriteContext &context );
146146
void writeProject( QDomDocument &doc );
147147
void layersRemoved( const QStringList &layers );
148148

‎src/core/qgstranslationcontext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void QgsTranslationContext::writeTsFile( const QString &locale )
6565
tsElement.setAttribute( QStringLiteral( "sourcelanguage" ), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );
6666
doc.appendChild( tsElement );
6767

68-
for ( TranslatableObject translatableObject : mTranslatableObjects )
68+
for ( const TranslatableObject &translatableObject : mTranslatableObjects )
6969
{
7070
QDomElement contextElement = doc.createElement( QStringLiteral( "context" ) );
7171
tsElement.appendChild( contextElement );

0 commit comments

Comments
 (0)
Please sign in to comment.