bug767fix.diff

First try at fixing the 767 OGR multilayer bug - falahdab -, 2009-01-25 09:00 AM

Download (30.1 KB)

View differences:

src/plugins/ogrsublayers/ogrsublayers.cpp (révision 0)
1
/***************************************************************************
2
  ogrsublayers.cpp
3
  This plugin allows the user to choose which layers to load from an OGR multilayer datasource.
4
  -------------------
5
         begin                : [PluginDate]
6
         copyright            : [(C) Your Name and Date]
7
         email                : [Your Email]
8

  
9
 ***************************************************************************
10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 ***************************************************************************/
17
/*  $Id: plugin.cpp 9327 2008-09-14 11:18:44Z jef $ */
18

  
19
//
20
// QGIS Specific includes
21
//
22

  
23

  
24
#include <qgisinterface.h>
25
#include <qgisgui.h>
26
#include <qgslogger.h>
27
#include <qgsdataprovider.h>
28
#include <qgsvectordataprovider.h>
29
#include <qgsmaplayerregistry.h>
30

  
31
#include "ogrsublayers.h"
32
#include "ogrsublayersgui.h"
33
#include "../../providers/ogr/qgsogrprovider.h"
34

  
35
//
36
// Qt4 Related Includes
37
//
38

  
39
#include <QAction>
40
#include <QToolBar>
41
#include <QTableWidgetItem>
42
#include <QFileInfo>
43
#include <QMessageBox>
44

  
45
static const char * const sIdent = "$Id: plugin.cpp 9327 2008-09-14 11:18:44Z jef $";
46
static const QString sName = QObject::tr( "OGR SubLayers" );
47
static const QString sDescription = QObject::tr( "This plugin allows the user to choose which layers to load from an OGR multilayer datasource." );
48
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
49
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
50

  
51
//////////////////////////////////////////////////////////////////////
52
//
53
// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
54
//
55
//////////////////////////////////////////////////////////////////////
56

  
57
/**
58
 * Constructor for the plugin. The plugin is passed a pointer
59
 * an interface object that provides access to exposed functions in QGIS.
60
 * @param theQGisInterface - Pointer to the QGIS interface object
61
 */
62
OGRSubLayers::OGRSubLayers( QgisInterface * theQgisInterface ):
63
    QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
64
    mQGisIface( theQgisInterface )
65
{
66
}
67

  
68
OGRSubLayers::~OGRSubLayers()
69
{
70
}
71

  
72
/*
73
 * Initialize the GUI interface for the plugin - this is only called once when the plugin is
74
 * added to the plugin registry in the QGIS application.
75
 */
