Skip to content

Commit f91642f

Browse files
author
jef
committedMar 6, 2011
[FEATURE] allow setting CRS for multiple layers at once
git-svn-id: http://svn.osgeo.org/qgis/trunk@15352 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e332633 commit f91642f

File tree

6 files changed

+185
-30
lines changed

6 files changed

+185
-30
lines changed
 
1.08 KB
Loading

‎src/app/legend/qgslegend.cpp

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "qgsproject.h"
3434
#include "qgsrasterlayer.h"
3535
#include "qgsvectorlayer.h"
36-
#include "qgsprojectbadlayerguihandler.h"
36+
#include "qgsgenericprojectionselector.h"
3737

3838
#include <QFont>
3939
#include <QDomDocument>
@@ -525,16 +525,16 @@ void QgsLegend::mouseDoubleClickEvent( QMouseEvent* e )
525525
{
526526
QSettings settings;
527527

528-
switch( settings.value( "/qgis/legendDoubleClickAction", 0 ).toInt() )
528+
switch ( settings.value( "/qgis/legendDoubleClickAction", 0 ).toInt() )
529529
{
530-
case 0:
531-
QgisApp::instance()->layerProperties();
532-
break;
533-
case 1:
534-
QgisApp::instance()->attributeTable();
535-
break;
536-
default:
537-
break;
530+
case 0:
531+
QgisApp::instance()->layerProperties();
532+
break;
533+
case 1:
534+
QgisApp::instance()->attributeTable();
535+
break;
536+
default:
537+
break;
538538
}
539539
}
540540

@@ -558,7 +558,6 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
558558
{
559559
theMenu.addAction( tr( "&Make to toplevel item" ), this, SLOT( makeToTopLevelItem() ) );
560560
}
561-
562561
}
563562
else if ( li->type() == QgsLegendItem::LEGEND_GROUP )
564563
{
@@ -567,6 +566,9 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
567566

568567
theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ),
569568
tr( "&Remove" ), this, SLOT( legendGroupRemove() ) );
569+
570+
theMenu.addAction( QgisApp::getThemeIcon( "/mActionSetCRS.png" ),
571+
tr( "&Set group CRS" ), this, SLOT( legendGroupSetCRS() ) );
570572
}
571573

572574
if ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP )
@@ -739,6 +741,30 @@ void QgsLegend::legendGroupRemove()
739741
}
740742
}
741743

744+
void QgsLegend::legendGroupSetCRS()
745+
{
746+
if ( !mMapCanvas || mMapCanvas->isDrawing() )
747+
{
748+
return;
749+
}
750+
751+
QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
752+
mySelector->setMessage();
753+
if ( mySelector->exec() )
754+
{
755+
QgsCoordinateReferenceSystem crs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
756+
757+
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
758+
setGroupCRS( lg, crs );
759+
}
760+
else
761+
{
762+
QApplication::restoreOverrideCursor();
763+
}
764+
765+
delete mySelector;
766+
}
767+
742768
void QgsLegend::removeGroup( QgsLegendGroup *lg )
743769
{
744770
if ( !mMapCanvas || mMapCanvas->isDrawing() )
@@ -766,6 +792,36 @@ void QgsLegend::removeGroup( QgsLegendGroup *lg )
766792
adjustIconSize();
767793
}
768794

