Skip to content

Commit

Permalink
Merge pull request #36909 from m-kuhn/offline_editing_style_copy
Browse files Browse the repository at this point in the history
Fix styles are not copied in offline editing
  • Loading branch information
m-kuhn committed Jun 5, 2020
2 parents 3ae1b24 + 2b56773 commit a741700
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/auto_generated/qgsmaplayerstylemanager.sip.in
Expand Up @@ -141,6 +141,14 @@ Restore the original store after a call to setOverrideStyle()
Returns ``True`` if this is the default style

.. versionadded:: 3.0
%End

void copyStylesFrom( QgsMapLayerStyleManager *other );
%Docstring
Copies all styles from ``other``.
In case there is already a style with the same name it will be overwritten.

.. versionadded:: 3.14
%End

signals:
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsmaplayerstylemanager.cpp
Expand Up @@ -230,3 +230,14 @@ bool QgsMapLayerStyleManager::isDefault( const QString &styleName ) const
{
return styleName == defaultStyleName();
}

void QgsMapLayerStyleManager::copyStylesFrom( QgsMapLayerStyleManager *other )
{
const QStringList styleNames = other->mStyles.keys();

for ( const QString &styleName : styleNames )
{
mStyles.remove( styleName );
addStyle( styleName, other->style( styleName ) );
}
}
8 changes: 8 additions & 0 deletions src/core/qgsmaplayerstylemanager.h
Expand Up @@ -136,6 +136,14 @@ class CORE_EXPORT QgsMapLayerStyleManager : public QObject
*/
bool isDefault( const QString &styleName ) const;

/**
* Copies all styles from \a other.
* In case there is already a style with the same name it will be overwritten.
*
* \since QGIS 3.14
*/
void copyStylesFrom( QgsMapLayerStyleManager *other );

signals:
//! Emitted when a new style has been added
void styleAdded( const QString &name );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -40,6 +40,7 @@
#include "qgsvectorlayer.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgsmaplayerstylemanager.h"

#include <QDir>
#include <QDomDocument>
Expand Down Expand Up @@ -1096,6 +1097,8 @@ void QgsOfflineEditing::updateFidLookup( QgsVectorLayer *remoteLayer, sqlite3 *d

void QgsOfflineEditing::copySymbology( QgsVectorLayer *sourceLayer, QgsVectorLayer *targetLayer )
{
targetLayer->styleManager()->copyStylesFrom( sourceLayer->styleManager() );

QString error;
QDomDocument doc;
QgsReadWriteContext context;
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsmaplayerstylemanager.cpp
Expand Up @@ -39,6 +39,7 @@ class TestQgsMapLayerStyleManager : public QObject
void testStyle();
void testReadWrite();
void testSwitchingStyles();
void testCopyStyles();

private:
QgsVectorLayer *mVL = nullptr;
Expand Down Expand Up @@ -200,6 +201,25 @@ void TestQgsMapLayerStyleManager::testSwitchingStyles()
QCOMPARE( _getVLColor( mVL ), QColor( Qt::blue ) );
}

void TestQgsMapLayerStyleManager::testCopyStyles()
{
std::unique_ptr<QgsVectorLayer> lines = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "LineString" ), QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );
std::unique_ptr<QgsVectorLayer> lines2 = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "LineString" ), QStringLiteral( "Line Layer" ), QStringLiteral( "memory" ) );

QgsMapLayerStyleManager *sm = lines->styleManager();

sm->addStyleFromLayer( QStringLiteral( "style2" ) );

QgsMapLayerStyleManager *sm2 = lines2->styleManager();

sm2->copyStylesFrom( sm );
sm2->addStyleFromLayer( "style3" );

QVERIFY( sm2->styles().contains( "style2" ) );
QVERIFY( sm2->styles().contains( "style3" ) );
QVERIFY( sm2->styles().contains( "default" ) );
}


QGSTEST_MAIN( TestQgsMapLayerStyleManager )
#include "testqgsmaplayerstylemanager.moc"
6 changes: 6 additions & 0 deletions tests/src/core/testqgsofflineediting.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgstest.h"
#include "qgsvectorlayerref.h"
#include "qgslayertree.h"
#include "qgsmaplayerstylemanager.h"

/**
* \ingroup UnitTests
Expand Down Expand Up @@ -164,6 +165,10 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
QgsLayerTreeLayer *layerTreelayer = QgsProject::instance()->layerTreeRoot()->findLayer( mpLayer->id() );
layerTreelayer->setCustomProperty( QStringLiteral( "showFeatureCount" ), 1 );
layerTreelayer->setItemVisibilityChecked( false );
QgsMapLayerStyle style;
style.readFromLayer( mpLayer );

mpLayer->styleManager()->addStyle( QStringLiteral( "testStyle" ), style );

//convert
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, QgsOfflineEditing::GPKG );
Expand All @@ -177,6 +182,7 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
layerTreelayer = QgsProject::instance()->layerTreeRoot()->findLayer( mpLayer->id() );
QCOMPARE( layerTreelayer->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toInt(), 1 );
QCOMPARE( layerTreelayer->isVisible(), false );
QVERIFY( mpLayer->styleManager()->styles().contains( QStringLiteral( "testStyle" ) ) );

QgsFeature firstFeatureInAction;
it = mpLayer->getFeatures();
Expand Down

0 comments on commit a741700

Please sign in to comment.