Skip to content

Commit

Permalink
Applied patch from Jurgen Fischer with some modifications.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7235 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Sep 30, 2007
1 parent a030b22 commit e94e9f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
8 changes: 8 additions & 0 deletions python/gui/qgsrubberband.sip
Expand Up @@ -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<QgsPoint>& getPoints() const;
const QgsPoint& getPoint(int index) const;

protected:
virtual void paint(QPainter* p);

Expand Down
47 changes: 43 additions & 4 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -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();
Expand All @@ -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.
*/
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();
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/gui/qgsrubberband.h
Expand Up @@ -17,8 +17,8 @@
#define QGSRUBBERBAND_H

#include "qgsmapcanvasitem.h"
#include <deque>
#include <QBrush>
#include <QList>
#include <QPen>
#include <QPolygon>
class QPaintEvent;
Expand All @@ -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<QgsPoint>& getPoints() const;
const QgsPoint& getPoint(int index) const;

protected:
virtual void paint(QPainter* p);

Expand All @@ -50,7 +57,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
private:
QBrush mBrush;
QPen mPen;
std::deque<QgsPoint> mPoints;
QList<QgsPoint> mPoints;
bool mIsPolygon;
};

Expand Down

0 comments on commit e94e9f6

Please sign in to comment.