Skip to content
Permalink
Browse files

Fix pinned labels highlight causing forward transformation error by u…

…sing QgsRubberBand instead
  • Loading branch information
dakcarto committed Mar 12, 2014
1 parent 621dcce commit 551648f3a621fe2a208989f7f4bc34b6c91543c8
Showing with 20 additions and 35 deletions.
  1. +17 −31 src/app/qgsmaptoolpinlabels.cpp
  2. +3 −4 src/app/qgsmaptoolpinlabels.h
@@ -25,7 +25,6 @@
#include "qgsvectorlayer.h"

#include "qgsmaptoolselectutils.h"
#include "qgshighlight.h"
#include "qgsrubberband.h"
#include <qgslogger.h>
#include <QMouseEvent>
@@ -137,35 +136,22 @@ void QgsMapToolPinLabels::updatePinnedLabels()
}
}

void QgsMapToolPinLabels::highlightLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
void QgsMapToolPinLabels::highlightLabel( const QgsLabelPosition& labelpos,
const QString& id,
const QColor& color )
{
QgsRectangle rect = labelpos.labelRect;

const QgsMapSettings& ms = mCanvas->mapSettings();
if ( vlayer->crs().isValid() && ms.destinationCrs().isValid() )
{
// if label's layer is on-fly transformed, reverse-transform label rect
// QgsHighlight will convert it, yet again, to the correct map coords
if ( vlayer->crs() != ms.destinationCrs() )
{
rect = ms.mapToLayerCoordinates( vlayer, rect );
QgsDebugMsg( QString( "Reverse transform needed for highlight rectangle" ) );
}
}

QgsGeometry* highlightgeom = QgsGeometry::fromRect( rect );

QgsHighlight *h = new QgsHighlight( mCanvas, highlightgeom, vlayer );
if ( h )
{
h->setWidth( 0 );
h->setColor( color );
h->show();
mHighlights.insert( id, h );
}
QgsRubberBand *rb = new QgsRubberBand( mCanvas, QGis::Polygon );
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
rb->setColor( color );
rb->setWidth( 0 );
rb->show();

mHighlights.insert( id, rb );
}

// public slot to render highlight rectangles around pinned labels
@@ -209,7 +195,7 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
continue;
}

QColor lblcolor = QColor( 54, 129, 255, 255 );
QColor lblcolor = QColor( 54, 129, 255, 63 );
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
if ( !layer )
{
@@ -223,10 +209,10 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
}
if ( vlayer->isEditable() )
{
lblcolor = QColor( 54, 129, 0, 255 );
lblcolor = QColor( 54, 129, 0, 63 );
}

highlightLabel( vlayer, ( *it ), labelStringID, lblcolor );
highlightLabel(( *it ), labelStringID, lblcolor );
}
}
QApplication::restoreOverrideCursor();
@@ -235,9 +221,9 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
void QgsMapToolPinLabels::removePinnedHighlights()
{
QApplication::setOverrideCursor( Qt::BusyCursor );
foreach ( QgsHighlight *h, mHighlights )
foreach ( QgsRubberBand *rb, mHighlights )
{
delete h;
delete rb;
}
mHighlights.clear();
QApplication::restoreOverrideCursor();
@@ -23,7 +23,7 @@
#include "qgslegend.h"
#include "qgscoordinatetransform.h"

class QgsHighlight;
class QgsRubberBand;
class QgsLabelPosition;

/**A map tool for pinning (writing to attribute table) and unpinning labelpositions and rotation*/
@@ -64,7 +64,7 @@ class APP_EXPORT QgsMapToolPinLabels: public QgsMapToolLabel
protected:

//! Mapping of feature ids of layers that have been highlighted
QMap<QString, QgsHighlight*> mHighlights;
QMap<QString, QgsRubberBand*> mHighlights;

//! Flag to indicate a map canvas drag operation is taking place
bool mDragging;
@@ -80,8 +80,7 @@ class APP_EXPORT QgsMapToolPinLabels: public QgsMapToolLabel
private:

//! Highlights a given label relative to whether its pinned and editable
void highlightLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
void highlightLabel( const QgsLabelPosition& labelpos,
const QString& id,
const QColor& color );

0 comments on commit 551648f

Please sign in to comment.
You can’t perform that action at this time.