Skip to content

Commit d3142f6

Browse files
committedAug 8, 2013
Merge pull request #798 from 3nids/maptoolcapturerubber
[fix #8395] Add a second rubber band in map tool capture
2 parents 2087de1 + c1cde94 commit d3142f6

12 files changed

+108
-50
lines changed
 

‎src/app/qgsmaptooladdfeature.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
141141
mCanvas->refresh();
142142
}
143143
}
144+
145+
// LINE AND POLYGON CAPTURING
144146
else if ( mode() == CaptureLine || mode() == CapturePolygon )
145147
{
146148
//check we only use the line tool for line/multiline layers
@@ -181,8 +183,7 @@ void QgsMapToolAddFeature::canvasReleaseEvent( QMouseEvent * e )
181183
else if ( e->button() == Qt::RightButton )
182184
{
183185
// End of string
184-
185-
resetLastVertex();
186+
deleteTempRubberBand();
186187

187188
//lines: bail out if there are not at least two vertices
188189
if ( mode() == CaptureLine && size() < 2 )

‎src/app/qgsmaptooladdpart.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e )
112112
}
113113
else if ( e->button() != Qt::RightButton )
114114
{
115-
resetLastVertex();
115+
deleteTempRubberBand();
116116

117117
return;
118118
}

‎src/app/qgsmaptooladdring.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void QgsMapToolAddRing::canvasReleaseEvent( QMouseEvent * e )
6969
}
7070
else if ( e->button() == Qt::RightButton )
7171
{
72-
resetLastVertex();
72+
deleteTempRubberBand();
7373

7474
closePolygon();
7575

‎src/app/qgsmaptoolcapture.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas* canvas, enum CaptureMode too
3636
: QgsMapToolEdit( canvas )
3737
, mCaptureMode( tool )
3838
, mRubberBand( 0 )
39+
, mTempRubberBand( 0 )
3940
, mValidator( 0 )
4041
{
4142
mCaptureModeFromLayer = tool == CaptureNone;
@@ -100,7 +101,6 @@ void QgsMapToolCapture::currentLayerChanged( QgsMapLayer *layer )
100101
}
101102
}
102103

103-
104104
void QgsMapToolCapture::canvasMoveEvent( QMouseEvent * e )
105105
{
106106
QgsPoint mapPoint;
@@ -120,10 +120,10 @@ void QgsMapToolCapture::canvasMoveEvent( QMouseEvent * e )
120120
mSnappingMarkers << m;
121121
}
122122

123-
if ( mCaptureMode != CapturePoint && mRubberBand && mCapturing )
123+
if ( mCaptureMode != CapturePoint && mTempRubberBand && mCapturing )
124124
{
125125
mapPoint = snapPointFromResults( snapResults, e->pos() );
126-
mRubberBand->movePoint( mapPoint );
126+
mTempRubberBand->movePoint( mapPoint );
127127
}
128128
}
129129
} // mouseMoveEvent
@@ -180,7 +180,6 @@ int QgsMapToolCapture::nextPoint( const QPoint &p, QgsPoint &layerPoint, QgsPoin
180180
return 0;
181181
}
182182

183-
184183
int QgsMapToolCapture::addVertex( const QPoint &p )
185184
{
186185
QgsPoint layerPoint;
@@ -203,10 +202,28 @@ int QgsMapToolCapture::addVertex( const QPoint &p )
203202
{
204203
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
205204
}
206-
207205
mRubberBand->addPoint( mapPoint );
208206
mCaptureList.append( layerPoint );
209207

208+
if ( !mTempRubberBand )
209+
{
210+
mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line , true );
211+
}
212+
else{
213+
mTempRubberBand->reset(CapturePolygon ? true : false);
214+
}
215+
if ( mCaptureMode == CaptureLine )
216+
{
217+
mTempRubberBand->addPoint( mapPoint );
218+
}
219+
else if ( mCaptureMode == CapturePolygon )
220+
{
221+
const QgsPoint *firstPoint = mRubberBand->getPoint( 0 , 0 );
222+
mTempRubberBand->addPoint( *firstPoint );
223+
mTempRubberBand->movePoint( mapPoint );
224+
mTempRubberBand->addPoint( mapPoint );
225+
}
226+
210227
validateGeometry();
211228

