133 changes: 92 additions & 41 deletions src/gui/qgsrubberband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@
\brief The QgsRubberBand class provides a transparent overlay widget
for tracking the mouse while drawing polylines or polygons.
*/
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon )
QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, QGis::GeometryType geometryType )
: QgsMapCanvasItem( mapCanvas )
, mIsPolygon( isPolygon )
, mGeometryType( geometryType )
, mTranslationOffsetX( 0.0 )
, mTranslationOffsetY( 0.0 )
{
reset( isPolygon );
reset( geometryType );
setColor( QColor( Qt::lightGray ) );
}

QgsRubberBand::QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon )
: QgsMapCanvasItem( mapCanvas )
{
QgsRubberBand( mapCanvas, isPolygon ? QGis::Polygon : QGis::Line );
}

QgsRubberBand::QgsRubberBand(): QgsMapCanvasItem( 0 )
{
}
Expand All @@ -61,16 +67,24 @@ void QgsRubberBand::setColor( const QColor & color )
*/
void QgsRubberBand::setWidth( int width )
{
mPen.setWidth( width );
mWidth = width;
}

/*!
Remove all points from the shape being created.
*/
void QgsRubberBand::reset( QGis::GeometryType geometryType )
{
mPoints.clear();
mGeometryType = geometryType;
updateRect();
update();
}

void QgsRubberBand::reset( bool isPolygon )
{
mPoints.clear();
mIsPolygon = isPolygon;
mGeometryType = isPolygon ? QGis::Polygon : QGis::Line;
updateRect();
update();
}
Expand Down Expand Up @@ -170,7 +184,7 @@ void QgsRubberBand::movePoint( int index, const QgsPoint& p, int geometryIndex )

void QgsRubberBand::setToGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
{
reset( mIsPolygon );
reset( mGeometryType );
addGeometry( geom, layer );
}

