void QgsMapToolSelectUtils::selectSingleFeature( QgsMapCanvas *canvas, const QgsGeometry &selectGeometry, Qt::KeyboardModifiers modifiers ) { QgsVectorLayer *vlayer = QgsMapToolSelectUtils::getCurrentVectorLayer( canvas ); if ( !vlayer ) return; QApplication::setOverrideCursor( Qt::WaitCursor ); QgsFeatureIds selectedFeatures = getMatchingFeatures( canvas, selectGeometry, false, true ); if ( selectedFeatures.isEmpty() ) { if ( !( modifiers & Qt::ShiftModifier || modifiers & Qt::ControlModifier ) ) { // if no modifiers then clicking outside features clears the selection // but if there's a shift or ctrl modifier, then it's likely the user was trying // to modify an existing selection by adding or subtracting features and just // missed the feature vlayer->removeSelection(); } QApplication::restoreOverrideCursor(); return; } QgsVectorLayer::SelectBehavior behavior = QgsVectorLayer::SetSelection; //either shift or control modifier switches to "toggle" selection mode if ( modifiers & Qt::ShiftModifier || modifiers & Qt::ControlModifier ) { QgsFeatureId selectId = *selectedFeatures.constBegin(); QgsFeatureIds layerSelectedFeatures = vlayer->selectedFeatureIds(); if ( layerSelectedFeatures.contains( selectId ) ) behavior = QgsVectorLayer::RemoveFromSelection; else behavior = QgsVectorLayer::AddToSelection; } vlayer->selectByIds( selectedFeatures, behavior ); QApplication::restoreOverrideCursor(); }
/** * Selection routine called by the mouse release event * @param thePoint = QgsPoint representing the x, y coordinates of the mouse release event */ void eVisEventIdTool::select( QgsPoint thePoint ) { if ( 0 == mCanvas ) return; QgsVectorLayer* myLayer = ( QgsVectorLayer* )mCanvas->currentLayer( ); // create the search rectangle. this was modeled after the QgsMapIdentifyTool in core QGIS application double searchWidth = mCanvas->extent( ).width( ) * (( double )QGis::DEFAULT_IDENTIFY_RADIUS / 100.0 ); QgsRectangle myRectangle; myRectangle.setXMinimum( thePoint.x( ) - searchWidth ); myRectangle.setXMaximum( thePoint.x( ) + searchWidth ); myRectangle.setYMinimum( thePoint.y( ) - searchWidth ); myRectangle.setYMaximum( thePoint.y( ) + searchWidth ); //Transform rectange to map coordinates myRectangle = toLayerCoordinates( myLayer, myRectangle ); //Rather than add to the current selection, clear all selected features myLayer->removeSelection( false ); //select features QgsFeatureIterator fit = myLayer->getFeatures( QgsFeatureRequest().setFilterRect( myRectangle ).setFlags( QgsFeatureRequest::ExactIntersect ).setSubsetOfAttributes( QgsAttributeList() ) ); QgsFeature f; QgsFeatureIds newSelectedFeatures; while ( fit.nextFeature( f ) ) { newSelectedFeatures.insert( f.id() ); } myLayer->setSelectedFeatures( newSelectedFeatures ); //Launch a new event browser to view selected features mBrowser = new eVisGenericEventBrowserGui( mCanvas, mCanvas, NULL ); mBrowser->setAttribute( Qt::WA_DeleteOnClose ); }