Skip to content

Commit

Permalink
[roadgraph] Allow use of virtual and joined fields (fix #15041)
Browse files Browse the repository at this point in the history
Also modernise some code
  • Loading branch information
nyalldawson committed Jun 17, 2016
1 parent 5d54b5b commit 4411267
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 81 deletions.
19 changes: 9 additions & 10 deletions src/plugins/roadgraph/linevectorlayersettings.cpp
Expand Up @@ -19,6 +19,7 @@

// Qgis includes
#include <qgsproject.h>
#include "qgsmaplayercombobox.h"

// QT includes
#include <QLineEdit>
Expand All @@ -28,17 +29,15 @@
//standard includes

RgLineVectorLayerSettings::RgLineVectorLayerSettings()
: mDefaultDirection( Both )
, mDefaultSpeed( 40 )
{
mLayer = "";
mDirection = "";
mDefaultDirection = Both;
mSpeed = "";
mDefaultSpeed = 40;
}

RgLineVectorLayerSettings::~RgLineVectorLayerSettings()
{

}

bool RgLineVectorLayerSettings::test()
{
// implement me
Expand All @@ -48,7 +47,7 @@ bool RgLineVectorLayerSettings::test()
{
return false;
}
if ( mLayer == "" )
if ( mLayerName.isEmpty() )
{
return false;
}
Expand All @@ -68,7 +67,7 @@ void RgLineVectorLayerSettings::read( const QgsProject *project )
mBothDirectionVal = project->readEntry( "roadgraphplugin", "/BothDirectionVal" );
mSpeed = project->readEntry( "roadgraphplugin", "/speedField" );
mDefaultSpeed = project->readDoubleEntry( "roadgraphplugin", "/defaultSpeed" );
mLayer = project->readEntry( "roadgraphplugin", "/layer" );
mLayerName = project->readEntry( "roadgraphplugin", "/layer" );
mSpeedUnitName = project->readEntry( "roadgraphplugin", "/speedUnitName" );

if ( dd == 1 )
Expand Down Expand Up @@ -97,7 +96,7 @@ void RgLineVectorLayerSettings::write( QgsProject *project )
project->writeEntry( "roadgraphplugin", "/BothDirectionVal", mBothDirectionVal );
project->writeEntry( "roadgraphplugin", "/speedField", mSpeed );
project->writeEntry( "roadgraphplugin", "/defaultSpeed", mDefaultSpeed );
project->writeEntry( "roadgraphplugin", "/layer", mLayer );
project->writeEntry( "roadgraphplugin", "/layer", mLayerName );
project->writeEntry( "roadgraphplugin", "/speedUnitName", mSpeedUnitName );
} // RgLineVectorLayerSettings::write( QgsProject *project )

Expand All @@ -116,7 +115,7 @@ void RgLineVectorLayerSettings::setFromGui( QWidget *myGui )
mLastPointToFirstPointDirectionVal = w->mleLastPointToFirstPointDirection->text();
mBothDirectionVal = w->mleBothDirection->text();
mDirection = w->mcbDirection->currentText();
mLayer = w->mcbLayers->currentText();
mLayerName = w->mcbLayers->currentText();

if ( w->mcbDirectionDefault->currentIndex() == 0 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/roadgraph/linevectorlayersettings.h
Expand Up @@ -65,7 +65,7 @@ class RgLineVectorLayerSettings: public RgSettings
/**
* contained Layer name
*/
QString mLayer;
QString mLayerName;

/**
* contained direction field name
Expand Down
59 changes: 14 additions & 45 deletions src/plugins/roadgraph/linevectorlayerwidget.cpp
Expand Up @@ -26,9 +26,8 @@

// Qgis includes
#include "qgsfield.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayercombobox.h"

//standard includes

Expand All @@ -46,7 +45,8 @@ RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLa
tab->addTab( frame, tr( "Transportation layer" ) );
v = new QVBoxLayout( frame );
QLabel *l = new QLabel( tr( "Layer" ), frame );
mcbLayers = new QComboBox( frame );
mcbLayers = new QgsMapLayerComboBox( frame );
mcbLayers->setFilters( QgsMapLayerProxyModel::LineLayer );
QHBoxLayout *h = new QHBoxLayout();

h->addWidget( l );
Expand Down Expand Up @@ -122,29 +122,15 @@ RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLa
h->addWidget( msbSpeedDefault );
v->addLayout( h );


// fill list of layers
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();

for ( ; layer_it != mapLayers.end(); ++layer_it )
{
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
if ( !vl )
continue;
if ( vl->wkbType() == QGis::WKBLineString
|| vl->wkbType() == QGis::WKBMultiLineString )
mcbLayers->insertItem( 0, vl->name() );
}

//sets current settings
msbSpeedDefault->setValue( static_cast<int>( s->mDefaultSpeed ) );

int idx = mcbLayers->findText( s->mLayer );
int idx = mcbLayers->findText( s->mLayerName );
if ( idx != -1 )
{
mcbLayers->setCurrentIndex( idx );
}
on_mcbLayers_selectItem();

idx = mcbDirection->findText( s->mDirection );
if ( idx != -1 )
Expand Down Expand Up @@ -172,26 +158,12 @@ RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget( RgLineVectorLa
else if ( s->mSpeedUnitName == "m/s" )
mcbUnitOfSpeed->setCurrentIndex( 0 );

} // RgLineVectorLayerSettingsWidget::RgLineVectorLayerSettingsWidget()
}

QgsVectorLayer* RgLineVectorLayerSettingsWidget::selectedLayer()
{
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();

for ( ; layer_it != mapLayers.end(); ++layer_it )
{
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
if ( !vl )
continue;
if ( vl->geometryType() != QGis::Line )
continue;
if ( vl->name() == mcbLayers->currentText() )
return vl;
}

return nullptr;
} // RgLineVectorLayerSettingsWidget::setlectedLayer()
return dynamic_cast< QgsVectorLayer* >( mcbLayers->currentLayer() );
}

void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
{
Expand All @@ -205,21 +177,18 @@ void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
if ( !vl )
return;

QgsVectorDataProvider* provider = vl->dataProvider();
if ( !provider )
return;

Q_FOREACH ( const QgsField& currentField, provider->fields() )
Q_FOREACH ( const QgsField& currentField, vl->fields() )
{
QVariant currentType = currentField.type();
if ( currentType == QVariant::Int || currentType == QVariant::String )
if ( currentType == QVariant::Int || currentType == QVariant::LongLong ||
currentType == QVariant::String )
{
mcbDirection->insertItem( 1, currentField.name() );
}
if ( currentType == QVariant::Int || currentType == QVariant::Double )
if ( currentType == QVariant::Int || currentType == QVariant::LongLong ||
currentType == QVariant::Double )
{
mcbSpeed->insertItem( 1, currentField.name() );
}
}

} // RgDSettingsDlg::on_mcbLayers_selectItem()
}
3 changes: 2 additions & 1 deletion src/plugins/roadgraph/linevectorlayerwidget.h
Expand Up @@ -28,6 +28,7 @@ class QLineEdit;

// forward declaration Qgis-classes
class QgsVectorLayer;
class QgsMapLayerComboBox;

/**
@author Sergey Yakushev
Expand All @@ -52,7 +53,7 @@ class RgLineVectorLayerSettingsWidget : public QWidget
/**
* list of passible layers
*/
QComboBox *mcbLayers;
QgsMapLayerComboBox *mcbLayers;

/**
* list of possible fields for use as direction
Expand Down
37 changes: 13 additions & 24 deletions src/plugins/roadgraph/roadgraphplugin.cpp
Expand Up @@ -122,7 +122,7 @@ void RoadGraphPlugin::initGui()

// load settings
projectRead();
} // RoadGraphPlugin::initGui()
}

// Unload the plugin by cleaning up the GUI
void RoadGraphPlugin::unload()
Expand All @@ -136,24 +136,23 @@ void RoadGraphPlugin::unload()

delete mQSettingsAction;
delete mQShortestPathDock;
} // RoadGraphPlugin::unload()
}

void RoadGraphPlugin::setGuiElementsToDefault()
{

} // RoadGraphPlugin::setGuiElementsToDefault()
}

//method defined in interface
void RoadGraphPlugin::help()
{
//implement me!
} // RoadGraphPlugin::help()
}

void RoadGraphPlugin::onShowDirection()
{
mQGisIface->mapCanvas()->refresh();
} // RoadGraphPlugin::onShowDirection()

}

void RoadGraphPlugin::newProject()
{
Expand All @@ -180,7 +179,7 @@ void RoadGraphPlugin::property()
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginDistanceUnit", mDistanceUnitName );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/topologyToleranceFactor", mTopologyToleranceFactor );
setGuiElementsToDefault();
} //RoadGraphPlugin::property()
}

void RoadGraphPlugin::projectRead()
{
Expand All @@ -190,7 +189,7 @@ void RoadGraphPlugin::projectRead()
mTopologyToleranceFactor =
QgsProject::instance()->readDoubleEntry( "roadgraphplugin", "/topologyToleranceFactor", 0.0 );
setGuiElementsToDefault();
}// RoadGraphplguin::projectRead()
}

QgisInterface* RoadGraphPlugin::iface()
{
Expand All @@ -199,39 +198,29 @@ QgisInterface* RoadGraphPlugin::iface()

const QgsGraphDirector* RoadGraphPlugin::director() const
{
QgsVectorLayer *layer = nullptr;
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;
layer = dynamic_cast< QgsVectorLayer* >( it.value() );
break;
}
QList< QgsMapLayer* > mapLayers = QgsMapLayerRegistry::instance()->mapLayersByName( mSettings->mLayerName );
if ( mapLayers.isEmpty() )
return nullptr;

QgsVectorLayer *layer = dynamic_cast< QgsVectorLayer* >( mapLayers.at( 0 ) );
if ( !layer )
return nullptr;

if ( layer->wkbType() == QGis::WKBLineString
|| layer->wkbType() == QGis::WKBMultiLineString )
{
QgsVectorDataProvider *provider = layer->dataProvider();
if ( !provider )
return nullptr;

SpeedUnit speedUnit = SpeedUnit::byName( mSettings->mSpeedUnitName );

QgsLineVectorLayerDirector * director =
new QgsLineVectorLayerDirector( layer,
provider->fieldNameIndex( mSettings->mDirection ),
layer->fields().fieldNameIndex( mSettings->mDirection ),
mSettings->mFirstPointToLastPointDirectionVal,
mSettings->mLastPointToFirstPointDirectionVal,
mSettings->mBothDirectionVal,
mSettings->mDefaultDirection
);
director->addProperter( new QgsDistanceArcProperter() );
director->addProperter( new RgSpeedProperter( provider->fieldNameIndex( mSettings->mSpeed ),
director->addProperter( new RgSpeedProperter( layer->fields().fieldNameIndex( mSettings->mSpeed ),
mSettings->mDefaultSpeed, speedUnit.multipler() ) );
return director;
}
Expand Down

0 comments on commit 4411267

Please sign in to comment.