Skip to content
Permalink
Browse files
replace hardcoded magic numbers with enum
  • Loading branch information
alexbruy committed Nov 21, 2016
1 parent a61e8bb commit 9dffe64dabb4c29374e922e2a04b123cf4420d79
@@ -10,20 +10,34 @@ class QgsLineVectorLayerDirector : QgsGraphDirector
%End

public:
/** Road direction
* Road can be one-way with direct flow (one can move only from the start
* point to the end point), one-way with reversed flow (one can move only
* from the end point to the start point) and bidirectional or two-way
* (one can move in any direction)
*/
enum RoadDirection
{
RoadDirect, //!< One-way direct
RoadReversed, //!< One-way reversed
RoadBidirectional, //!< Two-way
};

/**
* @param myLayer source vector layer
* @param directionFieldId feield contain road direction value
* @param directDirectionValue value for one-way road
* @param reverseDirectionValue value for reverse one-way road
* @param bothDirectionValue value for road
* @param defaultDirection 1 - direct direction, 2 - reverse direction, 3 - both direction
* @param defaultDirection default road direction. Will be used if corresponding
* attribute value is not set or does not equal to the given values
*/
QgsLineVectorLayerDirector( QgsVectorLayer* myLayer,
int directionFieldId,
const QString& directDirectionValue,
const QString& reverseDirectionValue,
const QString& bothDirectionValue,
int defaultDirection
const RoadDirection defaultDirection
);

//! Destructor
@@ -102,12 +102,12 @@ bool TiePointInfoCompare( const TiePointInfo& a, const TiePointInfo& b )
return a.mFirstPoint.x() == b.mFirstPoint.x() ? a.mFirstPoint.y() < b.mFirstPoint.y() : a.mFirstPoint.x() < b.mFirstPoint.x();
}