76
void OGRSubLayers::initGui()
77
{
78

  
79
  // Create the action for tool
80
  mQActionPointer = new QAction( QIcon( ":/ogrsublayers/ogrsublayers.png" ), tr( "OGR SubLayers" ), this );
81
  // Set the what's this text
82
  mQActionPointer->setWhatsThis( tr( "Replace this with a short description of what the plugin does" ) );
83
  // Connect the action to the run
84
  connect( mQActionPointer, SIGNAL( triggered() ), this, SLOT( run() ) );
85
  // Add the icon to the toolbar
86
  mQGisIface->addToolBarIcon( mQActionPointer );
87
  mQGisIface->addPluginToMenu( tr( "&OGR SubLayers" ), mQActionPointer );
88
  
89

  
90
}
91
//method defined in interface
92
void OGRSubLayers::help()
93
{
94
  //implement me!
95
}
96

  
97
// Slot called when the menu item is triggered
98
// If you created more menu items / toolbar buttons in initiGui, you should
99
// create a separate handler for each action - this single run() method will
100
// not be enough
101
void OGRSubLayers::run()
102
{
103
  OGRSubLayersGui *myPluginGui;
104
  if ( mQGisIface->activeLayer() )
105
  {
106
    if ( mQGisIface->activeLayer()->type() == QgsMapLayer::VectorLayer )
107
    {
108
      // This plugin is OGR specific until proved safe to use with other providers.
109
      if (0 == (( QgsVectorLayer *)mQGisIface->activeLayer())->dataProvider()->name().compare("ogr"))
110
      {
111
        uri=mQGisIface->activeLayer()->source();
112
        // If there is no & in the uri, then the uri is just the filename. The loaded
113
        // layer will be layer 0.
114
        if ( uri.contains('&', Qt::CaseSensitive))
115
        {
116
        // If we get here, there are some options added to the filename. We must parse
117
        // the different parts separated by &, and among each option, the name and the
118
        // value around the =.
119
        // A valid uri is of the form: filename&option1=value1&option2=value2,...
120
           QStringList theURIParts = uri.split("&");
121
          uri = theURIParts.at( 0 );
122
        }
123
        QgsOgrProvider* theCurrentProvider = ( QgsOgrProvider * )((( QgsVectorLayer *)mQGisIface->activeLayer())->dataProvider());
124
      
125
        // We get the subLayers of activeLayer.dataProvider and we show them in the dialogbox.
126
        QStringList liste = theCurrentProvider->subLayers();
127
        if ( liste.count() < 2)
128
        {
129
          QMessageBox *myBox = new QMessageBox( QMessageBox::Information, "No sublayers!", "There are no sublayers in the active layer. Select another layer to activate the plugin.", QMessageBox::Ok, mQGisIface->mainWindow(),Qt::MSWindowsFixedSizeDialogHint);
130
          myBox -> setModal(true);
131
          myBox -> show();
132
          return;
133
        }else
134
        {
135
          myPluginGui = new OGRSubLayersGui( mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );
136
          myPluginGui->setAttribute( Qt::WA_DeleteOnClose );
137
          myPluginGui->show();
138
          connect( myPluginGui, SIGNAL( updated(QStringList * ) ), this, SLOT( load(QStringList *  ) ) );
139
          myPluginGui->populateLayerTable( & liste );
140
        }
141
  
142
        // We want to select the already selected layers so the user can choose to add or remove
143
        // others
144
        QgsMapLayerRegistry * theLayerRegistry = QgsMapLayerRegistry::instance();
145
        QList<QgsVectorLayer * > theVectorLayersList = QList<QgsVectorLayer * >();
146
        QMapIterator<QString, QgsMapLayer*> iterator(theLayerRegistry -> mapLayers());
147
        while (iterator.hasNext()){
148
          iterator.next();
149
          if (iterator.value()->type() == QgsMapLayer::VectorLayer){
150
            theVectorLayersList.append ( (QgsVectorLayer*) (iterator.value()) );
151
          }
152
        }
153
        QgsLogger::critical("Nb of vector layers loaded: " + QString::number( theVectorLayersList.size()));
154
        myPluginGui->updateSelection();
155
      }
156
    }else // activeLayer -> type != VectorLayer
157
    {
158
      QMessageBox *myBox = new QMessageBox( QMessageBox::Information, "Not a vector layer", "This plugin is OGR specific! Select another layer to activate the plugin.", QMessageBox::Ok, mQGisIface->mainWindow(),Qt::MSWindowsFixedSizeDialogHint);
159
      myBox -> setModal(true);
160
      myBox -> show();
161
      return;
162
    }
163
  }
164
}
165

  
166
void OGRSubLayers::load(QStringList * list)
167
{
168
  QString fileName = QFileInfo(uri).baseName();
169
  for (int i = 0; i < list->size(); i++)
170
  {
171
    QString composedURI=uri+"&layername="+list->at(i);    
172
    mQGisIface-> addVectorLayer(composedURI,fileName+":"+list->at(i),"ogr");
173
  }
174
}
175

  
176
// Unload the plugin by cleaning up the GUI
177
void OGRSubLayers::unload()
178
{
179
  // remove the GUI
180
  mQGisIface->removePluginMenu( "&OGR SubLayers", mQActionPointer );
181
  mQGisIface->removeToolBarIcon( mQActionPointer );
182
  delete mQActionPointer;
183
}
184

  
185

  
186
//////////////////////////////////////////////////////////////////////////
187
//
188
//
189
//  THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
190
//    YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
191
//      MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
192
//
193
//
194
//////////////////////////////////////////////////////////////////////////
195

  
196

  
197
/**
198
 * Required extern functions needed  for every plugin
199
 * These functions can be called prior to creating an instance
200
 * of the plugin class
201
 */
