Skip to content

Commit

Permalink
Simplify tool: show status message about the simplification results
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 22, 2015
1 parent 4322de9 commit 0f9e1f7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
45 changes: 43 additions & 2 deletions src/app/qgsmaptoolsimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ QgsSimplifyDialog::QgsSimplifyDialog( QgsMapToolSimplify* tool, QWidget* parent

}

void QgsSimplifyDialog::updateStatusText()
{
labelStatus->setText( mTool->statusText() );
}


////////////////////////////////////////////////////////////////////////////

Expand All @@ -51,6 +56,8 @@ QgsMapToolSimplify::QgsMapToolSimplify( QgsMapCanvas* canvas )
: QgsMapToolEdit( canvas )
, mSelectionRubberBand( 0 )
, mDragging( false )
, mOriginalVertexCount( 0 )
, mReducedVertexCount( 0 )
{
QSettings settings;
mTolerance = settings.value( "/digitizing/simplify_tolerance", 1 ).toDouble();
Expand Down Expand Up @@ -92,15 +99,40 @@ void QgsMapToolSimplify::updateSimplificationPreview()
{
QgsVectorLayer* vl = currentVectorLayer();

mReducedVertexCount = 0;
int i = 0;
foreach ( const QgsFeature& fSel, mSelectedFeatures )
{
// create a copy of selected feature and do the simplification
QgsFeature f = fSel;
QgsSimplifyFeature::simplify( f, mTolerance, mToleranceUnits, mCanvas->mapSettings().layerTransform( vl ) );
mReducedVertexCount += vertexCount( f.geometry() );
mRubberBands[i]->setToGeometry( f.geometry(), vl );
++i;
}

mSimplifyDialog->updateStatusText();
}


int QgsMapToolSimplify::vertexCount( QgsGeometry* g ) const
{
switch ( g->type() )
{
case QGis::Line:
{
return g->asPolyline().count();
}
case QGis::Polygon:
{
int count = 0;
foreach ( const QgsPolyline& ring, g->asPolygon() )
count += ring.count();
return count;
}
default:
return 0;
}
}


Expand Down Expand Up @@ -292,10 +324,12 @@ void QgsMapToolSimplify::canvasReleaseEvent( QMouseEvent * e )

mDragging = false;

// prepare rubber bands
// count vertices, prepare rubber bands
mOriginalVertexCount = 0;
foreach ( const QgsFeature& f, mSelectedFeatures )
{
Q_UNUSED( f );
mOriginalVertexCount += vertexCount( f.geometry() );

QgsRubberBand* rb = new QgsRubberBand( mCanvas );
rb->setColor( QColor( 255, 0, 0, 65 ) );
rb->setWidth( 2 );
Expand Down Expand Up @@ -384,6 +418,13 @@ void QgsMapToolSimplify::deactivate()
QgsMapTool::deactivate();
}

QString QgsMapToolSimplify::statusText() const
{
int percent = mOriginalVertexCount ? ( 100 * mReducedVertexCount / mOriginalVertexCount ) : 0;
return tr( "%1 feature(s): %2 to %3 vertices (%4%)" )
.arg( mSelectedFeatures.count() ).arg( mOriginalVertexCount ).arg( mReducedVertexCount ).arg( percent );
}


QVector<QgsPoint> QgsMapToolSimplify::getPointList( QgsFeature& f )
{
Expand Down
11 changes: 9 additions & 2 deletions src/app/qgsmaptoolsimplify.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class APP_EXPORT QgsSimplifyDialog : public QDialog, private Ui::SimplifyLineDia

QgsSimplifyDialog( QgsMapToolSimplify* tool, QWidget* parent = NULL );

void updateStatusText();

private:
QgsMapToolSimplify* mTool;

private slots:

};


Expand All @@ -63,6 +63,8 @@ class APP_EXPORT QgsMapToolSimplify: public QgsMapToolEdit

ToleranceUnits toleranceUnits() const { return mToleranceUnits; }

QString statusText() const;

public slots:
/** slot to change display when slidebar is moved */
void setTolerance( double tolerance );
Expand All @@ -81,6 +83,8 @@ class APP_EXPORT QgsMapToolSimplify: public QgsMapToolEdit

void updateSimplificationPreview();

int vertexCount( QgsGeometry* g ) const;

/** Function to calculate tolerance boudaries for simplifying */
//bool calculateSliderBoudaries();

Expand All @@ -107,6 +111,9 @@ class APP_EXPORT QgsMapToolSimplify: public QgsMapToolEdit
QgsRubberBand* mSelectionRubberBand;
//! Flag to indicate a map canvas drag operation is taking place
bool mDragging;

int mOriginalVertexCount;
int mReducedVertexCount;
};

/**
Expand Down
47 changes: 25 additions & 22 deletions src/ui/qgssimplifytolerancedialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,21 @@
<x>0</x>
<y>0</y>
<width>527</width>
<height>38</height>
<height>63</height>
</rect>
</property>
<property name="windowTitle">
<string>Simplification Tool</string>
</property>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="label">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="3">
<widget class="QPushButton" name="okButton">
<property name="text">
<string>Tolerance</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="spinTolerance">
<property name="decimals">
<number>6</number>
</property>
<property name="maximum">
<double>999999.000000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
<string>OK</string>
</property>
</widget>
</item>
<item>
<item row="0" column="2">
<widget class="QComboBox" name="cboToleranceUnits">
<item>
<property name="text">
Expand All @@ -48,13 +35,29 @@
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="okButton">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>OK</string>
<string>Tolerance</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="spinTolerance">
<property name="decimals">
<number>6</number>
</property>
<property name="maximum">
<double>999999.000000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QLabel" name="labelStatus"/>
</item>
</layout>
</widget>
<tabstops>
Expand Down

0 comments on commit 0f9e1f7

Please sign in to comment.