Skip to content

Commit

Permalink
Merge pull request #2061 from vmora/convert_old_size_scale
Browse files Browse the repository at this point in the history
remove SizeScale and Rotation from Advanced Menu
  • Loading branch information
nyalldawson committed May 22, 2015
2 parents f81d808 + e0098a5 commit f337d41
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 22 deletions.
33 changes: 27 additions & 6 deletions src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone() const
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
//r->setScaleMethod( scaleMethod() );

copyPaintEffect( r );
return r;
Expand Down Expand Up @@ -586,14 +586,35 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create( QDomElement& eleme
}

QDomElement rotationElem = element.firstChildElement( "rotation" );
if ( !rotationElem.isNull() )
r->setRotationField( rotationElem.attribute( "field" ) );
if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
{
QgsCategoryList::iterator it = r->mCategories.begin();
for ( ; it != r->mCategories.end(); ++it )
{
convertSymbolRotation( it->symbol(), rotationElem.attribute( "field" ) );
}
if ( r->mSourceSymbol.data() )
{
convertSymbolRotation( r->mSourceSymbol.data(), rotationElem.attribute( "field" ) );
}
}

QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
if ( !sizeScaleElem.isNull() )
if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
{
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );
QgsCategoryList::iterator it = r->mCategories.begin();
for ( ; it != r->mCategories.end(); ++it )
{
convertSymbolSizeScale( it->symbol(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}
if ( r->mSourceSymbol.data() && r->mSourceSymbol->type() == QgsSymbolV2::Marker )
{
convertSymbolSizeScale( r->mSourceSymbol.data(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}
}

// TODO: symbol levels
Expand Down
34 changes: 27 additions & 7 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone() const
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
//r->setScaleMethod( scaleMethod() );
r->setLabelFormat( labelFormat() );
r->setGraduatedMethod( graduatedMethod() );
copyPaintEffect( r );
Expand Down Expand Up @@ -937,7 +937,6 @@ void QgsGraduatedSymbolRendererV2::updateClasses( QgsVectorLayer *vlayer, Mode m
updateColorRamp( 0, mInvertedColorRamp );
}


QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element )
{
QDomElement symbolsElem = element.firstChildElement( "symbols" );
Expand Down Expand Up @@ -1027,13 +1026,34 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create( QDomElement& element
}

QDomElement rotationElem = element.firstChildElement( "rotation" );
if ( !rotationElem.isNull() )
r->setRotationField( rotationElem.attribute( "field" ) );
if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
{
for ( QgsRangeList::iterator it = r->mRanges.begin(); it != r->mRanges.end(); ++it )
{
convertSymbolRotation( it->symbol(), rotationElem.attribute( "field" ) );
}
if ( r->mSourceSymbol.data() )
{
convertSymbolRotation( r->mSourceSymbol.data(), rotationElem.attribute( "field" ) );
}
}

QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
if ( !sizeScaleElem.isNull() )
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );
if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
{
for ( QgsRangeList::iterator it = r->mRanges.begin(); it != r->mRanges.end(); ++it )
{
convertSymbolSizeScale( it->symbol(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}
if ( r->mSourceSymbol.data() && r->mSourceSymbol->type() == QgsSymbolV2::Marker )
{
convertSymbolSizeScale( r->mSourceSymbol.data(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}
}

QDomElement labelFormatElem = element.firstChildElement( "labelformat" );
if ( ! labelFormatElem.isNull() )
Expand Down
37 changes: 37 additions & 0 deletions src/core/symbology-ng/qgsrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsrulebasedrendererv2.h"
#include "qgsdatadefined.h"

#include "qgssinglesymbolrendererv2.h" // for default renderer

Expand Down Expand Up @@ -640,3 +641,39 @@ void QgsFeatureRendererV2::setPaintEffect( QgsPaintEffect *effect )
delete mPaintEffect;
mPaintEffect = effect;
}

void QgsFeatureRendererV2::convertSymbolSizeScale( QgsSymbolV2 * symbol, QgsSymbolV2::ScaleMethod method, const QString & field )
{
if ( symbol->type() == QgsSymbolV2::Marker )
{
QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( symbol );
if ( QgsSymbolV2::ScaleArea == QgsSymbolV2::ScaleMethod( method ) )
{
const QgsDataDefined dd( "sqrt(" + QString::number( s->size() ) + " * (" + field + "))" );
s->setDataDefinedSize( dd );
}
else
{
const QgsDataDefined dd( QString::number( s->size() ) + " * (" + field + ")" );
s->setDataDefinedSize( dd );
}
}
else if ( symbol->type() == QgsSymbolV2::Line )
{
QgsLineSymbolV2 * s = static_cast<QgsLineSymbolV2 *>( symbol );
const QgsDataDefined dd( QString::number( s->width() ) + " * (" + field + ")" );
s->setDataDefinedWidth( dd );
}
}

void QgsFeatureRendererV2::convertSymbolRotation( QgsSymbolV2 * symbol, const QString & field )
{
if ( symbol->type() == QgsSymbolV2::Marker )
{
QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( symbol );
const QgsDataDefined dd(( s->angle()
? QString::number( s->angle() ) + " + "
: QString() ) + field );
s->setDataDefinedAngle( dd );
}
}
11 changes: 10 additions & 1 deletion src/core/symbology-ng/qgsrendererv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgis.h"
#include "qgsrectangle.h"
#include "qgsrendercontext.h"
#include "qgssymbolv2.h"

#include <QList>
#include <QString>
Expand All @@ -28,7 +29,6 @@
#include <QDomDocument>
#include <QDomElement>

class QgsSymbolV2;
class QgsFeature;
class QgsFields;
class QgsVectorLayer;
Expand Down Expand Up @@ -272,6 +272,15 @@ class CORE_EXPORT QgsFeatureRendererV2

QgsPaintEffect* mPaintEffect;

/**@note this function is used to convert old sizeScale expresssions to symbol
* level DataDefined size
*/
static void convertSymbolSizeScale( QgsSymbolV2 * symbol, QgsSymbolV2::ScaleMethod method, const QString & field );
/**@note this function is used to convert old rotations expresssions to symbol
* level DataDefined angle
*/
static void convertSymbolRotation( QgsSymbolV2 * symbol, const QString & field );

private:
Q_DISABLE_COPY( QgsFeatureRendererV2 )
};
Expand Down
16 changes: 10 additions & 6 deletions src/core/symbology-ng/qgssinglesymbolrendererv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ QgsFeatureRendererV2* QgsSingleSymbolRendererV2::clone() const
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
//r->setScaleMethod( scaleMethod() );
copyPaintEffect( r );
return r;
}
Expand Down Expand Up @@ -216,6 +216,7 @@ QgsSymbolV2List QgsSingleSymbolRendererV2::symbols()
return lst;
}


QgsFeatureRendererV2* QgsSingleSymbolRendererV2::create( QDomElement& element )
{
QDomElement symbolsElem = element.firstChildElement( "symbols" );
Expand All @@ -233,14 +234,17 @@ QgsFeatureRendererV2* QgsSingleSymbolRendererV2::create( QDomElement& element )
QgsSymbolLayerV2Utils::clearSymbolMap( symbolMap );

QDomElement rotationElem = element.firstChildElement( "rotation" );
if ( !rotationElem.isNull() )
r->setRotationField( rotationElem.attribute( "field" ) );
if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
{
convertSymbolRotation( r->mSymbol.data(), rotationElem.attribute( "field" ) );
}

QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
if ( !sizeScaleElem.isNull() )
if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
{
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );
convertSymbolSizeScale( r->mSymbol.data(),
QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
sizeScaleElem.attribute( "field" ) );
}

// TODO: symbol levels
Expand Down
7 changes: 5 additions & 2 deletions src/gui/symbology-ng/qgsrendererv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ QgsRendererV2DataDefinedMenus::QgsRendererV2DataDefinedMenus( QMenu* menu, QgsVe

mSizeScaleMenu->addActions( mSizeMethodActionGroup->actions() );

menu->addMenu( mRotationMenu );
menu->addMenu( mSizeScaleMenu );
//@todo cleanup the class since Rotation and SizeScale are now
//defined using QgsDataDefinedButton
//
//menu->addMenu( mRotationMenu );
//menu->addMenu( mSizeScaleMenu );

connect( mSizeMethodActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( scaleMethodSelected( QAction* ) ) );
connect( mRotationAttributeActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( rotationFieldSelected( QAction* ) ) );
Expand Down

0 comments on commit f337d41

Please sign in to comment.