Skip to content

Commit

Permalink
[FEATURE]: maptool for parallel line displacement
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Feb 9, 2012
1 parent 2cea349 commit 82e5f91
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -58,6 +58,7 @@ SET(QGIS_APP_SRCS
qgsmaptoolmovefeature.cpp
qgsmaptoolmovelabel.cpp
qgsmaptoolmovevertex.cpp
qgsmaptooloffsetcurve.cpp
qgsmaptoolreshape.cpp
qgsmaptoolrotatelabel.cpp
qgsmaptoolrotatepointsymbols.cpp
Expand Down
14 changes: 14 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -210,6 +210,7 @@
#include "qgsmaptoolmeasureangle.h"
#include "qgsmaptoolmovefeature.h"
#include "qgsmaptoolmovevertex.h"
#include "qgsmaptooloffsetcurve.h"
#include "qgsmaptoolpan.h"
#include "qgsmaptoolselect.h"
#include "qgsmaptoolselectrectangle.h"
Expand Down Expand Up @@ -803,6 +804,7 @@ void QgisApp::createActions()
connect( mActionNodeTool, SIGNAL( triggered() ), this, SLOT( nodeTool() ) );
connect( mActionRotatePointSymbols, SIGNAL( triggered() ), this, SLOT( rotatePointSymbols() ) );
connect( mActionSnappingOptions, SIGNAL( triggered() ), this, SLOT( snappingOptions() ) );
connect( mActionOffsetCurve, SIGNAL( triggered() ), this, SLOT( offsetCurve() ) );

// View Menu Items

Expand Down Expand Up @@ -1005,6 +1007,7 @@ void QgisApp::createActionGroups()
mMapToolGroup->addAction( mActionMeasureAngle );
mMapToolGroup->addAction( mActionAddFeature );
mMapToolGroup->addAction( mActionMoveFeature );
mMapToolGroup->addAction( mActionOffsetCurve );
mMapToolGroup->addAction( mActionReshapeFeatures );
mMapToolGroup->addAction( mActionSplitFeatures );
mMapToolGroup->addAction( mActionDeleteSelected );
Expand Down Expand Up @@ -1653,6 +1656,8 @@ void QgisApp::createCanvasTools()
mMapTools.mAddFeature->setAction( mActionAddFeature );
mMapTools.mMoveFeature = new QgsMapToolMoveFeature( mMapCanvas );
mMapTools.mMoveFeature->setAction( mActionMoveFeature );
mMapTools.mOffsetCurve = new QgsMapToolOffsetCurve( mMapCanvas );
mMapTools.mOffsetCurve->setAction( mActionOffsetCurve );
mMapTools.mReshapeFeatures = new QgsMapToolReshape( mMapCanvas );
mMapTools.mReshapeFeatures->setAction( mActionReshapeFeatures );
mMapTools.mSplitFeatures = new QgsMapToolSplitFeatures( mMapCanvas );
Expand Down Expand Up @@ -3555,6 +3560,11 @@ void QgisApp::moveFeature()
mMapCanvas->setMapTool( mMapTools.mMoveFeature );
}

void QgisApp::offsetCurve()
{
mMapCanvas->setMapTool( mMapTools.mOffsetCurve );
}

void QgisApp::simplifyFeature()
{
mMapCanvas->setMapTool( mMapTools.mSimplifyFeature );
Expand Down Expand Up @@ -6094,6 +6104,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionFeatureAction->setEnabled( false );
mActionAddFeature->setEnabled( false );
mActionMoveFeature->setEnabled( false );
mActionOffsetCurve->setEnabled( false );
mActionNodeTool->setEnabled( false );
mActionDeleteSelected->setEnabled( false );
mActionCutFeatures->setEnabled( false );
Expand Down Expand Up @@ -6219,13 +6230,15 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionAddPart->setEnabled( true );
mActionDeletePart->setEnabled( true );
mActionMoveFeature->setEnabled( true );
mActionOffsetCurve->setEnabled( true );
mActionNodeTool->setEnabled( true );
}
else
{
mActionAddPart->setEnabled( false );
mActionDeletePart->setEnabled( false );
mActionMoveFeature->setEnabled( false );
mActionOffsetCurve->setEnabled( false );
mActionNodeTool->setEnabled( false );
}

