Logo Search packages:      
Sourcecode: qgis version File versions

void QgsVectorLayer::setDataProvider ( QString const &  provider  ) 

bind layer to a specific data provider

Parameters:
provider should be "postgres", "ogr", or ??
Todo:
XXX should this return bool? Throw exceptions?

Definition at line 1519 of file qgsvectorlayer.cpp.

References dataProvider, QgsMapLayer::dataSource, QgsDataProvider::extent(), QgsVectorDataProvider::fields(), QgsVectorDataProvider::geometryType(), QgsMapLayer::geometryType, QgsDataProvider::isValid(), QgsMapLayer::layerExtent, QgsMapLayer::layerName, mLabel, mLabelOn, providerKey, setDisplayField(), QgsRect::setXmax(), QgsRect::setXmin(), QgsRect::setYmax(), QgsRect::setYmin(), QgsRect::stringRep(), valid, QgsRect::xMax(), QgsRect::xMin(), QgsRect::yMax(), and QgsRect::yMin().

Referenced by QgsVectorLayer(), and readXML_().

{
    // XXX should I check for and possibly delete any pre-existing providers?
    // XXX How often will that scenario occur?

    providerKey = provider;     // XXX is this necessary?  Usually already set
                                // XXX when execution gets here.

    // load the plugin
    QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
    QString ogrlib = pReg->library(provider);

    //QString ogrlib = libDir + "/libpostgresprovider.so";

    const char *cOgrLib = (const char *) ogrlib;

#ifdef TESTPROVIDERLIB
    // test code to help debug provider loading problems
//  void *handle = dlopen(cOgrLib, RTLD_LAZY);
    void *handle = dlopen(cOgrLib, RTLD_LAZY | RTLD_GLOBAL);
    if (!handle)
    {
        std::cout << "Error in dlopen: " << dlerror() << std::endl;

    } else
    {
        std::cout << "dlopen suceeded" << std::endl;
        dlclose(handle);
    }

#endif

    // load the data provider
    myLib = new QLibrary((const char *) ogrlib);
#ifdef QGISDEBUG
    std::cout << "Library name is " << myLib->library() << std::endl;
#endif
    bool loaded = myLib->load();

    if (loaded)
    {
#ifdef QGISDEBUG
        std::cout << "Loaded data provider library" << std::endl;
        std::cout << "Attempting to resolve the classFactory function" << std::endl;
#endif
        create_it * classFactory = (create_it *) myLib->resolve("classFactory");

        valid = false;            // assume the layer is invalid until we
                                  // determine otherwise
        if (classFactory)
        {
#ifdef QGISDEBUG
            std::cout << "Getting pointer to a dataProvider object from the library\n";
#endif
            //XXX - This was a dynamic cast but that kills the Windows
            //      version big-time with an abnormal termination error
            dataProvider = (QgsVectorDataProvider*)(classFactory((const
                            char*)(dataSource.utf8())));

            if (dataProvider)
            {
#ifdef QGISDEBUG
                std::cout << "Instantiated the data provider plugin\n";
#endif
                if (dataProvider->isValid())
                {
                    valid = true;

                    // get the extent
                    QgsRect *mbr = dataProvider->extent();

                    // show the extent
                    QString s = mbr->stringRep();
#ifdef QGISDEBUG
                    std::cout << "Extent of layer: " << s << std::endl;
#endif
                    // store the extent
                    layerExtent.setXmax(mbr->xMax());
                    layerExtent.setXmin(mbr->xMin());
                    layerExtent.setYmax(mbr->yMax());
                    layerExtent.setYmin(mbr->yMin());

                    // get and store the feature type
                    geometryType = dataProvider->geometryType();

                    // look at the fields in the layer and set the primary
                    // display field using some real fuzzy logic
                    setDisplayField();

                    if (providerKey == "postgres")
                    {
#ifdef QGISDEBUG
                        std::cout << "Beautifying layer name " << layerName << std::endl;
#endif
                        // adjust the display name for postgres layers
                        layerName = layerName.mid(layerName.find(".") + 1);
                        layerName = layerName.left(layerName.find("("));
#ifdef QGISDEBUG
                        std::cout << "Beautified name is " << layerName << std::endl;
#endif
                    }

                    // upper case the first letter of the layer name
                    layerName = layerName.left(1).upper() + layerName.mid(1);

                    // label
                    mLabel = new QgsLabel ( dataProvider->fields() );
                    mLabelOn = false;
                }
            } else
            {
#ifdef QGISDEBUG
                std::cout << "Unable to instantiate the data provider plugin\n";
#endif
                valid = false;
            }
        }
    } else
    {
        valid = false;
#ifdef QGISDEBUG
        std::cout << "Failed to load " << "../providers/libproviders.so" << "\n";
#endif
    }

} // QgsVectorLayer:: setDataProvider


Generated by  Doxygen 1.6.0   Back to index