bool QgsProject::addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList ) { QString type = layerElem.attribute( "type" ); QgsDebugMsg( "Layer type is " + type ); QgsMapLayer *mapLayer = NULL; if ( type == "vector" ) { mapLayer = new QgsVectorLayer; } else if ( type == "raster" ) { mapLayer = new QgsRasterLayer; } else if ( type == "plugin" ) { QString typeName = layerElem.attribute( "name" ); mapLayer = QgsPluginLayerRegistry::instance()->createLayer( typeName ); } if ( !mapLayer ) { QgsDebugMsg( "Unable to create layer" ); return false; } Q_CHECK_PTR( mapLayer ); // have the layer restore state that is stored in Dom node if ( mapLayer->readLayerXML( layerElem ) && mapLayer->isValid() ) { emit readMapLayer( mapLayer, layerElem ); QList<QgsMapLayer *> myLayers; myLayers << mapLayer; QgsMapLayerRegistry::instance()->addMapLayers( myLayers ); QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer ); if ( vLayer && vLayer->vectorJoins().size() > 0 ) { vectorLayerList.push_back( qMakePair( vLayer, layerElem ) ); } return true; } else { delete mapLayer; QgsDebugMsg( "Unable to load " + type + " layer" ); brokenNodes.push_back( layerElem ); return false; } }
void QgsLegendLayer::addToPopupMenu( QMenu& theMenu ) { QgsMapLayer *lyr = layer(); QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing(); QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits(); QAction *allEditsAction = QgisApp::instance()->actionAllEdits(); // zoom to layer extent theMenu.addAction( QgsApplication::getThemeIcon( "/mActionZoomToLayer.svg" ), tr( "&Zoom to Layer Extent" ), legend(), SLOT( legendLayerZoom() ) ); if ( lyr->type() == QgsMapLayer::RasterLayer ) { theMenu.addAction( tr( "&Zoom to Best Scale (100%)" ), legend(), SLOT( legendLayerZoomNative() ) ); QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( lyr ); if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette ) { theMenu.addAction( tr( "&Stretch Using Current Extent" ), legend(), SLOT( legendLayerStretchUsingCurrentExtent() ) ); } } // show in overview QAction* showInOverviewAction = theMenu.addAction( tr( "&Show in Overview" ), this, SLOT( showInOverview() ) ); showInOverviewAction->setCheckable( true ); showInOverviewAction->blockSignals( true ); showInOverviewAction->setChecked( mLyr.isInOverview() ); showInOverviewAction->blockSignals( false ); // remove from canvas theMenu.addAction( QgsApplication::getThemeIcon( "/mActionRemoveLayer.svg" ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) ); // duplicate layer QAction* duplicateLayersAction = theMenu.addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) ); // set layer crs theMenu.addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) ); // assign layer crs to project theMenu.addAction( QgsApplication::getThemeIcon( "/mActionSetProjectCRS.png" ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCRSFromLayer() ) ); theMenu.addSeparator(); if ( lyr->type() == QgsMapLayer::VectorLayer ) { QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( lyr ); // attribute table theMenu.addAction( QgsApplication::getThemeIcon( "/mActionOpenTable.png" ), tr( "&Open Attribute Table" ), QgisApp::instance(), SLOT( attributeTable() ) ); // allow editing int cap = vlayer->dataProvider()->capabilities(); if ( cap & QgsVectorDataProvider::EditingCapabilities ) { if ( toggleEditingAction ) { theMenu.addAction( toggleEditingAction ); toggleEditingAction->setChecked( vlayer->isEditable() ); } if ( saveLayerEditsAction && vlayer->isModified() ) { theMenu.addAction( saveLayerEditsAction ); } } if ( allEditsAction->isEnabled() ) { theMenu.addAction( allEditsAction ); } // disable duplication of memory layers if ( vlayer->storageType() == "Memory storage" && legend()->selectedLayers().count() == 1 ) { duplicateLayersAction->setEnabled( false ); } // save as vector file theMenu.addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) ); // save selection as vector file QAction* saveSelectionAsAction = theMenu.addAction( tr( "Save Selection As..." ), QgisApp::instance(), SLOT( saveSelectionAsVectorFile() ) ); if ( vlayer->selectedFeatureCount() == 0 ) { saveSelectionAsAction->setEnabled( false ); } if ( !vlayer->isEditable() && vlayer->dataProvider()->supportsSubsetString() && vlayer->vectorJoins().isEmpty() ) theMenu.addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) ); //show number of features in legend if requested QAction* showNFeaturesAction = new QAction( tr( "Show Feature Count" ), &theMenu ); showNFeaturesAction->setCheckable( true ); showNFeaturesAction->setChecked( mShowFeatureCount ); QObject::connect( showNFeaturesAction, SIGNAL( toggled( bool ) ), this, SLOT( setShowFeatureCount( bool ) ) ); theMenu.addAction( showNFeaturesAction ); theMenu.addSeparator(); }
QMenu* QgsAppLayerTreeViewMenuProvider::createContextMenu() { QMenu* menu = new QMenu; QgsLayerTreeViewDefaultActions* actions = mView->defaultActions(); QModelIndex idx = mView->currentIndex(); if ( !idx.isValid() ) { // global menu menu->addAction( actions->actionAddGroup( menu ) ); // TODO: expand all, collapse all // TODO: update drawing order } else if ( QgsLayerTreeNode* node = mView->layerTreeModel()->index2node( idx ) ) { // layer or group selected if ( QgsLayerTree::isGroup( node ) ) { menu->addAction( actions->actionZoomToGroup( mCanvas, menu ) ); menu->addAction( actions->actionRemoveGroupOrLayer( menu ) ); menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCRS() ) ); menu->addAction( actions->actionRenameGroupOrLayer( menu ) ); if ( mView->selectedNodes( true ).count() >= 2 ) menu->addAction( actions->actionGroupSelected( menu ) ); menu->addAction( actions->actionAddGroup( menu ) ); } else if ( QgsLayerTree::isLayer( node ) ) { QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer(); menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) ); menu->addAction( actions->actionShowInOverview( menu ) ); if ( layer && layer->type() == QgsMapLayer::RasterLayer ) { menu->addAction( tr( "&Zoom to Best Scale (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) ); QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( layer ); if ( rasterLayer && rasterLayer->rasterType() != QgsRasterLayer::Palette ) menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) ); } menu->addAction( actions->actionRemoveGroupOrLayer( menu ) ); // duplicate layer QAction* duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( "/mActionDuplicateLayer.svg" ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) ); // set layer scale visibility menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) ); // set layer crs menu->addAction( QgsApplication::getThemeIcon( "/mActionSetCRS.png" ), tr( "&Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCRS() ) ); // assign layer crs to project menu->addAction( QgsApplication::getThemeIcon( "/mActionSetProjectCRS.png" ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCRSFromLayer() ) ); menu->addSeparator(); if ( layer && layer->type() == QgsMapLayer::VectorLayer ) { QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer ); QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing(); QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits(); QAction *allEditsAction = QgisApp::instance()->actionAllEdits(); // attribute table menu->addAction( QgsApplication::getThemeIcon( "/mActionOpenTable.png" ), tr( "&Open Attribute Table" ), QgisApp::instance(), SLOT( attributeTable() ) ); // allow editing int cap = vlayer->dataProvider()->capabilities(); if ( cap & QgsVectorDataProvider::EditingCapabilities ) { if ( toggleEditingAction ) { menu->addAction( toggleEditingAction ); toggleEditingAction->setChecked( vlayer->isEditable() ); } if ( saveLayerEditsAction && vlayer->isModified() ) { menu->addAction( saveLayerEditsAction ); } } if ( allEditsAction->isEnabled() ) menu->addAction( allEditsAction ); // disable duplication of memory layers if ( vlayer->storageType() == "Memory storage" && mView->selectedLayerNodes().count() == 1 ) duplicateLayersAction->setEnabled( false ); // save as vector file menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) ); menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) ); if ( !vlayer->isEditable() && vlayer->dataProvider()->supportsSubsetString() && vlayer->vectorJoins().isEmpty() ) menu->addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) ); menu->addAction( actions->actionShowFeatureCount( menu ) ); menu->addSeparator(); } else if ( layer && layer->type() == QgsMapLayer::RasterLayer ) { menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) ); menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) ); } else if ( layer && layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 ) { // disable duplication of plugin layers duplicateLayersAction->setEnabled( false ); } // TODO: custom actions if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() ) menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) ); if ( node->parent() != mView->layerTreeModel()->rootGroup() ) menu->addAction( actions->actionMakeTopLevel( menu ) ); menu->addAction( actions->actionRenameGroupOrLayer( menu ) ); if ( mView->selectedNodes( true ).count() >= 2 ) menu->addAction( actions->actionGroupSelected( menu ) ); if ( mView->selectedLayerNodes().count() == 1 ) { QgisApp* app = QgisApp::instance(); menu->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) ); if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) ) { menu->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) ); } } } } else { // symbology item? } return menu; }