212229
return 0;
@@ -231,20 +248,6 @@ void QgsMapToolCapture::undo()
231248
}
232249
}
233250

234-
void QgsMapToolCapture::resetLastVertex()
235-
{
236-
if ( mRubberBand )
237-
{
238-
int rubberBandSize = mRubberBand->numberOfVertices();
239-
if ( rubberBandSize < 2 )
240-
{
241-
return;
242-
}
243-
const QgsPoint *lastPoint = mRubberBand->getPoint(0, rubberBandSize-2);
244-
mRubberBand->movePoint( *lastPoint );
245-
}
246-
}
247-
248251
void QgsMapToolCapture::keyPressEvent( QKeyEvent* e )
249252
{
250253
if ( e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete )
@@ -266,6 +269,12 @@ void QgsMapToolCapture::stopCapturing()
266269
mRubberBand = 0;
267270
}
268271

272+
if ( mTempRubberBand )
273+
{
274+
delete mTempRubberBand;
275+
mTempRubberBand = 0;
276+
}
277+
269278
while ( !mGeomErrorMarkers.isEmpty() )
270279
{
271280
delete mGeomErrorMarkers.takeFirst();
@@ -283,6 +292,15 @@ void QgsMapToolCapture::stopCapturing()
283292
mCanvas->refresh();
284293
}
285294

295+
void QgsMapToolCapture::deleteTempRubberBand()
296+
{
297+
if ( mTempRubberBand )
298+
{
299+
delete mTempRubberBand;
300+
mTempRubberBand = 0;
301+
}
302+
}
303+
286304
void QgsMapToolCapture::closePolygon()
287305
{
288306
mCaptureList.append( mCaptureList[0] );

‎src/app/qgsmaptoolcapture.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ class QgsMapToolCapture : public QgsMapToolEdit
8282
/**Removes the last vertex from mRubberBand and mCaptureList*/
8383
void undo();
8484

85-
/**Reset the last vertex from RubberBand to the previous one position*/
86-
void resetLastVertex();
87-
8885
void startCapturing();
8986
void stopCapturing();
87+
void deleteTempRubberBand();
9088

9189
CaptureMode mode() { return mCaptureMode; }
9290

@@ -107,6 +105,9 @@ class QgsMapToolCapture : public QgsMapToolEdit
107105
/** rubber band for polylines and polygons */
108106
QgsRubberBand* mRubberBand;
109107

108+
/** temporary rubber band for polylines and polygons. this connects the last added point to the mouse cursor position */
109+
QgsRubberBand* mTempRubberBand;
110+
110111
/** List to store the points of digitised lines and polygons (in layer coordinates)*/
111112
QList<QgsPoint> mCaptureList;
112113

‎src/app/qgsmaptooledit.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,26 @@ QgsPoint QgsMapToolEdit::snapPointFromResults( const QList<QgsSnappingResult>& s
6868
}
6969
}
7070

71-
QgsRubberBand* QgsMapToolEdit::createRubberBand( QGis::GeometryType geometryType )
71+
QgsRubberBand* QgsMapToolEdit::createRubberBand( QGis::GeometryType geometryType, bool alternativeBand )
7272
{
7373
QSettings settings;
7474
QgsRubberBand* rb = new QgsRubberBand( mCanvas, geometryType );
75+
rb->setWidth( settings.value( "/qgis/digitizing/line_width", 1 ).toInt() );
7576
QColor color( settings.value( "/qgis/digitizing/line_color_red", 255 ).toInt(),
7677
settings.value( "/qgis/digitizing/line_color_green", 0 ).toInt(),
77-
settings.value( "/qgis/digitizing/line_color_blue", 0 ).toInt() );
78+
settings.value( "/qgis/digitizing/line_color_blue", 0 ).toInt());
79+
double myAlpha = settings.value( "/qgis/digitizing/line_color_alpha", 200 ).toInt() / 255.0 ;
80+
if ( alternativeBand )
81+
{
82+
myAlpha = myAlpha * settings.value( "/qgis/digitizing/line_color_alpha_scale" , 0.75 ).toDouble();
83+
rb->setLineStyle( Qt::DotLine );
84+
}
85+
if ( geometryType == QGis::Polygon )
86+
{
87+
color.setAlphaF ( myAlpha );
88+
}
89+
color.setAlphaF ( myAlpha );
7890
rb->setColor( color );
79-
rb->setWidth( settings.value( "/qgis/digitizing/line_width", 1 ).toInt() );
8091
rb->show();
8192
return rb;
8293
}

