Skip to content

Commit

Permalink
[tracer] Integration of tracing into GUI
Browse files Browse the repository at this point in the history
Checking the new 'enable tracing' button will switch capture tools
to tracing mode. Tracing can be turned on/off even while digitizing
is in progress
  • Loading branch information
wonder-sk committed Jan 10, 2016
1 parent 0cd9b68 commit bbe3598
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 3 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -529,6 +529,7 @@
<file>flags/zh.png</file>
<file>icons/qgis-icon-16x16_xmas.png</file>
<file>icons/qgis-icon-60x60_xmas.png</file>
<file>themes/default/mActionTracing.png</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
Binary file added images/themes/default/mActionTracing.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
524 changes: 524 additions & 0 deletions images/themes/default/mActionTracing.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -164,6 +164,7 @@
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgsmapcanvastracer.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaplayerstyleguiutils.h"
Expand Down Expand Up @@ -554,6 +555,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
, mComposerManager( nullptr )
, mpTileScaleWidget( nullptr )
, mpGpsWidget( nullptr )
, mTracer( nullptr )
, mSnappingUtils( nullptr )
, mProjectLastModified()
, mWelcomePage( nullptr )
Expand Down Expand Up @@ -1020,6 +1022,7 @@ QgisApp::QgisApp()
, mMacrosWarn( nullptr )
, mUserInputDockWidget( nullptr )
, mVectorLayerTools( nullptr )
, mTracer( nullptr )
, mActionFilterLegend( nullptr )
, mLegendExpressionFilterButton( nullptr )
, mSnappingUtils( nullptr )
Expand Down Expand Up @@ -1098,6 +1101,8 @@ QgisApp::~QgisApp()

delete mComposerManager;

delete mTracer;

delete mVectorLayerTools;
delete mWelcomePage;

Expand Down Expand Up @@ -1968,6 +1973,9 @@ void QgisApp::createToolBars()

// Cad toolbar
mAdvancedDigitizeToolBar->insertAction( mActionUndo, mAdvancedDigitizingDockWidget->enableAction() );

mTracer = new QgsMapCanvasTracer( mMapCanvas );
mAdvancedDigitizeToolBar->insertAction( mActionUndo, mTracer->actionEnableTracing() );
}

void QgisApp::createStatusBar()
Expand Down Expand Up @@ -9626,6 +9634,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionMergeFeatures->setEnabled( false );
mActionMergeFeatureAttributes->setEnabled( false );
mActionRotatePointSymbols->setEnabled( false );
mTracer->actionEnableTracing()->setEnabled( false );

mActionPinLabels->setEnabled( false );
mActionShowHideLabels->setEnabled( false );
Expand Down Expand Up @@ -9746,6 +9755,9 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
mActionRotateFeature->setEnabled( isEditable && canChangeGeometry );
mActionNodeTool->setEnabled( isEditable && canChangeGeometry );

mTracer->actionEnableTracing()->setEnabled( isEditable && canAddFeatures &&
( vlayer->geometryType() == QGis::Line || vlayer->geometryType() == QGis::Polygon ) );

