Skip to content

Commit

Permalink
Add QgsRubberBand class to display measurement lines for QgsMeasure.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4627 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
telwertowski committed Jan 9, 2006
1 parent fb8e968 commit 91c51cd
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 150 deletions.
2 changes: 2 additions & 0 deletions src/gui/Makefile.am
Expand Up @@ -125,6 +125,7 @@ libqgis_guiHEADERS = \
qgsprojectproperties.h \
qgsrangerenderitem.h \
qgsrasterlayerproperties.h \
qgsrubberband.h \
qgsrunprocess.h \
qgsserversourceselect.h \
qgssisydialog.h \
Expand Down Expand Up @@ -235,6 +236,7 @@ libqgis_gui_la_SOURCES = \
qgsproject.cpp \
qgsprojectproperties.cpp \
qgsrasterlayerproperties.cpp \
qgsrubberband.cpp \
qgsrunprocess.cpp \
qgsserversourceselect.cpp \
qgssinglesymrenderer.cpp \
Expand Down
161 changes: 29 additions & 132 deletions src/gui/qgsmeasure.cpp
Expand Up @@ -13,11 +13,15 @@
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */

#include "qgsmeasure.h"

#include "qgscontexthelp.h"
#include "qgsdistancearea.h"
#include "qgsmapcanvas.h"
#include "qgsmaptopixel.h"
#include "qgsrubberband.h"

#include <QSettings>
#include <iostream>
Expand All @@ -27,14 +31,11 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, QWidget *parent, cons
: QWidget(parent, name, f)
{
setupUi(this);
connect(btnHelp, SIGNAL(clicked()), this, SLOT(showHelp()));
connect(mRestartButton, SIGNAL(clicked()), this, SLOT(restart()));
connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));

mMeasureArea = measureArea;
mMapCanvas = mc;
mDynamic = false;
mPixmap = mMapCanvas->canvasPixmap();
mTotal = 0.;

mTable->setLeftMargin(0); // hide row labels
Expand All @@ -58,45 +59,40 @@ QgsMeasure::QgsMeasure(bool measureArea, QgsMapCanvas *mc, QWidget *parent, cons

updateUi();

connect ( mMapCanvas, SIGNAL(renderComplete(QPainter*)), this, SLOT(draw(QPainter*)) );
connect( mMapCanvas, SIGNAL(renderComplete(QPainter*)), this, SLOT(mapCanvasChanged()) );
restorePosition();

mCalc = new QgsDistanceArea;


mRubberBand = new QgsRubberBand(mMapCanvas, mMeasureArea);
mRubberBand->show();
}


void QgsMeasure::setMeasureArea(bool measureArea)
{
saveWindowLocation();
restart();
mMeasureArea = measureArea;
updateUi();
restart();
restorePosition();
}


QgsMeasure::~QgsMeasure()
{
delete mCalc;
delete mRubberBand;
}

void QgsMeasure::restart(void )
{
// Delete old line
drawLine();

mPoints.resize(0);
mTable->setNumRows(0);
mTotal = 0.;

updateUi();

if ( mDynamic ) {
drawDynamicLine();
mDynamic = false;
}


mRubberBand->reset(mMeasureArea);
}

void QgsMeasure::addPoint(QgsPoint &point)
Expand All @@ -105,15 +101,6 @@ void QgsMeasure::addPoint(QgsPoint &point)
std::cout << "QgsMeasure::addPoint" << point.x() << ", " << point.y() << std::endl;
#endif

// Delete dynamic
if ( mDynamic ) {
drawDynamicLine();
mDynamic = false;
}

// Delete old line
drawLine(true);

// don't add points with the same coordinates
if (mPoints.size() > 0 && point == mPoints[0])
return;
Expand Down Expand Up @@ -153,8 +140,9 @@ void QgsMeasure::addPoint(QgsPoint &point)
mTable->ensureCellVisible(row,0);
}

// Draw new line
drawLine();
QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
QgsPoint ppnt = trans->transform(point);
mRubberBand->addPoint(QPoint(int(ppnt.x()), int(ppnt.y())));
}

void QgsMeasure::mousePress(QgsPoint &point)
Expand All @@ -166,29 +154,6 @@ void QgsMeasure::mousePress(QgsPoint &point)
}

mouseMove(point);

if (mMeasureArea && mPoints.size() > 2)
{
// delete old line which connect 1. and last point

// TODO: Qt4 uses "QRubberBand"s - need to refactor.
#if QT_VERSION < 0x040000
QPainter p;
p.begin(mPixmap);
QPen pen(Qt::gray, 2);
p.setPen(pen);
p.setRasterOp(Qt::XorROP);

QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
QgsPoint ppnt = trans->transform(mPoints[mPoints.size()-1]);
p.moveTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
ppnt = trans->transform(mPoints[0]);
p.lineTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
p.end();
#endif
mMapCanvas->repaint(false);
}

}

void QgsMeasure::mouseMove(QgsPoint &point)
Expand All @@ -197,92 +162,24 @@ void QgsMeasure::mouseMove(QgsPoint &point)
//std::cout << "QgsMeasure::mouseMove" << point.x() << ", " << point.y() << std::endl;
#endif

