92 changes: 56 additions & 36 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1649,9 +1649,10 @@ void QgisApp::setupConnections()
this, SLOT( markDirty() ) );

// connect map layer registry
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer * ) ),
this, SLOT( layerWasAdded( QgsMapLayer * ) ) );
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ),
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QgsMapLayer * ) ),
this, SLOT( layersWereAdded( QgsMapLayer * ) ) );
connect( QgsMapLayerRegistry::instance(),
SIGNAL( layersWillBeRemoved( QStringList ) ),
this, SLOT( removingLayer( QString ) ) );

// Connect warning dialog from project reading
Expand Down Expand Up @@ -2207,6 +2208,7 @@ void QgisApp::addVectorLayer()

bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QString& enc, const QString dataSourceType )
{
QList<QgsMapLayer *> myList;
foreach( QString src, theLayerQStringList )
{
src = src.trimmed();
Expand Down Expand Up @@ -2237,8 +2239,8 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
{
mMapCanvas->freeze( false );

// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();
// Let render() do its own cursor management
// QApplication::restoreOverrideCursor();

// XXX insert meaningful whine to the user here
return false;
Expand Down Expand Up @@ -2268,8 +2270,7 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
QString ligne = sublayers.at( 0 );
QStringList elements = ligne.split( ":" );
layer->setLayerName( elements.at( 1 ) );
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
myList << layer;
}
else
{
Expand All @@ -2291,6 +2292,8 @@ bool QgisApp::addVectorLayers( QStringList const & theLayerQStringList, const QS
}

}
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayers( myList );

// update UI
qApp->processEvents();
Expand Down Expand Up @@ -2484,6 +2487,7 @@ void QgisApp::addDatabaseLayer()

void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString const & providerKey )
{
QList<QgsMapLayer *> myList;
if ( mMapCanvas && mMapCanvas->isDrawing() )
{
return;
Expand Down Expand Up @@ -2521,8 +2525,9 @@ void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString cons

if ( layer->isValid() )
{
// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
// add to list of layers to register
//with the central layers registry
myList << layer;
}
else
{
Expand All @@ -2533,6 +2538,7 @@ void QgisApp::addDatabaseLayers( QStringList const & layerPathList, QString cons
//qWarning("incrementing iterator");
}

QgsMapLayerRegistry::instance()->addMapLayers( myList );
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );

// update UI
Expand Down Expand Up @@ -4683,13 +4689,17 @@ void QgisApp::isInOverview()
mMapLegend->legendLayerShowInOverview();
}

void QgisApp::removingLayer( QString layerId )
void QgisApp::removingLayers( QStringList theLayers )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( !vlayer || !vlayer->isEditable() )
return;
foreach (const QString &layerId, theLayers)
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer*>(
QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( !vlayer || !vlayer->isEditable() )
return;

toggleEditing( vlayer, false );
toggleEditing( vlayer, false );
}
}

void QgisApp::removeAllLayers()
Expand Down Expand Up @@ -5235,7 +5245,9 @@ QgsVectorLayer* QgisApp::addVectorLayer( QString vectorLayerPath, QString baseNa
if ( layer && layer->isValid() )
{
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayer( layer );
QList<QgsMapLayer *> myList;
myList << layer;
QgsMapLayerRegistry::instance()->addMapLayers( myList );
statusBar()->showMessage( mMapCanvas->extent().toString( 2 ) );

}
Expand Down Expand Up @@ -5275,7 +5287,9 @@ void QgisApp::addMapLayer( QgsMapLayer *theMapLayer )
if ( theMapLayer->isValid() )
{
// Register this layer with the layers registry
QgsMapLayerRegistry::instance()->addMapLayer( theMapLayer );
QList<QgsMapLayer *> myList;
myList << theMapLayer;
QgsMapLayerRegistry::instance()->addMapLayers( myList );
// add it to the mapcanvas collection
// not necessary since adding to registry adds to canvas mMapCanvas->addLayer(theMapLayer);

Expand Down Expand Up @@ -6001,31 +6015,35 @@ void QgisApp::markDirty()
// notify the project that there was a change
QgsProject::instance()->dirty( true );
}

void QgisApp::layerWasAdded( QgsMapLayer *layer )
//changed from layerWasAdded to layersWereAdded in 1.8
void QgisApp::layersWereAdded( QList<QgsMapLayer *> theLayers )
{
QgsDataProvider *provider = 0;
for (int i = 0; i < theLayers.size(); ++i)
{
QgsMapLayer * layer = theLayers.at(i);
QgsDataProvider *provider = 0;

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer )
provider = vlayer->dataProvider();
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer )
provider = vlayer->dataProvider();

QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer )
{
// connect up any request the raster may make to update the app progress
connect( rlayer, SIGNAL( drawingProgress( int, int ) ), this, SLOT( showProgress( int, int ) ) );
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
if ( rlayer )
{
// connect up any request the raster may make to update the app progress
connect( rlayer, SIGNAL( drawingProgress( int, int ) ), this, SLOT( showProgress( int, int ) ) );

// connect up any request the raster may make to update the statusbar message
connect( rlayer, SIGNAL( statusChanged( QString ) ), this, SLOT( showStatusMessage( QString ) ) );
// connect up any request the raster may make to update the statusbar message
connect( rlayer, SIGNAL( statusChanged( QString ) ), this, SLOT( showStatusMessage( QString ) ) );

provider = rlayer->dataProvider();
}
provider = rlayer->dataProvider();
}

if ( provider )
{
connect( provider, SIGNAL( dataChanged() ), layer, SLOT( clearCacheImage() ) );
connect( provider, SIGNAL( dataChanged() ), mMapCanvas, SLOT( refresh() ) );
if ( provider )
{
connect( provider, SIGNAL( dataChanged() ), layer, SLOT( clearCacheImage() ) );
connect( provider, SIGNAL( dataChanged() ), mMapCanvas, SLOT( refresh() ) );
}
}
}

