Skip to content
Permalink
Browse files

Cleanups to emphasize that methods work with current label

  • Loading branch information
wonder-sk committed Apr 27, 2016
1 parent 42991ec commit ad82ac08d8f52bdc250ecfa7bae35e9c475fba8e
@@ -100,7 +100,7 @@ void QgsMapToolLabel::createRubberBands()

//fixpoint rubber band
QgsPoint fixPoint;
if ( rotationPoint( fixPoint, false, false ) )
if ( currentLabelRotationPoint( fixPoint, false, false ) )
{
if ( mCanvas )
{
@@ -222,7 +222,7 @@ bool QgsMapToolLabel::currentFeature( QgsFeature& f, bool fetchGeom )
).nextFeature( f );
}

QFont QgsMapToolLabel::labelFontCurrentFeature()
QFont QgsMapToolLabel::currentLabelFont()
{
QFont font;

@@ -293,7 +293,7 @@ QFont QgsMapToolLabel::labelFontCurrentFeature()
return font;
}

bool QgsMapToolLabel::preserveRotation()
bool QgsMapToolLabel::currentLabelPreserveRotation()
{
if ( mCurrentLabel.valid )
{
@@ -303,7 +303,7 @@ bool QgsMapToolLabel::preserveRotation()
return true; // default, so there is no accidental data loss
}

bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool rotatingUnpinned )
bool QgsMapToolLabel::currentLabelRotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool rotatingUnpinned )
{
QVector<QgsPoint> cornerPoints = mCurrentLabel.pos.cornerPoints;
if ( cornerPoints.size() < 4 )
@@ -438,8 +438,11 @@ int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedPro
return -1;
}

bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, QgsFeatureId featureId, double& x, bool& xSuccess, double& y, bool& ySuccess, int& xCol, int& yCol ) const
bool QgsMapToolLabel::currentLabelDataDefinedPosition( double& x, bool& xSuccess, double& y, bool& ySuccess, int& xCol, int& yCol ) const
{
QgsVectorLayer* vlayer = mCurrentLabel.layer;
QgsFeatureId featureId = mCurrentLabel.pos.featureId;

xSuccess = false;
ySuccess = false;

@@ -484,27 +487,33 @@ bool QgsMapToolLabel::layerIsRotatable( QgsVectorLayer* vlayer, int& rotationCol

Q_FOREACH ( const QString& providerId, vlayer->labeling()->subProviders() )
{
QString fieldname = dataDefinedColumnName( QgsPalLayerSettings::Rotation,
vlayer->labeling()->settings( vlayer, providerId ) );
rotationCol = vlayer->fieldNameIndex( fieldname );
if ( rotationCol != -1 )
if ( labelIsRotatable( vlayer, vlayer->labeling()->settings( vlayer, providerId ), rotationCol ) )
return true;
}

return false;
}

bool QgsMapToolLabel::labelIsRotatable( QgsVectorLayer* layer, const QgsPalLayerSettings& settings, int& rotationCol ) const
{
QString rColName = dataDefinedColumnName( QgsPalLayerSettings::Rotation, settings );
rotationCol = layer->fieldNameIndex( rColName );
return rotationCol != -1;
}


bool QgsMapToolLabel::dataDefinedRotation( QgsVectorLayer* vlayer, QgsFeatureId featureId, double& rotation, bool& rotationSuccess, bool ignoreXY ) const
bool QgsMapToolLabel::currentLabelDataDefinedRotation( double& rotation, bool& rotationSuccess, int& rCol, bool ignoreXY ) const
{
QgsVectorLayer* vlayer = mCurrentLabel.layer;
QgsFeatureId featureId = mCurrentLabel.pos.featureId;

rotationSuccess = false;
if ( !vlayer )
{
return false;
}

int rotationCol;
if ( !layerIsRotatable( vlayer, rotationCol ) )
if ( !labelIsRotatable( vlayer, mCurrentLabel.settings, rCol ) )
{
return false;
}
@@ -521,13 +530,13 @@ bool QgsMapToolLabel::dataDefinedRotation( QgsVectorLayer* vlayer, QgsFeatureId
int xCol, yCol;
double x, y;
bool xSuccess, ySuccess;
if ( !dataDefinedPosition( vlayer, featureId, x, xSuccess, y, ySuccess, xCol, yCol ) || !xSuccess || !ySuccess )
if ( !currentLabelDataDefinedPosition( x, xSuccess, y, ySuccess, xCol, yCol ) || !xSuccess || !ySuccess )
{
return false;
}
}

rotation = f.attribute( rotationCol ).toDouble( &rotationSuccess );
rotation = f.attribute( rCol ).toDouble( &rotationSuccess );
return true;
}

@@ -59,6 +59,7 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
/** Checks if labels in a layer can be rotated
@param rotationCol out: attribute column for data defined label rotation*/
bool layerIsRotatable( QgsVectorLayer *layer, int& rotationCol ) const;
bool labelIsRotatable( QgsVectorLayer *layer, const QgsPalLayerSettings& settings, int& rotationCol ) const;

protected:
QgsRubberBand* mLabelRubberBand;
@@ -89,7 +90,7 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
/** Finds out rotation point of current label position
@param ignoreUpsideDown treat label as right-side-up
@return true in case of success*/
bool rotationPoint( QgsPoint& pos, bool ignoreUpsideDown = false, bool rotatingUnpinned = false );
bool currentLabelRotationPoint( QgsPoint& pos, bool ignoreUpsideDown = false, bool rotatingUnpinned = false );

/** Creates label / feature / fixpoint rubber bands for the current label position*/
void createRubberBands();
@@ -108,48 +109,36 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
bool currentFeature( QgsFeature& f, bool fetchGeom = false );

/** Returns the font for the current feature (considering default font and data defined properties)*/
QFont labelFontCurrentFeature();
QFont currentLabelFont();

/** Returns a data defined attribute column name for particular property or empty string if not defined */
QString dataDefinedColumnName( QgsPalLayerSettings::DataDefinedProperties p, const QgsPalLayerSettings& labelSettings ) const;

// /**Returns a data defined attribute column
// @return invalid QVariant if one does not exist or an expression is used instead */
// QVariant dataDefinedColumn( QgsPalLayerSettings::DataDefinedProperties p );

// /**Returns a data defined attribute column - overloaded variation
// @return invalid QVariant if one does not exist or an expression is used instead */
// QVariant dataDefinedColumn( QgsPalLayerSettings::DataDefinedProperties p, QgsPalLayerSettings& labelSettings );

/** Returns a data defined attribute column index
@return -1 if column does not exist or an expression is used instead */
int dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsPalLayerSettings& labelSettings, const QgsVectorLayer* vlayer ) const;

//int dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, QgsVectorLayer* vlayer ) const;

/** Returns whether to preserve predefined rotation data during label pin/unpin operations*/
bool preserveRotation();
bool currentLabelPreserveRotation();

/** Get data defined position of a feature
@param vlayer vector layer
@param featureId feature identification integer
/** Get data defined position of current label
@param x out: data defined x-coordinate
@param xSuccess out: false if attribute value is NULL
@param y out: data defined y-coordinate
@param ySuccess out: false if attribute value is NULL
@param xCol out: index of the x position column
@param yCol out: index of the y position column
@return false if layer does not have data defined label position enabled*/
bool dataDefinedPosition( QgsVectorLayer* vlayer, QgsFeatureId featureId, double& x, bool& xSuccess, double& y, bool& ySuccess, int& xCol, int& yCol ) const;
bool currentLabelDataDefinedPosition( double& x, bool& xSuccess, double& y, bool& ySuccess, int& xCol, int& yCol ) const;

