Skip to content
Permalink
Browse files

Merge pull request #36909 from m-kuhn/offline_editing_style_copy

Fix styles are not copied in offline editing
  • Loading branch information
m-kuhn authored and nyalldawson committed Jun 19, 2020
1 parent a5062ab commit f5a71c08118f64ad48ec8bdbbd363a67dbd954a2
@@ -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.10.7
%End

signals:
@@ -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 ) );
}
}
@@ -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.10.7
*/
void copyStylesFrom( QgsMapLayerStyleManager *other );

signals:
//! Emitted when a new style has been added
void styleAdded( const QString &name );
@@ -38,6 +38,7 @@
#include "qgsogrutils.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerstylemanager.h"

#include <QDir>
#include <QDomDocument>
@@ -1083,6 +1084,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;
@@ -39,6 +39,7 @@ class TestQgsMapLayerStyleManager : public QObject
void testStyle();
void testReadWrite();
void testSwitchingStyles();
void testCopyStyles();

private:
QgsVectorLayer *mVL = nullptr;
@@ -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"
@@ -26,6 +26,7 @@
#include "qgstest.h"
#include "qgsvectorlayerref.h"
#include "qgslayertree.h"
#include "qgsmaplayerstylemanager.h"

/**
* \ingroup UnitTests
@@ -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 );
@@ -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();

0 comments on commit f5a71c0

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