void QgsLegendLayerFile::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingAction ) { QgsMapLayer* lyr = layer(); // zoom to layer extent theMenu.addAction( QgisApp::getThemeIcon( "/mActionZoomToLayer.png" ), tr( "&Zoom to layer extent" ), legend(), SLOT( legendLayerZoom() ) ); // 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( QgisApp::getThemeIcon( "/mActionRemove.png" ), tr( "&Remove" ), legend(), SLOT( legendLayerRemove() ) ); theMenu.addSeparator(); if ( lyr->type() == QgsMapLayer::VectorLayer ) { QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( lyr ); // attribute table theMenu.addAction( tr( "&Open attribute table" ), this, SLOT( table() ) ); // editing int cap = vlayer->dataProvider()->capabilities(); if ( cap & QgsVectorDataProvider::EditingCapabilities ) { if ( toggleEditingAction ) { theMenu.addAction( toggleEditingAction ); } } // save as shapefile theMenu.addAction( tr( "Save as shapefile..." ), this, SLOT( saveAsShapefile() ) ); QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) ); if ( vlayer->selectedFeatureCount() == 0 ) { saveSelectionAction->setEnabled( false ); } theMenu.addSeparator(); } else if ( lyr->type() == QgsMapLayer::RasterLayer ) { // TODO: what was this for? //QgsRasterLayer* rlayer = dynamic_cast<QgsRasterLayer*>(lyr); //theMenu.addAction(tr("&Convert to..."), rlayer, SLOT(convertTo())); } // properties goes on bottom of menu for consistency with normal ui standards // e.g. kde stuff theMenu.addAction( tr( "&Properties" ), legend(), SLOT( legendLayerShowProperties() ) ); }
bool QgsMapToolAddPart::checkSelection() { //check if we operate on a vector layer QgsVectorLayer *vlayer = currentVectorLayer(); if ( !vlayer ) { notifyNotVectorLayer(); return false; } //inform user at the begin of the digitizing action that the island tool only works if exactly one feature is selected int nSelectedFeatures = vlayer->selectedFeatureCount(); QString selectionErrorMsg; if ( nSelectedFeatures < 1 ) { selectionErrorMsg = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" ); } else if ( nSelectedFeatures > 1 ) { selectionErrorMsg = tr( "Several features are selected. Please select only one feature to which an part should be added." ); } if ( !selectionErrorMsg.isEmpty() ) { emit messageEmitted( tr( "Could not add part. %1" ).arg( selectionErrorMsg ), Qgis::Warning ); } return selectionErrorMsg.isEmpty(); }
QString QgsSpatialQueryDialog::getDescriptionLayerShow( bool isTarget ) { QgsVectorLayer* lyr = NULL; QCheckBox * checkBox = NULL; if ( isTarget ) { lyr = mLayerTarget; checkBox = ckbUsingSelectedTarget; } else { lyr = mLayerReference; checkBox = ckbUsingSelectedReference; } QString sDescFeatures = checkBox->isChecked() ? tr( "%1 of %2" ).arg( lyr->selectedFeatureCount() ).arg( lyr->featureCount() ) : tr( "all = %1" ).arg( lyr->featureCount() ); return QString( "%1 (%2)" ).arg( lyr->name() ).arg( sDescFeatures ); } // QString QgsSpatialQueryDialog::getDescriptionLayerShow(bool isTarget)
void QgsSpatialQueryDialog::evaluateCheckBoxLayer( bool isTarget ) { QgsVectorLayer* lyr = NULL; QCheckBox* checkbox = NULL; if ( isTarget ) { lyr = mLayerTarget; checkbox = ckbUsingSelectedTarget; } else { lyr = mLayerReference; checkbox = ckbUsingSelectedReference; } int selectedCount = lyr->selectedFeatureCount(); bool isCheckBoxValid = ( lyr != NULL && selectedCount > 0 ); checkbox->setChecked( isCheckBoxValid ); checkbox->setEnabled( isCheckBoxValid ); QString textCheckBox = isCheckBoxValid ? tr( "%n selected geometries", "selected geometries", selectedCount ) : tr( "Selected geometries" ); checkbox->setText( textCheckBox ); } // void QgsSpatialQueryDialog::evaluateCheckBoxLayer(bool isTarget)
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(); }
void QgsMapToolAddPart::canvasReleaseEvent( QMouseEvent * e ) { //check if we operate on a vector layer QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() ); if ( !vlayer ) { notifyNotVectorLayer(); return; } if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } //inform user at the begin of the digitising action that the island tool only works if exactly one feature is selected int nSelectedFeatures = vlayer->selectedFeatureCount(); QString selectionErrorMsg; if ( nSelectedFeatures < 1 ) { selectionErrorMsg = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" ); } else if ( nSelectedFeatures > 1 ) { selectionErrorMsg = tr( "Several features are selected. Please select only one feature to which an part should be added." ); } if ( !selectionErrorMsg.isEmpty() ) { QMessageBox::critical( 0, tr( "Error. Could not add part." ), selectionErrorMsg ); stopCapturing(); return; } int errorCode; switch ( mode() ) { case CapturePoint: { QgsPoint layerPoint; QgsPoint mapPoint; if ( nextPoint( e->pos(), layerPoint, mapPoint ) != 0 ) { QgsDebugMsg( "nextPoint failed" ); return; } vlayer->beginEditCommand( tr( "Part added" ) ); errorCode = vlayer->addPart( QList<QgsPoint>() << layerPoint ); } break; case CaptureLine: case CapturePolygon: { //add point to list and to rubber band if ( e->button() == Qt::LeftButton ) { int error = addVertex( e->pos() ); if ( error == 1 ) { QgsDebugMsg( "current layer is not a vector layer" ); return; } else if ( error == 2 ) { //problem with coordinate transformation QMessageBox::information( 0, tr( "Coordinate transform error" ), tr( "Cannot transform the point to the layers coordinate system" ) ); return; } startCapturing(); return; } else if ( e->button() != Qt::RightButton ) { deleteTempRubberBand(); return; } if ( mode() == CapturePolygon ) { //close polygon closePolygon(); //avoid intersections QgsGeometry* geom = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() ); if ( geom ) { geom->avoidIntersections(); QgsPolygon poly = geom->asPolygon(); if ( poly.size() < 1 ) { stopCapturing(); delete geom; vlayer->destroyEditCommand(); return; } setPoints( geom->asPolygon()[0].toList() ); delete geom; } } vlayer->beginEditCommand( tr( "Part added" ) ); errorCode = vlayer->addPart( points() ); stopCapturing(); } break; default: Q_ASSERT( !"invalid capture mode" ); errorCode = 6; break; } QString errorMessage; switch ( errorCode ) { case 0: { //add points to other features to keep topology up-to-date int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 ); if ( topologicalEditing ) { addTopologicalPoints( points() ); } vlayer->endEditCommand(); mCanvas->refresh(); return; } case 1: errorMessage = tr( "Selected feature is not multi part." ); break; case 2: errorMessage = tr( "New part's geometry is not valid." ); break; case 3: errorMessage = tr( "New polygon ring not disjoint with existing polygons." ); break; case 4: errorMessage = tr( "No feature selected. Please select a feature with the selection tool or in the attribute table" ); break; case 5: errorMessage = tr( "Several features are selected. Please select only one feature to which an island should be added." ); break; case 6: errorMessage = tr( "Selected geometry could not be found" ); break; } QMessageBox::critical( 0, tr( "Error, could not add part" ), errorMessage ); vlayer->destroyEditCommand(); }
void QgsMapToolRotateFeature::canvasReleaseEvent( QgsMapMouseEvent *e ) { if ( !mCanvas ) { return; } QgsVectorLayer *vlayer = currentVectorLayer(); if ( !vlayer ) { deleteRotationWidget(); deleteRubberband(); notifyNotVectorLayer(); return; } if ( e->button() == Qt::RightButton ) { cancel(); return; } // place anchor point on CTRL + click if ( e->modifiers() & Qt::ControlModifier ) { if ( !mAnchorPoint ) { return; } mAnchorPoint->setCenter( toMapCoordinates( e->pos() ) ); mStartPointMapCoords = toMapCoordinates( e->pos() ); mStPoint = e->pos(); return; } deleteRotationWidget(); // Initialize rotation if not yet active if ( !mRotationActive ) { mRotation = 0; mRotationOffset = 0; deleteRubberband(); mInitialPos = e->pos(); if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } QgsPointXY layerCoords = toLayerCoordinates( vlayer, e->pos() ); double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setNoAttributes() ); //find the closest feature QgsGeometry pointGeometry = QgsGeometry::fromPointXY( layerCoords ); if ( pointGeometry.isNull() ) { return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.hasGeometry() ) { double currentDistance = pointGeometry.distance( f.geometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } if ( minDistance == std::numeric_limits<double>::max() ) { emit messageEmitted( tr( "Could not find a nearby feature in the current layer." ) ); return; } QgsRectangle bound = cf.geometry().boundingBox(); mStartPointMapCoords = toMapCoordinates( vlayer, bound.center() ); if ( !mAnchorPoint ) { mAnchorPoint = qgis::make_unique<QgsVertexMarker>( mCanvas ); } mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS ); mAnchorPoint->setCenter( mStartPointMapCoords ); mStPoint = toCanvasCoordinates( mStartPointMapCoords ); mRotatedFeatures.clear(); mRotatedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.geometry(), vlayer ); } else { mRotatedFeatures = vlayer->selectedFeatureIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->getSelectedFeatures(); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.geometry(), vlayer ); } } mRubberBand->show(); double XDistance = mInitialPos.x() - mAnchorPoint->x(); double YDistance = mInitialPos.y() - mAnchorPoint->y(); mRotationOffset = std::atan2( YDistance, XDistance ) * ( 180 / M_PI ); createRotationWidget(); if ( e->modifiers() & Qt::ShiftModifier ) { if ( mRotationWidget ) { mRotationWidget->setMagnet( 45 ); } } mRotationActive = true; return; } applyRotation( mRotation ); }
void QgsMapToolMoveFeature::canvasPressEvent( QgsMapMouseEvent* e ) { delete mRubberBand; mRubberBand = nullptr; QgsVectorLayer* vlayer = currentVectorLayer(); if ( !vlayer ) { notifyNotVectorLayer(); return; } if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } //find first geometry under mouse cursor and store iterator to it QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() ); QSettings settings; double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setSubsetOfAttributes( QgsAttributeList() ) ); //find the closest feature QgsGeometry* pointGeometry = QgsGeometry::fromPoint( layerCoords ); if ( !pointGeometry ) { return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.constGeometry() ) { double currentDistance = pointGeometry->distance( *f.constGeometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } delete pointGeometry; if ( minDistance == std::numeric_limits<double>::max() ) { return; } mMovedFeatures.clear(); mMovedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.constGeometry(), vlayer ); } else { mMovedFeatures = vlayer->selectedFeaturesIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->selectedFeaturesIterator( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) ); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.constGeometry(), vlayer ); } } mStartPointMapCoords = toMapCoordinates( e->pos() ); mRubberBand->setColor( QColor( 255, 0, 0, 65 ) ); mRubberBand->setWidth( 2 ); mRubberBand->show(); }
QString QgsSpatialQueryDialog::getDescriptionInvalidFeaturesShow( bool isTarget ) { QgsVectorLayer* lyr = NULL; QCheckBox* checkBox = NULL; int totalInvalid = 0; if ( isTarget ) { lyr = mLayerTarget; checkBox = ckbUsingSelectedTarget; totalInvalid = mFeatureInvalidTarget.size(); } else { lyr = mLayerReference; checkBox = ckbUsingSelectedReference; totalInvalid = mFeatureInvalidReference.size(); } QString sDescFeatures = checkBox->isChecked() ? tr( "%1 of %2(selected features)" ).arg( totalInvalid ).arg( lyr->selectedFeatureCount() ) : tr( "%1 of %2" ).arg( totalInvalid ).arg( lyr->featureCount() ); return QString( "%1: %2" ).arg( lyr->name() ).arg( sDescFeatures ); } // QString QgsSpatialQueryDialog::getDescriptionInvalidFeatures(bool isTarget)
void QgsMapToolRotateFeature::canvasPressEvent( QMouseEvent * e ) { mRotation = 0; if ( mCtrl ) { return; } delete mRubberBand; mRubberBand = 0; mInitialPos = e->pos(); QgsVectorLayer* vlayer = currentVectorLayer(); if ( !vlayer ) { notifyNotVectorLayer(); return; } if ( !vlayer->isEditable() ) { notifyNotEditableLayer(); return; } QgsPoint layerCoords = toLayerCoordinates( vlayer, e->pos() ); double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setSubsetOfAttributes( QgsAttributeList() ) ); //find the closest feature QgsGeometry* pointGeometry = QgsGeometry::fromPoint( layerCoords ); if ( !pointGeometry ) { return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.geometry() ) { double currentDistance = pointGeometry->distance( *f.geometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } delete pointGeometry; if ( minDistance == std::numeric_limits<double>::max() ) { return; } QgsRectangle bound = cf.geometry()->boundingBox(); mStartPointMapCoords = toMapCoordinates( vlayer, bound.center() ); if ( !mAnchorPoint ) { mAnchorPoint = new QgsVertexMarker( mCanvas ); } mAnchorPoint->setIconType( QgsVertexMarker::ICON_CROSS ); mAnchorPoint->setCenter( mStartPointMapCoords ); mStPoint = toCanvasCoordinates( mStartPointMapCoords ); mRotatedFeatures.clear(); mRotatedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.geometry(), vlayer ); } else { mRotatedFeatures = vlayer->selectedFeaturesIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->selectedFeaturesIterator(); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.geometry(), vlayer ); } } mRubberBand->setColor( QColor( 255, 0, 0, 65 ) ); mRubberBand->setWidth( 2 ); mRubberBand->show(); double XDistance = mInitialPos.x() - mAnchorPoint->x(); double YDistance = mInitialPos.y() - mAnchorPoint->y() ; mRotationOffset = atan2( YDistance, XDistance ) * ( 180 / PI ); }
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 ) ); menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionExpandTree.svg" ) ), tr( "&Expand All" ), mView, SLOT( expandAll() ) ); menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionCollapseTree.svg" ) ), tr( "&Collapse All" ), mView, SLOT( collapseAll() ) ); menu->addSeparator(); if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) ) { QAction *actionPasteLayerOrGroup = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionEditPaste.svg" ) ), tr( "Paste Layer/Group" ), menu ); connect( actionPasteLayerOrGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::pasteLayer ); menu->addAction( actionPasteLayerOrGroup ); } // 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( tr( "Copy Group" ), QgisApp::instance(), SLOT( copyLayer() ) ); if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) ) { QAction *actionPasteLayerOrGroup = new QAction( tr( "Paste Layer/Group" ), menu ); connect( actionPasteLayerOrGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::pasteLayer ); menu->addAction( actionPasteLayerOrGroup ); } menu->addAction( actions->actionRenameGroupOrLayer( menu ) ); menu->addSeparator(); menu->addAction( actions->actionAddGroup( menu ) ); QAction *removeAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionRemoveLayer.svg" ) ), tr( "&Remove Group…" ), QgisApp::instance(), SLOT( removeLayer() ) ); removeAction->setEnabled( removeActionEnabled() ); menu->addSeparator(); menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSetCRS.png" ) ), tr( "&Set Group CRS…" ), QgisApp::instance(), SLOT( legendGroupSetCrs() ) ); menu->addAction( tr( "&Set Group WMS Data…" ), QgisApp::instance(), SLOT( legendGroupSetWmsData() ) ); menu->addSeparator(); menu->addAction( actions->actionMutuallyExclusiveGroup( menu ) ); menu->addAction( actions->actionCheckAndAllChildren( menu ) ); menu->addAction( actions->actionUncheckAndAllChildren( menu ) ); if ( !( mView->selectedNodes( true ).count() == 1 && idx.row() == 0 ) ) { menu->addAction( actions->actionMoveToTop( menu ) ); } menu->addSeparator(); if ( mView->selectedNodes( true ).count() >= 2 ) menu->addAction( actions->actionGroupSelected( menu ) ); if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) ) { menu->addAction( tr( "Paste Style" ), QgisApp::instance(), SLOT( applyStyleToGroup() ) ); } menu->addSeparator(); QMenu *menuExportGroup = new QMenu( tr( "Export" ), menu ); QAction *actionSaveAsDefinitionGroup = new QAction( tr( "Save as Layer Definition File…" ), menuExportGroup ); connect( actionSaveAsDefinitionGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::saveAsLayerDefinition ); menuExportGroup->addAction( actionSaveAsDefinitionGroup ); menu->addMenu( menuExportGroup ); } else if ( QgsLayerTree::isLayer( node ) ) { QgsMapLayer *layer = QgsLayerTree::toLayer( node )->layer(); QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer ); QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer ); if ( layer && layer->isSpatial() ) { menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) ); if ( vlayer ) { QAction *actionZoomSelected = actions->actionZoomToSelection( mCanvas, menu ); actionZoomSelected->setEnabled( !vlayer->selectedFeatures().isEmpty() ); menu->addAction( actionZoomSelected ); } menu->addAction( actions->actionShowInOverview( menu ) ); } if ( vlayer ) menu->addAction( actions->actionShowFeatureCount( menu ) ); QAction *actionCopyLayer = new QAction( tr( "Copy Layer" ), menu ); connect( actionCopyLayer, &QAction::triggered, QgisApp::instance(), &QgisApp::copyLayer ); menu->addAction( actionCopyLayer ); menu->addAction( actions->actionRenameGroupOrLayer( menu ) ); if ( rlayer ) { menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomActual.svg" ) ), tr( "&Zoom to Native Resolution (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) ); if ( rlayer->rasterType() != QgsRasterLayer::Palette ) menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) ); } addCustomLayerActions( menu, layer ); if ( layer && layer->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer *>( layer )->providerType() == QLatin1String( "virtual" ) ) { menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddVirtualLayer.svg" ) ), tr( "Edit Virtual Layer…" ), QgisApp::instance(), SLOT( addVirtualLayer() ) ); } menu->addSeparator(); // duplicate layer QAction *duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateLayer.svg" ) ), tr( "&Duplicate Layer" ), QgisApp::instance(), SLOT( duplicateLayers() ) ); QAction *removeAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionRemoveLayer.svg" ) ), tr( "&Remove Layer…" ), QgisApp::instance(), SLOT( removeLayer() ) ); removeAction->setEnabled( removeActionEnabled() ); menu->addSeparator(); if ( node->parent() != mView->layerTreeModel()->rootGroup() ) menu->addAction( actions->actionMoveOutOfGroup( menu ) ); if ( !( mView->selectedNodes( true ).count() == 1 && idx.row() == 0 ) ) { menu->addAction( actions->actionMoveToTop( menu ) ); } QAction *checkAll = actions->actionCheckAndAllParents( menu ); if ( checkAll ) menu->addAction( checkAll ); if ( mView->selectedNodes( true ).count() >= 2 ) menu->addAction( actions->actionGroupSelected( menu ) ); menu->addSeparator(); if ( vlayer ) { QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing(); QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits(); QAction *allEditsAction = QgisApp::instance()->actionAllEdits(); // attribute table menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ), 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() ); toggleEditingAction->setEnabled( true ); } if ( saveLayerEditsAction && vlayer->isModified() ) { menu->addAction( saveLayerEditsAction ); } } if ( allEditsAction->isEnabled() ) menu->addAction( allEditsAction ); // disable duplication of memory layers if ( vlayer->storageType() == QLatin1String( "Memory storage" ) && mView->selectedLayerNodes().count() == 1 ) duplicateLayersAction->setEnabled( false ); if ( vlayer->dataProvider()->supportsSubsetString() ) { QAction *action = menu->addAction( tr( "&Filter…" ), QgisApp::instance(), SLOT( layerSubsetString() ) ); action->setEnabled( !vlayer->isEditable() ); } } menu->addSeparator(); if ( layer && layer->isSpatial() ) { // set layer scale visibility menu->addAction( tr( "&Set Layer Scale Visibility…" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) ); if ( !layer->isInScaleRange( mCanvas->scale() ) ) menu->addAction( tr( "Zoom to &Visible Scale" ), QgisApp::instance(), SLOT( zoomToLayerScale() ) ); QMenu *menuSetCRS = new QMenu( tr( "Set CRS" ), menu ); // set layer crs QAction *actionSetLayerCrs = new QAction( tr( "Set Layer CRS…" ), menuSetCRS ); connect( actionSetLayerCrs, &QAction::triggered, QgisApp::instance(), &QgisApp::setLayerCrs ); menuSetCRS->addAction( actionSetLayerCrs ); // assign layer crs to project QAction *actionSetProjectCrs = new QAction( tr( "Set &Project CRS from Layer" ), menuSetCRS ); connect( actionSetProjectCrs, &QAction::triggered, QgisApp::instance(), &QgisApp::setProjectCrsFromLayer ); menuSetCRS->addAction( actionSetProjectCrs ); menu->addMenu( menuSetCRS ); } menu->addSeparator(); if ( vlayer ) { // save as vector file QMenu *menuExportVector = new QMenu( tr( "Export" ), menu ); QAction *actionSaveAs = new QAction( tr( "Save Features As…" ), menuExportVector ); connect( actionSaveAs, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveAsFile(); } ); menuExportVector->addAction( actionSaveAs ); QAction *actionSaveSelectedFeaturesAs = new QAction( tr( "Save Selected Features As…" ), menuExportVector ); connect( actionSaveSelectedFeaturesAs, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveAsFile( nullptr, true ); } ); actionSaveSelectedFeaturesAs->setEnabled( vlayer->selectedFeatureCount() > 0 ); menuExportVector->addAction( actionSaveSelectedFeaturesAs ); QAction *actionSaveAsDefinitionLayer = new QAction( tr( "Save as Layer Definition File…" ), menuExportVector ); connect( actionSaveAsDefinitionLayer, &QAction::triggered, QgisApp::instance(), &QgisApp::saveAsLayerDefinition ); menuExportVector->addAction( actionSaveAsDefinitionLayer ); if ( vlayer->isSpatial() ) { QAction *actionSaveStyle = new QAction( tr( "Save as QGIS Layer Style File…" ), menuExportVector ); connect( actionSaveStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveStyleFile(); } ); menuExportVector->addAction( actionSaveStyle ); } menu->addMenu( menuExportVector ); } else if ( rlayer ) { QMenu *menuExportRaster = new QMenu( tr( "Export" ), menu ); QAction *actionSaveAs = new QAction( tr( "Save As…" ), menuExportRaster ); QAction *actionSaveAsDefinitionLayer = new QAction( tr( "Save as Layer Definition File…" ), menuExportRaster ); QAction *actionSaveStyle = new QAction( tr( "Save as QGIS Layer Style File…" ), menuExportRaster ); connect( actionSaveAs, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveAsFile(); } ); menuExportRaster->addAction( actionSaveAs ); connect( actionSaveAsDefinitionLayer, &QAction::triggered, QgisApp::instance(), &QgisApp::saveAsLayerDefinition ); menuExportRaster->addAction( actionSaveAsDefinitionLayer ); connect( actionSaveStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveStyleFile(); } ); menuExportRaster->addAction( actionSaveStyle ); menu->addMenu( menuExportRaster ); } else if ( layer && layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 ) { // disable duplication of plugin layers duplicateLayersAction->setEnabled( false ); } menu->addSeparator(); // style-related actions if ( layer && mView->selectedLayerNodes().count() == 1 ) { menu->addSeparator(); QMenu *menuStyleManager = new QMenu( tr( "Styles" ), menu ); QgisApp *app = QgisApp::instance(); menuStyleManager->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) ); if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) ) { menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) ); } menuStyleManager->addSeparator(); QgsMapLayerStyleGuiUtils::instance()->addStyleManagerActions( menuStyleManager, layer ); if ( vlayer ) { const QgsSingleSymbolRenderer *singleRenderer = dynamic_cast< const QgsSingleSymbolRenderer * >( vlayer->renderer() ); if ( !singleRenderer && vlayer->renderer() && vlayer->renderer()->embeddedRenderer() ) { singleRenderer = dynamic_cast< const QgsSingleSymbolRenderer * >( vlayer->renderer()->embeddedRenderer() ); } if ( singleRenderer && singleRenderer->symbol() ) { //single symbol renderer, so add set color/edit symbol actions menuStyleManager->addSeparator(); QgsColorWheel *colorWheel = new QgsColorWheel( menuStyleManager ); colorWheel->setColor( singleRenderer->symbol()->color() ); QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, menuStyleManager, menuStyleManager ); colorAction->setDismissOnColorSelection( false ); connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor ); //store the layer id in action, so we can later retrieve the corresponding layer colorAction->setProperty( "layerId", vlayer->id() ); menuStyleManager->addAction( colorAction ); //add recent colors action QList<QgsRecentColorScheme *> recentSchemes; QgsApplication::colorSchemeRegistry()->schemes( recentSchemes ); if ( !recentSchemes.isEmpty() ) { QgsColorSwatchGridAction *recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menuStyleManager, QStringLiteral( "symbology" ), menuStyleManager ); recentColorAction->setProperty( "layerId", vlayer->id() ); recentColorAction->setDismissOnColorSelection( false ); menuStyleManager->addAction( recentColorAction ); connect( recentColorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor ); } menuStyleManager->addSeparator(); QAction *editSymbolAction = new QAction( tr( "Edit Symbol…" ), menuStyleManager ); //store the layer id in action, so we can later retrieve the corresponding layer editSymbolAction->setProperty( "layerId", vlayer->id() ); connect( editSymbolAction, &QAction::triggered, this, &QgsAppLayerTreeViewMenuProvider::editVectorSymbol ); menuStyleManager->addAction( editSymbolAction ); } } menu->addMenu( menuStyleManager ); } else { if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) ) { menu->addAction( tr( "Paste Style" ), QgisApp::instance(), SLOT( applyStyleToGroup() ) ); } } if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() ) menu->addAction( tr( "&Properties…" ), QgisApp::instance(), SLOT( layerProperties() ) ); } } else if ( QgsLayerTreeModelLegendNode *node = mView->layerTreeModel()->index2legendNode( idx ) ) { if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( node ) ) { // symbology item if ( symbolNode->flags() & Qt::ItemIsUserCheckable ) { menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) ), tr( "&Show All Items" ), symbolNode, SLOT( checkAllItems() ) ); menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionHideAllLayers.svg" ) ), tr( "&Hide All Items" ), symbolNode, SLOT( uncheckAllItems() ) ); menu->addSeparator(); } if ( symbolNode->symbol() ) { QgsColorWheel *colorWheel = new QgsColorWheel( menu ); colorWheel->setColor( symbolNode->symbol()->color() ); QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, menu, menu ); colorAction->setDismissOnColorSelection( false ); connect( colorAction, &QgsColorWidgetAction::colorChanged, this, &QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor ); //store the layer id and rule key in action, so we can later retrieve the corresponding //legend node, if it still exists colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() ); colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() ); menu->addAction( colorAction ); //add recent colors action QList<QgsRecentColorScheme *> recentSchemes; QgsApplication::colorSchemeRegistry()->schemes( recentSchemes ); if ( !recentSchemes.isEmpty() ) { QgsColorSwatchGridAction *recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menu, QStringLiteral( "symbology" ), menu ); recentColorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() ); recentColorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() ); recentColorAction->setDismissOnColorSelection( false ); menu->addAction( recentColorAction ); connect( recentColorAction, &QgsColorSwatchGridAction::colorChanged, this, &QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor ); } menu->addSeparator(); } QAction *editSymbolAction = new QAction( tr( "Edit Symbol…" ), menu ); //store the layer id and rule key in action, so we can later retrieve the corresponding //legend node, if it still exists editSymbolAction->setProperty( "layerId", symbolNode->layerNode()->layerId() ); editSymbolAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() ); connect( editSymbolAction, &QAction::triggered, this, &QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol ); menu->addAction( editSymbolAction ); } } return menu; }
void QgsMapToolMoveFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e ) { QgsVectorLayer *vlayer = currentVectorLayer(); if ( !vlayer || !vlayer->isEditable() ) { delete mRubberBand; mRubberBand = nullptr; mSnapIndicator->setMatch( QgsPointLocator::Match() ); cadDockWidget()->clear(); notifyNotEditableLayer(); return; } if ( !mRubberBand ) { //find first geometry under mouse cursor and store iterator to it QgsPointXY layerCoords = toLayerCoordinates( vlayer, e->mapPoint() ); double searchRadius = QgsTolerance::vertexSearchRadius( mCanvas->currentLayer(), mCanvas->mapSettings() ); QgsRectangle selectRect( layerCoords.x() - searchRadius, layerCoords.y() - searchRadius, layerCoords.x() + searchRadius, layerCoords.y() + searchRadius ); if ( vlayer->selectedFeatureCount() == 0 ) { QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectRect ).setNoAttributes() ); //find the closest feature QgsGeometry pointGeometry = QgsGeometry::fromPointXY( layerCoords ); if ( pointGeometry.isNull() ) { cadDockWidget()->clear(); return; } double minDistance = std::numeric_limits<double>::max(); QgsFeature cf; QgsFeature f; while ( fit.nextFeature( f ) ) { if ( f.hasGeometry() ) { double currentDistance = pointGeometry.distance( f.geometry() ); if ( currentDistance < minDistance ) { minDistance = currentDistance; cf = f; } } } if ( minDistance == std::numeric_limits<double>::max() ) { cadDockWidget()->clear(); return; } mMovedFeatures.clear(); mMovedFeatures << cf.id(); //todo: take the closest feature, not the first one... mRubberBand = createRubberBand( vlayer->geometryType() ); mRubberBand->setToGeometry( cf.geometry(), vlayer ); } else { mMovedFeatures = vlayer->selectedFeatureIds(); mRubberBand = createRubberBand( vlayer->geometryType() ); QgsFeature feat; QgsFeatureIterator it = vlayer->getSelectedFeatures( QgsFeatureRequest().setNoAttributes() ); bool allFeaturesInView = true; QgsRectangle viewRect = mCanvas->mapSettings().mapToLayerCoordinates( vlayer, mCanvas->extent() ); while ( it.nextFeature( feat ) ) { mRubberBand->addGeometry( feat.geometry(), vlayer ); if ( allFeaturesInView && !viewRect.intersects( feat.geometry().boundingBox() ) ) allFeaturesInView = false; } if ( !allFeaturesInView ) { // for extra safety to make sure we are not modifying geometries by accident int res = QMessageBox::warning( mCanvas, tr( "Move features" ), tr( "Some of the selected features are outside of the current map view. Would you still like to continue?" ), QMessageBox::Yes | QMessageBox::No ); if ( res != QMessageBox::Yes ) { mMovedFeatures.clear(); delete mRubberBand; mRubberBand = nullptr; mSnapIndicator->setMatch( QgsPointLocator::Match() ); return; } } } mStartPointMapCoords = e->mapPoint(); mRubberBand->show(); } else { // copy and move mode if ( e->button() != Qt::LeftButton ) { cadDockWidget()->clear(); delete mRubberBand; mRubberBand = nullptr; mSnapIndicator->setMatch( QgsPointLocator::Match() ); return; } QgsPointXY startPointLayerCoords = toLayerCoordinates( ( QgsMapLayer * )vlayer, mStartPointMapCoords ); QgsPointXY stopPointLayerCoords = toLayerCoordinates( ( QgsMapLayer * )vlayer, e->mapPoint() ); double dx = stopPointLayerCoords.x() - startPointLayerCoords.x(); double dy = stopPointLayerCoords.y() - startPointLayerCoords.y(); vlayer->beginEditCommand( mMode == Move ? tr( "Feature moved" ) : tr( "Feature copied and moved" ) ); switch ( mMode ) { case Move: Q_FOREACH ( QgsFeatureId id, mMovedFeatures ) { vlayer->translateFeature( id, dx, dy ); } delete mRubberBand; mRubberBand = nullptr; mSnapIndicator->setMatch( QgsPointLocator::Match() ); cadDockWidget()->clear(); break; case CopyMove: QgsFeatureRequest request; request.setFilterFids( mMovedFeatures ); QString *errorMsg = new QString(); if ( !QgisApp::instance()->vectorLayerTools()->copyMoveFeatures( vlayer, request, dx, dy, errorMsg ) ) { emit messageEmitted( *errorMsg, Qgis::Critical ); delete mRubberBand; mRubberBand = nullptr; mSnapIndicator->setMatch( QgsPointLocator::Match() ); } break; } vlayer->endEditCommand(); vlayer->triggerRepaint(); }