if ( vlayer->geometryType() == QGis::Point )
{
mActionAddFeature->setIcon( QgsApplication::getThemeIcon( "/mActionCapturePoint.svg" ) );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -81,6 +81,7 @@ class QgsAdvancedDigitizingDockWidget;
class QgsSnappingDialog;
class QgsGPSInformationWidget;
class QgsStatisticalSummaryDockWidget;
class QgsMapCanvasTracer;

class QgsDecorationItem;

Expand Down Expand Up @@ -1690,6 +1691,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QgsVectorLayerTools* mVectorLayerTools;

//! A class that facilitates tracing of features
QgsMapCanvasTracer* mTracer;

QAction* mActionFilterLegend;

QgsLegendFilterButton* mLegendExpressionFilterButton;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -212,6 +212,7 @@ SET(QGIS_GUI_SRCS
qgsmapcanvasmap.cpp
qgsmapcanvassnapper.cpp
qgsmapcanvassnappingutils.cpp
qgsmapcanvastracer.cpp
qgsmaplayeractionregistry.cpp
qgsmaplayercombobox.cpp
qgsmaplayermodel.cpp
Expand Down Expand Up @@ -344,6 +345,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsmanageconnectionsdialog.h
qgsmapcanvas.h
qgsmapcanvassnappingutils.h
qgsmapcanvastracer.h
qgsmaplayeractionregistry.h
qgsmaplayercombobox.h
qgsmaplayermodel.h
Expand Down Expand Up @@ -557,6 +559,7 @@ SET(QGIS_GUI_HDRS
qgsmapcanvasmap.h
qgsmapcanvassnapper.h
qgsmapcanvassnappingutils.h
qgsmapcanvastracer.h
qgsmaptip.h
qgsmapmouseevent.h
qgsnumericsortlistviewitem.h
Expand Down
51 changes: 51 additions & 0 deletions src/gui/qgsmapcanvastracer.cpp
@@ -0,0 +1,51 @@
#include "qgsmapcanvastracer.h"

#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"

#include <QAction>

QHash<QgsMapCanvas*, QgsMapCanvasTracer*> QgsMapCanvasTracer::sTracers;


QgsMapCanvasTracer::QgsMapCanvasTracer( QgsMapCanvas* canvas )
: mCanvas( canvas )
{
sTracers.insert( canvas, this );

connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( updateSettings() ) );
connect( canvas, SIGNAL( layersChanged() ), this, SLOT( updateSettings() ) );
// TODO: watch for snapping changes

mActionEnableTracing = new QAction( QIcon( QgsApplication::getThemeIcon( "/mActionTracing.png" ) ), tr( "Enable Tracing" ), this );
mActionEnableTracing->setShortcut( Qt::Key_T );
mActionEnableTracing->setCheckable( true );

updateSettings(); // initialize
}

QgsMapCanvasTracer::~QgsMapCanvasTracer()
{
sTracers.remove( mCanvas );
}

QgsMapCanvasTracer* QgsMapCanvasTracer::tracerForCanvas( QgsMapCanvas* canvas )
{
return sTracers.value( canvas, 0 );
}

void QgsMapCanvasTracer::updateSettings()
{
QList<QgsVectorLayer*> layers;
foreach ( const QString& layerId, mCanvas->mapSettings().layers() )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( vl )
layers << vl;
}
setLayers( layers );

setDestinationCrs( mCanvas->mapSettings().destinationCrs() );
}
35 changes: 35 additions & 0 deletions src/gui/qgsmapcanvastracer.h
@@ -0,0 +1,35 @@
#ifndef QGSMAPCANVASTRACER_H
#define QGSMAPCANVASTRACER_H

#include "qgstracer.h"

class QAction;
class QgsMapCanvas;

class GUI_EXPORT QgsMapCanvasTracer : public QgsTracer
{
Q_OBJECT

public:
explicit QgsMapCanvasTracer( QgsMapCanvas* canvas );
~QgsMapCanvasTracer();

QAction* actionEnableTracing() { return mActionEnableTracing; }

//! Retrieve instance of this class associated with given canvas (if any).
//! The class keeps a simple registry of tracers associated with map canvas
//! instances for easier access to the common tracer by various map tools
static QgsMapCanvasTracer* tracerForCanvas( QgsMapCanvas* canvas );

private slots:
void updateSettings();

private:
QgsMapCanvas* mCanvas;

QAction* mActionEnableTracing;

static QHash<QgsMapCanvas*, QgsMapCanvasTracer*> sTracers;
};

#endif // QGSMAPCANVASTRACER_H
84 changes: 81 additions & 3 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgslinestringv2.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvastracer.h"
#include "qgsmapmouseevent.h"
#include "qgsmaprenderer.h"
#include "qgspolygonv2.h"
Expand Down Expand Up @@ -134,6 +135,63 @@ void QgsMapToolCapture::currentLayerChanged( QgsMapLayer *layer )
}
}


bool QgsMapToolCapture::tracingEnabled()
{
QgsMapCanvasTracer* tracer = QgsMapCanvasTracer::tracerForCanvas( mCanvas );
return tracer && tracer->actionEnableTracing()->isChecked();
}


