Skip to content

Commit 551648f

Browse files
committed
Fix pinned labels highlight causing forward transformation error by using QgsRubberBand instead
1 parent 621dcce commit 551648f

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

src/app/qgsmaptoolpinlabels.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "qgsvectorlayer.h"
2626

2727
#include "qgsmaptoolselectutils.h"
28-
#include "qgshighlight.h"
2928
#include "qgsrubberband.h"
3029
#include <qgslogger.h>
3130
#include <QMouseEvent>
@@ -137,35 +136,22 @@ void QgsMapToolPinLabels::updatePinnedLabels()
137136
}
138137
}
139138

140-
void QgsMapToolPinLabels::highlightLabel( QgsVectorLayer* vlayer,
141-
const QgsLabelPosition& labelpos,
139+
void QgsMapToolPinLabels::highlightLabel( const QgsLabelPosition& labelpos,
142140
const QString& id,
143141
const QColor& color )
144142
{
145143
QgsRectangle rect = labelpos.labelRect;
146-
147-
const QgsMapSettings& ms = mCanvas->mapSettings();
148-
if ( vlayer->crs().isValid() && ms.destinationCrs().isValid() )
149-
{
150-
// if label's layer is on-fly transformed, reverse-transform label rect
151-
// QgsHighlight will convert it, yet again, to the correct map coords
152-
if ( vlayer->crs() != ms.destinationCrs() )
153-
{
154-
rect = ms.mapToLayerCoordinates( vlayer, rect );
155-
QgsDebugMsg( QString( "Reverse transform needed for highlight rectangle" ) );
156-
}
157-
}
158-
159-
QgsGeometry* highlightgeom = QgsGeometry::fromRect( rect );
160-
161-
QgsHighlight *h = new QgsHighlight( mCanvas, highlightgeom, vlayer );
162-
if ( h )
163-
{
164-
h->setWidth( 0 );
165-
h->setColor( color );
166-
h->show();
167-
mHighlights.insert( id, h );
168-
}
144+
QgsRubberBand *rb = new QgsRubberBand( mCanvas, QGis::Polygon );
145+
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
146+
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
147+
rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
148+
rb->addPoint( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
149+
rb->addPoint( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
150+
rb->setColor( color );
151+
rb->setWidth( 0 );
152+
rb->show();
153+
154+
mHighlights.insert( id, rb );
169155
}
170156

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

212-
QColor lblcolor = QColor( 54, 129, 255, 255 );
198+
QColor lblcolor = QColor( 54, 129, 255, 63 );
213199
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID );
214200
if ( !layer )
215201
{
@@ -223,10 +209,10 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
223209
}
224210
if ( vlayer->isEditable() )
225211
{
226-
lblcolor = QColor( 54, 129, 0, 255 );
212+
lblcolor = QColor( 54, 129, 0, 63 );
227213
}
228214

229-
highlightLabel( vlayer, ( *it ), labelStringID, lblcolor );
215+
highlightLabel(( *it ), labelStringID, lblcolor );
230216
}
231217
}
232218
QApplication::restoreOverrideCursor();
@@ -235,9 +221,9 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
235221
void QgsMapToolPinLabels::removePinnedHighlights()
236222
{
237223
QApplication::setOverrideCursor( Qt::BusyCursor );
238-
foreach ( QgsHighlight *h, mHighlights )
224+
foreach ( QgsRubberBand *rb, mHighlights )
239225
{
240-
delete h;
226+
delete rb;
241227
}
242228
mHighlights.clear();
243229
QApplication::restoreOverrideCursor();

src/app/qgsmaptoolpinlabels.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "qgslegend.h"
2424
#include "qgscoordinatetransform.h"
2525

26-
class QgsHighlight;
26+
class QgsRubberBand;
2727
class QgsLabelPosition;
2828

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

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

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

8282
//! Highlights a given label relative to whether its pinned and editable
83-
void highlightLabel( QgsVectorLayer* vlayer,
84-
const QgsLabelPosition& labelpos,
83+
void highlightLabel( const QgsLabelPosition& labelpos,
8584
const QString& id,
8685
const QColor& color );
8786

0 commit comments

Comments
 (0)