Expand All @@ -196,7 +210,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBPoint:
case QGis::WKBPoint25D:
{
mIsPolygon = true;
double d = mMapCanvas->extent().width() * 0.005;
QgsPoint pt;
if ( layer )
Expand All @@ -217,7 +230,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBMultiPoint:
case QGis::WKBMultiPoint25D:
{
mIsPolygon = true;
double d = mMapCanvas->extent().width() * 0.005;
QgsMultiPoint mpt = geom->asMultiPoint();
for ( int i = 0; i < mpt.size(); ++i, ++idx )
Expand All @@ -244,7 +256,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBLineString:
case QGis::WKBLineString25D:
{
mIsPolygon = false;
QgsPolyline line = geom->asPolyline();
for ( int i = 0; i < line.count(); i++ )
{
Expand All @@ -263,7 +274,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBMultiLineString:
case QGis::WKBMultiLineString25D:
{
mIsPolygon = false;
mPoints.clear();

QgsMultiPolyline mline = geom->asMultiPolyline();
Expand Down Expand Up @@ -294,7 +304,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBPolygon:
case QGis::WKBPolygon25D:
{
mIsPolygon = true;
QgsPolygon poly = geom->asPolygon();
QgsPolyline line = poly[0];
for ( int i = 0; i < line.count(); i++ )
Expand All @@ -314,7 +323,6 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
case QGis::WKBMultiPolygon:
case QGis::WKBMultiPolygon25D:
{
mIsPolygon = true;
mPoints.clear();

QgsMultiPolygon multipoly = geom->asMultiPolygon();
Expand Down Expand Up @@ -357,7 +365,7 @@ void QgsRubberBand::setToCanvasRectangle( const QRect& rect )
QgsPoint ll = transform->toMapCoordinates( rect.left(), rect.bottom() );
QgsPoint ur = transform->toMapCoordinates( rect.right(), rect.top() );

reset( true );
reset( QGis::Polygon );
addPoint( ll, false );
addPoint( QgsPoint( ur.x(), ll.y() ), false );
addPoint( ur, false );
Expand All @@ -372,25 +380,47 @@ void QgsRubberBand::paint( QPainter* p )
QList<QgsPoint> currentList;
if ( mPoints.size() > 0 )
{
p->setPen( mPen );
p->setBrush( mBrush );

for ( int i = 0; i < mPoints.size(); ++i )
{
QPolygonF pts;
QVector<QPointF> pts;
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin();
for ( ; it != mPoints.at( i ).constEnd(); ++it )
{
pts.append( toCanvasCoordinates( QgsPoint( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY ) ) - pos() );
}

if ( mIsPolygon )
switch ( mGeometryType )
{
p->drawPolygon( pts );
}
else
{
p->drawPolyline( pts );
case QGis::Polygon:
{
mPen.setWidth( mWidth );
p->setPen( mPen );
p->drawPolygon( pts );
}
break;

case QGis::Point:
{
mPen.setWidth( 1 );
p->setPen( mPen );
QVector<QPointF>::const_iterator ptIt = pts.constBegin();
for( ; ptIt != pts.constEnd(); ++ptIt )
{
p->drawEllipse( (*ptIt).x() - mWidth/2, (*ptIt).y() - mWidth/2, mWidth, mWidth );
}
}
break;

case QGis::Line:
default:
{
mPen.setWidth( mWidth );
p->setPen( mPen );
p->drawPolyline( pts );
}
break;
}
}
}
Expand Down Expand Up @@ -464,34 +494,55 @@ const QgsPoint *QgsRubberBand::getPoint( int i, int j ) const
QgsGeometry *QgsRubberBand::asGeometry()
{
QgsGeometry *geom = NULL;
if ( mIsPolygon )

switch( mGeometryType )
{
QgsPolygon polygon;
QList< QList<QgsPoint> >::const_iterator it = mPoints.constBegin();
for ( ; it != mPoints.constEnd(); ++it )
case QGis::Polygon:
{
polygon.append( getPolyline( *it ) );
QgsPolygon polygon;
QList< QList<QgsPoint> >::const_iterator it = mPoints.constBegin();
for ( ; it != mPoints.constEnd(); ++it )
{
polygon.append( getPolyline( *it ) );
}
geom = QgsGeometry::fromPolygon( polygon );
break;
}
geom = QgsGeometry::fromPolygon( polygon );
}
else
{
if ( mPoints.size() > 0 )

case QGis::Point:
{
if ( mPoints.size() > 1 )
QgsMultiPoint multiPoint;

QList< QList<QgsPoint> >::const_iterator it = mPoints.constBegin();
for ( ; it != mPoints.constEnd(); ++it )
{
QgsMultiPolyline multiPolyline;
QList< QList<QgsPoint> >::const_iterator it = mPoints.constBegin();
for ( ; it != mPoints.constEnd(); ++it )
{
multiPolyline.append( getPolyline( *it ) );
}
geom = QgsGeometry::fromMultiPolyline( multiPolyline );
multiPoint += getPolyline( *it );
}
else
geom = QgsGeometry::fromMultiPoint( multiPoint );
break;
}

case QGis::Line:
default:
{
if ( mPoints.size() > 0 )
{
geom = QgsGeometry::fromPolyline( getPolyline( mPoints[0] ) );
if ( mPoints.size() > 1 )
{
QgsMultiPolyline multiPolyline;
QList< QList<QgsPoint> >::const_iterator it = mPoints.constBegin();
for ( ; it != mPoints.constEnd(); ++it )
{
multiPolyline.append( getPolyline( *it ) );
}
geom = QgsGeometry::fromMultiPolyline( multiPolyline );
}
else
{
geom = QgsGeometry::fromPolyline( getPolyline( mPoints[0] ) );
}
}
break;
}
}
return geom;
Expand Down
34 changes: 31 additions & 3 deletions src/gui/qgsrubberband.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,39 @@ class QPaintEvent;
class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
{
public:
QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon = false );
/**
* Creates a new RubberBand.
* @param mapCanvas The map canvas to draw onto. It's CRS will be used map points onto screen coordinates.
* @param geometryType Defines how the data should be drawn onto the screen. (Use QGis::Line, QGis::Polygon or QGis::Point)
* Added in 1.9.
*/
QgsRubberBand( QgsMapCanvas* mapCanvas, QGis::GeometryType geometryType = QGis::Line );
/**
* Creates a new RubberBand.
* @deprecated
* @param mapCanvas The map canvas to draw onto. It's CRS will be used map points onto screen coordinates.
* @param isPolygon true: draw as (multi-)polygon, false draw as (multi-)linestring
*/
QgsRubberBand( QgsMapCanvas* mapCanvas, bool isPolygon );
~QgsRubberBand();

void setColor( const QColor & color );
void setWidth( int width );

void reset( bool isPolygon = false );
/**
* Clears all the geometries in this rubberband.
* Sets the representation type according to geometryType.
* @param geometryType Defines how the data should be drawn onto the screen. (Use QGis::Line, QGis::Polygon or QGis::Point)
* Added in 1.9.
*/
void reset( QGis::GeometryType geometryType = QGis::Line );
/**
* @deprecated
* Clears all the geometries in this rubberband.
* Sets the representation type according to isPolygon.
* @param isPolygon true: draw as (multi-)polygon, false draw as (multi-)linestring
*/
void reset( bool isPolygon );

//! Add point to rubberband and update canvas
//! If adding more points consider using update=false for better performance
Expand Down Expand Up @@ -101,9 +127,11 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
QBrush mBrush;
QPen mPen;

int mWidth;

/**Nested lists used for multitypes*/
QList< QList <QgsPoint> > mPoints;
bool mIsPolygon;
QGis::GeometryType mGeometryType;
double mTranslationOffsetX;
double mTranslationOffsetY;

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/coordinate_capture/coordinatecapturemaptool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CoordinateCaptureMapTool::CoordinateCaptureMapTool( QgsMapCanvas* thepCanvas )
QPixmap myCursor = QPixmap(( const char ** ) capture_point_cursor );
mCursor = QCursor( myCursor, 8, 8 ); //8,8 is the point in the cursor where clicks register
mpMapCanvas = thepCanvas;
mpRubberBand = new QgsRubberBand( mpMapCanvas, true ); //true - its a polygon
mpRubberBand = new QgsRubberBand( mpMapCanvas, QGis::Polygon );
mpRubberBand->setColor( Qt::red );
mpRubberBand->setWidth( 1 );
}
Expand Down Expand Up @@ -78,7 +78,7 @@ void CoordinateCaptureMapTool::canvasReleaseEvent( QMouseEvent * thepEvent )
QgsPoint myPoint4 =
mCanvas->getCoordinateTransform()->toMapCoordinates( thepEvent->x() - 1, thepEvent->y() + 1 );

mpRubberBand->reset( true );
mpRubberBand->reset( QGis::Polygon );
// convert screen coordinates to map coordinates
mpRubberBand->addPoint( myPoint1, false ); //true - update canvas
mpRubberBand->addPoint( myPoint2, false ); //true - update canvas
Expand All @@ -90,6 +90,6 @@ void CoordinateCaptureMapTool::canvasReleaseEvent( QMouseEvent * thepEvent )

void CoordinateCaptureMapTool::deactivate()
{
mpRubberBand->reset( false );
mpRubberBand->reset( QGis::Line );
QgsMapTool::deactivate();
}
18 changes: 9 additions & 9 deletions src/plugins/roadgraph/shortestpathwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ RgShortestPathWidget::RgShortestPathWidget( QWidget* theParent, RoadGraphPlugin
connect( mCalculate, SIGNAL( clicked( bool ) ), this, SLOT( findingPath() ) );
connect( mClear, SIGNAL( clicked( bool ) ), this, SLOT( clear() ) );

mrbFrontPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), true );
mrbFrontPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), QGis::Polygon );
mrbFrontPoint->setColor( Qt::green );
mrbFrontPoint->setWidth( 2 );

mrbBackPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), true );
mrbBackPoint = new QgsRubberBand( mPlugin->iface()->mapCanvas(), QGis::Polygon );
mrbBackPoint->setColor( Qt::red );
mrbBackPoint->setWidth( 2 );

mrbPath = new QgsRubberBand( mPlugin->iface()->mapCanvas(), false );
mrbPath = new QgsRubberBand( mPlugin->iface()->mapCanvas(), QGis::Line );
mrbPath->setWidth( 2 );

connect( mPlugin->iface()->mapCanvas(), SIGNAL( extentsChanged() ), this, SLOT( mapCanvasExtentsChanged() ) );
Expand Down Expand Up @@ -199,7 +199,7 @@ void RgShortestPathWidget::setFrontPoint( const QgsPoint& pt )

double mupp = mPlugin->iface()->mapCanvas()->getCoordinateTransform()->mapUnitsPerPixel() * 2;

mrbFrontPoint->reset( true );
mrbFrontPoint->reset( QGis::Polygon );
mrbFrontPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() - mupp ), false );
mrbFrontPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() - mupp ), false );
mrbFrontPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() + mupp ), false );
Expand All @@ -222,7 +222,7 @@ void RgShortestPathWidget::setBackPoint( const QgsPoint& pt )