Expand Down Expand Up @@ -6337,6 +6350,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionAddPart->setEnabled( false );
mActionNodeTool->setEnabled( false );
mActionMoveFeature->setEnabled( false );
mActionOffsetCurve->setEnabled( false );
mActionCopyFeatures->setEnabled( false );
mActionCutFeatures->setEnabled( false );
mActionPasteFeatures->setEnabled( false );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -237,6 +237,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QAction *actionDeletePart() { return mActionDeletePart; }
QAction *actionNodeTool() { return mActionNodeTool; }
QAction *actionSnappingOptions() { return mActionSnappingOptions; }
QAction *actionOffsetCurve() { return mActionOffsetCurve; }

QAction *actionPan() { return mActionPan; }
QAction *actionPanToSelected() { return mActionPanToSelected; }
Expand Down Expand Up @@ -668,6 +669,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void addFeature();
//! activates the move feature tool
void moveFeature();
//! activates the offset curve tool
void offsetCurve();
//! activates the reshape features tool
void reshapeFeatures();
//! activates the split features tool
Expand Down Expand Up @@ -985,6 +988,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
QgsMapTool* mMeasureAngle;
QgsMapTool* mAddFeature;
QgsMapTool* mMoveFeature;
QgsMapTool* mOffsetCurve;
QgsMapTool* mReshapeFeatures;
QgsMapTool* mSplitFeatures;
QgsMapTool* mSelect;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolmovefeature.cpp
Expand Up @@ -68,7 +68,7 @@ void QgsMapToolMoveFeature::canvasPressEvent( QMouseEvent * e )
}

