Skip to content

Commit

Permalink
tests, project store and other
Browse files Browse the repository at this point in the history
not yet working all translations
project handling can be improved
project store after translation working
and fix some errors
tests not ready yet
  • Loading branch information
signedav committed Aug 7, 2018
1 parent 778734f commit 2bfe229
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 68 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgseditformconfig.sip.in
Expand Up @@ -264,7 +264,7 @@ Write XML information
Serialize on project save
%End

QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent );
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = 0 );
%Docstring
Deserialize drag and drop designer elements.
%End
Expand Down
37 changes: 37 additions & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -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

Expand Down Expand Up @@ -983,6 +991,25 @@ 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.2
%End

QString translate( const QString &context, const QString &sourceText, const char *disambiguation = 0, int n = -1 );
%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
%End

signals:
Expand Down Expand Up @@ -1335,6 +1362,16 @@ home path will be automatically determined from the project's file path.
.. seealso:: :py:func:`homePathChanged`

.. versionadded:: 3.2
%End

void registerTranslatableObjects( QgsTranslationContext *translationContext );
%Docstring
Registers the translatable objects into the tranlationContext
so there can be created a ts file these values

.. versionadded:: 3.2

:param translationContext: where the objects will be registered
%End

};
Expand Down
79 changes: 79 additions & 0 deletions 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.2
%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( );
%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 *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -352,6 +352,7 @@
%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 Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5950,10 +5950,10 @@ void QgisApp::fileSaveAs()
return;

QFileInfo fullPath( path );

settings.setValue( QStringLiteral( "UI/lastProjectDir" ), fullPath.path() );

if ( filter == zipExt )

{
if ( fullPath.suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) != 0 )
fullPath.setFile( fullPath.filePath() + ".qgz" );
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -830,6 +830,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
connect( mMetadataWidget, &QgsMetadataWidget::titleChanged, titleEdit, &QLineEdit::setText );
connect( titleEdit, &QLineEdit::textChanged, mMetadataWidget, &QgsMetadataWidget::setTitle );

connect( generateTsFileButton, &QPushButton::clicked, this, &QgsProjectProperties::generateTsFileButton_clicked );
projectionSelectorInitialized();
populateRequiredLayers();
restoreOptionsBaseUi();
Expand Down Expand Up @@ -2190,3 +2191,8 @@ void QgsProjectProperties::setCurrentPage( const QString &pageWidgetName )
}
}
}

void QgsProjectProperties::generateTsFileButton_clicked()
{
QgsProject::instance()->generateTsFile( "de" );
}
3 changes: 3 additions & 0 deletions src/app/qgsprojectproperties.h
Expand Up @@ -92,6 +92,9 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
//! A scale in the list of project scales changed
void scaleItemChanged( QListWidgetItem *changedScaleItem );

//dave to document
void generateTsFileButton_clicked();

/**
* Set WMS default extent to current canvas extent
*/
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -399,7 +399,7 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
{
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();

QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr );
QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr, node.namedItem( QStringLiteral( "id" ) ).toElement().text() );
addTab( attributeEditorWidget );
}

Expand Down Expand Up @@ -518,13 +518,13 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
//// END TODO
}

QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent )
QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId )
{
QgsAttributeEditorElement *newElement = nullptr;

if ( elem.tagName() == QLatin1String( "attributeEditorContainer" ) )
{
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( "testdave" ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
QgsAttributeEditorContainer *container = new QgsAttributeEditorContainer( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ), elem.attribute( QStringLiteral( "name" ) ) ), parent );
bool ok;
int cc = elem.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
if ( !ok )
Expand All @@ -551,7 +551,7 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
for ( int i = 0; i < childNodeList.size(); i++ )
{
QDomElement childElem = childNodeList.at( i ).toElement();
QgsAttributeEditorElement *myElem = attributeEditorElementFromDomElement( childElem, container );
QgsAttributeEditorElement *myElem = attributeEditorElementFromDomElement( childElem, container, layerId );
if ( myElem )
container->addChildElement( myElem );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgseditformconfig.h
Expand Up @@ -288,7 +288,7 @@ class CORE_EXPORT QgsEditFormConfig
/**
* Deserialize drag and drop designer elements.
*/
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent );
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = nullptr );

