Logo Search packages:      
Sourcecode: qgis version File versions

bool QgisApp::addLayer ( QFileInfo const &  vectorFile  ) 

open a vector layer for the given file

Returns:
false if unable to open a raster layer for rasterFile
Note:
This is essentially a simplified version of the above

Definition at line 1029 of file qgisapp.cpp.

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

{
    // check to see if we have an ogr provider available
    QString pOgr = mProviderRegistry->library("ogr");

    if ( pOgr.isEmpty() )
    {
        QMessageBox::critical(this,
                              tr("No OGR Provider"),
                              tr("No OGR data provider was found in the QGIS lib directory"));
        return false;
    }

    // let the user know we're going to possibly be taking a while
    QApplication::setOverrideCursor(Qt::WaitCursor);

    mMapCanvas->freeze();         // XXX why do we do this?

    // create the layer

    QgsVectorLayer *layer = new QgsVectorLayer(vectorFile.filePath(),
                            vectorFile.baseName(),
                            "ogr");
    Q_CHECK_PTR( layer );

    if ( ! layer )
    {
        mMapCanvas->freeze(false);
        QApplication::restoreOverrideCursor();

        // XXX insert meaningful whine to the user here
        return false;
    }

    if (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
        // XXX move to legend::addLayer() layer->initContextMenu(this);

        // XXX What about the rest of these?  Where should they be moved, if at
        // XXX all?  Some of this functionality is taken care of in the
        // XXX QgsProject::read() (If layers added via that.)

        //add single symbol renderer as default
        QgsSingleSymRenderer *renderer = new QgsSingleSymRenderer();

        Q_CHECK_PTR( renderer );

        if ( ! renderer )
        {
            mMapCanvas->freeze(false);
            QApplication::restoreOverrideCursor();

            // XXX should we also delete the layer?

            // XXX insert meaningful whine to the user here
            return false;
        }

        layer->setRenderer(renderer);
        renderer->initializeSymbology(layer);
        // not necessary since registry will add to canvas mMapCanvas->addLayer(layer);
        // XXX some day will not necessary since connect up a request from
        // the raster layer to show in overview map
        //      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()));
    }
    else
    {
        QString msg = vectorFile.baseName() + " ";
        msg += tr("is not a valid or recognized data source");
        QMessageBox::critical(this, tr("Invalid Data Source"), msg);

        // since the layer is bad, stomp on it
        delete layer;

        mMapCanvas->freeze(false);
        QApplication::restoreOverrideCursor();

        return false;
    }

    mMapCanvas->freeze(false);
    // mMapLegend->update(); NOW UPDATED VIA SIGNAL/SLOT
    qApp->processEvents();       // XXX why does this need to be called manually?
    mMapCanvas->render();        // XXX eh, wot?

    QApplication::restoreOverrideCursor();

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

    return true;

} // QgisApp::addLayer()


Generated by  Doxygen 1.6.0   Back to index