Skip to content

Commit 8b74b9e

Browse files
committedAug 29, 2012
Cleaned up a bit, fixed area dialog also
1 parent 54133ec commit 8b74b9e

File tree

2 files changed

+49
-66
lines changed

2 files changed

+49
-66
lines changed
 

‎src/app/qgsmeasuredialog.cpp

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ QgsMeasureDialog::QgsMeasureDialog( QgsMeasureTool* tool, Qt::WFlags f )
5555
// but at least every time any canvas related settings changes
5656
connect( mTool->canvas(), SIGNAL( mapCanvasRefreshed() ),
5757
this, SLOT( updateSettings() ) );
58-
// // Update when project wide transformation has changed
59-
// connect( mTool->canvas()->mapRenderer(), SIGNAL( hasCrsTransformEnabled( bool ) ),
60-
// this, SLOT( changeProjectionEnabledState() ) );
61-
// // Update when project CRS has changed
62-
// connect( mTool->canvas()->mapRenderer(), SIGNAL( destinationSrsChanged() ),
63-
// this, SLOT( changeProjectionEnabledState() ) );
6458

6559
updateSettings();
6660
}
@@ -113,9 +107,7 @@ void QgsMeasureDialog::updateSettings()
113107

114108
// clear interface
115109
mTable->clear();
116-
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
117-
item->setTextAlignment( 0, Qt::AlignRight );
118-
mTable->addTopLevelItem( item );
110+
119111
mTotal = 0;
120112
updateUi();
121113

@@ -125,24 +117,16 @@ void QgsMeasureDialog::restart()
125117
{
126118
mTool->restart();
127119

128-
// Set one cell row where to update current distance
129-
// If measuring area, the table doesn't get shown
130120
mTable->clear();
131-
QTreeWidgetItem* item = new QTreeWidgetItem( QStringList( QString::number( 0, 'f', 1 ) ) );
132-
item->setTextAlignment( 0, Qt::AlignRight );
133-
mTable->addTopLevelItem( item );
134121
mTotal = 0.;
135122
updateUi();
136123
}
137124

138125

139126
void QgsMeasureDialog::mousePress( QgsPoint &point )
140127
{
141-
if ( mTool->points().size() == 0 )
142-
{
143-
addPoint( point );
144-
show();
145-
}
128+
129+
show();
146130
raise();
147131
if ( ! mTool->done() )
148132
{
@@ -152,25 +136,31 @@ void QgsMeasureDialog::mousePress( QgsPoint &point )
152136

153137
void QgsMeasureDialog::mouseMove( QgsPoint &point )
154138
{
139+
Q_UNUSED( point );
140+
155141
// show current distance/area while moving the point
156142
// by creating a temporary copy of point array
157143
// and adding moving point at the end
158-
if ( mMeasureArea && mTool->points().size() > 1 )
144+
if ( mMeasureArea && mTool->points().size() > 2 )
159145
{
160-
QList<QgsPoint> tmpPoints = mTool->points();
161-
tmpPoints.append( point );
162-
double area = mDa.measurePolygon( tmpPoints );
146+
double area = mDa.measurePolygon( mTool->points() );
163147
editTotal->setText( formatArea( area ) );
164148
}
165-
else if ( !mMeasureArea && mTool->points().size() > 0 )
149+
else if ( !mMeasureArea && mTool->points().size() > 1 )
166150
{
167-
QgsPoint p1( mTool->points().last() ), p2( point );
168-
151+
int last = mTool->points().size() - 1;
152+
QgsPoint p1 = mTool->points()[last];
153+
QgsPoint p2 = mTool->points()[last-1];
169154
double d = mDa.measureLine( p1, p2 );
170-
editTotal->setText( formatDistance( mTotal + d ) );
171-
QGis::UnitType myDisplayUnits;
172-
// Ignore units
173-
convertMeasurement( d, myDisplayUnits, false );
155+
156+
mTotal = mDa.measureLine( mTool->points() );
157+
editTotal->setText( formatDistance( mTotal ) );
158+
159+
QGis::UnitType displayUnits;
160+
// Meters or feet?
161+
convertMeasurement( d, displayUnits, false );
162+
163+
// Set moving
174164
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
175165
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
176166
QgsDebugMsg( QString( "Final result is %1" ).arg( item->text( 0 ) ) );
@@ -181,6 +171,7 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
181171
{
182172
Q_UNUSED( p );
183173

174+
QgsDebugMsg( "Entering" );
184175
int numPoints = mTool->points().size();
185176
if ( mMeasureArea && numPoints > 2 )
186177
{
@@ -189,27 +180,12 @@ void QgsMeasureDialog::addPoint( QgsPoint &p )
189180
}
190181
else if ( !mMeasureArea && numPoints > 1 )
191182
{
192-
int last = numPoints - 2;
193-
194-
QgsPoint p1 = mTool->points()[last], p2 = mTool->points()[last+1];
195-
196-
double d = mDa.measureLine( p1, p2 );
197-
198-
mTotal += d;
199-
editTotal->setText( formatDistance( mTotal ) );
200-
201-
QGis::UnitType myDisplayUnits;
202-
// Ignore units
203-
convertMeasurement( d, myDisplayUnits, false );
204-
205-
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
206-
item->setText( 0, QLocale::system().toString( d, 'f' ) );
207-
208-
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f' ) ) );
183+
QTreeWidgetItem * item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
209184
item->setTextAlignment( 0, Qt::AlignRight );
210185
mTable->addTopLevelItem( item );
211186
mTable->scrollToItem( item );
212187
}
188+
QgsDebugMsg( "Exiting" );
213189
}
214190

215191
void QgsMeasureDialog::on_buttonBox_rejected( void )
@@ -311,6 +287,7 @@ void QgsMeasureDialog::updateUi()
311287
{
312288
area = mDa.measurePolygon( mTool->points() );
313289
}
290+
mTable->hide(); // Hide the table, only show summary.
314291
editTotal->setText( formatArea( area ) );
315292
}
316293
else
@@ -326,22 +303,20 @@ void QgsMeasureDialog::updateUi()
326303
if ( !b )
327304
{
328305
double d = mDa.measureLine( p1, p2 );
329-
mTotal += d;
330-
editTotal->setText( formatDistance( mTotal ) );
331-
QGis::UnitType myDisplayUnits;
332-
333-
convertMeasurement( d, myDisplayUnits, false );
306+
QGis::UnitType dummyUnits;
307+
convertMeasurement( d, dummyUnits, false );
334308

335-
QTreeWidgetItem *item = mTable->topLevelItem( mTable->topLevelItemCount() - 1 );
336-
item->setText( 0, QLocale::system().toString( d, 'f', mDecimalPlaces ) );
337-
item = new QTreeWidgetItem( QStringList( QLocale::system().toString( 0.0, 'f', mDecimalPlaces ) ) );
309+
QTreeWidgetItem *item = new QTreeWidgetItem( QStringList( QLocale::system().toString( d , 'f', mDecimalPlaces ) ) );
338310
item->setTextAlignment( 0, Qt::AlignRight );
339311
mTable->addTopLevelItem( item );
340312
mTable->scrollToItem( item );
341313
}
342314
p1 = p2;
343315
b = false;
344316
}
317+
mTotal = mDa.measureLine( mTool->points() );
318+
mTable->show(); // Show the table with items
319+
editTotal->setText( formatDistance( mTotal ) );
345320
}
346321
}
347322

