Skip to content

Commit

Permalink
[FEATURE] allow setting CRS for multiple layers at once
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15352 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Mar 6, 2011
1 parent e332633 commit f91642f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 30 deletions.
Binary file added images/themes/default/mActionHelpAPI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 99 additions & 23 deletions src/app/legend/qgslegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsprojectbadlayerguihandler.h"
#include "qgsgenericprojectionselector.h"

#include <QFont>
#include <QDomDocument>
Expand Down Expand Up @@ -525,16 +525,16 @@ void QgsLegend::mouseDoubleClickEvent( QMouseEvent* e )
{
QSettings settings;

switch( settings.value( "/qgis/legendDoubleClickAction", 0 ).toInt() )
switch ( settings.value( "/qgis/legendDoubleClickAction", 0 ).toInt() )
{
case 0:
QgisApp::instance()->layerProperties();
break;
case 1:
QgisApp::instance()->attributeTable();
break;
default:
break;
case 0:
QgisApp::instance()->layerProperties();
break;
case 1:
QgisApp::instance()->attributeTable();
break;
default:
break;
}
}

Expand All @@ -558,7 +558,6 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
{
theMenu.addAction( tr( "&Make to toplevel item" ), this, SLOT( makeToTopLevelItem() ) );
}

}
else if ( li->type() == QgsLegendItem::LEGEND_GROUP )
{
Expand All @@ -567,6 +566,9 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi

theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ),
tr( "&Remove" ), this, SLOT( legendGroupRemove() ) );

theMenu.addAction( QgisApp::getThemeIcon( "/mActionSetCRS.png" ),
tr( "&Set group CRS" ), this, SLOT( legendGroupSetCRS() ) );
}

if ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP )
Expand Down Expand Up @@ -739,6 +741,30 @@ void QgsLegend::legendGroupRemove()
}
}

void QgsLegend::legendGroupSetCRS()
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}

QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
mySelector->setMessage();
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem crs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );

QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
setGroupCRS( lg, crs );
}
else
{
QApplication::restoreOverrideCursor();
}

delete mySelector;
}

void QgsLegend::removeGroup( QgsLegendGroup *lg )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
Expand Down Expand Up @@ -766,6 +792,36 @@ void QgsLegend::removeGroup( QgsLegendGroup *lg )
adjustIconSize();
}

void QgsLegend::setGroupCRS( QgsLegendGroup *lg, const QgsCoordinateReferenceSystem &crs )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}

//delete the legend layers first
QTreeWidgetItem * child = lg->child( 0 );
while ( child )
{
QgsLegendLayer *cl = dynamic_cast<QgsLegendLayer *>( child );
QgsLegendGroup *cg = dynamic_cast<QgsLegendGroup *>( child );

if ( cl )
{
cl->layer()->setCrs( crs );
}
else if ( cg )
{
setGroupCRS( cg, crs );
}

child = lg->child( 0 );
}

delete lg;
}


void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
{
if ( !ml )
Expand Down Expand Up @@ -999,20 +1055,11 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
{
bool isOpen;
QgsLegendLayer* currentLayer = readLayerFromXML( childelem, isOpen );

bool ignorePressed = QgsProjectBadLayerGuiHandler::mIgnore;

if ( !currentLayer )
{
if( ignorePressed == true )
{
continue;
}
else
{
return false;
}
{
continue;
}

// add to tree - either as a top-level node or a child of a group
if ( parent )
{
Expand Down Expand Up @@ -1920,3 +1967,32 @@ void QgsLegend::removeSelectedLayers()
if ( renderFlagState )
mMapCanvas->setRenderFlag( true );
}

void QgsLegend::setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs )
{
// Turn off rendering to improve speed.
bool renderFlagState = mMapCanvas->renderFlag();
if ( renderFlagState )
mMapCanvas->setRenderFlag( false );

foreach( QTreeWidgetItem * item, selectedItems() )
{
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item );
if ( lg )
{
setGroupCRS( lg, crs );
continue;
}

QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
if ( ll && ll->layer() )
{
ll->layer()->setCrs( crs );
continue;
}
}

// Turn on rendering (if it was on previously)
if ( renderFlagState )
mMapCanvas->setRenderFlag( true );
}
8 changes: 8 additions & 0 deletions src/app/legend/qgslegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class QDomElement;
class QDomNode;
class QMouseEvent;
class QTreeWidgetItem;
class QgsCoordinateReferenceSystem;

//Information about relationship between groups and layers
//key: group name (or null strings for single layers without groups)
Expand Down Expand Up @@ -266,6 +267,9 @@ class QgsLegend : public QTreeWidget
/** Remove selected layers */
void removeSelectedLayers();

/** Set CRS for selected layers */
void setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs );

protected:

/*!Event handler for mouse movements.
Expand Down Expand Up @@ -362,8 +366,12 @@ class QgsLegend : public QTreeWidget
void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
/**Removes the current legend group*/
void legendGroupRemove();
/**Set the CRS of the current legend group*/
void legendGroupSetCRS();
/**Removes a legend group and its layers*/
void removeGroup( QgsLegendGroup * lg );
/**Removes a legend group and its layers*/
void setGroupCRS( QgsLegendGroup * lg, const QgsCoordinateReferenceSystem &crs );
/**Sets all listview items to open*/
void expandAll();
/**Sets all listview items to closed*/
Expand Down
3 changes: 3 additions & 0 deletions src/app/legend/qgslegendlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
// remove from canvas
theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );

// remove from canvas
theMenu.addAction( QgisApp::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );

theMenu.addSeparator();

if ( lyr->type() == QgsMapLayer::VectorLayer )
Expand Down
Loading

0 comments on commit f91642f

Please sign in to comment.