Showing with 44 additions and 62 deletions.
  1. +19 −24 src/app/qgsmaptoollabel.cpp
  2. +17 −31 src/app/qgsmaptoolpinlabels.cpp
  3. +3 −4 src/app/qgsmaptoolpinlabels.h
  4. +5 −3 src/gui/qgsmapcanvas.cpp
43 changes: 19 additions & 24 deletions src/app/qgsmaptoollabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ QgsPalLayerSettings& QgsMapToolLabel::currentLabelSettings( bool* ok )
QgsVectorLayer* vlayer = currentLayer();
if ( vlayer )
{
if ( vlayer == mCurrentLayer )
return mCurrentSettings;

mCurrentLayer = vlayer;
mCurrentSettings = QgsPalLayerSettings::fromLayer( vlayer );
if ( vlayer != mCurrentLayer )
{
mCurrentLayer = vlayer;
mCurrentSettings = QgsPalLayerSettings::fromLayer( vlayer );
}

if ( ok )
{
*ok = true;
}

return mCurrentSettings;
}
Expand Down Expand Up @@ -337,11 +339,11 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool

if ( mCurrentLabelPos.upsideDown && !ignoreUpsideDown )
{
pos = mCurrentLabelPos.cornerPoints.at( 2 );
pos = cornerPoints.at( 2 );
}
else
{
pos = mCurrentLabelPos.cornerPoints.at( 0 );
pos = cornerPoints.at( 0 );
}

//alignment always center/center and rotation 0 for diagrams
Expand All @@ -366,23 +368,16 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool
// QFont labelFont = labelFontCurrentFeature();
QFontMetricsF labelFontMetrics( mCurrentLabelPos.labelFont );

//label text?
QString labelText = currentLabelText();

bool labelSettingsOk;
QgsPalLayerSettings& labelSettings = currentLabelSettings( &labelSettingsOk );
if ( !labelSettingsOk )
{
return false;
}

double labelSizeX, labelSizeY;
QgsFeature f;
if ( !currentFeature( f ) )
{
return false;
}
labelSettings.calculateLabelSize( &labelFontMetrics, labelText, labelSizeX, labelSizeY, &f );
// NOTE: this assumes the label corner points comprise a rectangle and that the
// CRS supports equidistant measurements to accurately determine hypotenuse
QgsPoint cp_0 = cornerPoints.at( 0 );
QgsPoint cp_1 = cornerPoints.at( 1 );
QgsPoint cp_3 = cornerPoints.at( 3 );
// QgsDebugMsg( QString( "cp_0: x=%1, y=%2" ).arg( cp_0.x() ).arg( cp_0.y() ) );
// QgsDebugMsg( QString( "cp_1: x=%1, y=%2" ).arg( cp_1.x() ).arg( cp_1.y() ) );
// QgsDebugMsg( QString( "cp_3: x=%1, y=%2" ).arg( cp_3.x() ).arg( cp_3.y() ) );
double labelSizeX = qSqrt( cp_0.sqrDist( cp_1 ) );
double labelSizeY = qSqrt( cp_0.sqrDist( cp_3 ) );

double xdiff = 0;
double ydiff = 0;
Expand Down
48 changes: 17 additions & 31 deletions src/app/qgsmaptoolpinlabels.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
8 changes: 5 additions & 3 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,11 @@ void QgsMapCanvas::rendererJobFinished()

if ( !mJobCancelled )
{
// take labeling results before emitting renderComplete, so labeling map tools
// connected to signal work with correct results
delete mLabelingResults;
mLabelingResults = mJob->takeLabelingResults();

QImage img = mJob->renderedImage();

// emit renderComplete to get our decorations drawn
Expand All @@ -733,9 +738,6 @@ void QgsMapCanvas::rendererJobFinished()
p.end();

mMap->setContent( img, mSettings.visibleExtent() );

delete mLabelingResults;
mLabelingResults = mJob->takeLabelingResults();
}

// now we are in a slot called from mJob - do not delete it immediately
Expand Down