‎src/app/qgsmeasuretool.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ QgsMeasureTool::QgsMeasureTool( QgsMapCanvas* canvas, bool measureArea )
4141
mCursor = QCursor( myCrossHairQPixmap, 8, 8 );
4242

4343
mDone = false;
44+
// Append point we will move
45+
mPoints.append( QgsPoint (0, 0) );
4446

4547
mDialog = new QgsMeasureDialog( this );
4648
mSnapper.setMapCanvas( canvas );
@@ -96,6 +98,9 @@ void QgsMeasureTool::deactivate()
9698
void QgsMeasureTool::restart()
9799
{
98100
mPoints.clear();
101+
// Append point we will move
102+
mPoints.append( QgsPoint (0, 0) );
103+
99104
mRubberBand->reset( mMeasureArea );
100105

101106
// re-read settings
@@ -106,10 +111,6 @@ void QgsMeasureTool::restart()
106111

107112
}
108113

109-
110-
111-
112-
113114
void QgsMeasureTool::updateSettings()
114115
{
115116
QSettings settings;
@@ -128,10 +129,11 @@ void QgsMeasureTool::canvasPressEvent( QMouseEvent * e )
128129
if ( e->button() == Qt::LeftButton )
129130
{
130131
if ( mDone )
132+
{
131133
mDialog->restart();
132-
134+
}
133135
QgsPoint idPoint = snapPoint( e->pos() );
134-
mDialog->mousePress( idPoint );
136+
// mDialog->mousePress( idPoint );
135137
}
136138
}
137139

@@ -142,7 +144,13 @@ void QgsMeasureTool::canvasMoveEvent( QMouseEvent * e )
142144
QgsPoint point = snapPoint( e->pos() );
143145

144146
mRubberBand->movePoint( point );
145-
mDialog->mouseMove( point );
147+
if( ! mPoints.isEmpty() )
148+
{
149+
// Update last point
150+
mPoints.removeLast();
151+
mPoints.append( point ) ;
152+
mDialog->mouseMove( point );
153+
}
146154
}
147155
}
148156

@@ -159,9 +167,8 @@ void QgsMeasureTool::canvasReleaseEvent( QMouseEvent * e )
159167
}
160168
else
161169
{
162-
// The figure is finished, store last point.
170+
// The figure is finished
163171
mDone = true;
164-
addPoint( point );
165172
mDialog->show();
166173
}
167174
}
@@ -178,14 +185,15 @@ void QgsMeasureTool::addPoint( QgsPoint &point )
178185
{
179186
QgsDebugMsg( "point=" + point.toString() );
180187

188+
int last = mPoints.size() - 1;
181189
// don't add points with the same coordinates
182-
if ( mPoints.size() > 0 && point == mPoints[0] )
190+
if ( mPoints.size() > 1 && mPoints[ last ] == mPoints[ last - 1 ] )
183191
return;
184192

185193
QgsPoint pnt( point );
194+
// Append point that we will be moving.
186195
mPoints.append( pnt );
187196

188-
189197
mRubberBand->addPoint( point );
190198
if ( ! mDone )
191199
{

0 commit comments

Comments
 (0)
Please sign in to comment.