Logo Search packages:      
Sourcecode: qgis version File versions

void QgisApp::addVectorLayer ( QString  vectorLayerPath,
QString  baseName,
QString  providerKey 
)

Add a vector layer directly without prompting user for location The caller must provide information compatible with the provider plugin using the vectorLayerPath and baseName. The provider can use these parameters in any way necessary to initialize the layer. The baseName parameter is used in the Map Legend so it should be formed in a meaningful way.

Definition at line 3202 of file qgisapp.cpp.

References QgsMapLayerRegistry::addMapLayer(), QgsProject::dirty(), QgsMapCanvas::extent(), QgsMapCanvas::freeze(), QgsSingleSymRenderer::initializeSymbology(), QgsProject::instance(), QgsMapLayerRegistry::instance(), QgsVectorLayer::isValid(), keyPressed(), mMapCanvas, mOverviewCanvas, QgsMapCanvas::render(), QgsVectorLayer::setRenderer(), and QgsRect::stringRep().

Referenced by QgsDbSourceSelect::addLayer(), and QgisIface::addVectorLayer().

{
    // check to see if the appropriate provider is available
    QString providerName;

    QString pProvider = mProviderRegistry->library(providerKey);

    if ( ! pProvider.isNull() )
    {
        mMapCanvas->freeze();
        QApplication::setOverrideCursor(Qt::WaitCursor);
        // create the layer
        QgsVectorLayer *layer;
        /* Eliminate the need to instantiate the layer based on provider type.
           The caller is responsible for cobbling together the needed information to
           open the layer
           */
#ifdef QGISDEBUG

        std::cout << "Creating new vector layer using " <<
        vectorLayerPath << " with baseName of " << baseName <<
        " and providerKey of " << providerKey << std::endl;
#endif

        layer = new QgsVectorLayer(vectorLayerPath, baseName, providerKey);

        if( layer && layer->isValid() )
        {
            // Register this layer with the layers registry
            QgsMapLayerRegistry::instance()->addMapLayer(layer);
            // init the context menu so it can connect to slots in main app
            // now taken care of in legend layer->initContextMenu(this);

            // give it a random color
            QgsSingleSymRenderer *renderer = new QgsSingleSymRenderer();  //add single symbol renderer as default
            layer->setRenderer(renderer);
            renderer->initializeSymbology(layer);
            // add it to the mapcanvas collection
            // mMapCanvas->addLayer(layer); No longer necessary since adding to registry will add to canvas

            //connect up a request from the raster layer to show in overview map
            // XXX some day will no longer necessary since adding to registry will add to overview layer
            //       QObject::connect(layer,
            //               SIGNAL(showInOverview(QString,bool)),
            //               this,
            //               SLOT(setLayerOverviewStatus(QString,bool)));

            // connect up any keypresses to be passed tot he layer (e.g. so esc can stop rendering)
#ifdef QGISDEBUG
  std::cout << " Connecting up maplayers keyPressed event to the QgisApp keyPress signal" << std::endl;
#endif
            QObject::connect(this,
                             SIGNAL(keyPressed(QKeyEvent * )),
                             layer,
                             SLOT(keyPressed(QKeyEvent* )));


            //add hooks for letting layer know canvas needs to recalc the layer extents
            QObject::connect(layer,
                             SIGNAL(recalculateExtents()),
                             mMapCanvas,
                             SLOT(recalculateExtents()));

            QObject::connect(layer,
                             SIGNAL(recalculateExtents()),
                             mOverviewCanvas,
                             SLOT(recalculateExtents()));


            QgsProject::instance()->dirty(false); // XXX this might be redundant

            statusBar()->message(mMapCanvas->extent().stringRep(2));

        }
        else
        {
            QMessageBox::critical(this,"Layer is not valid",
                                  "The layer is not a valid layer and can not be added to the map");
        }
        qApp->processEvents();
        mMapCanvas->freeze(false);
        mMapCanvas->render();
        QApplication::restoreOverrideCursor();
    }

} // QgisApp::addVectorLayer


Generated by  Doxygen 1.6.0   Back to index