Skip to content
Permalink
Browse files

Read/write mesh styling from/to project files

  • Loading branch information
wonder-sk committed Aug 14, 2018
1 parent 9fc2e3e commit 358d12946b1772d18e1aed01aeeffdc43a44d711
@@ -53,6 +53,15 @@ Returns color used for rendering
void setColor( const QColor &color );
%Docstring
Sets color used for rendering of the mesh
%End

QDomElement writeXml( QDomDocument &doc ) const;
%Docstring
Writes configuration to a new DOM element
%End
void readXml( const QDomElement &elem );
%Docstring
Reads configuration from the given DOM element
%End

};
@@ -81,6 +90,15 @@ Returns color ramp shader function
void setColorRampShader( const QgsColorRampShader &shader );
%Docstring
Sets color ramp shader function
%End

QDomElement writeXml( QDomDocument &doc ) const;
%Docstring
Writes configuration to a new DOM element
%End
void readXml( const QDomElement &elem );
%Docstring
Reads configuration from the given DOM element
%End

};
@@ -242,6 +260,15 @@ Returns ratio of the head length of the arrow (range 0-1)
void setArrowHeadLengthRatio( double arrowHeadLengthRatio );
%Docstring
Sets ratio of the head length of the arrow (range 0-1)
%End

QDomElement writeXml( QDomDocument &doc ) const;
%Docstring
Writes configuration to a new DOM element
%End
void readXml( const QDomElement &elem );
%Docstring
Reads configuration from the given DOM element
%End

};
@@ -318,6 +345,15 @@ Returns active vector dataset
void setActiveVectorDataset( QgsMeshDatasetIndex index = QgsMeshDatasetIndex() );
%Docstring
Sets active vector dataset for rendering.
%End

QDomElement writeXml( QDomDocument &doc ) const;
%Docstring
Writes configuration to a new DOM element
%End
void readXml( const QDomElement &elem );
%Docstring
Reads configuration from the given DOM element
%End

};
@@ -82,7 +82,7 @@ Returns the custom colormap.
Returns the color ramp type.
%End

QString colorRampTypeAsQString();
QString colorRampTypeAsQString() const;
%Docstring
Returns the color ramp type as a string.
%End
@@ -160,6 +160,20 @@ Generates and new RGB value based on original RGB value
virtual void legendSymbologyItems( QList< QPair< QString, QColor > > &symbolItems /Out/ ) const;


QDomElement writeXml( QDomDocument &doc ) const;
%Docstring
Writes configuration to a new DOM element

.. versionadded:: 3.4
%End

void readXml( const QDomElement &elem );
%Docstring
Reads configuration from the given DOM element

.. versionadded:: 3.4
%End

void setClassificationMode( ClassificationMode classificationMode );
%Docstring
Sets classification mode
@@ -164,11 +164,13 @@ void QgsMeshRendererActiveDatasetWidget::syncToLayer()
if ( mMeshLayer )
{
const QgsMeshRendererSettings rendererSettings = mMeshLayer->rendererSettings();
mActiveDatasetGroup = mDatasetGroupTreeView->activeGroup();
mActiveScalarDataset = rendererSettings.activeScalarDataset();
mActiveVectorDataset = rendererSettings.activeVectorDataset();
}
else
{
mActiveDatasetGroup = -1;
mActiveScalarDataset = QgsMeshDatasetIndex();
mActiveVectorDataset = QgsMeshDatasetIndex();
}
@@ -26,6 +26,8 @@ QgsMeshRendererVectorSettingsWidget::QgsMeshRendererVectorSettingsWidget( QWidge
{
setupUi( this );

mShaftLengthComboBox->setCurrentIndex( -1 );

connect( mColorWidget, &QgsColorButton::colorChanged, this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
connect( mLineWidthSpinBox, qgis::overload<double>::of( &QgsDoubleSpinBox::valueChanged ),
this, &QgsMeshRendererVectorSettingsWidget::widgetChanged );
@@ -221,18 +221,26 @@ QgsMapLayerRenderer *QgsMeshLayer::createMapRenderer( QgsRenderContext &renderer

bool QgsMeshLayer::readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context )
{
Q_UNUSED( node );
Q_UNUSED( errorMessage );
Q_UNUSED( context );

QDomElement elem = node.toElement();
QDomElement elemRendererSettings = elem.firstChildElement( "mesh-renderer-settings" );
if ( !elemRendererSettings.isNull() )
mRendererSettings.readXml( elemRendererSettings );

return true;
}

bool QgsMeshLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context ) const
{
Q_UNUSED( node );
Q_UNUSED( doc );
Q_UNUSED( errorMessage );
Q_UNUSED( context );

QDomElement elem = node.toElement();
QDomElement elemRendererSettings = mRendererSettings.writeXml( doc );
elem.appendChild( elemRendererSettings );

return true;
}

@@ -258,8 +266,6 @@ QString QgsMeshLayer::encodedSource( const QString &source, const QgsReadWriteCo

bool QgsMeshLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
{
Q_UNUSED( context );

QgsDebugMsgLevel( QStringLiteral( "Datasource in QgsMeshLayer::readXml: %1" ).arg( mDataSource.toLocal8Bit().data() ), 3 );

//process provider key
@@ -296,6 +302,9 @@ bool QgsMeshLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &con
}
}

QString errorMsg;
readSymbology( layer_node, errorMsg, context );

return mValid; // should be true if read successfully
}

@@ -17,6 +17,9 @@

#include "qgsmeshrenderersettings.h"

#include "qgssymbollayerutils.h"


bool QgsMeshRendererMeshSettings::isEnabled() const
{
return mEnabled;
@@ -47,18 +50,50 @@ void QgsMeshRendererMeshSettings::setColor( const QColor &color )
mColor = color;
}

QDomElement QgsMeshRendererMeshSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "mesh-settings" );
elem.setAttribute( "enabled", mEnabled ? "1" : "0" );
elem.setAttribute( "line-width", mLineWidth );
elem.setAttribute( "color", QgsSymbolLayerUtils::encodeColor( mColor ) );
return elem;
}

