Skip to content
Permalink
Browse files

Move/rotate/hide/pin map tools are always available

  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent ad1e158 commit 5b278d746ecb58b15e7ce26895bde9ea532b3964
@@ -179,6 +179,32 @@ class QgsAuxiliaryLayer : QgsVectorLayer
:rtype: bool
%End

static int createProperty( QgsPalLayerSettings::Property p, const QString &providerId, QgsVectorLayer *vlayer );
%Docstring
Create if necessary a new auxiliary field for a PAL property and
activate this property in settings.

\param p The property to create
\param providerId The id of the provider to use
\param vlayer The vector layer

:return: The index of the auxiliary field or -1
:rtype: int
%End

static int createProperty( QgsDiagramLayerSettings::Property p, QgsVectorLayer *vlayer );
%Docstring
Create if necessary a new auxiliary field for a diagram's property and
activate this this property in settings.

\param p The property to create
\param providerId The id of the provider to use
\param vlayer The vector layer

:return: The index of the auxiliary field or -1
:rtype: int
%End

};


@@ -223,6 +223,7 @@ Try to find a rule given its unique key
:rtype: QgsRuleBasedLabeling.Rule
%End


QgsRuleBasedLabeling::Rule *clone() const /Factory/;
%Docstring
clone this rule, return new instance
@@ -285,6 +286,16 @@ Create the instance from a DOM element with saved configuration
virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
virtual QStringList subProviders() const;
virtual QgsPalLayerSettings settings( const QString &providerId = QString() ) const;

virtual void setSettings( QgsPalLayerSettings *settings /Transfer/, const QString &providerId = QString() );
%Docstring
Set pal settings for a specific provider (takes ownership).

\param settings Pal layer settings
\param providerId The id of the provider

.. versionadded:: 3.0
%End
virtual bool requiresAdvancedEffects() const;


@@ -1092,9 +1092,11 @@ Return the provider type for this layer
:rtype: int
%End

const QgsAbstractVectorLayerLabeling *labeling() const;

QgsAbstractVectorLayerLabeling *labeling();
%Docstring
Access to labeling configuration. May be null if labeling is not used.

.. versionadded:: 3.0
:rtype: QgsAbstractVectorLayerLabeling
%End
@@ -61,6 +61,16 @@ Get list of sub-providers within the layer's labeling.
:rtype: QgsPalLayerSettings
%End

virtual void setSettings( QgsPalLayerSettings *settings /Transfer/, const QString &providerId = QString() ) = 0;
%Docstring
Set pal settings for a specific provider (takes ownership).

\param settings Pal layer settings
\param providerId The id of the provider

.. versionadded:: 3.0
%End

virtual bool requiresAdvancedEffects() const = 0;
%Docstring
Returns true if drawing labels requires advanced effects like composition
@@ -109,6 +119,17 @@ Constructs simple labeling configuration with given initial settings
virtual QgsAbstractVectorLayerLabeling *clone() const /Factory/;
virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context ) const;
virtual QgsPalLayerSettings settings( const QString &providerId = QString() ) const;

virtual void setSettings( QgsPalLayerSettings *settings /Transfer/, const QString &providerId = QString() );
%Docstring
Set pal settings (takes ownership).

\param settings Pal layer settings
\param providerId Unused parameter

.. versionadded:: 3.0
%End

virtual bool requiresAdvancedEffects() const;

virtual void toSld( QDomNode &parent, const QgsStringMap &props ) const;
@@ -10997,38 +10997,16 @@ void QgisApp::updateLabelToolButtons()
for ( QMap<QString, QgsMapLayer *>::iterator it = layers.begin(); it != layers.end(); ++it )
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( it.value() );
if ( !vlayer || ( !vlayer->diagramsEnabled() && !vlayer->labelsEnabled() ) )
continue;

int colX, colY, colShow, colAng;
enablePin =
enablePin ||
( qobject_cast<QgsMapToolPinLabels *>( mMapTools.mPinLabels ) &&
( qobject_cast<QgsMapToolPinLabels *>( mMapTools.mPinLabels )->labelMoveable( vlayer, colX, colY )
|| qobject_cast<QgsMapToolPinLabels *>( mMapTools.mPinLabels )->diagramMoveable( vlayer, colX, colY ) ) );

enableShowHide =
enableShowHide ||
( qobject_cast<QgsMapToolShowHideLabels *>( mMapTools.mShowHideLabels ) &&
( qobject_cast<QgsMapToolShowHideLabels *>( mMapTools.mShowHideLabels )->labelCanShowHide( vlayer, colShow )
|| qobject_cast<QgsMapToolShowHideLabels *>( mMapTools.mShowHideLabels )->diagramCanShowHide( vlayer, colShow ) ) );

