- QgisInterface's functions addRasterLayer/addVectorLayer return pointer to layer instead of just bool

- renamed QgisApp::addLayer to more appropriate QgisApp::addVectorLayer
- cleanups in loading of raster/vector layers in QgisApp


git-svn-id: http://svn.osgeo.org/qgis/trunk@8055 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
wonder 2008-01-27 04:11:27 +00:00
parent 6b0f7eadf1
commit a85df15615
9 changed files with 93 additions and 255 deletions

View File

@ -36,11 +36,9 @@ class QgisInterface : QObject
virtual void zoomActiveLayer()=0;
//! Add a vector layer
virtual bool addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)=0;
virtual QgsVectorLayer* addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)=0;
//! Add a raster layer given a raster layer file name
virtual bool addRasterLayer(QString rasterLayerPath)=0;
//! Add a raster layer given a QgsRasterLayer object
virtual bool addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRenderFlag=false)=0;
virtual QgsRasterLayer* addRasterLayer(QString rasterLayerPath, QString baseName = QString())=0;
//! Add a project
virtual bool addProject(QString theProject)=0;

View File

@ -514,7 +514,7 @@ void QgisApp::createActions()
mActionAddNonDbLayer= new QAction(QIcon(myIconPath+"/mActionAddNonDbLayer.png"), tr("Add a Vector Layer..."), this);
mActionAddNonDbLayer->setShortcut(tr("V","Add a Vector Layer"));
mActionAddNonDbLayer->setStatusTip(tr("Add a Vector Layer"));
connect(mActionAddNonDbLayer, SIGNAL(triggered()), this, SLOT(addLayer()));
connect(mActionAddNonDbLayer, SIGNAL(triggered()), this, SLOT(addVectorLayer()));
//
mActionAddRasterLayer= new QAction(QIcon(myIconPath+"/mActionAddRasterLayer.png"), tr("Add a Raster Layer..."), this);
mActionAddRasterLayer->setShortcut(tr("R","Add a Raster Layer"));
@ -1676,7 +1676,7 @@ static QString createFileFilter_(QString const &longName, QString const &glob)
/**
Builds the list of file filter strings to later be used by
QgisApp::addLayer()
QgisApp::addVectorLayer()
We query OGR for a list of supported vector formats; we then build a list
of file filter strings from that list. We return a string that contains
@ -1905,11 +1905,8 @@ static void openFilesRememberingFilter_(QString const &filterName,
/**
This method prompts the user for a list of vector filenames with a dialog.
@todo XXX I'd really like to return false, but can't because this
XXX is for a slot that was defined void; need to fix.
*/
void QgisApp::addLayer()
void QgisApp::addVectorLayer()
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
@ -1932,96 +1929,16 @@ void QgisApp::addLayer()
return;
}
addLayer(selectedFiles, enc);
} // QgisApp::addLayer()
bool QgisApp::addLayer(QFileInfo const & vectorFile)
{
// let the user know we're going to possibly be taking a while
// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);
mMapCanvas->freeze(); // XXX why do we do this?
// create the layer
QgsDebugMsg("completeBaseName is: " + vectorFile.completeBaseName());
QgsVectorLayer *layer = new QgsVectorLayer(vectorFile.filePath(),
vectorFile.completeBaseName(),
"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);
}
else
{
QString msg = vectorFile.completeBaseName() + " ";
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);
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();
return false;
}
// update UI
qApp->processEvents();
addVectorLayers(selectedFiles, enc);
}
// draw the map
mMapCanvas->freeze(false);
mMapCanvas->refresh();
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();
statusBar()->message(mMapCanvas->extent().stringRep(2));
return true;
} // QgisApp::addLayer()
/** \brief overloaded vesion of the above method that takes a list of
* filenames instead of prompting user with a dialog.
XXX yah know, this could be changed to just iteratively call the above
*/
bool QgisApp::addLayer(QStringList const &theLayerQStringList, const QString& enc)
bool QgisApp::addVectorLayers(QStringList const & theLayerQStringList, const QString& enc)
{
mMapCanvas->freeze();
// Let render() do its own cursor management
// QApplication::setOverrideCursor(Qt::WaitCursor);
for ( QStringList::ConstIterator it = theLayerQStringList.begin();
it != theLayerQStringList.end();
++it )
it != theLayerQStringList.end();
++it )
{
QFileInfo fi(*it);
QString base = fi.completeBaseName();
@ -2080,9 +1997,7 @@ bool QgisApp::addLayer(QStringList const &theLayerQStringList, const QString& en
statusBar()->message(mMapCanvas->extent().stringRep(2));
return true;
} // QgisApp::addLayer()
} // QgisApp::addVectorLayer()
@ -2188,8 +2103,6 @@ void QgisApp::addWmsLayer()
QgsServerSourceSelect *wmss = new QgsServerSourceSelect(this);
mMapCanvas->freeze();
if (wmss->exec())
{
@ -2712,10 +2625,9 @@ void QgisApp::newVectorLayer()
}
//then add the layer to the view
QStringList filelist;
filelist.append(filename);
addLayer(filelist, enc);
return;
QStringList filenames;
filenames.append(filename);
addVectorLayers(filenames, enc);
}
void QgisApp::fileOpen()
@ -3107,12 +3019,12 @@ bool QgisApp::openLayer(const QString & fileName)
{
QFileInfo fileInfo(fileName);
// try to load it as raster
bool ok = false;
QgsMapLayer* ok = NULL;
CPLPushErrorHandler(CPLQuietErrorHandler);
if (QgsRasterLayer::isValidRasterFileName(fileName))
ok = addRasterLayer(fileInfo, false);
ok = addRasterLayer(fileName, false);
else // nope - try to load it as a shape/ogr
ok = addLayer(fileInfo);
ok = addVectorLayer(fileName, fileName, "ogr");
CPLPopErrorHandler();
if (!ok)
@ -3754,10 +3666,6 @@ void QgisApp::userScale()
}
void QgisApp::testButton()
{
/* QgsShapeFileLayer *sfl = new QgsShapeFileLayer("foo");
mMapCanvas->addLayer(sfl); */
// delete sfl;
}
void QgisApp::menubar_highlighted( int i )
@ -4394,11 +4302,11 @@ QgsMapLayer *QgisApp::activeLayer()
parameter is used in the Map Legend so it should be formed in a meaningful
way.
*/
void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)
QgsVectorLayer* QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
return;
return NULL;
}
mMapCanvas->freeze();
@ -4423,8 +4331,6 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayer(layer);
QgsProject::instance()->dirty(false); // XXX this might be redundant
statusBar()->message(mMapCanvas->extent().stringRep(2));
}
@ -4432,6 +4338,10 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
{
QMessageBox::critical(this,tr("Layer is not valid"),
tr("The layer is not a valid layer and can not be added to the map"));
delete layer;
mMapCanvas->freeze(false);
return NULL;
}
// update UI
@ -4443,6 +4353,8 @@ void QgisApp::addVectorLayer(QString vectorLayerPath, QString baseName, QString
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();
return layer;
} // QgisApp::addVectorLayer
@ -5067,9 +4979,7 @@ void QgisApp::showCapturePointCoordinate(QgsPoint & theQgsPoint)
/////////////////////////////////////////////////////////////////
/** @todo XXX I'd *really* like to return, ya know, _false_.
*/
//create a raster layer object and delegate to addRasterLayer(QgsRasterLayer *)
// this is a slot for action from GUI to add raster layer
void QgisApp::addRasterLayer()
{
if(mMapCanvas && mMapCanvas->isDrawing())
@ -5077,8 +4987,6 @@ void QgisApp::addRasterLayer()
return;
}
//mMapCanvas->freeze(true);
QString fileFilters;
QStringList selectedFiles;
@ -5093,9 +5001,8 @@ void QgisApp::addRasterLayer()
return;
}
addRasterLayer(selectedFiles);
mMapCanvas->freeze(false);
mMapCanvas->refresh();
addRasterLayers(selectedFiles);
}// QgisApp::addRasterLayer()
//
@ -5104,7 +5011,7 @@ void QgisApp::addRasterLayer()
// of the calling method to manage things such as the frozen state of the mapcanvas and
// using waitcursors etc. - this method wont and SHOULDNT do it
//
bool QgisApp::addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRedrawFlag)
bool QgisApp::addRasterLayer(QgsRasterLayer * theRasterLayer)
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
@ -5120,70 +5027,48 @@ bool QgisApp::addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRedra
return false;
}
if (theRasterLayer->isValid())
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer(theRasterLayer);
// XXX doesn't the mMapCanvas->addLayer() do this?
// XXX now it does
// QObject::connect(theRasterLayer,
// SIGNAL(repaintRequested()),
// mMapCanvas,
// SLOT(refresh()));
// connect up any request the raster may make to update the app progress
QObject::connect(theRasterLayer,
SIGNAL(drawingProgress(int,int)),
this,
SLOT(showProgress(int,int)));
// connect up any request the raster may make to update the statusbar message
QObject::connect(theRasterLayer,
SIGNAL(setStatus(QString)),
this,
SLOT(showStatusMessage(QString)));
// add it to the mapcanvas collection
// no longer necessary since adding to registry automatically adds to canvas
// mMapCanvas->addLayer(theRasterLayer);
}
else
if (!theRasterLayer->isValid())
{
delete theRasterLayer;
return false;
}
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer(theRasterLayer);
// connect up any request the raster may make to update the app progress
QObject::connect(theRasterLayer,
SIGNAL(drawingProgress(int,int)),
this,
SLOT(showProgress(int,int)));
// connect up any request the raster may make to update the statusbar message
QObject::connect(theRasterLayer,
SIGNAL(setStatus(QString)),
this,
SLOT(showStatusMessage(QString)));
if (theForceRedrawFlag)
{
// update UI
qApp->processEvents();
// draw the map
mMapCanvas->freeze(false);
mMapCanvas->refresh();
}
return true;
}
//create a raster layer object and delegate to addRasterLayer(QgsRasterLayer *)
bool QgisApp::addRasterLayer(QFileInfo const & rasterFile, bool guiWarning)
QgsRasterLayer* QgisApp::addRasterLayer(QString const & rasterFile, QString const & baseName, bool guiWarning)
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
return false;
return NULL;
}
// let the user know we're going to possibly be taking a while
QApplication::setOverrideCursor(Qt::WaitCursor);
mMapCanvas->freeze(true);
// XXX ya know QgsRasterLayer can snip out the basename on its own;
// XXX why do we have to pass it in for it?
QgsRasterLayer *layer =
new QgsRasterLayer(rasterFile.filePath(), rasterFile.completeBaseName());
new QgsRasterLayer(rasterFile, baseName); // fi.completeBaseName());
if (!addRasterLayer(layer))
{
@ -5196,12 +5081,12 @@ bool QgisApp::addRasterLayer(QFileInfo const & rasterFile, bool guiWarning)
if(guiWarning)
{
// don't show the gui warning (probably because we are loading from command line)
QString msg(rasterFile.completeBaseName()
QString msg(rasterFile
+ tr(" is not a valid or recognized raster data source"));
QMessageBox::critical(this, tr("Invalid Data Source"), msg);
}
delete layer;
return false;
return NULL;
}
else
{
@ -5212,7 +5097,9 @@ bool QgisApp::addRasterLayer(QFileInfo const & rasterFile, bool guiWarning)
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();
return true;
mMapCanvas->refresh();
return layer;
}
} // QgisApp::addRasterLayer
@ -5275,22 +5162,8 @@ void QgisApp::addRasterLayer(QString const & rasterLayerPath,
if( layer && layer->isValid() )
{
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayer(layer);
// connect up any request the raster may make to update the app progress
QObject::connect(layer,
SIGNAL(drawingProgress(int,int)),
this,
SLOT(showProgress(int,int)));
// connect up any request the raster may make to update the statusbar message
QObject::connect(layer,
SIGNAL(setStatus(QString)),
this,
SLOT(showStatusMessage(QString)));
QgsProject::instance()->dirty(false); // XXX this might be redundant
addRasterLayer(layer);
statusBar()->message(mMapCanvas->extent().stringRep(2));
}
@ -5314,7 +5187,7 @@ void QgisApp::addRasterLayer(QString const & rasterLayerPath,
//create a raster layer object and delegate to addRasterLayer(QgsRasterLayer *)
bool QgisApp::addRasterLayer(QStringList const &theFileNameQStringList, bool guiWarning)
bool QgisApp::addRasterLayers(QStringList const &theFileNameQStringList, bool guiWarning)
{
if(mMapCanvas && mMapCanvas->isDrawing())
{
@ -5384,6 +5257,7 @@ bool QgisApp::addRasterLayer(QStringList const &theFileNameQStringList, bool gui
statusBar()->message(mMapCanvas->extent().stringRep(2));
mMapCanvas->freeze(false);
mMapCanvas->refresh();
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();

View File

@ -50,6 +50,7 @@ class QgsProviderRegistry;
class QgsPythonDialog;
class QgsRasterLayer;
class QgsRect;
class QgsVectorLayer;
#include <map>
@ -72,36 +73,23 @@ class QgisApp : public QMainWindow, public Ui::QgisAppBase
//! Destructor
~QgisApp();
/**
* Add a vector layer to the canvas
* Add a vector layer to the canvas, returns pointer to it
*/
void addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey);
QgsVectorLayer* addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey);
/** \brief overloaded vesion of the privat addLayer method that takes a list of
* filenames instead of prompting user with a dialog.
@param enc encoding type for the layer
@returns true if successfully added layer
@note
This should be deprecated because it's possible to have a
heterogeneous set of files; i.e., a mix of raster and vector.
It's much better to try to just open one file at a time.
*/
bool addLayer(QStringList const & theLayerQStringList, const QString& enc);
bool addVectorLayers(QStringList const & theLayerQStringList, const QString& enc);
/** 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
*/
bool addLayer(QFileInfo const & vectorFile);
/** overloaded vesion of the private addRasterLayer()
Method that takes a list of filenames instead of prompting
user with a dialog.
@returns true if successfully added layer(s)
@note
This should be deprecated because it's possible to have a
heterogeneous set of files; i.e., a mix of raster and vector.
It's much better to try to just open one file at a time.
*/
bool addRasterLayer(QStringList const & theLayerQStringList, bool guiWarning=true);
bool addRasterLayers(QStringList const & theLayerQStringList, bool guiWarning=true);
/** Open a raster layer using the Raster Data Provider.
* Note this is included to support WMS layers only at this stage,
@ -124,9 +112,11 @@ class QgisApp : public QMainWindow, public Ui::QgisAppBase
@note
This is essentially a simplified version of the above
*/
bool addRasterLayer(QFileInfo const & rasterFile, bool guiWarning=true);
QgsRasterLayer* addRasterLayer(QString const & rasterFile, QString const & baseName, bool guiWarning=true);
/** Add a 'pre-made' map layer to the project */
void addMapLayer(QgsMapLayer *theMapLayer);
/** Set the extents of the map canvas */
void setExtent(QgsRect theRect);
//! Remove all layers from the map and legend - reimplements same method from qgisappbase
@ -330,7 +320,7 @@ public slots:
void destinationSrsChanged();
// void debugHook();
//! Add a vector layer to the map
void addLayer();
void addVectorLayer();
//! Exit Qgis
void fileExit();
//! Add a WMS layer to the map
@ -414,10 +404,9 @@ signals:
private:
/** Add a raster layer to the map (passed in as a ptr).
* It won't force a refresh unless you explicitly
* use the force redraw flag.
* It won't force a refresh.
*/
bool addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRedrawFlag=false);
bool addRasterLayer(QgsRasterLayer * theRasterLayer);
//@todo We should move these next two into vector layer class
/** This helper checks to see whether the filename appears to be a valid vector file name */
bool isValidVectorFileName (QString theFileNameQString);

View File

@ -18,6 +18,7 @@
/* $Id$ */
#include <iostream>
#include <QFileInfo>
#include <QString>
#include <QMenu>
@ -56,22 +57,26 @@ void QgisAppInterface::zoomActiveLayer()
qgis->zoomToLayerExtent();
}
bool QgisAppInterface::addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)
QgsVectorLayer* QgisAppInterface::addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)
{
qgis->addVectorLayer(vectorLayerPath, baseName, providerKey);
//TODO fix this so it returns something meaningfull
return true;
if (baseName.isEmpty())
{
QFileInfo fi(vectorLayerPath);
baseName = fi.completeBaseName();
}
return qgis->addVectorLayer(vectorLayerPath, baseName, providerKey);
}
bool QgisAppInterface::addRasterLayer(QString rasterLayerPath)
QgsRasterLayer* QgisAppInterface::addRasterLayer(QString rasterLayerPath, QString baseName)
{
return qgis->addRasterLayer( QStringList(rasterLayerPath) );
if (baseName.isEmpty())
{
QFileInfo fi(rasterLayerPath);
baseName = fi.completeBaseName();
}
return qgis->addRasterLayer(rasterLayerPath, baseName);
}
bool QgisAppInterface::addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRenderFlag)
{
return qgis->addRasterLayer(theRasterLayer, theForceRenderFlag);
}
bool QgisAppInterface::addProject(QString theProjectName)
{
@ -116,16 +121,6 @@ void QgisAppInterface::openURL(QString url, bool useQgisDocDirectory)
qgis->openURL(url, useQgisDocDirectory);
}
std::map<QString, int> QgisAppInterface::menuMapByName()
{
return qgis->menuMapByName();
}
std::map<int, QString> QgisAppInterface::menuMapById()
{
return qgis->menuMapById();
}
QgsMapCanvas * QgisAppInterface::getMapCanvas()
{
return qgis->getMapCanvas();

View File

@ -51,11 +51,9 @@ class QgisAppInterface : public QgisInterface
void zoomActiveLayer();
//! Add a vector layer
bool addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey);
QgsVectorLayer* addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey);
//! Add a raster layer given its file name
bool addRasterLayer(QString rasterLayerPath);
//! Add a raster layer given a raster layer obj
bool addRasterLayer(QgsRasterLayer * theRasterLayer,bool theForceRenderFlag=false);
QgsRasterLayer* addRasterLayer(QString rasterLayerPath, QString baseName);
//! Add a project
bool addProject(QString theProjectName);
@ -81,11 +79,6 @@ class QgisAppInterface : public QgisInterface
*/
void openURL(QString url, bool useQgisDocDirectory=true);
/** Get the menu info mapped by menu name (key is name, value is menu id) */
std::map<QString,int> menuMapByName();
/** Get the menu info mapped by menu id (key is menu id, value is name) */
std::map<int,QString> menuMapById();
/** Return a pointer to the map canvas used by qgisapp */
QgsMapCanvas * getMapCanvas();