QgsLineVectorLayerDirector::QgsLineVectorLayerDirector( QgsVectorLayer *myLayer,
QgsLineVectorLayerDirector::QgsLineVectorLayerDirector(QgsVectorLayer *myLayer,
int directionFieldId,
const QString& directDirectionValue,
const QString& reverseDirectionValue,
const QString& bothDirectionValue,
int defaultDirection
const RoadDirection defaultDirection
)
{
mVectorLayer = myLayer;
@@ -284,21 +284,21 @@ void QgsLineVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, c
fit = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
while ( fit.nextFeature( feature ) )
{
int directionType = mDefaultDirection;
RoadDirection directionType = mDefaultDirection;

// What direction have feature?
QString str = feature.attribute( mDirectionFieldId ).toString();
if ( str == mBothDirectionValue )
{
directionType = 3;
directionType = RoadDirection::RoadBidirectional;
}
else if ( str == mDirectDirectionValue )
{
directionType = 1;
directionType = RoadDirection::RoadDirect;
}
else if ( str == mReverseDirectionValue )
{
directionType = 2;
directionType = RoadDirection::RoadReversed;
}

// begin features segments and add arc to the Graph;
@@ -372,13 +372,13 @@ void QgsLineVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, c
prop.push_back(( *it )->cost( distance, feature ) );
}

if ( directionType == 1 ||
directionType == 3 )
if ( directionType == RoadDirection::RoadDirect ||
directionType == RoadDirection::RoadBidirectional )
{
builder->addEdge( pt1idx, pt1, pt2idx, pt2, prop );
}
if ( directionType == 2 ||
directionType == 3 )
if ( directionType == RoadDirection::RoadReversed ||
directionType == RoadDirection::RoadBidirectional )
{
builder->addEdge( pt2idx, pt2, pt1idx, pt1, prop );
}
@@ -1,5 +1,5 @@
/***************************************************************************
linevectorlayerdirector.h
qgslinevectorlayerdirector.h
--------------------------------------
Date : 2010-10-20
Copyright : (C) 2010 by Yakushev Sergey
@@ -32,21 +32,35 @@ class ANALYSIS_EXPORT QgsLineVectorLayerDirector : public QgsGraphDirector

public:

/** Road direction
* Road can be one-way with direct flow (one can move only from the start
* point to the end point), one-way with reversed flow (one can move only
* from the end point to the start point) and bidirectional or two-way
* (one can move in any direction)
*/
enum RoadDirection
{
RoadDirect, //!< One-way direct
RoadReversed, //!< One-way reversed
RoadBidirectional, //!< Two-way
};

/**
* Default constructor
* @param myLayer source vector layer
* @param directionFieldId field contain road direction value
* @param directDirectionValue value for one-way road
* @param directDirectionValue value for direct one-way road
* @param reverseDirectionValue value for reversed one-way road
* @param bothDirectionValue value for bidirectional road
* @param defaultDirection 1 - direct direction, 2 - reverse direction, 3 - both directions
* @param bothDirectionValue value for two-way (bidirectional) road
* @param defaultDirection default road direction. Will be used if corresponding
* attribute value is not set or does not equal to the given values
*/
QgsLineVectorLayerDirector( QgsVectorLayer* myLayer,
int directionFieldId,
const QString& directDirectionValue,
const QString& reverseDirectionValue,
const QString& bothDirectionValue,
int defaultDirection
const RoadDirection defaultDirection
);

//! Destructor
@@ -72,8 +86,7 @@ class ANALYSIS_EXPORT QgsLineVectorLayerDirector : public QgsGraphDirector

QString mBothDirectionValue;

//FIXME: need enum
int mDefaultDirection;
RoadDirection mDefaultDirection;
};

#endif // QGSLINEVECTORLAYERDIRECTOR_H
@@ -29,7 +29,7 @@
//standard includes

RgLineVectorLayerSettings::RgLineVectorLayerSettings()
: mDefaultDirection( Both )
: mDefaultDirection( QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional )
, mDefaultSpeed( 40 )
{
}
@@ -58,31 +58,17 @@ bool RgLineVectorLayerSettings::test()

void RgLineVectorLayerSettings::read( const QgsProject *project )
{
int dd = project->readNumEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultDirection" ) );
mDirection = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/directionField" ) );
mDefaultDirection = static_cast<QgsLineVectorLayerDirector::RoadDirection> ( project->readNumEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultDirection" ) ) );
mDirection = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/directionField" ) );
mFirstPointToLastPointDirectionVal =
project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/FirstPointToLastPointDirectionVal" ) );
mLastPointToFirstPointDirectionVal =
project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/LastPointToFirstPointDirectionVal" ) );
mBothDirectionVal = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/BothDirectionVal" ) );
mSpeed = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedField" ) );
mSpeed = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedField" ) );
mDefaultSpeed = project->readDoubleEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultSpeed" ) );
mLayerName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/layer" ) );
mLayerName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/layer" ) );
mSpeedUnitName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedUnitName" ) );

if ( dd == 1 )
{
mDefaultDirection = FirstPointToLastPoint;
}
else if ( dd == 2 )
{
mDefaultDirection = LastPointToFirstPoint;
}
else if ( dd == 3 )
{
mDefaultDirection = Both;
}

} // RgLineVectorLayerSettings::read( const QgsProject *project )

void RgLineVectorLayerSettings::write( QgsProject *project )
@@ -115,19 +101,19 @@ void RgLineVectorLayerSettings::setFromGui( QWidget *myGui )
mLastPointToFirstPointDirectionVal = w->mleLastPointToFirstPointDirection->text();
mBothDirectionVal = w->mleBothDirection->text();
mDirection = w->mcbDirection->currentText();
mLayerName = w->mcbLayers->currentText();
mLayerName = w->mcbLayers->currentText();

if ( w->mcbDirectionDefault->currentIndex() == 0 )
{
mDefaultDirection = Both;
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional;
}
else if ( w->mcbDirectionDefault->currentIndex() == 1 )
{
mDefaultDirection = FirstPointToLastPoint;
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadDirect;
}
else if ( w->mcbDirectionDefault->currentIndex() == 2 )
{
mDefaultDirection = LastPointToFirstPoint;
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadReversed;
}

mSpeed = w->mcbSpeed->currentText();
@@ -17,6 +17,8 @@
// QT includes
#include <qstring.h>

#include <qgslinevectorlayerdirector.h>

// Qgis includes

// standard includes
@@ -93,7 +95,7 @@ class RgLineVectorLayerSettings: public RgSettings
/**
* contained Default direction
*/
DirectionType mDefaultDirection;
QgsLineVectorLayerDirector::RoadDirection mDefaultDirection;

/**
* contained speed filed name
@@ -143,13 +143,13 @@ RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLa

switch ( s->mDefaultDirection )
{
case RgLineVectorLayerSettings::Both:
case QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional:
mcbDirectionDefault->setCurrentIndex( 0 );
break;
case RgLineVectorLayerSettings::FirstPointToLastPoint:
case QgsLineVectorLayerDirector::RoadDirection::RoadDirect:
mcbDirectionDefault->setCurrentIndex( 1 );
break;
case RgLineVectorLayerSettings::LastPointToFirstPoint:
case QgsLineVectorLayerDirector::RoadDirection::RoadReversed:
mcbDirectionDefault->setCurrentIndex( 2 );
break;
}

0 comments on commit 9dffe64

Please sign in to comment.