Skip to content

Commit

Permalink
Move management of avoidIntersectionLayers to QgsProject
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 20, 2016
1 parent 5c919fb commit 85c105d
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 51 deletions.
21 changes: 21 additions & 0 deletions python/core/qgsproject.sip
Expand Up @@ -390,6 +390,20 @@ class QgsProject : QObject
*/
void setSnappingConfig( const QgsSnappingConfig& snappingConfig );

/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
QStringList avoidIntersectionsList() const;

/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
void setAvoidIntersectionsList(const QStringList& avoidIntersectionsList);

signals:
//! emitted when project is being read
void readProject( const QDomDocument & );
Expand Down Expand Up @@ -462,6 +476,13 @@ class QgsProject : QObject
*/
void topologicalEditingChanged();

/**
* Emitted whenever avoidIntersectionsList has changed.
*
* @note Added in QGIS 3.0
*/
void avoidIntersectionsListChanged();

public slots:
/**
* Flag the project as dirty (modified). If this flag is set, the user will
Expand Down
9 changes: 1 addition & 8 deletions python/core/qgssnappingconfig.sip
Expand Up @@ -41,9 +41,8 @@ class QgsSnappingConfig
* @param type
* @param tolerance
* @param units
* @param avoidIntersection
*/
IndividualLayerSettings( bool enabled, QgsSnappingConfig::SnappingType type, double tolerance, QgsTolerance::UnitType units, bool avoidIntersection = false );
IndividualLayerSettings( bool enabled, QgsSnappingConfig::SnappingType type, double tolerance, QgsTolerance::UnitType units );

/**
* Constructs an invalid setting
Expand Down Expand Up @@ -77,12 +76,6 @@ class QgsSnappingConfig
//! set the type of units
void setUnits( QgsTolerance::UnitType units );

//! return if it shall avoid intersection (polygon layers only)
bool avoidIntersection() const;

//! set if it shall avoid intersection (polygon layers only)
void setAvoidIntersection( bool avoidIntersection );

/**
* Compare this configuration to other.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -296,7 +296,7 @@ void QgsMapToolAddFeature::cadCanvasReleaseEvent( QgsMapMouseEvent* e )

//use always topological editing for avoidIntersection.
//Otherwise, no way to guarantee the geometries don't have a small gap in between.
QStringList intersectionLayers = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList" );
QStringList intersectionLayers = QgsProject::instance()->avoidIntersectionsList();
bool avoidIntersection = !intersectionLayers.isEmpty();
if ( avoidIntersection ) //try to add topological points also to background layers
{
Expand Down
18 changes: 9 additions & 9 deletions src/app/qgssnappinglayertreemodel.cpp
Expand Up @@ -494,7 +494,7 @@ QVariant QgsSnappingLayerTreeModel::data( const QModelIndex& idx, int role ) con
{
if ( role == Qt::CheckStateRole && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
{
if ( ls.avoidIntersection() )
if ( mProject->avoidIntersectionsList().contains( vl->id() ) )
{
return Qt::Checked;
}
Expand Down Expand Up @@ -614,20 +614,20 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex& index, const QVarian

if ( index.column() == AvoidIntersectionColumn && role == Qt::CheckStateRole )
{
QgsVectorLayer *vl = vectorLayer( index );
QgsVectorLayer* vl = vectorLayer( index );
if ( vl )
{
if ( !mIndividualLayerSettings.contains( vl ) )
return false;

QgsSnappingConfig::IndividualLayerSettings ls = mIndividualLayerSettings.value( vl );
if ( !ls.valid() )
return false;
QStringList avoidIntersectionsList = mProject->avoidIntersectionsList();

ls.setAvoidIntersection( value.toInt() == Qt::Checked );
QgsSnappingConfig config = mProject->snappingConfig();
config.setIndividualLayerSettings( vl, ls );
mProject->setSnappingConfig( config );
if ( value.toInt() == Qt::Checked && !avoidIntersectionsList.contains( vl->id() ) )
avoidIntersectionsList.append( vl->id() );
else
avoidIntersectionsList.removeAll( vl->id() );

mProject->setAvoidIntersectionsList( avoidIntersectionsList );
return true;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/geometry/qgsgeometryeditutils.cpp
Expand Up @@ -241,10 +241,8 @@ QgsAbstractGeometry* QgsGeometryEditUtils::avoidIntersections( const QgsAbstract
return nullptr;
}

//read avoid intersections list from project properties
bool listReadOk;
QStringList avoidIntersectionsList = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList", QStringList(), &listReadOk );
if ( !listReadOk )
QStringList avoidIntersectionsList = QgsProject::instance()->avoidIntersectionsList();
if ( avoidIntersectionsList.isEmpty() )
return nullptr; //no intersections stored in project does not mean error

QList< QgsAbstractGeometry* > nearGeometries;
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -1002,6 +1002,17 @@ void QgsProject::loadEmbeddedNodes( QgsLayerTreeGroup *group )
}
}

QStringList QgsProject::avoidIntersectionsList() const
{
return readListEntry( "Digitizing", "/AvoidIntersectionsList", QStringList() );
}

void QgsProject::setAvoidIntersectionsList( const QStringList& avoidIntersectionsList )
{
writeEntry( "Digitizing", "/AvoidIntersectionsList", avoidIntersectionsList );
emit avoidIntersectionsListChanged();
}

QgsExpressionContext QgsProject::createExpressionContext() const
{
QgsExpressionContext context;
Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -80,6 +80,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs )
Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection )
Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
Q_PROPERTY( QStringList avoidIntersectionsList READ avoidIntersectionsList WRITE setAvoidIntersectionsList NOTIFY avoidIntersectionsListChanged )

public:

Expand Down Expand Up @@ -469,6 +470,20 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void setSnappingConfig( const QgsSnappingConfig& snappingConfig );

/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
QStringList avoidIntersectionsList() const;

/**
* A list of layers with which intersections should be avoided.
*
* @note Added in QGIS 3.0
*/
void setAvoidIntersectionsList( const QStringList& avoidIntersectionsList );