/** Returns data defined rotation of a feature.
@param vlayer vector layer
@param featureId feature identification integer
/** Returns data defined rotation of current label
@param rotation out: rotation value
@param rotationSuccess out: false if rotation value is NULL
@param rCol out: index of the rotation column
@param ignoreXY ignore that x and y are required to be data-defined
@return true if data defined rotation is enabled on the layer
*/
bool dataDefinedRotation( QgsVectorLayer* vlayer, QgsFeatureId featureId, double& rotation, bool& rotationSuccess, bool ignoreXY = false ) const;
bool currentLabelDataDefinedRotation( double& rotation, bool& rotationSuccess, int& rCol, bool ignoreXY = false ) const;

/** Returns data defined show/hide of a feature.
@param vlayer vector layer
@@ -59,7 +59,7 @@ void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent* e )
{
mStartPointMapCoords = toMapCoordinates( e->pos() );
QgsPoint referencePoint;
if ( !rotationPoint( referencePoint, !preserveRotation(), false ) )
if ( !currentLabelRotationPoint( referencePoint, !currentLabelPreserveRotation(), false ) )
{
referencePoint.setX( mCurrentLabel.pos.labelRect.xMinimum() );
referencePoint.setY( mCurrentLabel.pos.labelRect.yMinimum() );
@@ -109,7 +109,7 @@ void QgsMapToolMoveLabel::canvasReleaseEvent( QgsMapMouseEvent* e )
double xPosOrig, yPosOrig;
bool xSuccess, ySuccess;

if ( !dataDefinedPosition( vlayer, mCurrentLabel.pos.featureId, xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
if ( !currentLabelDataDefinedPosition( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
{
return;
}
@@ -154,15 +154,14 @@ void QgsMapToolMoveLabel::canvasReleaseEvent( QgsMapMouseEvent* e )
// set rotation to that of label, if data-defined and no rotation set yet
// honor whether to preserve preexisting data on pin
// must come after setting x and y positions
int rCol;
if ( !mCurrentLabel.pos.isDiagram
&& !mCurrentLabel.pos.isPinned
&& !preserveRotation()
&& layerIsRotatable( vlayer, rCol ) )
&& !currentLabelPreserveRotation() )
{
double defRot;
bool rSuccess;
if ( dataDefinedRotation( vlayer, mCurrentLabel.pos.featureId, defRot, rSuccess ) )
int rCol;
if ( currentLabelDataDefinedRotation( defRot, rSuccess, rCol ) )
{
double labelRot = mCurrentLabel.pos.rotation * 180 / M_PI;
vlayer->changeAttributeValue( mCurrentLabel.pos.featureId, rCol, labelRot );
@@ -272,7 +272,7 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle& ext, QMouseEvent *
if ( isPinned() && ( doUnpin || toggleUnpinOrPin ) )
{
// unpin previously pinned label (set attribute table fields to NULL)
if ( pinUnpinLabel( vlayer, pos, false ) )
if ( pinUnpinCurrentFeature( false ) )
{
labelChanged = true;
}
@@ -285,7 +285,7 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle& ext, QMouseEvent *
if ( !isPinned() && ( !doUnpin || toggleUnpinOrPin ) )
{
// pin label's location, and optionally rotation, to attribute table
if ( pinUnpinLabel( vlayer, pos, true ) )
if ( pinUnpinCurrentFeature( true ) )
{
labelChanged = true;
}
@@ -308,10 +308,11 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle& ext, QMouseEvent *
}
}

bool QgsMapToolPinLabels::pinUnpinLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin )
bool QgsMapToolPinLabels::pinUnpinCurrentLabel( bool pin )
{
QgsVectorLayer* vlayer = mCurrentLabel.layer;
const QgsLabelPosition& labelpos = mCurrentLabel.pos;

// skip diagrams
if ( labelpos.isDiagram )
{
@@ -324,7 +325,7 @@ bool QgsMapToolPinLabels::pinUnpinLabel( QgsVectorLayer* vlayer,
double xPosOrig, yPosOrig;
bool xSuccess, ySuccess;

if ( !dataDefinedPosition( vlayer, labelpos.featureId, xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
if ( !currentLabelDataDefinedPosition( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
{
QgsDebugMsg( QString( "Label X or Y column not mapped, skipping" ) );
return false;
@@ -335,11 +336,10 @@ bool QgsMapToolPinLabels::pinUnpinLabel( QgsVectorLayer* vlayer,
bool rSuccess = false;
double defRot;

bool hasRCol = ( layerIsRotatable( vlayer, rCol )
&& dataDefinedRotation( vlayer, labelpos.featureId, defRot, rSuccess, true ) );
bool hasRCol = currentLabelDataDefinedRotation( defRot, rSuccess, rCol, true );

// get whether to preserve predefined rotation data during label pin/unpin operations
bool preserveRot = preserveRotation();
bool preserveRot = currentLabelPreserveRotation();

// edit attribute table
int fid = labelpos.featureId;
@@ -353,7 +353,7 @@ bool QgsMapToolPinLabels::pinUnpinLabel( QgsVectorLayer* vlayer,
// QgsPoint labelpoint = labelpos.cornerPoints.at( 0 );

QgsPoint referencePoint;
if ( !rotationPoint( referencePoint, !preserveRot, false ) )
if ( !currentLabelRotationPoint( referencePoint, !preserveRot, false ) )
{
referencePoint.setX( labelpos.labelRect.xMinimum() );
referencePoint.setY( labelpos.labelRect.yMinimum() );
@@ -417,46 +417,42 @@ bool QgsMapToolPinLabels::pinUnpinLabel( QgsVectorLayer* vlayer,
return true;
}

bool QgsMapToolPinLabels::pinUnpinFeature( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin )
bool QgsMapToolPinLabels::pinUnpinCurrentFeature( bool pin )
{
bool rc = false;

if ( ! mCurrentLabelPos.isDiagram )
rc = pinUnpinLabel( vlayer, labelpos, pin );
if ( ! mCurrentLabel.pos.isDiagram )
rc = pinUnpinCurrentLabel( pin );
else
rc = pinUnpinDiagram( vlayer, labelpos, pin );
rc = pinUnpinCurrentDiagram( pin );

return rc;
}

bool QgsMapToolPinLabels::pinUnpinDiagram( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin )
bool QgsMapToolPinLabels::pinUnpinCurrentDiagram( bool pin )
{

// skip diagrams
if ( ! labelpos.isDiagram )
if ( ! mCurrentLabel.pos.isDiagram )
return false;

// verify attribute table has x, y fields mapped
int xCol, yCol;
double xPosOrig, yPosOrig;
bool xSuccess, ySuccess;

if ( !dataDefinedPosition( vlayer, mCurrentLabelPos.featureId, xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
if ( !currentLabelDataDefinedPosition( xPosOrig, xSuccess, yPosOrig, ySuccess, xCol, yCol ) )
return false;

// edit attribute table
int fid = labelpos.featureId;
int fid = mCurrentLabel.pos.featureId;

bool writeFailed = false;
QString labelText = currentLabelText( 24 );

if ( pin )
{
QgsPoint referencePoint = mCurrentLabelPos.labelRect.center();
QgsPoint referencePoint = mCurrentLabel.pos.labelRect.center();
double labelX = referencePoint.x();
double labelY = referencePoint.y();

@@ -86,20 +86,14 @@ class APP_EXPORT QgsMapToolPinLabels: public QgsMapToolLabel
//! Select valid labels to pin or unpin
void pinUnpinLabels( const QgsRectangle& ext, QMouseEvent * e );

//! Pin or unpin label relative to whether its editable
bool pinUnpinLabel( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin );
//! Pin or unpin current label relative to whether its editable
bool pinUnpinCurrentLabel( bool pin );

//! Pin or unpin diagram relative to whether its editable
bool pinUnpinDiagram( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin );

//! Pin or unpin a feature (diagram or label)
bool pinUnpinFeature( QgsVectorLayer* vlayer,
const QgsLabelPosition& labelpos,
const bool pin );
bool pinUnpinCurrentDiagram( bool pin );

//! Pin or unpin current feature (diagram or label)
bool pinUnpinCurrentFeature( bool pin );
};

#endif // QGSMAPTOOLPINLABELS_H

0 comments on commit ad82ac0

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