/**
* Create a new edit form config. Normally invoked by QgsVectorLayer
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -314,7 +314,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
// set name
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
mne = mnl.toElement();
//trans dave

//name can be translated
setName( QgsProject::instance()->translate( QStringLiteral( "project:layers:%1" ).arg( layerElement.namedItem( QStringLiteral( "id" ) ).toElement().text() ), mne.text() ) );

//short name
Expand Down
62 changes: 47 additions & 15 deletions src/core/qgsproject.cpp
Expand Up @@ -367,6 +367,9 @@ QgsProject::QgsProject( QObject *parent )
connect( mLayerStore.get(), &QgsMapLayerStore::layersAdded, this, &QgsProject::layersAdded );
connect( mLayerStore.get(), &QgsMapLayerStore::layerWasAdded, this, &QgsProject::layerWasAdded );
connect( QgsApplication::instance(), &QgsApplication::requestForTranslatableObjects, this, &QgsProject::registerTranslatableObjects );

mTranslator = new QTranslator();

}


Expand Down Expand Up @@ -928,6 +931,10 @@ bool QgsProject::read()
QString filename = mFile.fileName();
bool rc;

//dave dirty hack
delete mTranslator;
mTranslator = new QTranslator();

if ( QgsProjectStorage *storage = projectStorage() )
{
QTemporaryFile inDevice;
Expand Down Expand Up @@ -961,32 +968,30 @@ bool QgsProject::read()
rc = readProjectFile( mFile.fileName() );
}

mFile.setFileName( filename );
return rc;
}

//dave put to another place
QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
{
if ( mTranslator.isEmpty() )
//on translation we should not change the filename back
if ( mTranslator->isEmpty() )
{
return sourceText;
mFile.setFileName( filename );
}

return mTranslator.translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
return rc;
}


bool QgsProject::readProjectFile( const QString &filename )
{
QFile projectFile( filename );
clearError();

QgsSettings settings;

if ( mTranslator.load( QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() ), QFileInfo( projectFile.fileName() ).absolutePath() ) )
QString localeFileName = QStringLiteral( "%1_%2" ).arg( QFileInfo( projectFile.fileName() ).baseName(), settings.value( QStringLiteral( "locale/userLocale" ), "" ).toString() );

if ( QFile( QStringLiteral( "%1/%2.qm" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) ).exists() )
{
QgsDebugMsg( "Translation loaded" );
if ( mTranslator->load( localeFileName, QFileInfo( projectFile.fileName() ).absolutePath() ) )
{
QgsDebugMsg( "Translation loaded" );
}
}

std::unique_ptr<QDomDocument> doc( new QDomDocument( QStringLiteral( "qgis" ) ) );
Expand Down Expand Up @@ -1285,6 +1290,23 @@ bool QgsProject::readProjectFile( const QString &filename )

emit nonIdentifiableLayersChanged( nonIdentifiableLayers() );

if ( !mTranslator->isEmpty() )
{
//project possibly translated -> rename it with locale postfix
QString newFileName( QStringLiteral( "%1/%2.qgs" ).arg( QFileInfo( projectFile.fileName() ).absolutePath(), localeFileName ) );
QgsProject::instance()->setFileName( newFileName );

if ( QgsProject::instance()->write() )
{
QgsProject::instance()->setTitle( localeFileName );

QgsDebugMsg( "Translated project saved with locale prefix " + newFileName );
}
else
{
QgsDebugMsg( "Error saving translated project with locale prefix " + newFileName );
}
}
return true;
}

Expand Down Expand Up @@ -2765,13 +2787,23 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
}

void QgsProject::generateTsFile()
void QgsProject::generateTsFile( const QString &locale )
{
QgsTranslationContext translationContext;
translationContext.setProject( this );
translationContext.setFileName( QStringLiteral( "%1/%2.ts" ).arg( absolutePath(), baseName() ) );
translationContext.setFileName( QStringLiteral( "%1/%2_%3.ts" ).arg( absolutePath(), baseName(), locale ) );

emit QgsApplication::instance()->collectTranslatableObjects( &translationContext );

translationContext.writeTsFile();
}

QString QgsProject::translate( const QString &context, const QString &sourceText, const char *disambiguation, int n )
{
if ( mTranslator->isEmpty() )
{
return sourceText;
}

return mTranslator->translate( context.toUtf8(), sourceText.toUtf8(), disambiguation, n );
}

0 comments on commit 2bfe229

Please sign in to comment.