[QGIS Commit] r15093 - trunk/qgis/src/plugins/roadgraph

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Jan 28 05:50:34 EST 2011


Author: stopa85
Date: 2011-01-28 02:50:34 -0800 (Fri, 28 Jan 2011)
New Revision: 15093

Modified:
   trunk/qgis/src/plugins/roadgraph/graphdirector.h
   trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.cpp
   trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.h
   trunk/qgis/src/plugins/roadgraph/roadgraphplugin.cpp
   trunk/qgis/src/plugins/roadgraph/roadgraphplugin.h
   trunk/qgis/src/plugins/roadgraph/settingsdlg.cpp
   trunk/qgis/src/plugins/roadgraph/settingsdlg.h
   trunk/qgis/src/plugins/roadgraph/shortestpathwidget.cpp
Log:
Separate gui-part and graph construction part. Added message box in shortestpath widget.

Modified: trunk/qgis/src/plugins/roadgraph/graphdirector.h
===================================================================
--- trunk/qgis/src/plugins/roadgraph/graphdirector.h	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/graphdirector.h	2011-01-28 10:50:34 UTC (rev 15093)
@@ -41,11 +41,6 @@
     virtual void makeGraph( RgGraphBuilder * ) const = 0;
 
     /**
-     * return pointer to my Settings
-     */
-    virtual RgSettings* settings() = 0;
-
-    /**
      * return Director name
      */
     virtual QString name() const = 0;

Modified: trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.cpp
===================================================================
--- trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.cpp	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.cpp	2011-01-28 10:50:34 UTC (rev 15093)
@@ -14,7 +14,6 @@
  * \brief implementation of RgLineVectorLayerDirector
  */
 
-#include "linevectorlayersettings.h"
 #include "linevectorlayerdirector.h"
 #include "graphbuilder.h"
 #include "units.h"
@@ -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
 {
@@ -50,69 +63,51 @@
 
 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;
-    }
-  }
+  QgsVectorLayer *vl = myLayer();
+
   if ( vl == NULL )
     return;
 
-  QgsVectorDataProvider *provider = dynamic_cast<QgsVectorDataProvider*>( vl->dataProvider() );
-  if ( provider == 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;
       }
@@ -120,7 +115,7 @@
     }
     if ( speed <= 0.0 )
     {
-      speed = mSettings.mDefaultSpeed;
+      speed = mDefaultSpeed;
     }
 
     // begin features segments and add arc to the Graph;
@@ -134,13 +129,13 @@
       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() );
         }
@@ -151,3 +146,15 @@
 
   } // 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() );
+}

Modified: trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.h
===================================================================
--- trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.h	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/linevectorlayerdirector.h	2011-01-28 10:50:34 UTC (rev 15093)
@@ -21,10 +21,10 @@
 
 // Road-graph plugin includes
 #include "graphdirector.h"
-#include "linevectorlayersettings.h"
 
 //forward declarations
 class RgGraphBuilder;
+class QgsVectorLayer;
 
 /**
 * \class RgLineVectorLayerDirector
@@ -33,7 +33,16 @@
 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();
     /**
@@ -41,13 +50,31 @@
      */
     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

Modified: trunk/qgis/src/plugins/roadgraph/roadgraphplugin.cpp
===================================================================
--- trunk/qgis/src/plugins/roadgraph/roadgraphplugin.cpp	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/roadgraphplugin.cpp	2011-01-28 10:50:34 UTC (rev 15093)
@@ -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
 //
@@ -70,7 +74,7 @@
 {
 
   mQShortestPathDock = NULL;
-  mDirector = new RgLineVectorLayerDirector();
+  mSettings = new RgLineVectorLayerSettings();
   mTimeUnitName = "h";
   mDistanceUnitName = "km";
 }
@@ -166,17 +170,18 @@
 
 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 );
 
@@ -236,7 +241,7 @@
 
 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();
@@ -249,17 +254,51 @@
 
 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;
@@ -293,7 +332,7 @@
       painter->drawPolygon( tmp );
     }
   }