795+
void QgsLegend::setGroupCRS( QgsLegendGroup *lg, const QgsCoordinateReferenceSystem &crs )
796+
{
797+
if ( !mMapCanvas || mMapCanvas->isDrawing() )
798+
{
799+
return;
800+
}
801+
802+
//delete the legend layers first
803+
QTreeWidgetItem * child = lg->child( 0 );
804+
while ( child )
805+
{
806+
QgsLegendLayer *cl = dynamic_cast<QgsLegendLayer *>( child );
807+
QgsLegendGroup *cg = dynamic_cast<QgsLegendGroup *>( child );
808+
809+
if ( cl )
810+
{
811+
cl->layer()->setCrs( crs );
812+
}
813+
else if ( cg )
814+
{
815+
setGroupCRS( cg, crs );
816+
}
817+
818+
child = lg->child( 0 );
819+
}
820+
821+
delete lg;
822+
}
823+
824+
769825
void QgsLegend::moveLayer( QgsMapLayer *ml, int groupIndex )
770826
{
771827
if ( !ml )
@@ -999,20 +1055,11 @@ bool QgsLegend::readXML( QgsLegendGroup *parent, const QDomNode &node )
9991055
{
10001056
bool isOpen;
10011057
QgsLegendLayer* currentLayer = readLayerFromXML( childelem, isOpen );
1002-
1003-
bool ignorePressed = QgsProjectBadLayerGuiHandler::mIgnore;
1004-
10051058
if ( !currentLayer )
1006-
{
1007-
if( ignorePressed == true )
1008-
{
1009-
continue;
1010-
}
1011-
else
1012-
{
1013-
return false;
1014-
}
1059+
{
1060+
continue;
10151061
}
1062+
10161063
// add to tree - either as a top-level node or a child of a group
10171064
if ( parent )
10181065
{
@@ -1920,3 +1967,32 @@ void QgsLegend::removeSelectedLayers()
19201967
if ( renderFlagState )
19211968
mMapCanvas->setRenderFlag( true );
19221969
}
1970+
1971+
void QgsLegend::setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs )
1972+
{
1973+
// Turn off rendering to improve speed.
1974+
bool renderFlagState = mMapCanvas->renderFlag();
1975+
if ( renderFlagState )
1976+
mMapCanvas->setRenderFlag( false );
1977+
1978+
foreach( QTreeWidgetItem * item, selectedItems() )
1979+
{
1980+
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item );
1981+
if ( lg )
1982+
{
1983+
setGroupCRS( lg, crs );
1984+
continue;
1985+
}
1986+
1987+
QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
1988+
if ( ll && ll->layer() )
1989+
{
1990+
ll->layer()->setCrs( crs );
1991+
continue;
1992+
}
1993+
}
1994+
1995+
// Turn on rendering (if it was on previously)
1996+
if ( renderFlagState )
1997+
mMapCanvas->setRenderFlag( true );
1998+
}

‎src/app/legend/qgslegend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QDomElement;
3434
class QDomNode;
3535
class QMouseEvent;
3636
class QTreeWidgetItem;
37+
class QgsCoordinateReferenceSystem;
3738

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

270+
/** Set CRS for selected layers */
271+
void setCRSForSelectedLayers( const QgsCoordinateReferenceSystem &crs );
272+
269273
protected:
270274

271275
/*!Event handler for mouse movements.
@@ -362,8 +366,12 @@ class QgsLegend : public QTreeWidget
362366
void handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position );
363367
/**Removes the current legend group*/
364368
void legendGroupRemove();
369+
/**Set the CRS of the current legend group*/
370+
void legendGroupSetCRS();
365371
/**Removes a legend group and its layers*/
366372
void removeGroup( QgsLegendGroup * lg );
373+
/**Removes a legend group and its layers*/
374+
void setGroupCRS( QgsLegendGroup * lg, const QgsCoordinateReferenceSystem &crs );
367375
/**Sets all listview items to open*/
368376
void expandAll();
369377
/**Sets all listview items to closed*/

‎src/app/legend/qgslegendlayer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu )
432432
// remove from canvas
433433
theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemoveLayer.png" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );
434434

435+
// remove from canvas
436+
theMenu.addAction( QgisApp::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) );
437+
435438
theMenu.addSeparator();
436439

437440
if ( lyr->type() == QgsMapLayer::VectorLayer )

‎src/app/qgisapp.cpp

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
#include "qgspluginmetadata.h"
142142
#include "qgspluginregistry.h"
143143
#include "qgspoint.h"
144-
#include "qgsprojectbadlayerguihandler.h"
144+
#include "qgshandlebadlayers.h"
145145
#include "qgsproject.h"
146146
#include "qgsprojectproperties.h"
147147
#include "qgsproviderregistry.h"
@@ -506,7 +506,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
506506
QgsRasterLayer::buildSupportedRasterFileFilter( mRasterFileFilter );
507507

508508
// set handler for missing layers (will be owned by QgsProject)
509-
QgsProject::instance()->setBadLayerHandler( new QgsProjectBadLayerGuiHandler() );
509+
QgsProject::instance()->setBadLayerHandler( new QgsHandleBadLayersHandler() );
510510

511511
#if 0
512512
// Set the background color for toolbox and overview as they default to
@@ -1125,6 +1125,12 @@ void QgisApp::createActions()
11251125
connect( mActionRemoveLayer, SIGNAL( triggered() ), this, SLOT( removeLayer() ) );
11261126
mActionRemoveLayer->setEnabled( false );
11271127