‎src/app/qgsmaptooledit.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ class QgsMapToolEdit: public QgsMapTool
5050
@return the snapped point in map coordinates*/
5151
QgsPoint snapPointFromResults( const QList<QgsSnappingResult>& snapResults, const QPoint& screenCoords );
5252

53-
/**Creates a rubber band with the color/line width from
54-
the QGIS settings. The caller takes ownership of the
55-
returned object*/
56-
QgsRubberBand* createRubberBand( QGis::GeometryType geometryType = QGis::Line );
53+
/** Creates a rubber band with the color/line width from
54+
* the QGIS settings. The caller takes ownership of the
55+
* returned object
56+
* @param geometryType
57+
* @param alternativeBand if true, rubber band will be set with more transparency and a dash pattern. defaut is false.
58+
*/
59+
QgsRubberBand* createRubberBand(QGis::GeometryType geometryType = QGis::Line , bool alternativeBand = false );
5760

5861
/**Returns the current vector layer of the map canvas or 0*/
5962
QgsVectorLayer* currentVectorLayer();

‎src/app/qgsmaptoolreshape.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void QgsMapToolReshape::canvasReleaseEvent( QMouseEvent * e )
6868
}
6969
else if ( e->button() == Qt::RightButton )
7070
{
71-
resetLastVertex();
71+
deleteTempRubberBand();
7272

7373
//find out bounding box of mCaptureList
7474
if ( size() < 1 )

‎src/app/qgsmaptoolsplitfeatures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void QgsMapToolSplitFeatures::canvasReleaseEvent( QMouseEvent * e )
7474
}
7575
else if ( e->button() == Qt::RightButton )
7676
{
77-
resetLastVertex();
77+
deleteTempRubberBand();
7878

7979
//bring up dialog if a split was not possible (polygon) or only done once (line)
8080
int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );

‎src/app/qgsoptions.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
638638
myRed = settings.value( "/qgis/digitizing/line_color_red", 255 ).toInt();
639639
myGreen = settings.value( "/qgis/digitizing/line_color_green", 0 ).toInt();
640640
myBlue = settings.value( "/qgis/digitizing/line_color_blue", 0 ).toInt();
641-
mLineColorToolButton->setColor( QColor( myRed, myGreen, myBlue ) );
641+
myAlpha = settings.value( "/qgis/digitizing/line_color_alpha", 200 ).toInt();
642+
mLineColorToolButton->setColor( QColor( myRed, myGreen, myBlue, myAlpha ) );
643+
mLineColorToolButton->setColorDialogOptions(QColorDialog::ShowAlphaChannel);
642644

643645
//default snap mode
644646
mDefaultSnapModeComboBox->insertItem( 0, tr( "To vertex" ), "to vertex" );
@@ -1083,6 +1085,7 @@ void QgsOptions::saveOptions()
10831085
settings.setValue( "/qgis/digitizing/line_color_red", digitizingColor.red() );
10841086
settings.setValue( "/qgis/digitizing/line_color_green", digitizingColor.green() );
10851087
settings.setValue( "/qgis/digitizing/line_color_blue", digitizingColor.blue() );
1088+
settings.setValue( "/qgis/digitizing/line_color_alpha", digitizingColor.alpha() );
10861089

10871090
//default snap mode
10881091
QString defaultSnapModeString = mDefaultSnapModeComboBox->itemData( mDefaultSnapModeComboBox->currentIndex() ).toString();

‎src/gui/qgsrubberband.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,35 @@
2929
*/
3030
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, QGis::GeometryType geometryType )
3131
: QgsMapCanvasItem( mapCanvas )
32-
, mWidth( 1 )
3332
, mIconSize( 5 )
3433
, mIconType( ICON_CIRCLE )
3534
, mGeometryType( geometryType )
3635
, mTranslationOffsetX( 0.0 )
3736
, mTranslationOffsetY( 0.0 )
3837
{
3938
reset( geometryType );
40-
QColor color(Qt::lightGray);
41-
color.setAlpha(63);
39+
QColor color( Qt::lightGray );
40+
color.setAlpha( 63 );
4241
setColor( color );
42+
setWidth( 1 );
43+
setLineStyle( Qt::SolidLine );
44+
setBrushStyle( Qt::SolidPattern );
4345
}
4446