202
// Class factory to return a new instance of the plugin class
203
QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
204
{
205
  return new OGRSubLayers( theQgisInterfacePointer );
206
}
207
// Return the name of the plugin - note that we do not user class members as
208
// the class may not yet be insantiated when this method is called.
209
QGISEXTERN QString name()
210
{
211
  return sName;
212
}
213

  
214
// Return the description
215
QGISEXTERN QString description()
216
{
217
  return sDescription;
218
}
219

  
220
// Return the type (either UI or MapLayer plugin)
221
QGISEXTERN int type()
222
{
223
  return sPluginType;
224
}
225

  
226
// Return the version number for the plugin
227
QGISEXTERN QString version()
228
{
229
  return sPluginVersion;
230
}
231

  
232
// Delete ourself
233
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
234
{
235
  delete thePluginPointer;
236
}
src/plugins/ogrsublayers/ogrsublayers.qrc (révision 0)
1
<RCC>
2
    <qresource prefix="/ogrsublayers/" >
3
        <file>ogrsublayers.png</file>
4
    </qresource>
5
</RCC>
src/plugins/ogrsublayers/ogrsublayersgui.cpp (révision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2003 by Tim Sutton                                      *
3
 *   [email protected]                                                     *
4
 *                                                                         *
5
 *   This is a plugin generated from the QGIS plugin template              *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 ***************************************************************************/
12
#include "ogrsublayersgui.h"
13
#include "qgscontexthelp.h"
14

  
15
//qt includes
16

  
17
//standard includes
18

  
19
OGRSubLayersGui::OGRSubLayersGui( QWidget* parent, Qt::WFlags fl )
20
    : QDialog( parent, fl )
21
{
22
  setupUi( this );
23
  layersTable->setColumnCount(3);
24
  QStringList labels;
25
  labels << tr("Layer ID") << tr("Layer name") << tr("Nb of features");
26
  layersTable->setHorizontalHeaderLabels(labels);
27
  layersTable->setColumnWidth(0,188);
28
  layersTable->setColumnWidth(1,188);
29
  layersTable->setColumnWidth(2,188);
30
  layersTable->setShowGrid(false);
31
}
32

  
33
OGRSubLayersGui::~OGRSubLayersGui()
34
{
35
}
36

  
37
void OGRSubLayersGui::on_buttonBox_accepted()
38
{
39
  //close the dialog
40
  QStringList list=QStringList();
41
  for (int i = 0; i < layersTable-> selectedItems().size(); i++)
42
  {
43
    if (1 == layersTable-> selectedItems().at(i)->column())
44
    {
45
      list.append(layersTable-> selectedItems().at(i)->text() );
46
    }
47
  }
48
  emit updated( &list );
49
  accept();
50
}
51

  
52
void OGRSubLayersGui::on_buttonBox_rejected()
53
{
54
  reject();
55
}
56

  
57
void OGRSubLayersGui::populateLayerTable (QStringList *theList)
58
{
59
  for (int i =0; i< theList->size(); i++){
60
    QString ligne = theList -> at(i);
61
    QStringList elements = ligne.split(":");
62

  
63
    QTableWidgetItem *layerIDItem = new QTableWidgetItem(elements.at(0));
64
    QTableWidgetItem *layerNameItem = new QTableWidgetItem(elements.at(1));
65
    QTableWidgetItem *featuresItem = new QTableWidgetItem(elements.at(2));
66

  
67
    int row = layersTable->rowCount();
68
    layersTable->insertRow(row);
69
    layersTable->setItem(row, 0, layerIDItem);
70
    layersTable->setItem(row, 1, layerNameItem);
71
    layersTable->setItem(row, 2, featuresItem);
72
  }  
73
}
74

  
75
void  OGRSubLayersGui::updateSelection()
76
{
77
}
src/plugins/ogrsublayers/ogrsublayers.h (révision 0)
1
/***************************************************************************
2
    ogrsublayers.h
3
    -------------------
4
    begin                : Jan 21, 2004
5
    copyright            : (C) 2004 by Tim Sutton
6
    email                : [email protected]
7

  
8
 ***************************************************************************/
9

  
10
/***************************************************************************
11
 *                                                                         *
12
 *   This program is free software; you can redistribute it and/or modify  *
13
 *   it under the terms of the GNU General Public License as published by  *
14
 *   the Free Software Foundation; either version 2 of the License, or     *
15
 *   (at your option) any later version.                                   *
16
 *                                                                         *
17
 ***************************************************************************/
18
/*  $Id: plugin.h 9138 2008-08-23 21:37:31Z jef $ */
19
/***************************************************************************
20
 *   QGIS Programming conventions:
21
 *
22
 *   mVariableName - a class level member variable
23
 *   sVariableName - a static class level member variable
24
 *   variableName() - accessor for a class member (no 'get' in front of name)
25
 *   setVariableName() - mutator for a class member (prefix with 'set')
26
 *
27
 *   Additional useful conventions:
28
 *
29
 *   theVariableName - a method parameter (prefix with 'the')
30
 *   myVariableName - a locally declared variable within a method ('my' prefix)
31
 *
32
 *   DO: Use mixed case variable names - myVariableName
33
 *   DON'T: separate variable names using underscores: my_variable_name (NO!)
34
 *
35
 * **************************************************************************/
36
#ifndef OGRSubLayers_H
37
#define OGRSubLayers_H
38

  
39
//QT4 includes
40
#include <QObject>
41

  
42
//QGIS includes
43
#include "../qgisplugin.h"
44

  
45
//forward declarations
46
class QAction;
47
class QToolBar;
48

  
49
class QgisInterface;
50

  
51
/**
52
* \class Plugin
53
* \brief [name] plugin for QGIS
54
* [description]
55
*/
56
class OGRSubLayers: public QObject, public QgisPlugin
57
{
58
    Q_OBJECT
59
  public:
60

  
61
    //////////////////////////////////////////////////////////////////////
62
    //
63
    //                MANDATORY PLUGIN METHODS FOLLOW
64
    //
65
    //////////////////////////////////////////////////////////////////////
66

  
67
    /**
68
    * Constructor for a plugin. The QgisInterface pointer is passed by
69
    * QGIS when it attempts to instantiate the plugin.
70
    * @param theInterface Pointer to the QgisInterface object.
71
     */
72
    OGRSubLayers( QgisInterface * theInterface );
73
    //! Destructor
74
    virtual ~OGRSubLayers();
75

  
76

  
77
  public slots:
78
    //! init the gui
79
    virtual void initGui();
80
    //! Show the dialog box
81
    void run();
82
    //! unload the plugin
83
    void unload();
84
    //! show the help document
85
    void help();
86
    void load( QStringList * list);
87

  
88

  
89
  private:
90
    QString uri;
91
    ////////////////////////////////////////////////////////////////////
92
    //
93
    // MANDATORY PLUGIN PROPERTY DECLARATIONS  .....
94
    //
95
    ////////////////////////////////////////////////////////////////////
96

  
97
    int mPluginType;
98
    //! Pointer to the QGIS interface object
99
    QgisInterface *mQGisIface;
100
    //!pointer to the qaction for this plugin
101
    QAction * mQActionPointer;
102
    ////////////////////////////////////////////////////////////////////
103
    //
104
    // ADD YOUR OWN PROPERTY DECLARATIONS AFTER THIS POINT.....
105
    //
106
    ////////////////////////////////////////////////////////////////////
107
};
108

  
109
#endif //OGRSubLayers_H
src/plugins/ogrsublayers/ogrsublayersgui.h (révision 0)
1
/***************************************************************************
2
 *   Copyright (C) 2003 by Tim Sutton                                      *
3
 *   [email protected]                                                     *
4
 *                                                                         *
5
 *   This is a plugin generated from the QGIS plugin template              *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 ***************************************************************************/
12
#ifndef OGRSubLayersGUI_H
13
#define OGRSubLayersGUI_H
14

  
15
#include <QDialog>
16
#include <QTableWidgetItem>
17
#include <ui_ogrsublayersguibase.h>
18

  
19
#include <qgslogger.h>
20

  
21
/**
22
@author Tim Sutton
23
*/
24
class OGRSubLayersGui : public QDialog, private Ui::OGRSubLayersGuiBase
25
{
26
    Q_OBJECT
27
  public:
28
    OGRSubLayersGui( QWidget* parent = 0, Qt::WFlags fl = 0 );
29
    ~OGRSubLayersGui();
30
    void populateLayerTable (QStringList *theList);
31
    QStringList * getSelection ();
32
    void updateSelection();
33

  
34
  private:
35
    static const int context_id = 0;
36

  
37
  private slots:
38
    void on_buttonBox_accepted();
39
    void on_buttonBox_rejected();
40

  
41
  signals:
42
    void updated(QStringList*list);
43

  
44
};
45

  
46
#endif
src/plugins/ogrsublayers/ogrsublayersguibase.ui (révision 0)
1
<ui version="4.0" >
2
 <class>OGRSubLayersGuiBase</class>
3
 <widget class="QDialog" name="OGRSubLayersGuiBase" >
4
  <property name="geometry" >
5
   <rect>
6
    <x>0</x>
7
    <y>0</y>
8
    <width>584</width>
9
    <height>535</height>
10
   </rect>
11
  </property>
12
  <property name="windowTitle" >
13
   <string>Select OGR layers to load</string>
14
  </property>
15
  <property name="windowIcon" >
16
   <iconset/>
17
  </property>
18
  <layout class="QGridLayout" >
19
   <property name="leftMargin" >
20
    <number>9</number>
21
   </property>
22
   <property name="topMargin" >
23
    <number>9</number>
24
   </property>
25
   <property name="rightMargin" >
26
    <number>9</number>
27
   </property>
28
   <property name="bottomMargin" >
29
    <number>9</number>
30
   </property>
31
   <property name="horizontalSpacing" >
32
    <number>6</number>
33
   </property>
34
   <property name="verticalSpacing" >
35
    <number>6</number>
36
   </property>
37
   <item row="0" column="0" >
38
    <widget class="QLabel" name="txtHeading" >
39
     <property name="sizePolicy" >
40
      <sizepolicy vsizetype="Minimum" hsizetype="Preferred" >
41
       <horstretch>0</horstretch>
42
       <verstretch>0</verstretch>
43
      </sizepolicy>
44
     </property>
45
     <property name="font" >
46
      <font>
47
       <family>Sans Serif</family>
48
       <pointsize>24</pointsize>
49
       <weight>75</weight>
50
       <italic>false</italic>
51
       <bold>true</bold>
52
       <underline>false</underline>
53
       <strikeout>false</strikeout>
54
      </font>
55
     </property>
56
     <property name="text" >
57
      <string>Sub layers list</string>
58
     </property>
59
     <property name="alignment" >
60
      <set>Qt::AlignCenter</set>
61
     </property>
62
    </widget>
63
   </item>
64
   <item row="6" column="0" >
65
    <widget class="QDialogButtonBox" name="buttonBox" >
66
     <property name="orientation" >
67
      <enum>Qt::Horizontal</enum>
68
     </property>
69
     <property name="standardButtons" >
70
      <set>QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok</set>
71
     </property>
72
    </widget>
73
   </item>
74
   <item row="2" column="0" >
75
    <widget class="QTableWidget" name="layersTable" >
76
     <property name="minimumSize" >
77
      <size>
78
       <width>564</width>
79
       <height>274</height>
80
      </size>
81
     </property>
82
     <property name="maximumSize" >
83
      <size>
84
       <width>564</width>
85
       <height>274</height>
86
      </size>
87
     </property>
88
     <property name="editTriggers" >
89
      <set>QAbstractItemView::NoEditTriggers</set>
90
     </property>
91
     <property name="selectionMode" >
92
      <enum>QAbstractItemView::MultiSelection</enum>
93
     </property>
94
     <property name="selectionBehavior" >
95
      <enum>QAbstractItemView::SelectRows</enum>
96
     </property>
97
    </widget>
98
   </item>
99
   <item row="1" column="0" >
100
    <widget class="QTextEdit" name="textEdit" >
101
     <property name="html" >
102
      <string>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;style type="text/css">
103
p, li { white-space: pre-wrap; }
104
&lt;/style>&lt;/head>&lt;body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;">
105
&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is the list of all layers available in the datasource of the active layer. You can select the layers to load. The layers will be loaded when you press "OK".&lt;/p>
106
&lt;p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">&lt;/p>
107
&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">The layer name is format dependant. Consult the OGR documentation or the documentation of your data format to determine the nature of the included information.&lt;/p>
108
&lt;p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">&lt;/p>
109
&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">&lt;span style=" font-weight:600;">Be advised: &lt;/span>selecting an already opened layer will not generate an error message and the layer will end up loaded twice!&lt;/p>&lt;/body>&lt;/html></string>
110
     </property>
111
    </widget>
112
   </item>
113
  </layout>
114
 </widget>
115
 <layoutdefault spacing="6" margin="11" />
116
 <resources/>
117
 <connections/>
118
</ui>
src/plugins/ogrsublayers/README (révision 0)
1
Welcome to the OGR multilayer plugin
2
-------------------------------------------------------------
3

  
4
This plugin is designed to allow you to choose the layers you
5
want to load from an OGR multilayer datasource like KML or S57.
6

  
7
-------------------------------------------------------------
8

  
9
The plugin usage is very simple.
10

  
11
Once the plugin has been loaded with the Plugins Manager, you 
12
will find a new icon in the toolbar.
13

  
14
To choose the layers to load:
15
First load you datasource as you always did until now. 
16
("V" key, or add a new vector layer menu entry, then
17
select the datafile from which you wish to load the data).
18

  
19
If everything goes well, you will see a new layer in the layer 
20
lists on the left side of the screen (assuming you still have
21
the standard screen layout...). In fact, this layer is the
22
layer which has an ID of 0 in OGR. This is the default layer
23
loader by QGis normally.
24

  
25
If you select this layer (which makes it the active layer) and
26
clic on the plugin icon, and if the datasource contains more
27
than just one layer, then you will be presented with the list
28
of all the layers that are available for you to load and display.
29

  
30
Select the different layers you want to load into QGis. 
31

  
32
Be advised: nothing prevents you from loading a layer that you
33
have already loaded (for exemple, il you select the first line in
34
the list, you will load the first layer a second time)
35

  
36
When you are done with the selection, you can clic OK and
37
the selected layers will hopefully be loaded into QGis.
38

  
39
You can then manage the loaded layers as you are used to do with
40
usual data sources. There is absolutly no difference.
41

  
42
Please feel free to report any bug, comment or question to
43
[email protected]
src/providers/ogr/qgsogrprovider.h (copie de travail)
47 47

  
48 48
    virtual QgsCoordinateReferenceSystem crs();
49 49

  
50
	   /**
51
     * Sub-layers handled by this provider, in order from bottom to top
52
     *
53
     * Sub-layers are used when the provider's source can combine layers
54
     * it knows about in some way before it hands them off to the provider.
55
     */
56
    virtual QStringList subLayers();
50 57

  
51 58
    /**
52 59
     *   Returns the permanent storage type for this layer as a friendly name.
......
181 188
     *  @param index the index of the attribute
182 189
     *  @param values reference to the list of unique values */
183 190
    virtual void uniqueValues( int index, QList<QVariant> &uniqueValues );
184

  
185
  protected:
186
    /** loads fields from input file to member attributeFields */
187
    void loadFields();
188

  
189
    /**Get an attribute associated with a feature*/
190
    void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );
191

  
192 191
    /** return a provider name
193 192

  
194 193
    Essentially just returns the provider key.  Should be used to build file
......
219 218
     */
220 219
    QString description() const;
221 220

  
221
  protected:
222
    /** loads fields from input file to member attributeFields */
223
    void loadFields();
222 224

  
225
    /**Get an attribute associated with a feature*/
226
    void getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex );
227

  
228

  
229

  
223 230
  private:
224 231
    unsigned char *getGeometryPointer( OGRFeatureH fet );
225 232

  
......
247 254

  
248 255
    //! Selection rectangle
249 256
    OGRGeometryH mSelectionRectangle;
250

  
251 257
    /**Adds one feature*/
252 258
    bool addFeature( QgsFeature& f );
253 259
    /**Deletes one feature*/
src/providers/ogr/qgsogrprovider.cpp (copie de travail)
73 73
  // try to open for update, but disable error messages to avoid a
74 74
  // message if the file is read only, because we cope with that
75 75
  // ourselves.
76

  
77
  // This part of the code parses the uri transmitted to the ogr provider to 
78
  // get the options the client wants us to apply
79

  
80
  QString mFilePath;
81
  QString theLayerName;
82
  int theLayerIndex=0;
83

  
84
  // If there is no & in the uri, then the uri is just the filename. The loaded