1128+
mActionSetLayerCRS = new QAction( getThemeIcon( "mActionSetLayerCRS.png" ), tr( "Set CRS of Layer(s)" ), this );
1129+
shortcuts->registerAction( mActionSetLayerCRS, tr( "Ctrl+Shift+C", "Set CRS of Layer(s)" ) );
1130+
mActionSetLayerCRS->setStatusTip( tr( "Set CRS of Layer(s)" ) );
1131+
connect( mActionSetLayerCRS, SIGNAL( triggered() ), this, SLOT( setLayerCRS() ) );
1132+
mActionSetLayerCRS->setEnabled( false );
1133+
11281134
mActionTileScale = new QAction( getThemeIcon( "mActionTileScale.png" ), tr( "Tile scale slider" ), this );
11291135
shortcuts->registerAction( mActionTileScale, tr( "", "Tile scale slider" ) );
11301136
mActionTileScale->setStatusTip( tr( "Show tile scale slider" ) );
@@ -1249,6 +1255,14 @@ void QgisApp::createActions()
12491255
#endif
12501256
mActionHelpContents->setStatusTip( tr( "Help Documentation" ) );
12511257
connect( mActionHelpContents, SIGNAL( triggered() ), this, SLOT( helpContents() ) );
1258+
mActionHelpContents->setEnabled( QFileInfo( QgsApplication::pkgDataPath() + "/doc/index.html" ).exists() );
1259+
1260+
#ifdef WITH_APIDOC
1261+
mActionHelpAPI = new QAction( getThemeIcon( "mActionHelpAPI.png" ), tr( "API documentation" ), this );
1262+
connect( mActionHelpAPI, SIGNAL( triggered() ), this, SLOT( apiDocumentation() ) );
1263+
mActionHelpAPI->setEnabled( QFileInfo( QgsApplication::pkgDataPath() + "/doc/api/index.html" ).exists() );
1264+
#endif
1265+
12521266

12531267
mActionQgisHomePage = new QAction( getThemeIcon( "mActionQgisHomePage.png" ), tr( "QGIS Home Page" ), this );
12541268
#ifndef Q_WS_MAC
@@ -1609,6 +1623,7 @@ void QgisApp::createMenus()
16091623
mLayerMenu->addAction( mActionLayerSaveAs );
16101624
mLayerMenu->addAction( mActionLayerSelectionSaveAs );
16111625
mLayerMenu->addAction( mActionRemoveLayer );
1626+
mLayerMenu->addAction( mActionSetLayerCRS );
16121627
mLayerMenu->addAction( mActionLayerProperties );
16131628
mLayerMenu->addAction( mActionLayerSubsetString );
16141629
mActionLayerSeparator2 = mLayerMenu->addSeparator();
@@ -1684,6 +1699,9 @@ void QgisApp::createMenus()
16841699
mHelpMenu = menuBar()->addMenu( tr( "&Help" ) );
16851700

16861701
mHelpMenu->addAction( mActionHelpContents );
1702+
#ifdef WITH_APIDOC
1703+
mHelpMenu->addAction( mActionHelpAPI );
1704+
#endif
16871705
mActionHelpSeparator1 = mHelpMenu->addSeparator();
16881706

16891707
mHelpMenu->addAction( mActionQgisHomePage );
@@ -1730,6 +1748,7 @@ void QgisApp::createToolBars()
17301748
mLayerToolBar->addAction( mActionAddWmsLayer );
17311749
mLayerToolBar->addAction( mActionNewVectorLayer );
17321750
mLayerToolBar->addAction( mActionRemoveLayer );
1751+
mLayerToolBar->addAction( mActionSetLayerCRS );
17331752
//commented out for QGIS 1.4 by Tim
17341753
//mLayerToolBar->addAction( mActionAddToOverview );
17351754
//mLayerToolBar->addAction( mActionShowAllLayers );
@@ -1898,6 +1917,9 @@ void QgisApp::createToolBars()
18981917
mHelpToolBar = addToolBar( tr( "Help" ) );
18991918
mHelpToolBar->setObjectName( "Help" );
19001919
mHelpToolBar->addAction( mActionHelpContents );
1920+
#ifdef WITH_APIDOC
1921+
mHelpToolBar->addAction( mActionHelpAPI );
1922+
#endif
19011923
mHelpToolBar->addAction( QWhatsThis::createAction() );
19021924
mToolbarMenu->addAction( mHelpToolBar->toggleViewAction() );
19031925

