Skip to content

Commit 9dffe64

Browse files
committed
replace hardcoded magic numbers with enum
1 parent a61e8bb commit 9dffe64

File tree

6 files changed

+61
-46
lines changed

6 files changed

+61
-46
lines changed

python/analysis/network/qgslinevectorlayerdirector.sip

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@ class QgsLineVectorLayerDirector : QgsGraphDirector
1010
%End
1111

1212
public:
13+
/** Road direction
14+
* Road can be one-way with direct flow (one can move only from the start
15+
* point to the end point), one-way with reversed flow (one can move only
16+
* from the end point to the start point) and bidirectional or two-way
17+
* (one can move in any direction)
18+
*/
19+
enum RoadDirection
20+
{
21+
RoadDirect, //!< One-way direct
22+
RoadReversed, //!< One-way reversed
23+
RoadBidirectional, //!< Two-way
24+
};
25+
1326
/**
1427
* @param myLayer source vector layer
1528
* @param directionFieldId feield contain road direction value
1629
* @param directDirectionValue value for one-way road
1730
* @param reverseDirectionValue value for reverse one-way road
1831
* @param bothDirectionValue value for road
19-
* @param defaultDirection 1 - direct direction, 2 - reverse direction, 3 - both direction
32+
* @param defaultDirection default road direction. Will be used if corresponding
33+
* attribute value is not set or does not equal to the given values
2034
*/
2135
QgsLineVectorLayerDirector( QgsVectorLayer* myLayer,
2236
int directionFieldId,
2337
const QString& directDirectionValue,
2438
const QString& reverseDirectionValue,
2539
const QString& bothDirectionValue,
26-
int defaultDirection
40+
const RoadDirection defaultDirection
2741
);
2842

2943
//! Destructor

src/analysis/network/qgslinevectorlayerdirector.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ bool TiePointInfoCompare( const TiePointInfo& a, const TiePointInfo& b )
102102
return a.mFirstPoint.x() == b.mFirstPoint.x() ? a.mFirstPoint.y() < b.mFirstPoint.y() : a.mFirstPoint.x() < b.mFirstPoint.x();
103103
}
104104

