From 442c7323f70b5744f15e68d4ea333f7b975011a5 Mon Sep 17 00:00:00 2001 From: signedav Date: Tue, 31 Jul 2018 20:32:54 +0200 Subject: [PATCH] sip files and some improvements like make a list copy before go through for loop and log messages --- .../core/auto_generated/qgsapplication.sip.in | 7 ++ python/core/auto_generated/qgsproject.sip.in | 55 ++++++++++++- .../qgsprojecttranslator.sip.in | 41 ++++++++++ .../auto_generated/qgsreadwritecontext.sip.in | 14 ++++ python/core/auto_generated/qgsrelation.sip.in | 3 +- .../qgstranslationcontext.sip.in | 79 +++++++++++++++++++ src/app/qgsprojectproperties.cpp | 7 +- src/app/qgsprojectproperties.h | 2 +- src/core/qgsapplication.h | 2 +- src/core/qgsproject.cpp | 25 +++--- 10 files changed, 213 insertions(+), 22 deletions(-) create mode 100644 python/core/auto_generated/qgsprojecttranslator.sip.in create mode 100644 python/core/auto_generated/qgstranslationcontext.sip.in diff --git a/python/core/auto_generated/qgsapplication.sip.in b/python/core/auto_generated/qgsapplication.sip.in index 2ec2ad6f277..7d7042ee9bc 100644 --- a/python/core/auto_generated/qgsapplication.sip.in +++ b/python/core/auto_generated/qgsapplication.sip.in @@ -808,6 +808,13 @@ The maximum number of concurrent connections per connections pool. QGIS may in some situations allocate more than this amount of connections to avoid deadlocks. +.. versionadded:: 3.4 +%End + + void collectTranslatableObjects( QgsTranslationContext *translationContext ); +%Docstring +Emits the signal to collect all the strings of .qgs to be included in ts file + .. versionadded:: 3.4 %End diff --git a/python/core/auto_generated/qgsproject.sip.in b/python/core/auto_generated/qgsproject.sip.in index 5170f482f4c..a34169106dd 100644 --- a/python/core/auto_generated/qgsproject.sip.in +++ b/python/core/auto_generated/qgsproject.sip.in @@ -13,7 +13,7 @@ -class QgsProject : QObject, QgsExpressionContextGenerator +class QgsProject : QObject, QgsExpressionContextGenerator, QgsProjectTranslator { %Docstring Reads and writes project states. @@ -135,6 +135,14 @@ Returns last modified time of the project file as returned by the file system (o Returns full absolute path to the project file if the project is stored in a file system - derived from fileName(). Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages). +.. versionadded:: 3.2 +%End + + QString absolutePath() const; +%Docstring +Returns full absolute path to the project folder if the project is stored in a file system - derived from fileName(). +Returns empty string when the project is stored in a project storage (there is no concept of paths for custom project storages). + .. versionadded:: 3.2 %End @@ -983,6 +991,28 @@ and it is mainly a hint for the user interface to protect users from removing la in the project. The removeMapLayer(), removeMapLayers() calls do not block removal of layers listed here. .. versionadded:: 3.2 +%End + + void generateTsFile( const QString &locale ); +%Docstring +Triggers the collection strings of .qgs to be included in ts file and calls writeTsFile() + +.. versionadded:: 3.4 +%End + + virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 ) const; + +%Docstring +Translates the project with QTranslator and qm file + +:return: the result string (in case there is no QTranslator loaded the sourceText) + +:param context: describing layer etc. +:param sourceText: is the identifier of this text +:param disambiguation: it's the disambiguation +:param n: if -1 uses the appropriate form + +.. versionadded:: 3.4 %End signals: @@ -997,7 +1027,7 @@ just before a new project is read). .. versionadded:: 3.2 %End - void readProject( const QDomDocument & ); + void readProject( const QDomDocument &, QgsReadWriteContext &context ); %Docstring Emitted when a project is being read. %End @@ -1335,6 +1365,27 @@ home path will be automatically determined from the project's file path. .. seealso:: :py:func:`homePathChanged` .. versionadded:: 3.2 +%End + + void registerTranslatableContainers( QgsTranslationContext *translationContext, QgsAttributeEditorContainer *parent, const QString &layerId ); +%Docstring +Registers the translatable containers into the tranlation context +this is a rekursive function to get all the child containers + +:param translationContext: where the objects will be registered +:param parent: parent-container containing list of children + +.. versionadded:: 3.4 +%End + + void registerTranslatableObjects( QgsTranslationContext *translationContext ); +%Docstring +Registers the translatable objects into the tranlation context +so there can be created a ts file these values + +:param translationContext: where the objects will be registered + +.. versionadded:: 3.4 %End }; diff --git a/python/core/auto_generated/qgsprojecttranslator.sip.in b/python/core/auto_generated/qgsprojecttranslator.sip.in new file mode 100644 index 00000000000..725867b17b4 --- /dev/null +++ b/python/core/auto_generated/qgsprojecttranslator.sip.in @@ -0,0 +1,41 @@ +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/qgsprojecttranslator.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ + + + +class QgsProjectTranslator +{ +%Docstring +This abstract class is to call translate() for project data from wherever QgsReadWriteContext is available. + +.. versionadded:: 3.4 +%End + +%TypeHeaderCode +#include "qgsprojecttranslator.h" +%End + public: + + + virtual QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 ) const = 0; +%Docstring +This method needs to be reimplemented in all classes which implement this interface + +.. versionadded:: 3.4 +%End + + virtual ~QgsProjectTranslator(); +}; + +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/qgsprojecttranslator.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ diff --git a/python/core/auto_generated/qgsreadwritecontext.sip.in b/python/core/auto_generated/qgsreadwritecontext.sip.in index 30d66d5d112..40d544118e0 100644 --- a/python/core/auto_generated/qgsreadwritecontext.sip.in +++ b/python/core/auto_generated/qgsreadwritecontext.sip.in @@ -98,7 +98,21 @@ Returns the stored messages and remove them .. versionadded:: 3.2 %End + const QgsProjectTranslator *projectTranslator( ) const; +%Docstring +Returns the project translator +.. versionadded:: 3.4 +%End + + void setProjectTranslator( QgsProjectTranslator *projectTranslator ); +%Docstring +Sets the project translator + +.. versionadded:: 3.4 +%End + + public: }; diff --git a/python/core/auto_generated/qgsrelation.sip.in b/python/core/auto_generated/qgsrelation.sip.in index 89fa7e81db5..d73b9bb13a0 100644 --- a/python/core/auto_generated/qgsrelation.sip.in +++ b/python/core/auto_generated/qgsrelation.sip.in @@ -35,11 +35,12 @@ class QgsRelation Default constructor. Creates an invalid relation. %End - static QgsRelation createFromXml( const QDomNode &node ); + static QgsRelation createFromXml( const QDomNode &node, QgsReadWriteContext &context ); %Docstring Creates a relation from an XML structure. Used for reading .qgs projects. :param node: The dom node containing the relation information +:param context: to pass project translator :return: A relation %End diff --git a/python/core/auto_generated/qgstranslationcontext.sip.in b/python/core/auto_generated/qgstranslationcontext.sip.in new file mode 100644 index 00000000000..49421208984 --- /dev/null +++ b/python/core/auto_generated/qgstranslationcontext.sip.in @@ -0,0 +1,79 @@ +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/qgstranslationcontext.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ + + + + +class QgsTranslationContext +{ +%Docstring +used for the collecting of strings of .qgs to be translated and writing of ts file + +.. versionadded:: 3.4 +%End + +%TypeHeaderCode +#include "qgstranslationcontext.h" +%End + public: + + QgsTranslationContext( ); +%Docstring +Constructor +%End + + QgsProject *project() const; +%Docstring +Returns the project + +.. seealso:: :py:func:`setProject` +%End + + void setProject( QgsProject *project ); +%Docstring +Sets the ``project`` where the translation need to be done for + +.. seealso:: :py:func:`project` +%End + + QString fileName() const; +%Docstring +Returns the TS fileName + +.. seealso:: :py:func:`setFileName` +%End + + void setFileName( const QString &fileName ); +%Docstring +Sets the ``name`` of the TS file + +.. seealso:: :py:func:`fileName` +%End + + void registerTranslation( const QString &context, const QString &source ); +%Docstring +Registers the ``string`` to be translated + +:param translationString: name and path of the object need to be translated +:param layerName: the name of the layer +%End + + void writeTsFile( const QString &locale ); +%Docstring +Writes the Ts-file +%End + +}; + +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/qgstranslationcontext.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ diff --git a/src/app/qgsprojectproperties.cpp b/src/app/qgsprojectproperties.cpp index 4cfaf79940a..0b058fb36b1 100644 --- a/src/app/qgsprojectproperties.cpp +++ b/src/app/qgsprojectproperties.cpp @@ -830,7 +830,6 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa connect( mMetadataWidget, &QgsMetadataWidget::titleChanged, titleEdit, &QLineEdit::setText ); connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle ); - //fill ts language checkbox //fill ts language checkbox QString i18nPath = QgsApplication::i18nPath(); QDir i18Dir( i18nPath, QStringLiteral( "qgis*.qm" ) ); @@ -845,10 +844,10 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa // 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( QStringLiteral( ":/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( QStringLiteral( ":/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() ) ); connect( generateTsFileButton, &QPushButton::clicked, this, &QgsProjectProperties::onGenerateTsFileButton ); @@ -2214,7 +2213,7 @@ void QgsProjectProperties::setCurrentPage( const QString &pageWidgetName ) } } -void QgsProjectProperties::onGenerateTsFileButton() +void QgsProjectProperties::onGenerateTsFileButton() const { QString l = cbtsLocale->currentData().toString(); QgsProject::instance()->generateTsFile( l ); diff --git a/src/app/qgsprojectproperties.h b/src/app/qgsprojectproperties.h index 1be137a856c..f691e307639 100644 --- a/src/app/qgsprojectproperties.h +++ b/src/app/qgsprojectproperties.h @@ -93,7 +93,7 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui: void scaleItemChanged( QListWidgetItem *changedScaleItem ); //! generate the ts file with the locale selected in the checkbox - void onGenerateTsFileButton(); + void onGenerateTsFileButton() const; /** * Set WMS default extent to current canvas extent diff --git a/src/core/qgsapplication.h b/src/core/qgsapplication.h index df80c65094a..cab2531a579 100644 --- a/src/core/qgsapplication.h +++ b/src/core/qgsapplication.h @@ -746,7 +746,7 @@ class CORE_EXPORT QgsApplication : public QApplication /** * Emits the signal to collect all the strings of .qgs to be included in ts file * - * \since QGIS 3.2 + * \since QGIS 3.4 */ void collectTranslatableObjects( QgsTranslationContext *translationContext ); diff --git a/src/core/qgsproject.cpp b/src/core/qgsproject.cpp index 3b4ab9b2652..0cc41e0d9ea 100644 --- a/src/core/qgsproject.cpp +++ b/src/core/qgsproject.cpp @@ -444,7 +444,7 @@ void QgsProject::setPresetHomePath( const QString &path ) void QgsProject::registerTranslatableContainers( QgsTranslationContext *translationContext, QgsAttributeEditorContainer *parent, const QString &layerId ) { - QList elements = parent->children(); + const QList elements = parent->children(); for ( QgsAttributeEditorElement *element : elements ) { @@ -463,7 +463,9 @@ void QgsProject::registerTranslatableContainers( QgsTranslationContext *translat void QgsProject::registerTranslatableObjects( QgsTranslationContext *translationContext ) { //register layers - for ( QgsLayerTreeLayer *layer : mRootGroup->findLayers() ) + const QList layers = mRootGroup->findLayers(); + + for ( const QgsLayerTreeLayer *layer : layers ) { translationContext->registerTranslation( QStringLiteral( "project:layers:%1" ).arg( layer->layerId() ), layer->name() ); @@ -489,7 +491,8 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation } //register layergroups - for ( const QgsLayerTreeGroup *groupLayer : mRootGroup->findGroups() ) + const QList groupLayers = mRootGroup->findGroups(); + for ( const QgsLayerTreeGroup *groupLayer : groupLayers ) { translationContext->registerTranslation( QStringLiteral( "project:layergroups" ), groupLayer->name() ); } @@ -1012,10 +1015,7 @@ bool QgsProject::readProjectFile( const QString &filename ) if ( QFile( QStringLiteral( "%1/%2.qm" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) ).exists() ) { mTranslator.reset( new QTranslator() ); - if ( mTranslator->load( localeFileName, QFileInfo( projectFile.fileName() ).absolutePath() ) ) - { - QgsDebugMsg( "Translation loaded" ); - } + mTranslator->load( localeFileName, QFileInfo( projectFile.fileName() ).absolutePath() ); } std::unique_ptr doc( new QDomDocument( QStringLiteral( "qgis" ) ) ); @@ -1319,17 +1319,16 @@ bool QgsProject::readProjectFile( const QString &filename ) { //project possibly translated -> rename it with locale postfix QString newFileName( QStringLiteral( "%1/%2.qgs" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) ); - QgsProject::instance()->setFileName( newFileName ); + setFileName( newFileName ); - if ( QgsProject::instance()->write() ) + if ( write() ) { - QgsProject::instance()->setTitle( localeFileName ); - - QgsDebugMsg( "Translated project saved with locale prefix " + newFileName ); + setTitle( localeFileName ); + QgsMessageLog::logMessage( tr( "Translated project saved with locale prefix %1" ).arg( newFileName ), QObject::tr( "Project translation" ), Qgis::Success ); } else { - QgsDebugMsg( "Error saving translated project with locale prefix " + newFileName ); + QgsMessageLog::logMessage( tr( "Error saving translated project with locale prefix %1" ).arg( newFileName ), QObject::tr( "Project translation" ), Qgis::Critical ); } } return true;