Skip to content

Commit

Permalink
Fix pinned labels highlight causing forward transformation error by u…
Browse files Browse the repository at this point in the history
…sing QgsRubberBand instead
  • Loading branch information
dakcarto committed Mar 12, 2014
1 parent 621dcce commit 551648f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
48 changes: 17 additions & 31 deletions src/app/qgsmaptoolpinlabels.cpp
Expand Up @@ -25,7 +25,6 @@
#include "qgsvectorlayer.h"

#include "qgsmaptoolselectutils.h"
#include "qgshighlight.h"
#include "qgsrubberband.h"
#include <qgslogger.h>
#include <QMouseEvent>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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();
Expand All @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions src/app/qgsmaptoolpinlabels.h
Expand Up @@ -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*/
Expand Down Expand Up @@ -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;
Expand All @@ -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 );

Expand Down

0 comments on commit 551648f

Please sign in to comment.