enableMove =
enableMove ||
( qobject_cast<QgsMapToolMoveLabel *>( mMapTools.mMoveLabel ) &&
( qobject_cast<QgsMapToolMoveLabel *>( mMapTools.mMoveLabel )->labelMoveable( vlayer, colX, colY )
|| qobject_cast<QgsMapToolMoveLabel *>( mMapTools.mMoveLabel )->diagramMoveable( vlayer, colX, colY ) ) );

enableRotate =
enableRotate ||
( qobject_cast<QgsMapToolRotateLabel *>( mMapTools.mRotateLabel ) &&
qobject_cast<QgsMapToolRotateLabel *>( mMapTools.mRotateLabel )->layerIsRotatable( vlayer, colAng ) );

if ( vlayer->isEditable() )
if ( vlayer && ( vlayer->diagramsEnabled() || vlayer->labelsEnabled() ) )
{
enablePin = true;
enableShowHide = true;
enableMove = true;
enableRotate = true;
enableChange = true;

if ( enablePin && enableShowHide && enableMove && enableRotate && enableChange )
break;
}
}

mActionPinLabels->setEnabled( enablePin );
@@ -626,7 +626,7 @@ bool QgsMapToolLabel::diagramMoveable( QgsVectorLayer *vlayer, int &xCol, int &y

bool QgsMapToolLabel::labelMoveable( QgsVectorLayer *vlayer, int &xCol, int &yCol ) const
{
if ( !vlayer || !vlayer->labeling() )
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
{
return false;
}
@@ -648,26 +648,7 @@ bool QgsMapToolLabel::labelMoveable( QgsVectorLayer *vlayer, const QgsPalLayerSe
//return !xColName.isEmpty() && !yColName.isEmpty();
xCol = vlayer->fields().lookupField( xColName );
yCol = vlayer->fields().lookupField( yColName );

// labels may be moveable even if layer is not editable when data defined
// columns come from auxiliary storage
if ( xCol >= 0 && yCol >= 0 )
{
bool xAuxiliaryField = vlayer->isAuxiliaryField( xCol );
bool yAuxiliaryField = vlayer->isAuxiliaryField( yCol );

if ( ! xAuxiliaryField || ! yAuxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}

return false;
return ( xCol != -1 && yCol != -1 );
}

bool QgsMapToolLabel::layerCanPin( QgsVectorLayer *vlayer, int &xCol, int &yCol ) const
@@ -679,7 +660,7 @@ bool QgsMapToolLabel::layerCanPin( QgsVectorLayer *vlayer, int &xCol, int &yCol

bool QgsMapToolLabel::labelCanShowHide( QgsVectorLayer *vlayer, int &showCol ) const
{
if ( !vlayer || !vlayer->labeling() )
if ( !vlayer || !vlayer->isEditable() || !vlayer->labeling() )
{
return false;
}
@@ -689,20 +670,8 @@ bool QgsMapToolLabel::labelCanShowHide( QgsVectorLayer *vlayer, int &showCol ) c
QString fieldname = dataDefinedColumnName( QgsPalLayerSettings::Show,
vlayer->labeling()->settings( providerId ) );
showCol = vlayer->fields().lookupField( fieldname );
if ( showCol >= 0 )
{
bool auxiliaryField = vlayer->isAuxiliaryField( showCol );

if ( ! auxiliaryField )
{
if ( vlayer->isEditable() )
return true;
else
return false;
}
else
return true;
}
if ( showCol != -1 )
return true;
}

return false;
@@ -773,14 +742,14 @@ QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
: pos( p )
{
layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( pos.layerID ) );
if ( layer && layer->labeling() )
if ( layer && layer->labeling() && !p.isDiagram )
{
settings = layer->labeling()->settings( pos.providerID );

if ( p.isDiagram )
valid = layer->diagramsEnabled();
else
valid = true;
valid = true;
}
else if ( layer && layer->diagramsEnabled() && p.isDiagram )
{
valid = true;
}

if ( !valid )
@@ -789,3 +758,69 @@ QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
settings = QgsPalLayerSettings();
}
}

bool QgsMapToolLabel::createAuxiliaryFields( QgsPalIndexes &indexes )
{
return createAuxiliaryFields( mCurrentLabel, indexes );
}

bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsPalIndexes &indexes ) const
{
bool newAuxiliaryLayer = false;
QgsVectorLayer *vlayer = details.layer;
QString providerId = details.pos.providerID;

if ( !vlayer )
return newAuxiliaryLayer;

if ( !vlayer->auxiliaryLayer() )
{
QgsNewAuxiliaryLayerDialog dlg( vlayer );
dlg.exec();
newAuxiliaryLayer = true;
}

if ( !vlayer->auxiliaryLayer() )
return false;

Q_FOREACH ( const QgsPalLayerSettings::Property &p, mPalProperties )
{
indexes[p] = QgsAuxiliaryLayer::createProperty( p, providerId, vlayer );
}

details.settings = vlayer->labeling()->settings( providerId );

return newAuxiliaryLayer;
}

bool QgsMapToolLabel::createAuxiliaryFields( QgsDiagramIndexes &indexes )
{
return createAuxiliaryFields( mCurrentLabel, indexes );
}


bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsDiagramIndexes &indexes )
{
bool newAuxiliaryLayer = false;
QgsVectorLayer *vlayer = details.layer;

if ( !vlayer )
return newAuxiliaryLayer;

if ( !vlayer->auxiliaryLayer() )
{
QgsNewAuxiliaryLayerDialog dlg( vlayer );
dlg.exec();
newAuxiliaryLayer = true;
}

if ( !vlayer->auxiliaryLayer() )
return false;

Q_FOREACH ( const QgsDiagramLayerSettings::Property &p, mDiagramProperties )
{
indexes[p] = QgsAuxiliaryLayer::createProperty( p, vlayer );
}

return newAuxiliaryLayer;
}
@@ -20,10 +20,15 @@

