Move management of avoidIntersectionLayers to QgsProject

This commit is contained in:
Matthias Kuhn 2016-10-18 16:27:30 +02:00
parent 5c919fbcca
commit 85c105d6b0
9 changed files with 72 additions and 51 deletions

View File

@ -390,6 +390,20 @@ class QgsProject : QObject
*/ */
void setSnappingConfig( const QgsSnappingConfig& snappingConfig ); 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: signals:
//! emitted when project is being read //! emitted when project is being read
void readProject( const QDomDocument & ); void readProject( const QDomDocument & );
@ -462,6 +476,13 @@ class QgsProject : QObject
*/ */
void topologicalEditingChanged(); void topologicalEditingChanged();
/**
* Emitted whenever avoidIntersectionsList has changed.
*
* @note Added in QGIS 3.0
*/
void avoidIntersectionsListChanged();
public slots: public slots:
/** /**
* Flag the project as dirty (modified). If this flag is set, the user will * Flag the project as dirty (modified). If this flag is set, the user will

View File

@ -41,9 +41,8 @@ class QgsSnappingConfig
* @param type * @param type
* @param tolerance * @param tolerance
* @param units * @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 * Constructs an invalid setting
@ -77,12 +76,6 @@ class QgsSnappingConfig
//! set the type of units //! set the type of units
void setUnits( QgsTolerance::UnitType 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. * Compare this configuration to other.
*/ */

View File