void QgsMeshRendererMeshSettings::readXml( const QDomElement &elem )
{
mEnabled = elem.attribute( "enabled" ).toInt();
mLineWidth = elem.attribute( "line-width" ).toDouble();
mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( "color" ) );
}

// ---------------------------------------------------------------------

QgsColorRampShader QgsMeshRendererScalarSettings::colorRampShader() const
{
return mColorRampShader;

}

void QgsMeshRendererScalarSettings::setColorRampShader( const QgsColorRampShader &shader )
{
mColorRampShader = shader;
}

QDomElement QgsMeshRendererScalarSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "scalar-settings" );
QDomElement elemShader = mColorRampShader.writeXml( doc );
elem.appendChild( elemShader );
return elem;
}

void QgsMeshRendererScalarSettings::readXml( const QDomElement &elem )
{
QDomElement elemShader = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
mColorRampShader.readXml( elemShader );
}

// ---------------------------------------------------------------------

double QgsMeshRendererVectorSettings::lineWidth() const
{
return mLineWidth;
@@ -168,3 +203,153 @@ void QgsMeshRendererVectorSettings::setArrowHeadLengthRatio( double vectorHeadLe
{
mArrowHeadLengthRatio = vectorHeadLengthRatio;
}

QDomElement QgsMeshRendererVectorSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "vector-settings" );
elem.setAttribute( "line-width", mLineWidth );
elem.setAttribute( "color", QgsSymbolLayerUtils::encodeColor( mColor ) );
elem.setAttribute( "filter-min", mFilterMin );
elem.setAttribute( "filter-max", mFilterMax );
elem.setAttribute( "arrow-head-width-ratio", mArrowHeadWidthRatio );
elem.setAttribute( "arrow-head-length-ratio", mArrowHeadLengthRatio );

QDomElement elemShaft = doc.createElement( "shaft-length" );
QString methodTxt;
switch ( mShaftLengthMethod )
{
case MinMax:
methodTxt = "minmax";
elemShaft.setAttribute( "min", mMinShaftLength );
elemShaft.setAttribute( "max", mMaxShaftLength );
break;
case Scaled:
methodTxt = "scaled";
elemShaft.setAttribute( "scale-factor", mScaleFactor );
break;
case Fixed:
methodTxt = "fixed";
elemShaft.setAttribute( "fixed-length", mFixedShaftLength );
break;
}
elemShaft.setAttribute( "method", methodTxt );
elem.appendChild( elemShaft );
return elem;
}

