void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent* e ) { deleteRubberBands(); QgsLabelPosition labelPos; if ( !labelAtPosition( e, labelPos ) ) { mCurrentLabel = LabelDetails(); return; } mCurrentLabel = LabelDetails( labelPos ); QgsVectorLayer* vlayer = mCurrentLabel.layer; if ( !vlayer || !vlayer->isEditable() ) { return; } int xCol, yCol; if ( labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) || diagramMoveable( vlayer, xCol, yCol ) ) { mStartPointMapCoords = toMapCoordinates( e->pos() ); QgsPoint referencePoint; if ( !currentLabelRotationPoint( referencePoint, !currentLabelPreserveRotation(), false ) ) { referencePoint.setX( mCurrentLabel.pos.labelRect.xMinimum() ); referencePoint.setY( mCurrentLabel.pos.labelRect.yMinimum() ); } mClickOffsetX = mStartPointMapCoords.x() - referencePoint.x(); mClickOffsetY = mStartPointMapCoords.y() - referencePoint.y(); createRubberBands(); } }
void QgsMapToolLabel::createRubberBands() { delete mLabelRubberBand; delete mFeatureRubberBand; //label rubber band QgsRectangle rect = mCurrentLabel.pos.labelRect; mLabelRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::LineGeometry ); mLabelRubberBand->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) ); mLabelRubberBand->addPoint( QgsPoint( rect.xMinimum(), rect.yMaximum() ) ); mLabelRubberBand->addPoint( QgsPoint( rect.xMaximum(), rect.yMaximum() ) ); mLabelRubberBand->addPoint( QgsPoint( rect.xMaximum(), rect.yMinimum() ) ); mLabelRubberBand->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) ); mLabelRubberBand->setColor( QColor( 0, 255, 0, 65 ) ); mLabelRubberBand->setWidth( 3 ); mLabelRubberBand->show(); //feature rubber band QgsVectorLayer* vlayer = mCurrentLabel.layer; if ( vlayer ) { QgsFeature f; if ( currentFeature( f, true ) ) { QgsGeometry geom = f.geometry(); if ( !geom.isEmpty() ) { QSettings settings; int r = settings.value( QStringLiteral( "/qgis/digitizing/line_color_red" ), 255 ).toInt(); int g = settings.value( QStringLiteral( "/qgis/digitizing/line_color_green" ), 0 ).toInt(); int b = settings.value( QStringLiteral( "/qgis/digitizing/line_color_blue" ), 0 ).toInt(); int a = settings.value( QStringLiteral( "/qgis/digitizing/line_color_alpha" ), 200 ).toInt(); mFeatureRubberBand = new QgsRubberBand( mCanvas, geom.type() ); mFeatureRubberBand->setColor( QColor( r, g, b, a ) ); mFeatureRubberBand->setToGeometry( geom, vlayer ); mFeatureRubberBand->show(); } } //fixpoint rubber band QgsPoint fixPoint; if ( currentLabelRotationPoint( fixPoint, false, false ) ) { if ( mCanvas ) { const QgsMapSettings& s = mCanvas->mapSettings(); if ( s.hasCrsTransformEnabled() ) { fixPoint = s.mapToLayerCoordinates( vlayer, fixPoint ); } } QgsGeometry pointGeom = QgsGeometry::fromPoint( fixPoint ); mFixPointRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::LineGeometry ); mFixPointRubberBand->setColor( QColor( 0, 0, 255, 65 ) ); mFixPointRubberBand->setToGeometry( pointGeom, vlayer ); mFixPointRubberBand->show(); } } }
void QgsMapToolRotateLabel::canvasPressEvent( QgsMapMouseEvent* e ) { deleteRubberBands(); QgsLabelPosition labelPos; if ( !labelAtPosition( e, labelPos ) ) { mCurrentLabel = LabelDetails(); return; } mCurrentLabel = LabelDetails( labelPos ); if ( !mCurrentLabel.valid ) return; // only rotate non-pinned OverPoint placements until other placements are supported in pal::Feature if ( !mCurrentLabel.pos.isPinned && mCurrentLabel.settings.placement != QgsPalLayerSettings::OverPoint ) { return; } // rotate unpinned labels (i.e. no hali/vali settings) as if hali/vali was Center/Half if ( !currentLabelRotationPoint( mRotationPoint, false, !mCurrentLabel.pos.isPinned ) ) { return; } if ( true ) { mCurrentMouseAzimuth = azimuthToCCW( mRotationPoint.azimuth( toMapCoordinates( e->pos() ) ) ); bool hasRotationValue; int rotationCol; if ( currentLabelDataDefinedRotation( mCurrentRotation, hasRotationValue, rotationCol, true ) ) { if ( !hasRotationValue ) { mCurrentRotation = 0; } mStartRotation = mCurrentRotation; createRubberBands(); mRotationPreviewBox = createRotationPreviewBox(); mRotationItem = new QgsPointRotationItem( mCanvas ); mRotationItem->setOrientation( QgsPointRotationItem::Counterclockwise ); mRotationItem->setSymbol( QgsApplication::getThemePixmap( "mActionRotatePointSymbols.svg" ).toImage() ); mRotationItem->setPointLocation( mRotationPoint ); mRotationItem->setSymbolRotation( mCurrentRotation ); } } }
bool QgsMapToolPinLabels::pinUnpinCurrentLabel( bool pin ) { QgsVectorLayer *vlayer = mCurrentLabel.layer; const QgsLabelPosition &labelpos = mCurrentLabel.pos; // skip diagrams if ( labelpos.isDiagram ) { QgsDebugMsg( QStringLiteral( "Label is diagram, skipping" ) ); return false; } // verify attribute table has x, y fields mapped int xCol, yCol; double xPosOrig, yPosOrig; bool xSuccess, ySuccess; if ( !currentLabelDataDefinedPosition( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) ) { QgsDebugMsg( QStringLiteral( "Label X or Y column not mapped, skipping" ) ); return false; } // rotation field is optional, but will be used if available, unless data exists int rCol; bool rSuccess = false; double defRot; bool hasRCol = currentLabelDataDefinedRotation( defRot, rSuccess, rCol, true ); // get whether to preserve predefined rotation data during label pin/unpin operations bool preserveRot = currentLabelPreserveRotation(); // edit attribute table int fid = labelpos.featureId; bool writeFailed = false; QString labelText = currentLabelText( 24 ); if ( pin ) { // QgsPointXY labelpoint = labelpos.cornerPoints.at( 0 ); QgsPointXY referencePoint; if ( !currentLabelRotationPoint( referencePoint, !preserveRot, false ) ) { referencePoint.setX( labelpos.labelRect.xMinimum() ); referencePoint.setY( labelpos.labelRect.yMinimum() ); } double labelX = referencePoint.x(); double labelY = referencePoint.y(); double labelR = labelpos.rotation * 180 / M_PI; // transform back to layer crs QgsPointXY transformedPoint = mCanvas->mapSettings().mapToLayerCoordinates( vlayer, referencePoint ); labelX = transformedPoint.x(); labelY = transformedPoint.y(); vlayer->beginEditCommand( tr( "Pinned label" ) + QStringLiteral( " '%1'" ).arg( labelText ) ); writeFailed = !vlayer->changeAttributeValue( fid, xCol, labelX ); if ( !vlayer->changeAttributeValue( fid, yCol, labelY ) ) writeFailed = true; if ( hasRCol && !preserveRot ) { if ( !vlayer->changeAttributeValue( fid, rCol, labelR ) ) writeFailed = true; } vlayer->endEditCommand(); } else { vlayer->beginEditCommand( tr( "Unpinned label" ) + QStringLiteral( " '%1'" ).arg( labelText ) ); writeFailed = !vlayer->changeAttributeValue( fid, xCol, QVariant( QString() ) ); if ( !vlayer->changeAttributeValue( fid, yCol, QVariant( QString() ) ) ) writeFailed = true; if ( hasRCol && !preserveRot ) { if ( !vlayer->changeAttributeValue( fid, rCol, QVariant( QString() ) ) ) writeFailed = true; } vlayer->endEditCommand(); } if ( writeFailed ) { QgsDebugMsg( QStringLiteral( "Write to attribute table failed" ) ); #if 0 QgsDebugMsg( QStringLiteral( "Undoing and removing failed command from layer's undo stack" ) ); int lastCmdIndx = vlayer->undoStack()->count(); const QgsUndoCommand *lastCmd = qobject_cast<const QgsUndoCommand *>( vlayer->undoStack()->command( lastCmdIndx ) ); if ( lastCmd ) { vlayer->undoEditCommand( lastCmd ); delete vlayer->undoStack()->command( lastCmdIndx ); } #endif return false; } return true; }
void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent *e ) { deleteRubberBands(); QgsLabelPosition labelPos; if ( !labelAtPosition( e, labelPos ) ) { mCurrentLabel = LabelDetails(); return; } mCurrentLabel = LabelDetails( labelPos ); QgsVectorLayer *vlayer = mCurrentLabel.layer; if ( !vlayer ) { return; } int xCol = -1, yCol = -1; if ( !mCurrentLabel.pos.isDiagram && !labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) ) { QgsPalIndexes indexes; if ( createAuxiliaryFields( indexes ) ) return; if ( !labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) ) return; xCol = indexes[ QgsPalLayerSettings::PositionX ]; yCol = indexes[ QgsPalLayerSettings::PositionY ]; } else if ( mCurrentLabel.pos.isDiagram && !diagramMoveable( vlayer, xCol, yCol ) ) { QgsDiagramIndexes indexes; if ( createAuxiliaryFields( indexes ) ) return; if ( !diagramMoveable( vlayer, xCol, yCol ) ) return; xCol = indexes[ QgsDiagramLayerSettings::PositionX ]; yCol = indexes[ QgsDiagramLayerSettings::PositionY ]; } if ( xCol >= 0 && yCol >= 0 ) { mStartPointMapCoords = toMapCoordinates( e->pos() ); QgsPointXY referencePoint; if ( !currentLabelRotationPoint( referencePoint, !currentLabelPreserveRotation(), false ) ) { referencePoint.setX( mCurrentLabel.pos.labelRect.xMinimum() ); referencePoint.setY( mCurrentLabel.pos.labelRect.yMinimum() ); } mClickOffsetX = mStartPointMapCoords.x() - referencePoint.x(); mClickOffsetY = mStartPointMapCoords.y() - referencePoint.y(); createRubberBands(); } }