Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix label and diagram editing if otf reprojection is enabled (ticket #…
…3685)

git-svn-id: http://svn.osgeo.org/qgis/trunk@15692 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 11, 2011
1 parent afa4214 commit 54f3321
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -90,6 +90,15 @@ void QgsMapToolLabel::createRubberBands( )
QgsPoint fixPoint;
if ( rotationPoint( fixPoint ) )
{
if ( mCanvas )
{
QgsMapRenderer* r = mCanvas->mapRenderer();
if ( r && r->hasCrsTransformEnabled() )
{
fixPoint = r->mapToLayerCoordinates( vlayer, fixPoint );
}
}

QgsGeometry* pointGeom = QgsGeometry::fromPoint( fixPoint );
mFixPointRubberBand = new QgsRubberBand( mCanvas, false );
mFixPointRubberBand->setColor( Qt::blue );
Expand Down
20 changes: 20 additions & 0 deletions src/app/qgsmaptoolmovelabel.cpp
Expand Up @@ -125,10 +125,30 @@ void QgsMapToolMoveLabel::canvasReleaseEvent( QMouseEvent * e )
}
else
{
//transform to map crs first, because xdiff,ydiff are in map coordinates
QgsMapRenderer* r = mCanvas->mapRenderer();
if ( r && r->hasCrsTransformEnabled() )
{
QgsPoint transformedPoint = r->layerToMapCoordinates( vlayer, QgsPoint( xPosOrig, yPosOrig ) );
xPosOrig = transformedPoint.x();
yPosOrig = transformedPoint.y();
}
xPosNew = xPosOrig + xdiff;
yPosNew = yPosOrig + ydiff;
}

//transform back to layer crs
if ( mCanvas )
{
QgsMapRenderer* r = mCanvas->mapRenderer();
if ( r && r->hasCrsTransformEnabled() )
{
QgsPoint transformedPoint = r->mapToLayerCoordinates( vlayer, QgsPoint( xPosNew, yPosNew ) );
xPosNew = transformedPoint.x();
yPosNew = transformedPoint.y();
}
}

vlayer->beginEditCommand( tr( "Label moved" ) );
vlayer->changeAttributeValue( mCurrentLabelPos.featureId, xCol, xPosNew, false );
vlayer->changeAttributeValue( mCurrentLabelPos.featureId, yCol, yPosNew, false );
Expand Down
20 changes: 15 additions & 5 deletions src/core/qgslabelsearchtree.cpp
Expand Up @@ -19,12 +19,22 @@ QgsLabelSearchTree::~QgsLabelSearchTree()

void QgsLabelSearchTree::label( const QgsPoint& p, QList<QgsLabelPosition*>& posList )
{
double c_min[2]; c_min[0] = p.x() - 1; c_min[1] = p.y() - 1;
double c_max[2]; c_max[0] = p.x() + 1; c_max[1] = p.y() + 1;
double c_min[2]; c_min[0] = p.x() - 0.1; c_min[1] = p.y() - 0.1;
double c_max[2]; c_max[0] = p.x() + 0.1; c_max[1] = p.y() + 0.1;

mSearchResults.clear();
mSpatialIndex.Search( c_min, c_max, searchCallback, &mSearchResults );
posList = mSearchResults;
QList<QgsLabelPosition*> searchResults;
mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );

//tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
posList.clear();
QList<QgsLabelPosition*>::const_iterator resultIt = searchResults.constBegin();
for ( ; resultIt != searchResults.constEnd(); ++resultIt )
{
if (( *resultIt )->labelRect.contains( p ) )
{
posList.push_back( *resultIt );
}
}
}

bool QgsLabelSearchTree::insertLabel( LabelPosition* labelPos, int featureId, const QString& layerName, bool diagram )
Expand Down
1 change: 0 additions & 1 deletion src/core/qgslabelsearchtree.h
Expand Up @@ -48,7 +48,6 @@ class CORE_EXPORT QgsLabelSearchTree

private:
RTree<QgsLabelPosition*, double, 2, double> mSpatialIndex;
QList<QgsLabelPosition*> mSearchResults;
};

#endif // QGSLABELTREE_H
19 changes: 18 additions & 1 deletion src/core/qgspallabeling.cpp
Expand Up @@ -548,8 +548,16 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
ydiff = yd;
}

//project xPos and yPos from layer to map CRS
double z = 0;
if ( ct )
{
ct->transformInPlace( xPos, yPos, z );
}

yPos += ydiff;
xPos += xdiff;

}
}
}
Expand Down Expand Up @@ -786,7 +794,7 @@ void QgsPalLabeling::registerDiagramFeature( QgsVectorLayer* layer, QgsFeature&
//convert geom to geos
QgsGeometry* geom = feat.geometry();

if ( layerIt.value().ct ) // reproject the geometry if necessary
if ( layerIt.value().ct && !willUseLayer( layer ) ) // reproject the geometry if feature not already transformed for labeling
{
geom->transform( *( layerIt.value().ct ) );
}
Expand Down Expand Up @@ -841,6 +849,15 @@ void QgsPalLabeling::registerDiagramFeature( QgsVectorLayer* layer, QgsFeature&
{
ddPos = false;
}
else
{
const QgsCoordinateTransform* ct = layerIt.value().ct;
if ( ct )
{
double z = 0;
ct->transformInPlace( ddPosX, ddPosY, z );
}
}
}

try
Expand Down

0 comments on commit 54f3321

Please sign in to comment.