if ( mDynamic ) {
drawDynamicLine(); // delete old
}

if ( mPoints.size() > 0 ) {
mDynamicPoints[0] = mPoints[mPoints.size()-1];
mDynamicPoints[1] = point;
drawDynamicLine();
mDynamic = true;
}
QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
QgsPoint ppnt = trans->transform(point);
mRubberBand->movePoint(QPoint(int(ppnt.x()), int(ppnt.y())));
}

void QgsMeasure::draw(QPainter *p)
void QgsMeasure::mapCanvasChanged()
{
#ifdef QGISDEBUG
std::cout << "QgsMeasure::draw" << std::endl;
std::cout << "QgsMeasure::mapCanvasChanged" << std::endl;
#endif

drawLine();
mDynamic = false;
}

void QgsMeasure::drawLine(bool erase)
{
#ifdef QGISDEBUG
std::cout << "QgsMeasure::drawLine" << std::endl;
#endif

// TODO: Qt4 uses "QRubberBand"s - need to refactor.
#if QT_VERSION < 0x040000
QPainter p;
p.begin(mPixmap);
QPen pen(Qt::gray, 2);
p.setPen(pen);
p.setRasterOp(Qt::XorROP);

QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
for ( int i = 0; i < mPoints.size(); i++ ) {
QgsPoint ppnt = trans->transform(mPoints[i]);
if ( i == 0 ) {
p.moveTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
} else {
p.lineTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
}
}

if (!erase && mMeasureArea && mPoints.size() > 2) // draw the last point of the polygon
{
QgsPoint ppnt = trans->transform(mPoints[0]);
p.lineTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
}

p.end();
#endif
mMapCanvas->repaint(false);
}

void QgsMeasure::drawDynamicLine( void )
{
#ifdef QGISDEBUG
//std::cout << "QgsMeasure::drawDynamicLine" << std::endl;
#endif

// TODO: Qt4 uses "QRubberBand"s and "QPainterPath"s - need to refactor.
#if QT_VERSION < 0x040000
QPainter p;
p.begin(mPixmap);
QPen pen(Qt::gray, 2);
p.setPen(pen);
p.setRasterOp(Qt::XorROP);

QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
QgsPoint ppnt = trans->transform(mDynamicPoints[0]);
p.moveTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
QgsPoint ppnt2 = trans->transform(mDynamicPoints[1]);
p.lineTo(static_cast<int>(ppnt2.x()), static_cast<int>(ppnt2.y()));

if (mMeasureArea && mPoints.size() >= 2)
{
ppnt = trans->transform(mPoints[0]);
p.lineTo(static_cast<int>(ppnt.x()), static_cast<int>(ppnt.y()));
}

p.end();
#endif
mMapCanvas->repaint(false);
mRubberBand->setGeometry(mMapCanvas->rect());
mRubberBand->reset(mMeasureArea);
QgsMapToPixel *trans = mMapCanvas->getCoordinateTransform();
for (std::vector<QgsPoint>::iterator it = mPoints.begin(); it != mPoints.end(); ++it)
{
QgsPoint ppnt = trans->transform(*it);
mRubberBand->addPoint(QPoint(int(ppnt.x()), int(ppnt.y())));
}
}

void QgsMeasure::close(void)
Expand Down Expand Up @@ -331,7 +228,7 @@ void QgsMeasure::saveWindowLocation()
settings.writeEntry("/Windows/Measure/h", s.height());
}

void QgsMeasure::showHelp()
void QgsMeasure::on_btnHelp_clicked()
{
QgsContextHelp::run(context_id);
}
Expand Down
24 changes: 6 additions & 18 deletions src/gui/qgsmeasure.h
Expand Up @@ -23,10 +23,9 @@

class QgsDistanceArea;
class QgsMapCanvas;
class QgsRubberBand;

class QCloseEvent;
class QPainter;
class QPixmap;


class QgsMeasure:public QWidget, private Ui::QgsMeasureBase
Expand Down Expand Up @@ -69,11 +68,11 @@ public slots:
//! Close event
void closeEvent(QCloseEvent *e);

//! Connected to canvas renderComplete
void draw(QPainter *);
//! Redraw lines to match current state of canvas
void mapCanvasChanged();

//! Show the help for the dialog
void showHelp();
void on_btnHelp_clicked();

private:

Expand All @@ -87,8 +86,6 @@ public slots:
void updateUi();

QgsMapCanvas *mMapCanvas;

QPixmap *mPixmap;

//! distance/area calculator
QgsDistanceArea* mCalc;
Expand All @@ -97,17 +94,8 @@ public slots:

double mTotal;

//! Dynamic line from last point to current position was drawn
bool mDynamic;

//! Dynamic line
QgsPoint mDynamicPoints[2];

//! Draw current points with XOR
void drawLine(bool erase = false);

//! Draw current dynamic line
void drawDynamicLine(void);
//! Rubberband widget tracking the lines being drawn
QgsRubberBand *mRubberBand;

//! Help context id
static const int context_id = 940759457;
Expand Down

0 comments on commit 91c51cd

Please sign in to comment.