Skip to content

Commit

Permalink
Move/rotate/hide/pin map tools are always available
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent ad1e158 commit 5b278d7
Show file tree
Hide file tree
Showing 20 changed files with 435 additions and 154 deletions.
26 changes: 26 additions & 0 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -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

};


Expand Down
11 changes: 11 additions & 0 deletions python/core/qgsrulebasedlabeling.sip
Expand Up @@ -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
Expand Down Expand Up @@ -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;


Expand Down
4 changes: 3 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions python/core/qgsvectorlayerlabeling.sip
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 7 additions & 29 deletions src/app/qgisapp.cpp
Expand Up @@ -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 );
Expand Down
119 changes: 77 additions & 42 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -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;
}
13 changes: 13 additions & 0 deletions src/app/qgsmaptoollabel.h
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
31 changes: 29 additions & 2 deletions src/app/qgsmaptoolmovelabel.cpp
Expand Up @@ -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 )
Expand All @@ -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;
Expand Down

0 comments on commit 5b278d7

Please sign in to comment.