105-
QgsLineVectorLayerDirector::QgsLineVectorLayerDirector( QgsVectorLayer *myLayer,
105+
QgsLineVectorLayerDirector::QgsLineVectorLayerDirector(QgsVectorLayer *myLayer,
106106
int directionFieldId,
107107
const QString& directDirectionValue,
108108
const QString& reverseDirectionValue,
109109
const QString& bothDirectionValue,
110-
int defaultDirection
110+
const RoadDirection defaultDirection
111111
)
112112
{
113113
mVectorLayer = myLayer;
@@ -284,21 +284,21 @@ void QgsLineVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, c
284284
fit = vl->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
285285
while ( fit.nextFeature( feature ) )
286286
{
287-
int directionType = mDefaultDirection;
287+
RoadDirection directionType = mDefaultDirection;
288288

289289
// What direction have feature?
290290
QString str = feature.attribute( mDirectionFieldId ).toString();
291291
if ( str == mBothDirectionValue )
292292
{
293-
directionType = 3;
293+
directionType = RoadDirection::RoadBidirectional;
294294
}
295295
else if ( str == mDirectDirectionValue )
296296
{
297-
directionType = 1;
297+
directionType = RoadDirection::RoadDirect;
298298
}
299299
else if ( str == mReverseDirectionValue )
300300
{
301-
directionType = 2;
301+
directionType = RoadDirection::RoadReversed;
302302
}
303303

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

375-
if ( directionType == 1 ||
376-
directionType == 3 )
375+
if ( directionType == RoadDirection::RoadDirect ||
376+
directionType == RoadDirection::RoadBidirectional )
377377
{
378378
builder->addEdge( pt1idx, pt1, pt2idx, pt2, prop );
379379
}
380-
if ( directionType == 2 ||
381-
directionType == 3 )
380+
if ( directionType == RoadDirection::RoadReversed ||
381+
directionType == RoadDirection::RoadBidirectional )
382382
{
383383
builder->addEdge( pt2idx, pt2, pt1idx, pt1, prop );
384384
}

src/analysis/network/qgslinevectorlayerdirector.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
linevectorlayerdirector.h
2+
qgslinevectorlayerdirector.h
33
--------------------------------------
44
Date : 2010-10-20
55
Copyright : (C) 2010 by Yakushev Sergey
@@ -32,21 +32,35 @@ class ANALYSIS_EXPORT QgsLineVectorLayerDirector : public QgsGraphDirector
3232

3333
public:
3434

35+
/** Road direction
36+
* Road can be one-way with direct flow (one can move only from the start
37+
* point to the end point), one-way with reversed flow (one can move only
38+
* from the end point to the start point) and bidirectional or two-way
39+
* (one can move in any direction)
40+
*/
41+
enum RoadDirection
42+
{
43+
RoadDirect, //!< One-way direct
44+
RoadReversed, //!< One-way reversed
45+
RoadBidirectional, //!< Two-way
46+
};
47+
3548
/**
3649
* Default constructor
3750
* @param myLayer source vector layer
3851
* @param directionFieldId field contain road direction value
39-
* @param directDirectionValue value for one-way road
52+
* @param directDirectionValue value for direct one-way road
4053
* @param reverseDirectionValue value for reversed one-way road
41-
* @param bothDirectionValue value for bidirectional road
42-
* @param defaultDirection 1 - direct direction, 2 - reverse direction, 3 - both directions
54+
* @param bothDirectionValue value for two-way (bidirectional) road
55+
* @param defaultDirection default road direction. Will be used if corresponding
56+
* attribute value is not set or does not equal to the given values
4357
*/
4458
QgsLineVectorLayerDirector( QgsVectorLayer* myLayer,
4559
int directionFieldId,
4660
const QString& directDirectionValue,
4761
const QString& reverseDirectionValue,
4862
const QString& bothDirectionValue,
49-
int defaultDirection
63+
const RoadDirection defaultDirection
5064
);
5165

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

7387
QString mBothDirectionValue;
7488

75-
//FIXME: need enum
76-
int mDefaultDirection;
89+
RoadDirection mDefaultDirection;
7790
};
7891

7992
#endif // QGSLINEVECTORLAYERDIRECTOR_H

src/plugins/roadgraph/linevectorlayersettings.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//standard includes
3030

3131
RgLineVectorLayerSettings::RgLineVectorLayerSettings()
32-
: mDefaultDirection( Both )
32+
: mDefaultDirection( QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional )
3333
, mDefaultSpeed( 40 )
3434
{
3535
}
@@ -58,31 +58,17 @@ bool RgLineVectorLayerSettings::test()
5858

5959
void RgLineVectorLayerSettings::read( const QgsProject *project )
6060
{
61-
int dd = project->readNumEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultDirection" ) );
62-
mDirection = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/directionField" ) );
61+
mDefaultDirection = static_cast<QgsLineVectorLayerDirector::RoadDirection> ( project->readNumEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultDirection" ) ) );
62+
mDirection = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/directionField" ) );
6363
mFirstPointToLastPointDirectionVal =
6464
project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/FirstPointToLastPointDirectionVal" ) );
6565
mLastPointToFirstPointDirectionVal =
6666
project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/LastPointToFirstPointDirectionVal" ) );
6767
mBothDirectionVal = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/BothDirectionVal" ) );
68-
mSpeed = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedField" ) );
68+
mSpeed = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedField" ) );
6969
mDefaultSpeed = project->readDoubleEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/defaultSpeed" ) );
70-
mLayerName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/layer" ) );
70+
mLayerName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/layer" ) );
7171
mSpeedUnitName = project->readEntry( QStringLiteral( "roadgraphplugin" ), QStringLiteral( "/speedUnitName" ) );
72-
73-
if ( dd == 1 )
74-
{
75-
mDefaultDirection = FirstPointToLastPoint;
76-
}
77-
else if ( dd == 2 )
78-
{
79-
mDefaultDirection = LastPointToFirstPoint;
80-
}
81-
else if ( dd == 3 )
82-
{
83-
mDefaultDirection = Both;
84-
}
85-
8672
} // RgLineVectorLayerSettings::read( const QgsProject *project )
8773

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

120106
if ( w->mcbDirectionDefault->currentIndex() == 0 )
121107
{
122-
mDefaultDirection = Both;
108+
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional;
123109
}
124110
else if ( w->mcbDirectionDefault->currentIndex() == 1 )
125111
{
126-
mDefaultDirection = FirstPointToLastPoint;
112+
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadDirect;
127113
}
128114
else if ( w->mcbDirectionDefault->currentIndex() == 2 )
129115
{
130-
mDefaultDirection = LastPointToFirstPoint;
116+
mDefaultDirection = QgsLineVectorLayerDirector::RoadDirection::RoadReversed;
131117
}
132118

133119
mSpeed = w->mcbSpeed->currentText();

src/plugins/roadgraph/linevectorlayersettings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// QT includes
1818
#include <qstring.h>
1919

20+
#include <qgslinevectorlayerdirector.h>
21+
2022
// Qgis includes
2123

2224
// standard includes
@@ -93,7 +95,7 @@ class RgLineVectorLayerSettings: public RgSettings
9395
/**
9496
* contained Default direction
9597
*/
96-
DirectionType mDefaultDirection;
98+
QgsLineVectorLayerDirector::RoadDirection mDefaultDirection;
9799

98100
/**
99101
* contained speed filed name

src/plugins/roadgraph/linevectorlayerwidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLa
143143

144144
switch ( s->mDefaultDirection )
145145
{
146-
case RgLineVectorLayerSettings::Both:
146+
case QgsLineVectorLayerDirector::RoadDirection::RoadBidirectional:
147147
mcbDirectionDefault->setCurrentIndex( 0 );
148148
break;
149-
case RgLineVectorLayerSettings::FirstPointToLastPoint:
149+
case QgsLineVectorLayerDirector::RoadDirection::RoadDirect:
150150
mcbDirectionDefault->setCurrentIndex( 1 );
151151
break;
152-
case RgLineVectorLayerSettings::LastPointToFirstPoint:
152+
case QgsLineVectorLayerDirector::RoadDirection::RoadReversed:
153153
mcbDirectionDefault->setCurrentIndex( 2 );
154154
break;
155155
}

0 commit comments

Comments
 (0)