Skip to content

Commit

Permalink
Simplify code for straight line rubber bands
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Apr 4, 2017
1 parent 3ae2297 commit 028b8dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 78 deletions.
112 changes: 48 additions & 64 deletions src/app/nodetool/qgsnodetool2.cpp
Expand Up @@ -219,22 +219,26 @@ void QgsNodeTool2::deactivate()
QgsMapToolAdvancedDigitizing::deactivate();
}

void QgsNodeTool2::addDragBand( const QgsPoint &v1, const QgsPoint &v2, const QgsVector &offset )
void QgsNodeTool2::addDragBand( const QgsPoint &v1, const QgsPoint &v2 )
{
QgsRubberBand *dragBand = createRubberBand( QgsWkbTypes::LineGeometry, true );
dragBand->addPoint( v1 );
dragBand->addPoint( v2 );
mDragBands << dragBand;
mDragBandsOffset << offset;
addDragStraightBand( v1, v2, false, true, v2 );
}

void QgsNodeTool2::addDragMiddleBand( const QgsPoint &v1, const QgsPoint &v2, const QgsVector &offset1, const QgsVector &offset2 )
void QgsNodeTool2::addDragStraightBand( const QgsPoint &v0, const QgsPoint &v1, bool moving0, bool moving1, const QgsPoint &mapPoint )
{
QgsRubberBand *dragBand = createRubberBand( QgsWkbTypes::LineGeometry, true );
dragBand->addPoint( v1 );
dragBand->addPoint( v2 );
mDragMiddleBands << dragBand;
mDragMiddleBandsOffset << qMakePair( offset1, offset2 );
StraightBand b;
b.band = createRubberBand( QgsWkbTypes::LineGeometry, true );
b.p0 = v0;
b.p1 = v1;
b.moving0 = moving0;
b.moving1 = moving1;
b.offset0 = v0 - mapPoint;
b.offset1 = v1 - mapPoint;

b.band->addPoint( v0 );
b.band->addPoint( v1 );

mDragStraightBands << b;
}