#include "qgsmaptool.h"
#include "qgspallabeling.h"
#include "qgsnewauxiliarylayerdialog.h"
#include "qgsauxiliarystorage.h"
#include "qgis_app.h"

class QgsRubberBand;

typedef QMap<QgsPalLayerSettings::Property, int> QgsPalIndexes;
typedef QMap<QgsDiagramLayerSettings::Property, int> QgsDiagramIndexes;

//! Base class for map tools that modify label properties
class APP_EXPORT QgsMapToolLabel: public QgsMapTool
{
@@ -175,6 +180,14 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapTool
\since QGIS 2.16
*/
bool isPinned();

bool createAuxiliaryFields( QgsPalIndexes &palIndexes );
bool createAuxiliaryFields( LabelDetails &details, QgsPalIndexes &palIndexes ) const;
bool createAuxiliaryFields( QgsDiagramIndexes &diagIndexes );
bool createAuxiliaryFields( LabelDetails &details, QgsDiagramIndexes &diagIndexes );

QList<QgsPalLayerSettings::Property> mPalProperties;
QList<QgsDiagramLayerSettings::Property> mDiagramProperties;
};

#endif // QGSMAPTOOLLABEL_H
@@ -27,6 +27,12 @@ QgsMapToolMoveLabel::QgsMapToolMoveLabel( QgsMapCanvas *canvas )
, mClickOffsetY( 0 )
{
mToolName = tr( "Move label" );

mPalProperties << QgsPalLayerSettings::PositionX;
mPalProperties << QgsPalLayerSettings::PositionY;

mDiagramProperties << QgsDiagramLayerSettings::PositionX;
mDiagramProperties << QgsDiagramLayerSettings::PositionY;
}

void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent *e )
@@ -49,8 +55,29 @@ void QgsMapToolMoveLabel::canvasPressEvent( QgsMapMouseEvent *e )
}

int xCol, yCol;
if ( labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) ||
diagramMoveable( vlayer, xCol, yCol ) )

if ( !mCurrentLabel.pos.isDiagram && !labelMoveable( vlayer, mCurrentLabel.settings, xCol, yCol ) )
{
QgsPalIndexes indexes;

if ( createAuxiliaryFields( indexes ) )
return;

xCol = indexes[ QgsPalLayerSettings::PositionX ];
yCol = indexes[ QgsPalLayerSettings::PositionY ];
}
else if ( mCurrentLabel.pos.isDiagram && !diagramMoveable( vlayer, xCol, yCol ) )
{
QgsDiagramIndexes indexes;

if ( createAuxiliaryFields( indexes ) )
return;

xCol = indexes[ QgsDiagramLayerSettings::PositionX ];
yCol = indexes[ QgsDiagramLayerSettings::PositionY ];
}

if ( xCol >= 0 && yCol >= 0 )
{
mStartPointMapCoords = toMapCoordinates( e->pos() );
QgsPointXY referencePoint;

0 comments on commit 5b278d7

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