signals:
//! emitted when project is being read
void readProject( const QDomDocument& );
Expand Down Expand Up @@ -543,6 +558,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
void topologicalEditingChanged();

/**
* Emitted whenever avoidIntersectionsList has changed.
*
* @note Added in QGIS 3.0
*/
void avoidIntersectionsListChanged();

public slots:
/**
* Flag the project as dirty (modified). If this flag is set, the user will
Expand Down
24 changes: 4 additions & 20 deletions src/core/qgssnappingconfig.cpp
Expand Up @@ -30,17 +30,15 @@ QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings()
, mType( Vertex )
, mTolerance( 0 )
, mUnits( QgsTolerance::Pixels )
, mAvoidIntersection( false )
{}


QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings( bool enabled, SnappingType type, double tolerance, QgsTolerance::UnitType units, bool avoidIntersection )
QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings( bool enabled, SnappingType type, double tolerance, QgsTolerance::UnitType units )
: mValid( true )
, mEnabled( enabled )
, mType( type )
, mTolerance( tolerance )
, mUnits( units )
, mAvoidIntersection( avoidIntersection )
{}

bool QgsSnappingConfig::IndividualLayerSettings::valid() const
Expand Down Expand Up @@ -88,24 +86,13 @@ void QgsSnappingConfig::IndividualLayerSettings::setUnits( QgsTolerance::UnitTyp
mUnits = units;
}

bool QgsSnappingConfig::IndividualLayerSettings::avoidIntersection() const
{
return mAvoidIntersection;
}

void QgsSnappingConfig::IndividualLayerSettings::setAvoidIntersection( bool avoidIntersection )
{
mAvoidIntersection = avoidIntersection;
}

bool QgsSnappingConfig::IndividualLayerSettings::operator !=( const QgsSnappingConfig::IndividualLayerSettings& other ) const
{
return mValid != other.mValid
|| mEnabled != other.mEnabled
|| mType != other.mType
|| mTolerance != other.mTolerance
|| mUnits != other.mUnits
|| mAvoidIntersection != other.mAvoidIntersection;
|| mUnits != other.mUnits;
}

bool QgsSnappingConfig::IndividualLayerSettings::operator ==( const QgsSnappingConfig::IndividualLayerSettings& other ) const
Expand All @@ -114,8 +101,7 @@ bool QgsSnappingConfig::IndividualLayerSettings::operator ==( const QgsSnappingC
&& mEnabled == other.mEnabled
&& mType == other.mType
&& mTolerance == other.mTolerance
&& mUnits == other.mUnits
&& mAvoidIntersection == other.mAvoidIntersection;
&& mUnits == other.mUnits;
}


Expand Down Expand Up @@ -347,15 +333,14 @@ void QgsSnappingConfig::readProject( const QDomDocument& doc )
SnappingType type = ( SnappingType )settingElement.attribute( "type" ).toInt();
double tolerance = settingElement.attribute( "tolerance" ).toDouble();
QgsTolerance::UnitType units = ( QgsTolerance::UnitType )settingElement.attribute( "units" ).toInt();
bool avoidIntersection = settingElement.attribute( "avoid-intersection" ) == "1";

QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( !ml || ml->type() != QgsMapLayer::VectorLayer )
continue;

QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( ml );

IndividualLayerSettings setting = IndividualLayerSettings( enabled, type, tolerance, units, avoidIntersection );
IndividualLayerSettings setting = IndividualLayerSettings( enabled, type, tolerance, units );
mIndividualLayerSettings.insert( vl, setting );
}
}
Expand All @@ -382,7 +367,6 @@ void QgsSnappingConfig::writeProject( QDomDocument& doc )
layerElement.setAttribute( "type", ( int )setting.type() );
layerElement.setAttribute( "tolerance", setting.tolerance() );
layerElement.setAttribute( "units", ( int )setting.units() );
layerElement.setAttribute( "avoid-intersection", QString::number( setting.avoidIntersection() ) );
ilsElement.appendChild( layerElement );
}
snapSettingsElem.appendChild( ilsElement );
Expand Down
10 changes: 1 addition & 9 deletions src/core/qgssnappingconfig.h
Expand Up @@ -62,9 +62,8 @@ class CORE_EXPORT QgsSnappingConfig
* @param type
* @param tolerance
* @param units
* @param avoidIntersection
*/
IndividualLayerSettings( bool enabled, QgsSnappingConfig::SnappingType type, double tolerance, QgsTolerance::UnitType units, bool avoidIntersection = false );
IndividualLayerSettings( bool enabled, QgsSnappingConfig::SnappingType type, double tolerance, QgsTolerance::UnitType units );

/**
* Constructs an invalid setting
Expand Down Expand Up @@ -98,12 +97,6 @@ class CORE_EXPORT QgsSnappingConfig
//! set the type of units
void setUnits( QgsTolerance::UnitType units );

//! return if it shall avoid intersection (polygon layers only)
bool avoidIntersection() const;

//! set if it shall avoid intersection (polygon layers only)
void setAvoidIntersection( bool avoidIntersection );

/**
* Compare this configuration to other.
*/
Expand All @@ -117,7 +110,6 @@ class CORE_EXPORT QgsSnappingConfig
SnappingType mType;
double mTolerance;
QgsTolerance::UnitType mUnits;
bool mAvoidIntersection;
};

/**
Expand Down

0 comments on commit 85c105d

Please sign in to comment.