Expand Down Expand Up @@ -6621,7 +6639,9 @@ bool QgisApp::addRasterLayer( QgsRasterLayer *theRasterLayer )
}

// register this layer with the central layers registry
QgsMapLayerRegistry::instance()->addMapLayer( theRasterLayer );
QList<QgsMapLayer *> myList;
myList << theRasterLayer;
QgsMapLayerRegistry::instance()->addMapLayers( myList );

return true;
}
Expand Down
10 changes: 6 additions & 4 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,13 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
//! mark project dirty
void markDirty();

//! layer was added
void layerWasAdded( QgsMapLayer * );
/* changed from layerWasAdded in 1.8 */
void layersWereAdded( QList<QgsMapLayer *> );

//! layer will be removed
void removingLayer( QString );
/* layer will be removed - changed from removingLayer to removingLayers
in 1.8.
*/
void removingLayers( QStringList );

void updateUndoActions();

Expand Down
25 changes: 13 additions & 12 deletions src/core/qgsmaplayerregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds,
emit layersWillBeRemoved( theLayerIds );

foreach (const QString &myId, theLayerIds) {
delete mMapLayers[myId];
mMapLayers.remove( myId );
if ( theEmitSignal )
emit layerWillBeRemoved( myId );
delete mMapLayers[myId];
mMapLayers.remove( myId );
}
emit layersWillBeRemoved(theLayerIds);
}
Expand All @@ -120,10 +122,9 @@ void QgsMapLayerRegistry::removeMapLayers( QStringList theLayerIds,
void QgsMapLayerRegistry::removeMapLayer( QString theLayerId,
bool theEmitSignal )
{
if ( theEmitSignal )
emit layerWillBeRemoved( theLayerId );
delete mMapLayers[theLayerId];
mMapLayers.remove( theLayerId );
QStringList myList;
myList << theLayerId;
removeMapLayers( myList, theEmitSignal );
}

void QgsMapLayerRegistry::removeAllMapLayers()
Expand All @@ -133,14 +134,14 @@ void QgsMapLayerRegistry::removeAllMapLayers()

// now let all canvas observers know to clear themselves,
// and then consequently any of their map legends
while ( mMapLayers.size() > 0 )
QStringList myList;
QMap<QString, QgsMapLayer *>::iterator it;
for ( it = mMapLayers.begin(); it != mMapLayers.end() ; ++it )
{
QString id = mMapLayers.begin().key();
emit layerWillBeRemoved( id );
delete mMapLayers[ id ]; // delete the map layer
mMapLayers.remove( id );
QString id = it.key();
myList << id;
}

removeMapLayers(myList, false);
mMapLayers.clear();
} // QgsMapLayerRegistry::removeAllMapLayers()