@ -296,7 +296,7 @@ void QgsMapToolAddFeature::cadCanvasReleaseEvent( QgsMapMouseEvent* e )
//use always topological editing for avoidIntersection. //use always topological editing for avoidIntersection.
//Otherwise, no way to guarantee the geometries don't have a small gap in between. //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(); bool avoidIntersection = !intersectionLayers.isEmpty();
if ( avoidIntersection ) //try to add topological points also to background layers if ( avoidIntersection ) //try to add topological points also to background layers
{ {

View File

@ -494,7 +494,7 @@ QVariant QgsSnappingLayerTreeModel::data( const QModelIndex& idx, int role ) con
{ {
if ( role == Qt::CheckStateRole && vl->geometryType() == QgsWkbTypes::PolygonGeometry ) if ( role == Qt::CheckStateRole && vl->geometryType() == QgsWkbTypes::PolygonGeometry )
{ {
if ( ls.avoidIntersection() ) if ( mProject->avoidIntersectionsList().contains( vl->id() ) )
{ {
return Qt::Checked; return Qt::Checked;
} }
@ -614,20 +614,20 @@ bool QgsSnappingLayerTreeModel::setData( const QModelIndex& index, const QVarian
if ( index.column() == AvoidIntersectionColumn && role == Qt::CheckStateRole ) if ( index.column() == AvoidIntersectionColumn && role == Qt::CheckStateRole )
{ {
QgsVectorLayer *vl = vectorLayer( index ); QgsVectorLayer* vl = vectorLayer( index );
if ( vl ) if ( vl )
{ {
if ( !mIndividualLayerSettings.contains( vl ) ) if ( !mIndividualLayerSettings.contains( vl ) )
return false; return false;
QgsSnappingConfig::IndividualLayerSettings ls = mIndividualLayerSettings.value( vl ); QStringList avoidIntersectionsList = mProject->avoidIntersectionsList();
if ( !ls.valid() )
return false;
ls.setAvoidIntersection( value.toInt() == Qt::Checked ); if ( value.toInt() == Qt::Checked && !avoidIntersectionsList.contains( vl->id() ) )
QgsSnappingConfig config = mProject->snappingConfig(); avoidIntersectionsList.append( vl->id() );
config.setIndividualLayerSettings( vl, ls ); else
mProject->setSnappingConfig( config ); avoidIntersectionsList.removeAll( vl->id() );
mProject->setAvoidIntersectionsList( avoidIntersectionsList );
return true; return true;
} }
} }

View File

@ -241,10 +241,8 @@ QgsAbstractGeometry* QgsGeometryEditUtils::avoidIntersections( const QgsAbstract
return nullptr; return nullptr;
} }
//read avoid intersections list from project properties QStringList avoidIntersectionsList = QgsProject::instance()->avoidIntersectionsList();
bool listReadOk; if ( avoidIntersectionsList.isEmpty() )
QStringList avoidIntersectionsList = QgsProject::instance()->readListEntry( "Digitizing", "/AvoidIntersectionsList", QStringList(), &listReadOk );
if ( !listReadOk )
return nullptr; //no intersections stored in project does not mean error return nullptr; //no intersections stored in project does not mean error
QList< QgsAbstractGeometry* > nearGeometries; QList< QgsAbstractGeometry* > nearGeometries;

View File

@ -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 QgsProject::createExpressionContext() const
{ {
QgsExpressionContext context; QgsExpressionContext context;

View File

@ -80,6 +80,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs ) Q_PROPERTY( QgsCoordinateReferenceSystem crs READ crs WRITE setCrs )
Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection ) Q_PROPERTY( QgsMapThemeCollection* mapThemeCollection READ mapThemeCollection )
Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged ) Q_PROPERTY( QgsSnappingConfig snappingConfig READ snappingConfig WRITE setSnappingConfig NOTIFY snappingConfigChanged )
Q_PROPERTY( QStringList avoidIntersectionsList READ avoidIntersectionsList WRITE setAvoidIntersectionsList NOTIFY avoidIntersectionsListChanged )
public: public:
@ -469,6 +470,20 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/ */
void setSnappingConfig( const QgsSnappingConfig& snappingConfig ); 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: signals:
//! emitted when project is being read //! emitted when project is being read
void readProject( const QDomDocument& ); void readProject( const QDomDocument& );
@ -543,6 +558,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/ */
void topologicalEditingChanged(); void topologicalEditingChanged();
/**
* Emitted whenever avoidIntersectionsList has changed.
*
* @note Added in QGIS 3.0
*/
void avoidIntersectionsListChanged();
public slots: public slots:
/** /**
* Flag the project as dirty (modified). If this flag is set, the user will * Flag the project as dirty (modified). If this flag is set, the user will

View File

@ -30,17 +30,15 @@ QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings()
, mType( Vertex ) , mType( Vertex )
, mTolerance( 0 ) , mTolerance( 0 )
, mUnits( QgsTolerance::Pixels ) , 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 ) : mValid( true )
, mEnabled( enabled ) , mEnabled( enabled )
, mType( type ) , mType( type )
, mTolerance( tolerance ) , mTolerance( tolerance )
, mUnits( units ) , mUnits( units )
, mAvoidIntersection( avoidIntersection )
{} {}
bool QgsSnappingConfig::IndividualLayerSettings::valid() const bool QgsSnappingConfig::IndividualLayerSettings::valid() const
@ -88,24 +86,13 @@ void QgsSnappingConfig::IndividualLayerSettings::setUnits( QgsTolerance::UnitTyp
mUnits = units; 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 bool QgsSnappingConfig::IndividualLayerSettings::operator !=( const QgsSnappingConfig::IndividualLayerSettings& other ) const
{ {
return mValid != other.mValid return mValid != other.mValid
|| mEnabled != other.mEnabled || mEnabled != other.mEnabled
|| mType != other.mType || mType != other.mType
|| mTolerance != other.mTolerance || mTolerance != other.mTolerance
|| mUnits != other.mUnits || mUnits != other.mUnits;
|| mAvoidIntersection != other.mAvoidIntersection;
} }
bool QgsSnappingConfig::IndividualLayerSettings::operator ==( const QgsSnappingConfig::IndividualLayerSettings& other ) const bool QgsSnappingConfig::IndividualLayerSettings::operator ==( const QgsSnappingConfig::IndividualLayerSettings& other ) const
@ -114,8 +101,7 @@ bool QgsSnappingConfig::IndividualLayerSettings::operator ==( const QgsSnappingC
&& mEnabled == other.mEnabled && mEnabled == other.mEnabled
&& mType == other.mType && mType == other.mType
&& mTolerance == other.mTolerance && mTolerance == other.mTolerance
&& mUnits == other.mUnits && mUnits == other.mUnits;
&& mAvoidIntersection == other.mAvoidIntersection;
} }
@ -347,7 +333,6 @@ void QgsSnappingConfig::readProject( const QDomDocument& doc )
SnappingType type = ( SnappingType )settingElement.attribute( "type" ).toInt(); SnappingType type = ( SnappingType )settingElement.attribute( "type" ).toInt();
double tolerance = settingElement.attribute( "tolerance" ).toDouble(); double tolerance = settingElement.attribute( "tolerance" ).toDouble();
QgsTolerance::UnitType units = ( QgsTolerance::UnitType )settingElement.attribute( "units" ).toInt(); QgsTolerance::UnitType units = ( QgsTolerance::UnitType )settingElement.attribute( "units" ).toInt();
bool avoidIntersection = settingElement.attribute( "avoid-intersection" ) == "1";
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId ); QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( !ml || ml->type() != QgsMapLayer::VectorLayer ) if ( !ml || ml->type() != QgsMapLayer::VectorLayer )
@ -355,7 +340,7 @@ void QgsSnappingConfig::readProject( const QDomDocument& doc )
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( ml ); 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 ); mIndividualLayerSettings.insert( vl, setting );
} }
} }
@ -382,7 +367,6 @@ void QgsSnappingConfig::writeProject( QDomDocument& doc )
layerElement.setAttribute( "type", ( int )setting.type() ); layerElement.setAttribute( "type", ( int )setting.type() );
layerElement.setAttribute( "tolerance", setting.tolerance() ); layerElement.setAttribute( "tolerance", setting.tolerance() );
layerElement.setAttribute( "units", ( int )setting.units() ); layerElement.setAttribute( "units", ( int )setting.units() );
layerElement.setAttribute( "avoid-intersection", QString::number( setting.avoidIntersection() ) );
ilsElement.appendChild( layerElement ); ilsElement.appendChild( layerElement );
} }
snapSettingsElem.appendChild( ilsElement ); snapSettingsElem.appendChild( ilsElement );

View File

@ -62,9 +62,8 @@ class CORE_EXPORT QgsSnappingConfig
* @param type * @param type
* @param tolerance * @param tolerance
* @param units * @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 * Constructs an invalid setting
@ -98,12 +97,6 @@ class CORE_EXPORT QgsSnappingConfig
//! set the type of units //! set the type of units
void setUnits( QgsTolerance::UnitType 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. * Compare this configuration to other.
*/ */
@ -117,7 +110,6 @@ class CORE_EXPORT QgsSnappingConfig
SnappingType mType; SnappingType mType;
double mTolerance; double mTolerance;
QgsTolerance::UnitType mUnits; QgsTolerance::UnitType mUnits;
bool mAvoidIntersection;
}; };
/** /**