Applied patch from Jurgen Fischer with some modifications.

git-svn-id: http://svn.osgeo.org/qgis/trunk@7235 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
wonder 2007-09-30 23:45:36 +00:00
parent 5f1925f738
commit c5b914a0b6
3 changed files with 60 additions and 6 deletions

View File

@ -14,9 +14,17 @@ class QgsRubberBand: QgsMapCanvasItem
void reset(bool isPolygon = false); void reset(bool isPolygon = false);
void addPoint(const QgsPoint & p); void addPoint(const QgsPoint & p);
// ! Remove last point
void removePoint(bool update = true);
void movePoint(const QgsPoint & p); void movePoint(const QgsPoint & p);
void movePoint(int index, const QgsPoint& p); void movePoint(int index, const QgsPoint& p);
int size() const;
const QList<QgsPoint>& getPoints() const;
const QgsPoint& getPoint(int index) const;
protected: protected:
virtual void paint(QPainter* p); virtual void paint(QPainter* p);

View File

@ -56,7 +56,8 @@ void QgsRubberBand::setWidth(int width)
*/ */
void QgsRubberBand::reset(bool isPolygon) 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; mIsPolygon = isPolygon;
updateRect(); updateRect();
update(); 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<QgsPoint>& 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. Update the line between the last added point and the mouse position.
*/ */
void QgsRubberBand::movePoint(const QgsPoint & p) void QgsRubberBand::movePoint(const QgsPoint & p)
{ {
mPoints[mPoints.size()-1] = p; // Update current mouse position mPoints[ size()-1 ] = p; // Update current mouse position
updateRect(); updateRect();
update(); update();
} }
@ -101,7 +140,7 @@ void QgsRubberBand::paint(QPainter* p)
if (mPoints.size() > 1) if (mPoints.size() > 1)
{ {
QPolygonF pts; QPolygonF pts;
for (uint i = 0; i < mPoints.size(); i++) for (int i = 0; i < mPoints.size(); i++)
pts.append(toCanvasCoords(mPoints[i])-pos()); pts.append(toCanvasCoords(mPoints[i])-pos());
p->setPen(mPen); p->setPen(mPen);
@ -122,7 +161,7 @@ void QgsRubberBand::updateRect()
if (mPoints.size() > 0) if (mPoints.size() > 0)
{ {
QgsRect r(mPoints[0], mPoints[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()); r.combineExtentWith(mPoints[i].x(), mPoints[i].y());
setRect(r); setRect(r);
} }

View File

@ -17,8 +17,8 @@
#define QGSRUBBERBAND_H #define QGSRUBBERBAND_H
#include "qgsmapcanvasitem.h" #include "qgsmapcanvasitem.h"
#include <deque>
#include <QBrush> #include <QBrush>
#include <QList>
#include <QPen> #include <QPen>
#include <QPolygon> #include <QPolygon>
class QPaintEvent; class QPaintEvent;
@ -38,9 +38,16 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
//! If adding more points consider using update=false for better performance //! If adding more points consider using update=false for better performance
void addPoint(const QgsPoint & p, bool update = true); void addPoint(const QgsPoint & p, bool update = true);
// ! Remove last point
void removePoint(bool update = true);
void movePoint(const QgsPoint & p); void movePoint(const QgsPoint & p);
void movePoint(int index, const QgsPoint& p); void movePoint(int index, const QgsPoint& p);
int size() const;
const QList<QgsPoint>& getPoints() const;
const QgsPoint& getPoint(int index) const;
protected: protected:
virtual void paint(QPainter* p); virtual void paint(QPainter* p);
@ -50,7 +57,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
private: private:
QBrush mBrush; QBrush mBrush;
QPen mPen; QPen mPen;
std::deque<QgsPoint> mPoints; QList<QgsPoint> mPoints;
bool mIsPolygon; bool mIsPolygon;
}; };