Expand Down
17 changes: 8 additions & 9 deletions src/core/qgsmaplayerregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject

~QgsMapLayerRegistry();

//! Retrieve a pointer to a loaded layer by id
//! Retrieve a pointer to a loaded layer by id
QgsMapLayer * mapLayer( QString theLayerId );

//! Retrieve the mapLayers collection (mainly intended for use by projectio)
//! Retrieve the mapLayers collection (mainly intended for use by projectio)
QMap<QString, QgsMapLayer*> & mapLayers();

/** Add a layer to the map of loaded layers
Expand All @@ -63,9 +63,9 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
@note This method is deprecated since QGIS 1.8, you should use addMapLayers rather.
*/
QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal = true );
Q_DECL_DEPRECATED QgsMapLayer *addMapLayer( QgsMapLayer * theMapLayer, bool theEmitSignal = true );

/** Add a layer to the map of loaded layers
/** Add a list of layers to the map of loaded layers
@returns QList<QgsMapLayer *> - a list of the map layers that were added
successfully. If a layer is invalid, or already exists in the registry,
it will not be part of the returned QList.
Expand All @@ -92,11 +92,10 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
table entry.
@note This method is deprecated since QGIS 1.8, you should use removeMapLayers rather.
*/
void removeMapLayer( QString theLayerId, bool theEmitSignal = true );
Q_DECL_DEPRECATED void removeMapLayer( QString theLayerId, bool theEmitSignal = true );

/** Remove a set of layers from qgis
@note
As a side-effect QgsProject is made dirty.
@note As a side-effect QgsProject is made dirty.
Any canvases using the affected layers will need to remove them
If theEmitSignal is true (by default), a layersRemoved( QStringList theLayerIds )
Expand Down Expand Up @@ -136,7 +135,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject

/** emitted when a layer is removed from the registry
connected to main map canvas and overview map canvas remove()
@note Deprecated in 1.8 - see layersWillBeRemoved
@note we should deprecate this at some stage
*/
void layerWillBeRemoved( QString theLayerId );

Expand All @@ -146,7 +145,7 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
void layersAdded( QList<QgsMapLayer *> theMapLayers );
/** emitted when a layer is added to the registry
connected to main map canvas and overview map canvas addLayer()
@note Deprecated in 1.8 - see layersAdded
@note we should deprecate this at some stage
*/
void layerWasAdded( QgsMapLayer * theMapLayer );

Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,9 @@ bool QgsProject::addLayer( const QDomElement& layerElem, QList<QDomNode>& broken
// have the layer restore state that is stored in Dom node
if ( mapLayer->readXML( layerElem ) && mapLayer->isValid() )
{
mapLayer = QgsMapLayerRegistry::instance()->addMapLayer( mapLayer );
QList<QgsMapLayer *> myLayers;
myLayers << mapLayer;
QgsMapLayerRegistry::instance()->addMapLayers( myLayers );
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
if ( vLayer && vLayer->vectorJoins().size() > 0 )
{
Expand Down