diff --git a/python/gui/qgsrubberband.sip b/python/gui/qgsrubberband.sip index a7c8d45ad3d..4a4559b2af2 100644 --- a/python/gui/qgsrubberband.sip +++ b/python/gui/qgsrubberband.sip @@ -14,9 +14,17 @@ class QgsRubberBand: QgsMapCanvasItem void reset(bool isPolygon = false); void addPoint(const QgsPoint & p); + + // ! Remove last point + void removePoint(bool update = true); + void movePoint(const QgsPoint & p); void movePoint(int index, const QgsPoint& p); + int size() const; + const QList& getPoints() const; + const QgsPoint& getPoint(int index) const; + protected: virtual void paint(QPainter* p); diff --git a/src/gui/qgsrubberband.cpp b/src/gui/qgsrubberband.cpp index b4fd2748e0e..9b792f5f015 100644 --- a/src/gui/qgsrubberband.cpp +++ b/src/gui/qgsrubberband.cpp @@ -56,7 +56,8 @@ void QgsRubberBand::setWidth(int width) */ void QgsRubberBand::reset(bool isPolygon) { - mPoints.resize(1); // addPoint assumes an initial allocated point + mPoints.clear(); + mPoints.append(QgsPoint()); // addPoint assumes an initial allocated point mIsPolygon = isPolygon; updateRect(); update(); @@ -76,12 +77,50 @@ void QgsRubberBand::addPoint(const QgsPoint & p, bool do_update /* = true */) } } +/*! + Remove a point + */ +void QgsRubberBand::removePoint(bool do_update) +{ + mPoints.pop_back(); + if(do_update) + { + updateRect(); + update(); + } +} + +/*! + Return number of points + */ +int QgsRubberBand::size() const +{ + return mPoints.size(); +} + +/*! + Return the points + */ +const QList& QgsRubberBand::getPoints() const +{ + return mPoints; +} + +/*! + Return a point + */ +const QgsPoint& QgsRubberBand::getPoint(int index) const +{ + return mPoints[index]; +} + + /*! Update the line between the last added point and the mouse position. */ void QgsRubberBand::movePoint(const QgsPoint & p) { - mPoints[mPoints.size()-1] = p; // Update current mouse position + mPoints[ size()-1 ] = p; // Update current mouse position updateRect(); update(); } @@ -101,7 +140,7 @@ void QgsRubberBand::paint(QPainter* p) if (mPoints.size() > 1) { QPolygonF pts; - for (uint i = 0; i < mPoints.size(); i++) + for (int i = 0; i < mPoints.size(); i++) pts.append(toCanvasCoords(mPoints[i])-pos()); p->setPen(mPen); @@ -122,7 +161,7 @@ void QgsRubberBand::updateRect() if (mPoints.size() > 0) { QgsRect r(mPoints[0], mPoints[0]); - for (uint i = 1; i < mPoints.size(); i++) + for (int i = 1; i < mPoints.size(); i++) r.combineExtentWith(mPoints[i].x(), mPoints[i].y()); setRect(r); } diff --git a/src/gui/qgsrubberband.h b/src/gui/qgsrubberband.h index c1e19f3e504..c24aa082371 100644 --- a/src/gui/qgsrubberband.h +++ b/src/gui/qgsrubberband.h @@ -17,8 +17,8 @@ #define QGSRUBBERBAND_H #include "qgsmapcanvasitem.h" -#include #include +#include #include #include class QPaintEvent; @@ -38,9 +38,16 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem //! If adding more points consider using update=false for better performance void addPoint(const QgsPoint & p, bool update = true); + // ! Remove last point + void removePoint(bool update = true); + void movePoint(const QgsPoint & p); void movePoint(int index, const QgsPoint& p); + int size() const; + const QList& getPoints() const; + const QgsPoint& getPoint(int index) const; + protected: virtual void paint(QPainter* p); @@ -50,7 +57,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem private: QBrush mBrush; QPen mPen; - std::deque mPoints; + QList mPoints; bool mIsPolygon; };