void QgsMeshRendererVectorSettings::readXml( const QDomElement &elem )
{
mLineWidth = elem.attribute( "line-width" ).toDouble();
mColor = QgsSymbolLayerUtils::decodeColor( elem.attribute( "color" ) );
mFilterMin = elem.attribute( "filter-min" ).toDouble();
mFilterMax = elem.attribute( "filter-max" ).toDouble();
mArrowHeadWidthRatio = elem.attribute( "arrow-head-width-ratio" ).toDouble();
mArrowHeadLengthRatio = elem.attribute( "arrow-head-length-ratio" ).toDouble();

QDomElement elemShaft = elem.firstChildElement( "shaft-length" );
QString methodTxt = elemShaft.attribute( "method" );
if ( methodTxt == "minmax" )
{
mShaftLengthMethod = MinMax;
mMinShaftLength = elemShaft.attribute( "min" ).toDouble();
mMaxShaftLength = elemShaft.attribute( "max" ).toDouble();
}
else if ( methodTxt == "scaled" )
{
mShaftLengthMethod = Scaled;
mScaleFactor = elemShaft.attribute( "scale-factor" ).toDouble();
}
else // fixed
{
mShaftLengthMethod = Fixed;
mFixedShaftLength = elemShaft.attribute( "fixed-length" ).toDouble();
}
}

// ---------------------------------------------------------------------

QDomElement QgsMeshRendererSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "mesh-renderer-settings" );

QDomElement elemActiveDataset = doc.createElement( "active-dataset" );
if ( mActiveScalarDataset.isValid() )
elemActiveDataset.setAttribute( "scalar", QString( "%1,%2" ).arg( mActiveScalarDataset.group() ).arg( mActiveScalarDataset.dataset() ) );
if ( mActiveVectorDataset.isValid() )
elemActiveDataset.setAttribute( "vector", QString( "%1,%2" ).arg( mActiveVectorDataset.group() ).arg( mActiveVectorDataset.dataset() ) );
elem.appendChild( elemActiveDataset );

for ( int groupIndex : mRendererScalarSettings.keys() )
{
const QgsMeshRendererScalarSettings &scalarSettings = mRendererScalarSettings[groupIndex];
QDomElement elemScalar = scalarSettings.writeXml( doc );
elemScalar.setAttribute( "group", groupIndex );
elem.appendChild( elemScalar );
}

for ( int groupIndex : mRendererVectorSettings.keys() )
{
const QgsMeshRendererVectorSettings &vectorSettings = mRendererVectorSettings[groupIndex];
QDomElement elemVector = vectorSettings.writeXml( doc );
elemVector.setAttribute( "group", groupIndex );
elem.appendChild( elemVector );
}

QDomElement elemNativeMesh = mRendererNativeMeshSettings.writeXml( doc );
elemNativeMesh.setTagName( "mesh-settings-native" );
elem.appendChild( elemNativeMesh );

QDomElement elemTriangularMesh = mRendererTriangularMeshSettings.writeXml( doc );
elemTriangularMesh.setTagName( "mesh-settings-triangular" );
elem.appendChild( elemTriangularMesh );

return elem;
}

void QgsMeshRendererSettings::readXml( const QDomElement &elem )
{
mRendererScalarSettings.clear();
mRendererVectorSettings.clear();

QDomElement elemActiveDataset = elem.firstChildElement( "active-dataset" );
if ( elemActiveDataset.hasAttribute( "scalar" ) )
{
QStringList lst = elemActiveDataset.attribute( "scalar" ).split( QChar( ',' ) );
if ( lst.count() == 2 )
mActiveScalarDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
}
if ( elemActiveDataset.hasAttribute( "vector" ) )
{
QStringList lst = elemActiveDataset.attribute( "vector" ).split( QChar( ',' ) );
if ( lst.count() == 2 )
mActiveVectorDataset = QgsMeshDatasetIndex( lst[0].toInt(), lst[1].toInt() );
}

QDomElement elemScalar = elem.firstChildElement( "scalar-settings" );
while ( !elemScalar.isNull() )
{
int groupIndex = elemScalar.attribute( "group" ).toInt();
QgsMeshRendererScalarSettings scalarSettings;
scalarSettings.readXml( elemScalar );
mRendererScalarSettings.insert( groupIndex, scalarSettings );

elemScalar = elemScalar.nextSiblingElement( "scalar-settings" );
}

QDomElement elemVector = elem.firstChildElement( "vector-settings" );
while ( !elemVector.isNull() )
{
int groupIndex = elemVector.attribute( "group" ).toInt();
QgsMeshRendererVectorSettings vectorSettings;
vectorSettings.readXml( elemVector );
mRendererVectorSettings.insert( groupIndex, vectorSettings );

elemVector = elemVector.nextSiblingElement( "vector-settings" );
}

QDomElement elemNativeMesh = elem.firstChildElement( "mesh-settings-native" );
mRendererNativeMeshSettings.readXml( elemNativeMesh );

QDomElement elemTriangularMesh = elem.firstChildElement( "mesh-settings-triangular" );
mRendererTriangularMeshSettings.readXml( elemTriangularMesh );
}

0 comments on commit 358d129

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