4547
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon )
4648
: QgsMapCanvasItem( mapCanvas )
47-
, mWidth( 1 )
4849
, mIconSize( 5 )
4950
, mIconType( ICON_CIRCLE )
5051
, mTranslationOffsetX( 0.0 )
5152
, mTranslationOffsetY( 0.0 )
5253
{
5354
reset( isPolygon ? QGis::Polygon : QGis::Line );
54-
QColor color(Qt::lightGray);
55-
color.setAlpha(63);
55+
QColor color( Qt::lightGray );
56+
color.setAlpha( 63 );
5657
setColor( color );
58+
setWidth( 1 );
59+
setLineStyle( Qt::SolidLine );
60+
setBrushStyle( Qt::SolidPattern );
5761
}
5862

5963
QgsRubberBand::QgsRubberBand(): QgsMapCanvasItem( 0 )
@@ -72,15 +76,14 @@ void QgsRubberBand::setColor( const QColor & color )
7276
mPen.setColor( color );
7377
QColor fillColor( color.red(), color.green(), color.blue(), color.alpha() );
7478
mBrush.setColor( fillColor );
75-
mBrush.setStyle( Qt::SolidPattern );
7679
}
7780

7881
/*!
7982
Set the outline width.
8083
*/
8184
void QgsRubberBand::setWidth( int width )
8285
{
83-
mWidth = width;
86+
mPen.setWidth( width );
8487
}
8588

8689
void QgsRubberBand::setIcon( IconType icon )
@@ -93,6 +96,16 @@ void QgsRubberBand::setIconSize( int iconSize )
9396
mIconSize = iconSize;
9497
}
9598

99+
void QgsRubberBand::setLineStyle( Qt::PenStyle penStyle )
100+
{
101+
mPen.setStyle( penStyle );
102+
}
103+
104+
void QgsRubberBand::setBrushStyle( Qt::BrushStyle brushStyle )
105+
{
106+
mBrush.setStyle( brushStyle );
107+
}
108+
96109
/*!
97110
Remove all points from the shape being created.
98111
*/
@@ -406,7 +419,6 @@ void QgsRubberBand::paint( QPainter* p )
406419
if ( mPoints.size() > 0 )
407420
{
408421
p->setBrush( mBrush );
409-
mPen.setWidth( mWidth );
410422
p->setPen( mPen );
411423

412424
Q_FOREACH( const QList<QgsPoint>& line, mPoints )
@@ -488,7 +500,7 @@ void QgsRubberBand::updateRect()
488500
return;
489501
}
490502
qreal s = ( mIconSize - 1 ) / 2;
491-
qreal p = mWidth;
503+
qreal p = mPen.width();
492504

493505
QgsRectangle r( it->x() + mTranslationOffsetX - s - p, it->y() + mTranslationOffsetY - s - p,
494506
it->x() + mTranslationOffsetX + s + p, it->y() + mTranslationOffsetY + s + p );

‎src/gui/qgsrubberband.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
100100
*/
101101
void setIconSize( int iconSize );
102102

103+
/**
104+
* Set the style of the line
105+
* @note Added in 1.9
106+
*/
107+
void setLineStyle( Qt::PenStyle penStyle );
108+
109+
/**
110+
* Set the style of the brush
111+
* @note Added in 1.9
112+
*/
113+
void setBrushStyle( Qt::BrushStyle brushStyle );
114+
103115
/**
104116
* Clears all the geometries in this rubberband.
105117
* Sets the representation type according to geometryType.
@@ -226,16 +238,13 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
226238
QBrush mBrush;
227239
QPen mPen;
228240

229-
/** The width of any line within the rubberband. */
230-
int mWidth;
231-
232241
/** The size of the icon for points.
233242
* @note Added in 1.9 */
234243
int mIconSize;
235244

236245
/** Icon to be shown.
237246
* @note Added in 1.9 */
238-
IconType mIconType ;
247+
IconType mIconType;
239248

240249
/**
241250
* Nested lists used for multitypes

0 commit comments

Comments
 (0)
Please sign in to comment.