double mupp = mPlugin->iface()->mapCanvas()->getCoordinateTransform()->mapUnitsPerPixel() * 2;

mrbBackPoint->reset( true );
mrbBackPoint->reset( QGis::Polygon );
mrbBackPoint->addPoint( QgsPoint( pt.x() - mupp, pt.y() - mupp ), false );
mrbBackPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() - mupp ), false );
mrbBackPoint->addPoint( QgsPoint( pt.x() + mupp, pt.y() + mupp ), false );
Expand Down Expand Up @@ -306,7 +306,7 @@ void RgShortestPathWidget::findingPath()
if ( path == NULL )
return;

mrbPath->reset( false );
mrbPath->reset( QGis::Line );
double time = 0.0;
double cost = 0.0;

Expand Down Expand Up @@ -348,10 +348,10 @@ void RgShortestPathWidget::findingPath()
void RgShortestPathWidget::clear()
{
mFrontPointLineEdit->setText( QString() );
mrbFrontPoint->reset( true );
mrbFrontPoint->reset( QGis::Polygon );
mBackPointLineEdit->setText( QString() );
mrbBackPoint->reset( true );
mrbPath->reset();
mrbBackPoint->reset( QGis::Polygon );
mrbPath->reset( QGis::Line );
mPathCostLineEdit->setText( QString() );
mPathTimeLineEdit->setText( QString() );
}
Expand Down
14 changes: 6 additions & 8 deletions src/plugins/spatialquery/qgsrubberselectid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