85
  // layer will be layer 0.
86
  if ( ! uri.contains('&', Qt::CaseSensitive))
87
  {
88
    mFilePath = uri;
89
  }
90
  else
91
  {
92
  // If we get here, there are some options added to the filename. We must parse
93
  // the different parts separated by &, and among each option, the name and the
94
  // value around the =.
95
  // A valid uri is of the form: filename&option1=value1&option2=value2,...
96

  
97
 	  QStringList theURIParts = uri.split("&");
98
    mFilePath = theURIParts.at( 0 );
99

  
100
    for (int i = 1 ; i < theURIParts.size(); i++ )
101
    {
102
      QStringList theInstruction = theURIParts.at( i ).split( "=" );
103
      if ( 0 == theInstruction.at( 0 ).compare( QString( "layerid" )))
104
      {
105
        bool ok;
106
        theLayerIndex = theInstruction.at( 1 ).toInt( &ok );
107
        if ( ! ok )
108
        {
109
          theLayerIndex = 0;
110
        }
111
      }
112
      if ( 0 == theInstruction.at( 0 ).compare( QString( "layername" )))
113
      {
114
        theLayerName = theInstruction.at( 1 );
115
      }				
116
    }
117
  }
118

  
119
  QgsLogger::debug("mFilePath: " + mFilePath);