View File

@ -32,6 +32,7 @@ class QgisApp;
class QgsMapLayer;
class QgsMapCanvas;
class QgsRasterLayer;
class QgsVectorLayer;
/**
* \class QgisInterface
@ -68,11 +69,9 @@ class GUI_EXPORT QgisInterface : public QObject
virtual void zoomActiveLayer()=0;
//! Add a vector layer
virtual bool addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)=0;
virtual QgsVectorLayer* addVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey)=0;
//! Add a raster layer given a raster layer file name
virtual bool addRasterLayer(QString rasterLayerPath)=0;
//! Add a raster layer given a QgsRasterLayer object
virtual bool addRasterLayer(QgsRasterLayer * theRasterLayer, bool theForceRenderFlag=false)=0;
virtual QgsRasterLayer* addRasterLayer(QString rasterLayerPath, QString baseName = QString())=0;
//! Add a project
virtual bool addProject(QString theProject)=0;

View File

@ -173,8 +173,7 @@ void QgsGrassBrowser::addMap()
if ( type == QgsGrassModel::Raster )
{
std::cerr << "add raster: " << uri.ascii() << std::endl;
QgsRasterLayer *layer = new QgsRasterLayer( uri, map );
mIface->addRasterLayer(layer);
mIface->addRasterLayer(uri, map);
mapSelected = true;
}
else if ( type == QgsGrassModel::Vector )

View File

@ -1494,8 +1494,7 @@ void QgsGrassModule::viewOutput()
+ QgsGrass::getDefaultMapset()
+ "/cellhd/" + map;
QgsRasterLayer *layer = new QgsRasterLayer( uri, map );
mIface->addRasterLayer(layer);
mIface->addRasterLayer(uri, map);
}
}

View File

@ -411,16 +411,8 @@ void QgsGrassPlugin::addRaster()
pos = uri.findRev('/', pos-1);
QString name = uri.right( uri.length() - pos - 1 );
name.replace('/', ' ');
//qGisInterface->addRasterLayer( uri );
QgsRasterLayer *layer = new QgsRasterLayer( uri, sel->map );
if( !layer->isValid() ) {
// let the user know something went wrong - addRasterLayer cleans up
QMessageBox::warning( 0, tr("Warning"), tr("Could not add raster layer: " ) + uri);
}
qGisInterface->addRasterLayer(layer);
mCanvas->refresh();
qGisInterface->addRasterLayer(uri, sel->map);
}
}