@@ -2108,6 +2130,7 @@ void QgisApp::setTheme( QString theThemeName )
21082130
mActionAddPgLayer->setIcon( getThemeIcon( "/mActionAddLayer.png" ) );
21092131
mActionAddSpatiaLiteLayer->setIcon( getThemeIcon( "/mActionAddSpatiaLiteLayer.png" ) );
21102132
mActionRemoveLayer->setIcon( getThemeIcon( "/mActionRemoveLayer.png" ) );
2133+
mActionSetLayerCRS->setIcon( getThemeIcon( "/mActionSetLayerCRS.png" ) );
21112134
mActionNewVectorLayer->setIcon( getThemeIcon( "/mActionNewVectorLayer.png" ) );
21122135
mActionAddAllToOverview->setIcon( getThemeIcon( "/mActionAddAllToOverview.png" ) );
21132136
mActionHideAllLayers->setIcon( getThemeIcon( "/mActionHideAllLayers.png" ) );
@@ -2120,6 +2143,9 @@ void QgisApp::setTheme( QString theThemeName )
21202143
mActionOptions->setIcon( getThemeIcon( "/mActionOptions.png" ) );
21212144
mActionConfigureShortcuts->setIcon( getThemeIcon( "/mActionOptions.png" ) );
21222145
mActionHelpContents->setIcon( getThemeIcon( "/mActionHelpContents.png" ) );
2146+
#ifdef WITH_APIDOC
2147+
mActionHelpAPI->setIcon( getThemeIcon( "/mActionHelpApi.png" ) );
2148+
#endif
21232149
mActionLocalHistogramStretch->setIcon( getThemeIcon( "/mActionLocalHistogramStretch.png" ) );
21242150
mActionQgisHomePage->setIcon( getThemeIcon( "/mActionQgisHomePage.png" ) );
21252151
mActionAbout->setIcon( getThemeIcon( "/mActionHelpAbout.png" ) );
@@ -4107,7 +4133,7 @@ void QgisApp::saveAsVectorFileGeneral( bool saveOnlySelection )
41074133
}
41084134
else
41094135
{
4110-
destCRS = vlayer->srs();
4136+
destCRS = vlayer->crs();
41114137
}
41124138
}
41134139
else
@@ -4812,7 +4838,7 @@ void QgisApp::editCut( QgsMapLayer * layerContainingSelection )
48124838
{
48134839
QgsFeatureList features = selectionVectorLayer->selectedFeatures();
48144840
clipboard()->replaceWithCopyOf( selectionVectorLayer->pendingFields(), features );
4815-
clipboard()->setCRS( selectionVectorLayer->srs() );
4841+
clipboard()->setCRS( selectionVectorLayer->crs() );
48164842
selectionVectorLayer->beginEditCommand( tr( "Features cut" ) );
48174843
selectionVectorLayer->deleteSelectedFeatures();
48184844
selectionVectorLayer->endEditCommand();
@@ -4839,7 +4865,7 @@ void QgisApp::editCopy( QgsMapLayer * layerContainingSelection )
48394865
{
48404866
QgsFeatureList features = selectionVectorLayer->selectedFeatures();
48414867
clipboard()->replaceWithCopyOf( selectionVectorLayer->pendingFields(), features );
4842-
clipboard()->setCRS( selectionVectorLayer->srs() );
4868+
clipboard()->setCRS( selectionVectorLayer->crs() );
48434869
}
48444870
}
48454871
}
@@ -4865,7 +4891,7 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
48654891
QgsFeatureList features;
48664892
if ( mMapCanvas->mapRenderer()->hasCrsTransformEnabled() )
48674893
{
4868-
features = clipboard()->transformedCopyOf( pasteVectorLayer->srs() );
4894+
features = clipboard()->transformedCopyOf( pasteVectorLayer->crs() );
48694895
}
48704896
else
48714897
{
@@ -5233,6 +5259,34 @@ void QgisApp::removeLayer()
52335259
mMapCanvas->refresh();
52345260
}
52355261