120
  QgsLogger::debug("theLayerIndex: "+theLayerIndex);
121
  QgsLogger::debug("theLayerName: "+theLayerName);
122
	
76 123
  CPLPushErrorHandler( CPLQuietErrorHandler );
77
  ogrDataSource = OGROpen( QFile::encodeName( uri ).constData(), TRUE, &ogrDriver );
124
  ogrDataSource = OGROpen( QFile::encodeName( mFilePath ).constData(), TRUE, &ogrDriver );
78 125
  CPLPopErrorHandler();
79 126

  
80 127
  if ( ogrDataSource == NULL )
81 128
  {
82 129
    // try to open read-only
83
    ogrDataSource = OGROpen( QFile::encodeName( uri ).constData(), FALSE, &ogrDriver );
130
    ogrDataSource = OGROpen( QFile::encodeName( mFilePath ).constData(), FALSE, &ogrDriver );
84 131

  
85 132
    //TODO Need to set a flag or something to indicate that the layer
86 133
    //TODO is in read-only mode, otherwise edit ops will fail
......
95 142
    valid = true;
96 143

  
97 144
    ogrDriverName = OGR_Dr_GetName( ogrDriver );
145
		
146
    // We get the layer which was requested by the uri. The layername
147
    // has precedence over the layerid if both are given.
148
    if ( NULL == theLayerName )
149
    {
150
      ogrLayer = OGR_DS_GetLayer( ogrDataSource, theLayerIndex );
151
    }else
152
    {
153
      ogrLayer = OGR_DS_GetLayerByName( ogrDataSource, (char*)(theLayerName.toLocal8Bit().data()) );
154
    }
98 155

  
99
    ogrLayer = OGR_DS_GetLayer( ogrDataSource, 0 );
100

  
101 156
    // get the extent_ (envelope) of the layer
102 157

  
103 158
    QgsDebugMsg( "Starting get extent" );
......
145 200
  }
146 201
}
147 202

  
203
QStringList QgsOgrProvider::subLayers()
204
{
205
  QStringList theList = QStringList();
206
  if (! valid )
207
  {
208
    return theList;
209
  }
210
  for ( int i = 0; i < layerCount() ; i++ )
211
  {  
212
    QString theLayerName = QString(OGR_FD_GetName(OGR_L_GetLayerDefn(OGR_DS_GetLayer( ogrDataSource, i ))));
213
    int theLayerFeatureCount=OGR_L_GetFeatureCount(OGR_DS_GetLayer( ogrDataSource, i ),1) ;
214

  
215
    theList.append(QString::number(i)+":"+ theLayerName+":"+QString::number(theLayerFeatureCount));
216
  }
217
  return theList;
218
}
219

  
148 220
void QgsOgrProvider::setEncoding( const QString& e )
149 221
{
150 222
  QgsVectorDataProvider::setEncoding( e );