-
+  delete graphDirector;
 }// RoadGraphPlugin::render()
 QString RoadGraphPlugin::timeUnitName()
 {

Modified: trunk/qgis/src/plugins/roadgraph/roadgraphplugin.h
===================================================================
--- trunk/qgis/src/plugins/roadgraph/roadgraphplugin.h	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/roadgraphplugin.h	2011-01-28 10:50:34 UTC (rev 15093)
@@ -32,6 +32,7 @@
 //forward declarations RoadGraph plugins classes
 class RgGraphDirector;
 class RgShortestPathWidget;
+class RgLineVectorLayerSettings;
 
 /**
 * \class RoadGraphPlugin
@@ -56,7 +57,7 @@
     QgisInterface *iface();
 
     /**
-     * return pointer to my Graph director
+     * return pointer to graph director
      */
     const RgGraphDirector* director() const;
 
@@ -140,9 +141,10 @@
     RgShortestPathWidget *mQShortestPathDock;
 
     /**
-     * My Graph Director
+     * My graph settings.
+     * @note. Should be used RgSettings
      */
-    RgGraphDirector *mDirector;
+    RgLineVectorLayerSettings *mSettings;
 
     /**
      *  time unit for results presentation

Modified: trunk/qgis/src/plugins/roadgraph/settingsdlg.cpp
===================================================================
--- trunk/qgis/src/plugins/roadgraph/settingsdlg.cpp	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/settingsdlg.cpp	2011-01-28 10:50:34 UTC (rev 15093)
@@ -21,13 +21,12 @@
 
 
 // Qgis includes
-#include "graphdirector.h"
 #include "settings.h"
 
 //standard includes
 
-RgSettingsDlg::RgSettingsDlg( RgGraphDirector *director, QWidget* parent, Qt::WFlags fl )
-    : mDirector( director ), QDialog( parent, fl )
+RgSettingsDlg::RgSettingsDlg( RgSettings *settings, QWidget* parent, Qt::WFlags fl )
+    : mSettings( settings ), QDialog( parent, fl )
 {
   // create base widgets;
   setWindowTitle( tr( "Road graph plugins settings" ) );
@@ -55,7 +54,8 @@
   h->addWidget(mcbGraphDirector);
   v->addLayout(h);
   */
-  mSettingsWidget = director->settings()->getGui( this );
+
+  mSettingsWidget = mSettings->getGui( this );
   v->addWidget( mSettingsWidget );
 
   QDialogButtonBox *bb = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
@@ -76,7 +76,7 @@
 
 void RgSettingsDlg::on_buttonBox_accepted()
 {
-  mDirector->settings()->setFromGui( mSettingsWidget );
+  mSettings->setFromGui( mSettingsWidget );
   accept();
 }
 

Modified: trunk/qgis/src/plugins/roadgraph/settingsdlg.h
===================================================================
--- trunk/qgis/src/plugins/roadgraph/settingsdlg.h	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/settingsdlg.h	2011-01-28 10:50:34 UTC (rev 15093)
@@ -23,7 +23,7 @@
 // forward declaration Qgis-classes
 
 //forward declaration RoadGraph plugins classes
-class RgGraphDirector;
+class RgSettings;
 
 /**
 @author Sergey Yakushev
@@ -36,7 +36,7 @@
 {
     Q_OBJECT
   public:
-    RgSettingsDlg( RgGraphDirector *director, QWidget* parent = 0, Qt::WFlags fl = 0 );
+    RgSettingsDlg( RgSettings *settings, QWidget* parent = 0, Qt::WFlags fl = 0 );
     ~RgSettingsDlg();
 
     QString timeUnitName();
@@ -60,9 +60,9 @@
     QComboBox *mcbGraphDirector;
 
     /**
-     * current graph director
+     * current graph settings object
      */
-    RgGraphDirector *mDirector;
+    RgSettings *mSettings;
 
     QWidget *mSettingsWidget;
 

Modified: trunk/qgis/src/plugins/roadgraph/shortestpathwidget.cpp
===================================================================
--- trunk/qgis/src/plugins/roadgraph/shortestpathwidget.cpp	2011-01-28 09:02:35 UTC (rev 15092)
+++ trunk/qgis/src/plugins/roadgraph/shortestpathwidget.cpp	2011-01-28 10:50:34 UTC (rev 15093)
@@ -24,6 +24,7 @@
 #include <qlabel.h>
 #include <qlineedit.h>
 #include <QToolButton>
+#include <QMessageBox>
 
 // Qgis includes
 #include <qgsmapcanvas.h>
@@ -224,15 +225,28 @@
     return false;
   RgSimpleGraphBuilder builder;
   builder.setDestinationCrs( mPlugin->iface()->mapCanvas()->mapRenderer()->destinationSrs() );
-  mPlugin->director()->makeGraph( &builder );
+  {
+    const RgGraphDirector *director = mPlugin->director();
+    director->makeGraph( &builder );
+  
+    // not need
+    delete director;
+  }
+
   bool ok;
 
   p1 = builder.tiePoint( mFrontPoint, ok );
   if ( !ok )
+  {
+    QMessageBox::critical (this, tr("Tie point failed"), tr("Start point don't tie to the road!") );
     return false;
+  }
   p2 = builder.tiePoint( mBackPoint, ok );
   if ( !ok )
+  {
+    QMessageBox::critical (this, tr("Tie point failed"), tr("Stop point don't tie to the road!") );
     return false;
+  }
   AdjacencyMatrix m = builder.adjacencyMatrix();
 
   DijkstraFinder::OptimizationCriterion criterion = DijkstraFinder::byCost;



More information about the QGIS-commit mailing list