void QgsMapToolCapture::tracingMouseMove( QgsMapMouseEvent* e )
{
if ( !e->isSnapped() )
return;

QgsPointV2 v;
QgsVertexId::VertexType vt;
mCaptureCurve.pointAt( mCaptureCurve.numPoints() - 1, v, vt );

QgsMapCanvasTracer* tracer = QgsMapCanvasTracer::tracerForCanvas( mCanvas );
if ( !tracer )
return; // this should not happen!

QVector<QgsPoint> points = tracer->findShortestPath( QgsPoint( v.x(), v.y() ), e->mapPoint() );
if ( points.isEmpty() )
return;

// update rubberband
mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
for ( int i = 0; i < points.count(); ++i )
mTempRubberBand->addPoint( points.at( i ), i == points.count() - 1 );
}


void QgsMapToolCapture::tracingAddVertex( const QgsPoint& point )
{
QgsMapCanvasTracer* tracer = QgsMapCanvasTracer::tracerForCanvas( mCanvas );
if ( !tracer )
return; // this should not happen!

QgsPointV2 v;
QgsVertexId::VertexType vt;
mCaptureCurve.pointAt( mCaptureCurve.numPoints() - 1, v, vt );

QVector<QgsPoint> points = tracer->findShortestPath( QgsPoint( v.x(), v.y() ), point );
if ( points.isEmpty() )
return; // ignore the vertex - can't find path to the end point!

QgsPoint lp; // in layer coords
for ( int i = 1; i < points.count(); ++i )
{
if ( nextPoint( points[i], lp ) != 0 )
continue; // TODO: should completely abort
mRubberBand->addPoint( points[i], i == points.count() - 1 );
mCaptureCurve.addVertex( QgsPointV2( lp.x(), lp.y() ) );
}
}


void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent * e )
{
QgsMapToolAdvancedDigitizing::cadCanvasMoveEvent( e );
Expand Down Expand Up @@ -165,9 +223,17 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent * e )
mTempRubberBand->addPoint( point );
}


if ( mCaptureMode != CapturePoint && mTempRubberBand && mCapturing )
{
mTempRubberBand->movePoint( point );
if ( tracingEnabled() && mCaptureCurve.numPoints() != 0 )
{
tracingMouseMove( e );
}
else
{
mTempRubberBand->movePoint( point );
}
}
} // mouseMoveEvent

Expand Down Expand Up @@ -220,8 +286,19 @@ int QgsMapToolCapture::addVertex( const QgsPoint& point )
{
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
}
mRubberBand->addPoint( point );
mCaptureCurve.addVertex( QgsPointV2( layerPoint.x(), layerPoint.y() ) );

if ( tracingEnabled() && mCaptureCurve.numPoints() != 0 )
{
tracingAddVertex( point );
}
else
{
// ordinary digitizing
mRubberBand->addPoint( point );
mCaptureCurve.addVertex( QgsPointV2( layerPoint.x(), layerPoint.y() ) );
}

// re-initialize temporary rubber band for further digitizing

if ( !mTempRubberBand )
{
Expand All @@ -231,6 +308,7 @@ int QgsMapToolCapture::addVertex( const QgsPoint& point )
{
mTempRubberBand->reset( mCaptureMode == CapturePolygon ? QGis::Polygon : QGis::Line );
}

if ( mCaptureMode == CaptureLine )
{
mTempRubberBand->addPoint( point );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsmaptoolcapture.h
Expand Up @@ -140,6 +140,14 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
*/
void closePolygon();

private:
//! whether tracing has been requested by the user
bool tracingEnabled();
//! handle of mouse movement when tracing enabled and capturing has started
void tracingMouseMove( QgsMapMouseEvent* e );
//! handle of addition of clicked point (with the rest of the trace) when tracing enabled
void tracingAddVertex( const QgsPoint& point );

private:
/** Flag to indicate a map canvas capture operation is taking place */
bool mCapturing;
Expand Down

0 comments on commit bbe3598

Please sign in to comment.