5262+
void QgisApp::setLayerCRS()
5263+
{
5264+
if ( mMapCanvas && mMapCanvas->isDrawing() )
5265+
{
5266+
return;
5267+
}
5268+
5269+
if ( !mMapLegend )
5270+
{
5271+
return;
5272+
}
5273+
5274+
QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector( this );
5275+
mySelector->setMessage();
5276+
if ( mySelector->exec() )
5277+
{
5278+
QgsCoordinateReferenceSystem crs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
5279+
mMapLegend->setCRSForSelectedLayers( crs );
5280+
mMapCanvas->refresh();
5281+
}
5282+
else
5283+
{
5284+
QApplication::restoreOverrideCursor();
5285+
}
5286+
5287+
delete mySelector;
5288+
}
5289+
52365290
void QgisApp::showGpsTool()
52375291
{
52385292
if ( !mpGpsWidget )
@@ -5550,7 +5604,7 @@ void QgisApp::options()
55505604
myRenderer->setProjectionsEnabled( false );
55515605
}
55525606
mMapCanvas->refresh();
5553-
}
5607+
}
55545608

55555609
delete optionsDialog;
55565610
}
@@ -5602,6 +5656,11 @@ void QgisApp::helpContents()
56025656
openURL( "index.html" );
56035657
}
56045658

5659+
void QgisApp::apiDocumentation()
5660+
{
5661+
openURL( "api/index.html" );
5662+
}
5663+
56055664
void QgisApp::helpQgisHomePage()
56065665
{
56075666
openURL( "http://qgis.org", false );
@@ -6283,6 +6342,7 @@ void QgisApp::selectionChanged( QgsMapLayer *layer )
62836342
void QgisApp::legendLayerSelectionChanged( void )
62846343
{
62856344
mActionRemoveLayer->setEnabled( mMapLegend && mMapLegend->selectedLayers().size() > 0 );
6345+
mActionSetLayerCRS->setEnabled( mMapLegend && mMapLegend->selectedLayers().size() > 0 );
62866346
}
62876347

62886348
void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )

‎src/app/qgisapp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class QgisApp : public QMainWindow
287287
QAction *actionLayerSaveAs() { return mActionLayerSaveAs; }
288288
QAction *actionLayerSelectionSaveAs() { return mActionLayerSelectionSaveAs; }
289289
QAction *actionRemoveLayer() { return mActionRemoveLayer; }
290+
QAction *actionSetLayerCRS() { return mActionSetLayerCRS; }
290291
QAction *actionTileScale() { return mActionTileScale; }
291292
QAction *actionGpsTool() { return mActionGpsTool; }
292293
QAction *actionLayerProperties() { return mActionLayerProperties; }
@@ -321,6 +322,7 @@ class QgisApp : public QMainWindow
321322
#endif
322323

323324
QAction *actionHelpContents() { return mActionHelpContents; }
325+
QAction *actionHelpAPI() { return mActionHelpAPI; }
324326
QAction *actionHelpSeparator1() { return mActionHelpSeparator1; }
325327
QAction *actionQgisHomePage() { return mActionQgisHomePage; }
326328
QAction *actionCheckQgisVersion() { return mActionCheckQgisVersion; }
@@ -496,6 +498,8 @@ class QgisApp : public QMainWindow
496498
void userCenter();
497499
//! Remove a layer from the map and legend
498500
void removeLayer();
501+
//! Set CRS of a layer
502+
void setLayerCRS();
499503
//! Show GPS tool
500504
void showGpsTool();
501505
//! Show tile scale slider
@@ -577,6 +581,8 @@ class QgisApp : public QMainWindow
577581
bool setActiveLayer( QgsMapLayer * );
578582
//! Open the help contents in a browser
579583
void helpContents();
584+
//! Open the API documentation in a browser
585+
void apiDocumentation();
580586
//! Open the QGIS homepage in users browser
581587
void helpQgisHomePage();
582588
//! Open a url in the users configured browser
@@ -980,6 +986,7 @@ class QgisApp : public QMainWindow
980986
QAction *mActionLayerSaveAs;
981987
QAction *mActionLayerSelectionSaveAs;
982988
QAction *mActionRemoveLayer;
989+
QAction *mActionSetLayerCRS;
983990
QAction *mActionTileScale;
984991
QAction *mActionGpsTool;
985992
QAction *mActionLayerProperties;
@@ -1013,6 +1020,7 @@ class QgisApp : public QMainWindow
10131020
#endif
10141021

10151022
QAction *mActionHelpContents;
1023+
QAction *mActionHelpAPI;
10161024
QAction *mActionHelpSeparator1;
10171025
QAction *mActionQgisHomePage;
10181026
QAction *mActionCheckQgisVersion;

0 commit comments

Comments
 (0)
Please sign in to comment.