Skip to content

Commit

Permalink
Separate gui-part and graph construction part. Added message box in s…
Browse files Browse the repository at this point in the history
…hortestpath widget.

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15093 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
stopa85 committed Jan 28, 2011
1 parent dde98c7 commit 12dd395
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 80 deletions.
5 changes: 0 additions & 5 deletions src/plugins/roadgraph/graphdirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class RgGraphDirector
*/
virtual void makeGraph( RgGraphBuilder * ) const = 0;

/**
* return pointer to my Settings
*/
virtual RgSettings* settings() = 0;

/**
* return Director name
*/
Expand Down
93 changes: 50 additions & 43 deletions src/plugins/roadgraph/linevectorlayerdirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* \brief implementation of RgLineVectorLayerDirector
*/

#include "linevectorlayersettings.h"
#include "linevectorlayerdirector.h"
#include "graphbuilder.h"
#include "units.h"
Expand All @@ -32,16 +31,30 @@
//standard includes
#include <iostream>

RgLineVectorLayerDirector::RgLineVectorLayerDirector()
RgLineVectorLayerDirector::RgLineVectorLayerDirector( const QString& layerId,
int directionFieldId,
const QString& directDirectionValue,
const QString& reverseDirectionValue,
const QString& bothDirectionValue,
int defaultDirection,
const QString& speedUnitName,
int speedFieldId,
double defaultSpeed)
{
mLayerId = layerId;
mDirectionFieldId = directionFieldId;
mDirectDirectionValue = directDirectionValue;
mReverseDirectionValue = reverseDirectionValue;
mDefaultDirection = defaultDirection;
mBothDirectionValue = bothDirectionValue;
mSpeedUnitName = speedUnitName;
mSpeedFieldId = speedFieldId;
mDefaultSpeed = defaultSpeed;
}

RgLineVectorLayerDirector::~RgLineVectorLayerDirector()
{
}
RgSettings* RgLineVectorLayerDirector::settings()
{
return &mSettings;
}

