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 "qgsvectorlayer.h"


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


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

QgsRubberBand *rb = new QgsRubberBand( mCanvas, QGis::Polygon );
const QgsMapSettings& ms = mCanvas->mapSettings(); rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
if ( vlayer->crs().isValid() && ms.destinationCrs().isValid() ) rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
{ rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
// if label's layer is on-fly transformed, reverse-transform label rect rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
// QgsHighlight will convert it, yet again, to the correct map coords rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
if ( vlayer->crs() != ms.destinationCrs() ) rb->setColor( color );
{ rb->setWidth( 0 );
rect = ms.mapToLayerCoordinates( vlayer, rect ); rb->show();
QgsDebugMsg( QString( "Reverse transform needed for highlight rectangle" ) );
} mHighlights.insert( id, rb );
}

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 );
}
} }


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


QColor lblcolor = QColor( 54, 129, 255, 255 ); QColor lblcolor = QColor( 54, 129, 255, 63 );
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ); QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
if ( !layer ) if ( !layer )
{ {
Expand All @@ -223,10 +209,10 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
} }
if ( vlayer->isEditable() ) 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(); QApplication::restoreOverrideCursor();
Expand All @@ -235,9 +221,9 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
void QgsMapToolPinLabels::removePinnedHighlights() void QgsMapToolPinLabels::removePinnedHighlights()
{ {
QApplication::setOverrideCursor( Qt::BusyCursor ); QApplication::setOverrideCursor( Qt::BusyCursor );
foreach ( QgsHighlight *h, mHighlights ) foreach ( QgsRubberBand *rb, mHighlights )
{ {
delete h; delete rb;
} }
mHighlights.clear(); mHighlights.clear();
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
Expand Down
7 changes: 3 additions & 4 deletions src/app/qgsmaptoolpinlabels.h
Expand Up @@ -23,7 +23,7 @@
#include "qgslegend.h" #include "qgslegend.h"
#include "qgscoordinatetransform.h" #include "qgscoordinatetransform.h"


class QgsHighlight; class QgsRubberBand;
class QgsLabelPosition; class QgsLabelPosition;


/**A map tool for pinning (writing to attribute table) and unpinning labelpositions and rotation*/ /**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: protected:


//! Mapping of feature ids of layers that have been highlighted //! 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 //! Flag to indicate a map canvas drag operation is taking place
bool mDragging; bool mDragging;
Expand All @@ -80,8 +80,7 @@ class APP_EXPORT QgsMapToolPinLabels: public QgsMapToolLabel
private: private:


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


Expand Down

0 comments on commit 551648f

Please sign in to comment.