QgsRubberSelectId::QgsRubberSelectId( QgsMapCanvas* mapCanvas )
{
mIsPolygon = false;
mGeometryType = QGis::Line;
mMapCanvas = mapCanvas;
mRubberBand = new QgsRubberBand( mMapCanvas, mIsPolygon );
mRubberBand = new QgsRubberBand( mMapCanvas, mGeometryType );
mColorRGB[0] = 255;
mColorRGB[1] = 0;
mColorRGB[2] = 0;
Expand All @@ -42,7 +42,7 @@ QgsRubberSelectId::~QgsRubberSelectId()

void QgsRubberSelectId::reset()
{
mRubberBand->reset( mIsPolygon );
mRubberBand->reset( mGeometryType );
} // void QgsRubberSelectId::reset()

void QgsRubberSelectId::setStyle( int colorRed, int colorGreen, int colorBlue, int width )
Expand All @@ -56,13 +56,11 @@ void QgsRubberSelectId::setStyle( int colorRed, int colorGreen, int colorBlue, i

void QgsRubberSelectId::addFeature( QgsVectorLayer* lyr, QgsFeatureId fid )
{
bool isPolygon = ( lyr->geometryType() == QGis::Polygon );
if ( mIsPolygon != isPolygon )
if ( mGeometryType != lyr->geometryType() )
{
reset();
delete mRubberBand;
mIsPolygon = isPolygon;
mRubberBand = new QgsRubberBand( mMapCanvas, mIsPolygon );
mGeometryType = lyr->geometryType();
mRubberBand->reset( lyr->geometryType() );
setStyle();
}
QgsFeature feat;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/spatialquery/qgsrubberselectid.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class QgsRubberSelectId
QgsRubberBand* mRubberBand;
int mColorRGB[3];
int mWidth;
bool mIsPolygon;
QGis::GeometryType mGeometryType;
QgsMapCanvas* mMapCanvas;
};

Expand Down