QString RgLineVectorLayerDirector::name() const
{
Expand All @@ -50,77 +63,59 @@ QString RgLineVectorLayerDirector::name() const

void RgLineVectorLayerDirector::makeGraph( RgGraphBuilder *builder ) const
{
QgsVectorLayer *vl = NULL;
QMap< QString, QgsMapLayer*> m = QgsMapLayerRegistry::instance()->mapLayers();
QMap< QString, QgsMapLayer*>::const_iterator it;
for ( it = m.constBegin(); it != m.constEnd(); ++it )
{
if ( it.value()->name() == mSettings.mLayer )
{
vl = dynamic_cast<QgsVectorLayer*>( it.value() );
break;
}
}
if ( vl == NULL )
return;
QgsVectorLayer *vl = myLayer();

QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider*>( vl->dataProvider() );
if ( provider == NULL )
if ( vl == NULL )
return;

int directionFieldId = provider->fieldNameIndex( mSettings.mDirection );
int speedFieldId = provider->fieldNameIndex( mSettings.mSpeed );

builder->setSourceCrs( vl->crs() );
QgsAttributeList la;
if ( directionFieldId > -1 )
la.push_back( directionFieldId );
if ( speedFieldId > -1 )
la.push_back( speedFieldId );
la.push_back( mDirectionFieldId );
la.push_back( mSpeedFieldId );

SpeedUnit su = SpeedUnit::byName( mSettings.mSpeedUnitName );
SpeedUnit su = SpeedUnit::byName( mSpeedUnitName );

vl->select( la );
QgsFeature feature;
while ( vl->nextFeature( feature ) )
{
QgsAttributeMap attr = feature.attributeMap();
RgLineVectorLayerSettings::DirectionType directionType = mSettings.mDefaultDirection;
int directionType = mDefaultDirection;
QgsAttributeMap::const_iterator it;
// What direction have feature?
for ( it = attr.constBegin(); it != attr.constEnd(); ++it )
{
if ( it.key() != directionFieldId )
if ( it.key() != mDirectionFieldId )
{
continue;
}
QString str = it.value().toString();
if ( str == mSettings.mBothDirectionVal )
if ( str == mBothDirectionValue )
{
directionType = RgLineVectorLayerSettings::Both;
directionType = 3;
}
else if ( str == mSettings.mFirstPointToLastPointDirectionVal )
else if ( str == mDirectDirectionValue )
{
directionType = RgLineVectorLayerSettings::FirstPointToLastPoint;
directionType = 1;
}
else if ( str == mSettings.mLastPointToFirstPointDirectionVal )
else if ( str == mReverseDirectionValue )
{
directionType = RgLineVectorLayerSettings::LastPointToFirstPoint;
directionType = 2;
}
}
// What speed have feature?
double speed = 0.0;
for ( it = attr.constBegin(); it != attr.constEnd(); ++it )
{
if ( it.key() != speedFieldId )
if ( it.key() != mSpeedFieldId )
{
continue;
}
speed = it.value().toDouble();
}
if ( speed <= 0.0 )
{
speed = mSettings.mDefaultSpeed;
speed = mDefaultSpeed;
}

// begin features segments and add arc to the Graph;
Expand All @@ -134,13 +129,13 @@ void RgLineVectorLayerDirector::makeGraph( RgGraphBuilder *builder ) const
pt2 = *pointIt;
if ( !isFirstPoint )
{
if ( directionType == RgLineVectorLayerSettings::FirstPointToLastPoint ||
directionType == RgLineVectorLayerSettings::Both )
if ( directionType == 1 ||
directionType == 3 )
{
builder->addArc( pt1, pt2, speed*su.multipler() );
}
if ( directionType == RgLineVectorLayerSettings::LastPointToFirstPoint ||
directionType == RgLineVectorLayerSettings::Both )
if ( directionType == 2 ||
directionType == 3 )
{
builder->addArc( pt2, pt1, speed*su.multipler() );
}
Expand All @@ -151,3 +146,15 @@ void RgLineVectorLayerDirector::makeGraph( RgGraphBuilder *builder ) const

} // while( vl->nextFeature(feature) )
} // makeGraph( RgGraphBuilder *builder, const QgsRectangle& rt )

QgsVectorLayer* RgLineVectorLayerDirector::myLayer() const
{
QMap <QString, QgsMapLayer*> m = QgsMapLayerRegistry::instance()->mapLayers();
QMap <QString, QgsMapLayer*>::const_iterator it = m.find( mLayerId );
if ( it == m.end() )
{
return NULL;
}
// return NULL if it.value() isn't QgsVectorLayer()
return dynamic_cast<QgsVectorLayer*>( it.value() );
}
43 changes: 35 additions & 8 deletions src/plugins/roadgraph/linevectorlayerdirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

// Road-graph plugin includes
#include "graphdirector.h"
#include "linevectorlayersettings.h"

//forward declarations
class RgGraphBuilder;
class QgsVectorLayer;

/**
* \class RgLineVectorLayerDirector
Expand All @@ -33,21 +33,48 @@ class RgGraphBuilder;
class RgLineVectorLayerDirector : public RgGraphDirector
{
public:
RgLineVectorLayerDirector();
RgLineVectorLayerDirector( const QString& layerId,
int directionFiledId,
const QString& directDirectionValue,
const QString& reverseDirectionValue,
const QString& bothDirectionValue,
int defaultDirection,
const QString& speedValueUnit = QString("m/s"),
int speedFieldId = -1,
double defaultSpeed = 1.0 );

//! Destructor
virtual ~RgLineVectorLayerDirector();
/**
* MANDATORY DIRECTOR PROPERTY DECLARATION
*/
void makeGraph( RgGraphBuilder * ) const;

RgSettings* settings();

QString name() const;

private:
/**
* settings of this director
*/
RgLineVectorLayerSettings mSettings;

QgsVectorLayer* myLayer() const;

private:

QString mLayerId;

int mSpeedFieldId;

double mDefaultSpeed;

QString mSpeedUnitName;

int mDirectionFieldId;

QString mDirectDirectionValue;

QString mReverseDirectionValue;

QString mBothDirectionValue;

//FIXME: need enum
int mDefaultDirection;
};
#endif //GRAPHDIRECTOR
61 changes: 50 additions & 11 deletions src/plugins/roadgraph/roadgraphplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
#include <qgsmaptoolemitpoint.h>
#include <qgsmaprenderer.h>

#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>

// Road grap plugin includes
#include "roadgraphplugin.h"
#include "shortestpathwidget.h"
#include "settingsdlg.h"
#include "graphdirector.h"

#include "linevectorlayerdirector.h"
#include "linevectorlayersettings.h"
#include "simplegraphbuilder.h"

//
// Qt4 Related Includes
//
Expand Down Expand Up @@ -70,7 +74,7 @@ RoadGraphPlugin::RoadGraphPlugin( QgisInterface * theQgisInterface ):
{

mQShortestPathDock = NULL;
mDirector = new RgLineVectorLayerDirector();
mSettings = new RgLineVectorLayerSettings();
mTimeUnitName = "h";
mDistanceUnitName = "km";
}
Expand Down Expand Up @@ -166,17 +170,18 @@ void RoadGraphPlugin::newProject()

void RoadGraphPlugin::property()
{
RgSettingsDlg dlg( mDirector, mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );
RgSettingsDlg dlg( mSettings, mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );

dlg.setTimeUnitName( mTimeUnitName );
dlg.setDistanceUnitName( mDistanceUnitName );

if ( !dlg.exec() )
return;

mTimeUnitName = dlg.timeUnitName();
mDistanceUnitName = dlg.distanceUnitName();

mDirector->settings()->write( QgsProject::instance() );
mSettings->write( QgsProject::instance() );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginTimeUnit", mTimeUnitName );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginDistanceUnit", mDistanceUnitName );

Expand Down Expand Up @@ -236,7 +241,7 @@ void RoadGraphPlugin::about()

void RoadGraphPlugin::projectRead()
{
mDirector->settings()->read( QgsProject::instance() );
mSettings->read( QgsProject::instance() );
mTimeUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginTimeUnit", "h" );
mDistanceUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginDistanceUnit", "km" );
setGuiElementsToDefault();
Expand All @@ -249,17 +254,51 @@ QgisInterface* RoadGraphPlugin::iface()

const RgGraphDirector* RoadGraphPlugin::director() const
{
return mDirector;
QString layerId;
QgsVectorLayer *layer = NULL;
QMap< QString, QgsMapLayer* > mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap< QString, QgsMapLayer* >::const_iterator it;
for ( it = mapLayers.begin(); it != mapLayers.end(); ++it )
{
if ( it.value()->name() != mSettings->mLayer )
continue;
layerId = it.key();
layer = dynamic_cast< QgsVectorLayer* >( it.value() );
break;
}
if ( layer == NULL )
return NULL;

QgsVectorDataProvider *provider = dynamic_cast< QgsVectorDataProvider* > ( layer->dataProvider() );
if ( provider == NULL )
return NULL;

RgLineVectorLayerDirector * director =
new RgLineVectorLayerDirector( layerId,
provider->fieldNameIndex( mSettings->mDirection ),
mSettings->mFirstPointToLastPointDirectionVal,
mSettings->mLastPointToFirstPointDirectionVal,
mSettings->mBothDirectionVal,
mSettings->mDefaultDirection,
mSettings->mSpeedUnitName,
provider->fieldNameIndex( mSettings->mSpeed ),
mSettings->mDefaultSpeed );

return director;
}
void RoadGraphPlugin::render( QPainter *painter )
{
if ( mDirector == NULL )
return;
if ( !mQShowDirectionAction->isChecked() )
return;

const RgGraphDirector *graphDirector = director();

if ( graphDirector == NULL )
return;

RgSimpleGraphBuilder builder;
builder.setDestinationCrs( mQGisIface->mapCanvas()->mapRenderer()->destinationSrs() );
mDirector->makeGraph( &builder );
graphDirector->makeGraph( &builder );
AdjacencyMatrix m = builder.adjacencyMatrix();

AdjacencyMatrix::iterator it1;
Expand Down Expand Up @@ -293,7 +332,7 @@ void RoadGraphPlugin::render( QPainter *painter )
painter->drawPolygon( tmp );
}
}

delete graphDirector;
}// RoadGraphPlugin::render()
QString RoadGraphPlugin::timeUnitName()
{
Expand Down
Loading

0 comments on commit 12dd395

Please sign in to comment.