void QgsNodeTool2::addDragCircularBand( const QgsPoint &v0, const QgsPoint &v1, const QgsPoint &v2, bool moving0, bool moving1, bool moving2, const QgsPoint &mapPoint )
Expand All @@ -257,18 +261,14 @@ void QgsNodeTool2::addDragCircularBand( const QgsPoint &v0, const QgsPoint &v1,

void QgsNodeTool2::clearDragBands()
{
qDeleteAll( mDragBands );
mDragBands.clear();
mDragBandsOffset.clear();

qDeleteAll( mDragMiddleBands );
mDragMiddleBands.clear();
mDragMiddleBandsOffset.clear();

qDeleteAll( mDragPointMarkers );
mDragPointMarkers.clear();
mDragPointMarkersOffset.clear();

Q_FOREACH ( const StraightBand &b, mDragStraightBands )
delete b.band;
mDragStraightBands.clear();

Q_FOREACH ( const CircularBand &b, mDragCircularBands )
delete b.band;
mDragCircularBands.clear();
Expand Down Expand Up @@ -451,21 +451,13 @@ void QgsNodeTool2::mouseMoveDraggingVertex( QgsMapMouseEvent *e )

mEdgeCenterMarker->setVisible( false );

// rubber bands with one end moving
for ( int i = 0; i < mDragBands.count(); ++i )
for ( int i = 0; i < mDragStraightBands.count(); ++i )
{
QgsRubberBand *band = mDragBands[i];
const QgsVector &offset = mDragBandsOffset[i];
band->movePoint( 1, e->mapPoint() + offset );
}

// rubber bands with both ends moving
for ( int i = 0; i < mDragMiddleBands.count(); ++i )
{
QgsRubberBand *band = mDragMiddleBands[i];
const QPair<QgsVector, QgsVector> &offset = mDragMiddleBandsOffset[i];
band->movePoint( 0, e->mapPoint() + offset.first );
band->movePoint( 1, e->mapPoint() + offset.second );
StraightBand &b = mDragStraightBands[i];
if ( b.moving0 )
b.band->movePoint( 0, e->mapPoint() + b.offset0 );
if ( b.moving1 )
b.band->movePoint( 1, e->mapPoint() + b.offset1 );
}

for ( int i = 0; i < mDragCircularBands.count(); ++i )
Expand Down Expand Up @@ -896,6 +888,8 @@ void QgsNodeTool2::startDraggingMoveVertex( const QgsPoint &mapPoint, const QgsP

QgsPoint dragVertexMapPoint = m.point();

QSet<Vertex> verticesInStraightBands; // always the vertex with lower index

// set of middle vertices that are already in a circular rubber band
// i.e. every circular band is defined by its middle circular vertex
QSet<Vertex> verticesInCircularBands;
Expand Down Expand Up @@ -933,11 +927,11 @@ void QgsNodeTool2::startDraggingMoveVertex( const QgsPoint &mapPoint, const QgsP
if ( v0idx != -1 )
{
// there is another vertex to the left - let's build a rubber band for it
Vertex v0( v.layer, v.fid, v0idx );
QgsVertexId v0id;
if ( geom.vertexIdFromVertexNr( v0idx, v0id ) && v0id.type == QgsVertexId::CurveVertex )
{
// circular segment to the left
Vertex v0( v.layer, v.fid, v0idx );
if ( !verticesInCircularBands.contains( v0 ) )
{
addDragCircularBand( geom.vertexAt( v0idx - 1 ),
Expand All @@ -953,38 +947,33 @@ void QgsNodeTool2::startDraggingMoveVertex( const QgsPoint &mapPoint, const QgsP
else
{
// straight segment to the left
Vertex v0( v.layer, v.fid, v0idx );
QgsPoint otherPoint0 = geom.vertexAt( v0idx );
QgsPoint otherMapPoint0 = toMapCoordinates( v.layer, otherPoint0 );
if ( !movingVertices.contains( v0 ) )
if ( !verticesInStraightBands.contains( v0 ) )
{
// rubber band that is fixed on one side and moving with mouse cursor on the other
addDragBand( otherMapPoint0, pt, pt - dragVertexMapPoint );
}
else
{
// rubber band that has both endpoints moving with mouse cursor
if ( v0idx > v.vertexId )
addDragMiddleBand( otherMapPoint0, pt, otherMapPoint0 - dragVertexMapPoint, pt - dragVertexMapPoint );
addDragStraightBand( geom.vertexAt( v0idx ),
pt,
movingVertices.contains( v0 ),
true,
dragVertexMapPoint );
verticesInStraightBands << v0;
}
}
}

if ( v1idx != -1 )
{
// there is another vertex to the right - let's build a rubber band for it
Vertex v1( v.layer, v.fid, v1idx );
QgsVertexId v1id;
if ( geom.vertexIdFromVertexNr( v1idx, v1id ) && v1id.type == QgsVertexId::CurveVertex )
{
// circular segment to the right
Vertex v1( v.layer, v.fid, v1idx );
if ( !verticesInCircularBands.contains( v1 ) )
{
addDragCircularBand( pt,
geom.vertexAt( v1idx ),
geom.vertexAt( v1idx + 1 ),
true,
movingVertices.contains( Vertex( v.layer, v.fid, v1idx ) ),
movingVertices.contains( v1 ),
movingVertices.contains( Vertex( v.layer, v.fid, v1idx + 1 ) ),
dragVertexMapPoint );
verticesInCircularBands << v1;
Expand All @@ -993,19 +982,14 @@ void QgsNodeTool2::startDraggingMoveVertex( const QgsPoint &mapPoint, const QgsP
else
{
// straight segment to the right
Vertex v1( v.layer, v.fid, v1idx );
QgsPoint otherPoint1 = geom.vertexAt( v1idx );
QgsPoint otherMapPoint1 = toMapCoordinates( v.layer, otherPoint1 );
if ( !movingVertices.contains( v1 ) )
{
// rubber band that is fixed on one side and moving with mouse cursor on the other
addDragBand( otherMapPoint1, pt, pt - dragVertexMapPoint );
}
else
if ( !verticesInStraightBands.contains( v ) )
{
// rubber band that has both endpoints moving with mouse cursor
if ( v1idx > v.vertexId )
addDragMiddleBand( otherMapPoint1, pt, otherMapPoint1 - dragVertexMapPoint, pt - dragVertexMapPoint );
addDragStraightBand( pt,
geom.vertexAt( v1idx ),
true,
movingVertices.contains( v1 ),
dragVertexMapPoint );
verticesInStraightBands << v;
}
}
}
Expand Down Expand Up @@ -1115,17 +1099,17 @@ void QgsNodeTool2::startDraggingEdge( const QgsPointLocator::Match &m, const Qgs
QgsPoint layerPoint0 = geom.vertexAt( v0idx );
QgsPoint mapPoint0 = toMapCoordinates( m.layer(), layerPoint0 );
addDragBand( mapPoint0, edge_p0 );
mDraggingEdge->bandsTo0 << mDragBands.last();
mDraggingEdge->bandsTo0 << mDragStraightBands.last().band;
}
if ( v1idx != -1 )
{
QgsPoint layerPoint1 = geom.vertexAt( v1idx );
QgsPoint mapPoint1 = toMapCoordinates( m.layer(), layerPoint1 );
addDragBand( mapPoint1, edge_p1 );
mDraggingEdge->bandsTo1 << mDragBands.last();
mDraggingEdge->bandsTo1 << mDragStraightBands.last().band;
}

mDraggingEdge->band0to1 = mDragBands.first();
mDraggingEdge->band0to1 = mDragStraightBands.first().band;

mOverrideCadPoints.clear();
mOverrideCadPoints << m.point() << m.point();
Expand Down
27 changes: 13 additions & 14 deletions src/app/nodetool/qgsnodetool2.h
Expand Up @@ -108,9 +108,9 @@ class APP_EXPORT QgsNodeTool2 : public QgsMapToolAdvancedDigitizing

private:

void addDragBand( const QgsPoint &v1, const QgsPoint &v2, const QgsVector &offset = QgsVector() );
void addDragBand( const QgsPoint &v1, const QgsPoint &v2 );

void addDragMiddleBand( const QgsPoint &v1, const QgsPoint &v2, const QgsVector &offset1, const QgsVector &offset2 );
void addDragStraightBand( const QgsPoint &v0, const QgsPoint &v1, bool moving0, bool moving1, const QgsPoint &mapPoint );

void addDragCircularBand( const QgsPoint &v0, const QgsPoint &v1, const QgsPoint &v2, bool moving0, bool moving1, bool moving2, const QgsPoint &mapPoint );

Expand Down Expand Up @@ -221,18 +221,15 @@ class APP_EXPORT QgsNodeTool2 : public QgsMapToolAdvancedDigitizing
//! companion array to mDragPointMarkers: for each marker it keeps offset
//! (in map units) from the position of the main vertex
QList<QgsVector> mDragPointMarkersOffset;
//! list of QgsRubberBand instances used when dragging. All rubber bands
//! have two points: first point is fixed, the other one is moved as mouse moves
QList<QgsRubberBand *> mDragBands;
//! companion array to mDragBands: for each rubber band it keeps offset of the second
//! point (in map units) from the position of the main vertex (mDraggingVertex)
QList<QgsVector> mDragBandsOffset;
//! list of QgsRubberBand instances used when dragging multiple vertices - these rubber bands
//! compared to mDragBands have both points moving together with mouse cursor
QList<QgsRubberBand *> mDragMiddleBands;
//! companion array to mDragMiddleBands: for each rubber band it keeps offset of both
//! first and second point (in map units) from the position of the main vertex (mDraggingVertex)
QList< QPair<QgsVector, QgsVector> > mDragMiddleBandsOffset;

//! structure to keep information about a rubber band user for dragging of a straight line segment
struct StraightBand
{
QgsRubberBand *band = nullptr; //!< Pointer to the actual rubber band
QgsPoint p0, p1; //!< What are the original positions of points (in map units)
bool moving0, moving1; //!< Which points of the band are moving with mouse cursor
QgsVector offset0, offset1; //!< If the point is moving, what is the offset from the mouse cursor
};

//! structure to keep information about a rubber band used for dragging of a circular segment
struct CircularBand
Expand All @@ -246,6 +243,8 @@ class APP_EXPORT QgsNodeTool2 : public QgsMapToolAdvancedDigitizing
void updateRubberBand( const QgsPoint &mapPoint );
};

//! list of active straight line rubber bands
QList<StraightBand> mDragStraightBands;
//! list of active rubber bands for circular segments
QList<CircularBand> mDragCircularBands;
//! instance of Vertex that is being currently moved or nothing
Expand Down

0 comments on commit 028b8dc

Please sign in to comment.