Skip to content

Commit 654924d

Browse files
committed
Implement QgsProject::snapSettingsForLayer as a convenient way to access snap properties for a layer
1 parent 1d6943d commit 654924d

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

python/core/qgsproject.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ public:
240240
@note added in 1.4 */
241241
void setBadLayerHandler( QgsProjectBadLayerHandler* handler );
242242

243+
void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType, double tolerance );
244+
245+
bool snapSettingsForLayer( const QString& layerId, bool& enabled /Out/, QgsSnapper::SnappingType& type /Out/, QgsTolerance::UnitType& units /Out/, double& tolerance /Out/,
246+
bool& avoidIntersection /Out/ );
247+
243248
protected:
244249

245250
/** Set error message from read/write operation

src/core/qgsproject.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,81 @@ bool QgsProject::createEmbeddedLayer( const QString& layerId, const QString& pro
16211621
return false;
16221622
}
16231623

1624+
void QgsProject::setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType, double tolerance )
1625+
{
1626+
//soon...
1627+
}
1628+
1629+
bool QgsProject::snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType &type, QgsTolerance::UnitType& units, double& tolerance,
1630+
bool& avoidIntersection )
1631+
{
1632+
QStringList layerIdList, enabledList, snapTypeList, toleranceUnitList, toleranceList, avoidIntersectionList;
1633+
snapSettings( layerIdList, enabledList, snapTypeList, toleranceUnitList, toleranceList, avoidIntersectionList );
1634+
int idx = layerIdList.indexOf( layerId );
1635+
if ( idx == -1 )
1636+
{
1637+
return false;
1638+
}
1639+
1640+
//make sure all lists are long enough
1641+
int minListEntries = idx + 1;
1642+
if ( layerIdList.size() < minListEntries || enabledList.size() < minListEntries || snapTypeList.size() < minListEntries ||
1643+
toleranceUnitList.size() < minListEntries || toleranceList.size() < minListEntries )
1644+
{
1645+
return false;
1646+
}
1647+
1648+
//enabled
1649+
enabled = enabledList.at( idx ) == "enabled";
1650+
1651+
//snap type
1652+
QString snapType = snapTypeList.at( idx );
1653+
if ( snapType == "to_segment" )
1654+
{
1655+
type = QgsSnapper::SnapToSegment;
1656+
}
1657+
else if ( snapType == "to_vertex_and_segment" )
1658+
{
1659+
type = QgsSnapper::SnapToVertexAndSegment;
1660+
}
1661+
else //to vertex
1662+
{
1663+
type = QgsSnapper::SnapToVertex;
1664+
}
1665+
1666+
//units
1667+
if ( toleranceUnitList.at( idx ) == "1" )
1668+
{
1669+
units = QgsTolerance::Pixels;
1670+
}
1671+
else
1672+
{
1673+
units = QgsTolerance::MapUnits;
1674+
}
1675+
1676+
//tolerance
1677+
tolerance = toleranceList.at( idx ).toDouble();
1678+
1679+
//avoid intersection
1680+
avoidIntersection = ( avoidIntersectionList.indexOf( layerId ) != -1 );
1681+
1682+
return true;
1683+
}
1684+
1685+
void QgsProject::snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& toleranceUnitList, QStringList& toleranceList,
1686+
QStringList& avoidIntersectionList )
1687+
{
1688+
layerIdList = readListEntry( "Digitizing", "/LayerSnappingList" );
1689+
enabledList = readListEntry( "Digitizing", "/LayerSnappingEnabledList" );
1690+
toleranceList = readListEntry( "Digitizing", "/LayerSnappingToleranceList" );
1691+
toleranceUnitList = readListEntry( "Digitizing", "/LayerSnappingToleranceUnitList" );
1692+
snapTypeList = readListEntry( "Digitizing", "/LayerSnapToList" );
1693+
avoidIntersectionList = readListEntry( "Digitizing", "/AvoidIntersectionsList" );
1694+
}
1695+
16241696
void QgsProjectBadLayerDefaultHandler::handleBadLayers( QList<QDomNode> /*layers*/, QDomDocument /*projectDom*/ )
16251697
{
16261698
// just ignore any bad layers
16271699
}
1700+
1701+

src/core/qgsproject.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#include <QObject>
2929
#include <QPair>
3030

31+
//for the snap settings
32+
#include "qgssnapper.h"
33+
#include "qgstolerance.h"
34+
3135
//#include <QDomDocument>
3236

3337
class QFileInfo;
@@ -286,6 +290,12 @@ class CORE_EXPORT QgsProject : public QObject
286290
bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
287291
QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
288292

293+
//convenience interface for querying / modifying the project snap settings per layer
294+
void setSnapSettingsForLayer( const QString& layerId, bool enabled, QgsSnapper::SnappingType type, QgsTolerance::UnitType, double tolerance );
295+
296+
bool snapSettingsForLayer( const QString& layerId, bool& enabled, QgsSnapper::SnappingType& type, QgsTolerance::UnitType& units, double& tolerance,
297+
bool& avoidIntersection );
298+
289299
protected:
290300

291301
/** Set error message from read/write operation
@@ -339,6 +349,9 @@ class CORE_EXPORT QgsProject : public QObject
339349
If the project file path is empty, QgsProject is going to ignore the layer for saving (e.g. because it is part and managed by an embedded group)*/
340350
QHash< QString, QPair< QString, bool> > mEmbeddedLayers;
341351

352+
void snapSettings( QStringList& layerIdList, QStringList& enabledList, QStringList& snapTypeList, QStringList& snapUnitList, QStringList& toleranceUnitList,
353+
QStringList& avoidIntersectionList );
354+
342355
}; // QgsProject
343356

344357

0 commit comments

Comments
 (0)