//find first geometry under mouse cursor and store iterator to it
QgsPoint layerCoords = toLayerCoordinates(( QgsMapLayer* )vlayer, e->pos() );
QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() );
QSettings settings;
double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapRenderer() );
QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius,
Expand Down
105 changes: 105 additions & 0 deletions src/app/qgsmaptooloffsetcurve.cpp
@@ -0,0 +1,105 @@
/***************************************************************************
qgsmaptooloffsetcurve.cpp
------------------------------------------------------------
begin : February 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsmaptooloffsetcurve.h"
#include "qgsrubberband.h"
#include "qgsvectorlayer.h"
#include <QMouseEvent>

QgsMapToolOffsetCurve::QgsMapToolOffsetCurve( QgsMapCanvas* canvas ): QgsMapToolEdit( canvas ), mRubberBand( 0 ), mGeometry( 0 )
{
}

QgsMapToolOffsetCurve::~QgsMapToolOffsetCurve()
{
deleteRubberBandAndGeometry();
}

void QgsMapToolOffsetCurve::canvasPressEvent( QMouseEvent * e )
{
deleteRubberBandAndGeometry();

//get selected features or snap to nearest feature if no selection
QgsVectorLayer* layer = currentVectorLayer();
if ( !layer )
{
return;
}

//selection or take closest feature
//QgsPoint layerCoords = toLayerCoordinates( layer, e->pos() );

//optionally merge the features together

//create rubberband from feature(s)

//for now, just take the first selected feature
QgsFeatureList selectedFeatures = layer->selectedFeatures();
if ( selectedFeatures.size() > 0 )
{
mGeometry = selectedFeatures[0].geometryAndOwnership();
mRubberBand = createRubberBand();
mRubberBand->setToGeometry( mGeometry, layer );
}
}

void QgsMapToolOffsetCurve::canvasReleaseEvent( QMouseEvent * e )
{
deleteRubberBandAndGeometry();
}

void QgsMapToolOffsetCurve::canvasMoveEvent( QMouseEvent * e )
{
if ( !mGeometry || !mRubberBand )
{
return;
}

QgsVectorLayer* layer = currentVectorLayer();
if ( !layer )
{
return;
}

//get offset from current position rectangular to feature
QgsPoint layerCoords = toLayerCoordinates( layer, e->pos() );
QgsPoint minDistPoint;
int beforeVertex;
double offset = sqrt( mGeometry->closestSegmentWithContext( layerCoords, minDistPoint, beforeVertex ) );
qWarning( QString::number( offset ).toLocal8Bit().data() );

//create offset geometry using geos
QgsGeometry geomCopy( *mGeometry );
GEOSGeometry* geosGeom = geomCopy.asGeos();
if ( geosGeom )
{
//GEOSGeometry* offsetGeom = GEOSOffsetCurve( geosGeom, offset, 8, 1, 1 );
GEOSGeometry* offsetGeom = GEOSSingleSidedBuffer( geosGeom, offset, 8, 1, 1, 0 );
if ( offsetGeom )
{
QgsGeometry rubberBandGeometry;
rubberBandGeometry.fromGeos( offsetGeom );
mRubberBand->setToGeometry( &rubberBandGeometry, layer );
}
}
}

void QgsMapToolOffsetCurve::deleteRubberBandAndGeometry()
{
delete mRubberBand;
mRubberBand = 0;
delete mGeometry;
mGeometry = 0;
}
42 changes: 42 additions & 0 deletions src/app/qgsmaptooloffsetcurve.h
@@ -0,0 +1,42 @@
/***************************************************************************
qgsmaptooloffsetcurve.h
------------------------------------------------------------
begin : February 2012
copyright : (C) 2012 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSMAPTOOLOFFSETCURVE_H
#define QGSMAPTOOLOFFSETCURVE_H

#include "qgsmaptooledit.h"
class QgsGeometry;

class QgsMapToolOffsetCurve: public QgsMapToolEdit
{
public:
QgsMapToolOffsetCurve( QgsMapCanvas* canvas );
~QgsMapToolOffsetCurve();

void canvasPressEvent( QMouseEvent * e );
void canvasReleaseEvent( QMouseEvent * e );
void canvasMoveEvent( QMouseEvent * e );

private:

/**Rubberband that shows the position of the offset curve*/
QgsRubberBand* mRubberBand;
/**Geometry to manipulate*/
QgsGeometry* mGeometry;

void deleteRubberBandAndGeometry();
};

#endif // QGSMAPTOOLOFFSETCURVE_H
12 changes: 11 additions & 1 deletion src/ui/qgisapp.ui
Expand Up @@ -17,7 +17,7 @@
<x>0</x>
<y>0</y>
<width>1052</width>
<height>28</height>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="mEditMenu">
Expand All @@ -41,6 +41,7 @@
<addaction name="mActionDeleteRing"/>
<addaction name="mActionDeletePart"/>
<addaction name="mActionReshapeFeatures"/>
<addaction name="mActionOffsetCurve"/>
<addaction name="mActionSplitFeatures"/>
<addaction name="mActionMergeFeatures"/>
<addaction name="mActionMergeFeatureAttributes"/>
Expand Down Expand Up @@ -292,6 +293,7 @@
<addaction name="mActionDeleteRing"/>
<addaction name="mActionDeletePart"/>
<addaction name="mActionReshapeFeatures"/>
<addaction name="mActionOffsetCurve"/>
<addaction name="mActionSplitFeatures"/>
<addaction name="mActionMergeFeatures"/>
<addaction name="mActionMergeFeatureAttributes"/>
Expand Down Expand Up @@ -1604,6 +1606,14 @@
<string>Pan Map to Selection</string>
</property>
</action>
<action name="mActionOffsetCurve">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Offset curve</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down

0 comments on commit 82e5f91

Please sign in to comment.