Skip to content

Commit f557a19

Browse files
committedMar 15, 2021
[feature] Allow auto-creation of callout auxiliary fields when
attempting to move a callout start or end point interactively Makes the user experience for moving a callout follow the exact same behavior as that of moving a label, where aux fields are immediately created for users whenever required instead of forcing them to create them themselves in advance.
1 parent a1fa68d commit f557a19

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed
 

‎src/app/labeling/qgsmaptoollabel.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,6 @@ bool QgsMapToolLabel::createAuxiliaryFields( QgsDiagramIndexes &indexes, bool ov
877877
return createAuxiliaryFields( mCurrentLabel, indexes, overwriteExpression );
878878
}
879879

880-
881880
bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsDiagramIndexes &indexes, bool overwriteExpression )
882881
{
883882
bool newAuxiliaryLayer = false;
@@ -922,6 +921,56 @@ bool QgsMapToolLabel::createAuxiliaryFields( LabelDetails &details, QgsDiagramIn
922921
return newAuxiliaryLayer;
923922
}
924923

924+
bool QgsMapToolLabel::createAuxiliaryFields( QgsCalloutIndexes &calloutIndexes, bool overwriteExpression )
925+
{
926+
return createAuxiliaryFields( mCurrentCallout, calloutIndexes, overwriteExpression );
927+
}
928+
929+
bool QgsMapToolLabel::createAuxiliaryFields( QgsCalloutPosition &details, QgsCalloutIndexes &calloutIndexes, bool overwriteExpression )
930+
{
931+
bool newAuxiliaryLayer = false;
932+
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( details.layerID );
933+
934+
if ( !vlayer )
935+
return newAuxiliaryLayer;
936+
937+
if ( !vlayer->auxiliaryLayer() )
938+
{
939+
QgsNewAuxiliaryLayerDialog dlg( vlayer );
940+
dlg.exec();
941+
newAuxiliaryLayer = true;
942+
}
943+
944+
if ( !vlayer->auxiliaryLayer() )
945+
return false;
946+
947+
QgsTemporaryCursorOverride cursor( Qt::WaitCursor );
948+
bool changed = false;
949+
for ( const QgsCallout::Property &p : qgis::as_const( mCalloutProperties ) )
950+
{
951+
int index = -1;
952+
953+
// always use the default activated property
954+
QgsProperty prop = vlayer->labeling() && vlayer->labeling()->settings( details.providerID ).callout() ? vlayer->labeling()->settings( details.providerID ).callout()->dataDefinedProperties().property( p ) :
955+
QgsProperty();
956+
if ( prop.propertyType() == QgsProperty::FieldBasedProperty && prop.isActive() )
957+
{
958+
index = vlayer->fields().lookupField( prop.field() );
959+
}
960+
else if ( prop.propertyType() != QgsProperty::ExpressionBasedProperty || overwriteExpression )
961+
{
962+
index = QgsAuxiliaryLayer::createProperty( p, vlayer );
963+
changed = true;
964+
}
965+
966+
calloutIndexes[p] = index;
967+
}
968+
if ( changed )
969+
emit vlayer->styleChanged();
970+
971+
return newAuxiliaryLayer;
972+
}
973+
925974
void QgsMapToolLabel::updateHoveredLabel( QgsMapMouseEvent *e )
926975
{
927976
if ( !mHoverRubberBand )
@@ -938,9 +987,7 @@ void QgsMapToolLabel::updateHoveredLabel( QgsMapMouseEvent *e )
938987

939988
QgsCalloutPosition calloutPosition;
940989
bool isOrigin = false;
941-
int xCol = 0;
942-
int yCol = 0;
943-
if ( calloutAtPosition( e, calloutPosition, isOrigin ) && canModifyCallout( calloutPosition, isOrigin, xCol, yCol ) )
990+
if ( calloutAtPosition( e, calloutPosition, isOrigin ) )
944991
{
945992
if ( !mCalloutOtherPointsRubberBand )
946993
{

‎src/app/labeling/qgsmaptoollabel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
#include "qgsnewauxiliarylayerdialog.h"
2424
#include "qgsauxiliarystorage.h"
2525
#include "qgscalloutposition.h"
26+
#include "qgscallout.h"
2627
#include "qgis_app.h"
2728

2829
class QgsRubberBand;
2930

3031
typedef QMap<QgsPalLayerSettings::Property, int> QgsPalIndexes;
3132
typedef QMap<QgsDiagramLayerSettings::Property, int> QgsDiagramIndexes;
33+
typedef QMap<QgsCallout::Property, int> QgsCalloutIndexes;
3234

3335
//! Base class for map tools that modify label properties
3436
class APP_EXPORT QgsMapToolLabel: public QgsMapToolAdvancedDigitizing
@@ -112,6 +114,8 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapToolAdvancedDigitizing
112114
//! Currently hovered label position
113115
LabelDetails mCurrentHoverLabel;
114116

117+
QgsCalloutPosition mCurrentCallout;
118+
115119
/**
116120
* Returns label position for mouse click location
117121
* \param e mouse event
@@ -220,6 +224,8 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapToolAdvancedDigitizing
220224
bool createAuxiliaryFields( LabelDetails &details, QgsPalIndexes &palIndexes, bool overwriteExpression = true ) const;
221225
bool createAuxiliaryFields( QgsDiagramIndexes &diagIndexes, bool overwriteExpression = true );
222226
bool createAuxiliaryFields( LabelDetails &details, QgsDiagramIndexes &diagIndexes, bool overwriteExpression = true );
227+
bool createAuxiliaryFields( QgsCalloutIndexes &calloutIndexes, bool overwriteExpression = true );
228+
bool createAuxiliaryFields( QgsCalloutPosition &details, QgsCalloutIndexes &calloutIndexes, bool overwriteExpression = true );
223229

224230
void updateHoveredLabel( QgsMapMouseEvent *e );
225231
void clearHoveredLabel();
@@ -228,6 +234,7 @@ class APP_EXPORT QgsMapToolLabel: public QgsMapToolAdvancedDigitizing
228234

229235
QList<QgsPalLayerSettings::Property> mPalProperties;
230236
QList<QgsDiagramLayerSettings::Property> mDiagramProperties;
237+
QList<QgsCallout::Property> mCalloutProperties;
231238

232239
friend class TestQgsMapToolLabel;
233240
};

‎src/app/labeling/qgsmaptoolmovelabel.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ QgsMapToolMoveLabel::QgsMapToolMoveLabel( QgsMapCanvas *canvas, QgsAdvancedDigit
3737

3838
mDiagramProperties << QgsDiagramLayerSettings::PositionX;
3939
mDiagramProperties << QgsDiagramLayerSettings::PositionY;
40+
41+
mCalloutProperties << QgsCallout::OriginX;
42+
mCalloutProperties << QgsCallout::OriginY;
43+
mCalloutProperties << QgsCallout::DestinationX;
44+
mCalloutProperties << QgsCallout::DestinationY;
4045
}
4146

4247
QgsMapToolMoveLabel::~QgsMapToolMoveLabel()
@@ -100,8 +105,19 @@ void QgsMapToolMoveLabel::cadCanvasPressEvent( QgsMapMouseEvent *e )
100105

101106
int xCol = 0;
102107
int yCol = 0;
103-
if ( calloutAtPosition( e, calloutPosition, mCurrentCalloutMoveOrigin ) && canModifyCallout( calloutPosition, mCurrentCalloutMoveOrigin, xCol, yCol ) )
108+
if ( calloutAtPosition( e, calloutPosition, mCurrentCalloutMoveOrigin ) )
104109
{
110+
if ( !canModifyCallout( calloutPosition, mCurrentCalloutMoveOrigin, xCol, yCol ) )
111+
{
112+
QgsCalloutIndexes indexes;
113+
114+
if ( createAuxiliaryFields( calloutPosition, indexes ) )
115+
return;
116+
117+
if ( !canModifyCallout( calloutPosition, mCurrentCalloutMoveOrigin, xCol, yCol ) )
118+
return;
119+
}
120+
105121
// callouts are a smaller target, so they take precedence over labels
106122
mCurrentLabel = LabelDetails();
107123
mCurrentCallout = calloutPosition;

‎src/app/labeling/qgsmaptoolmovelabel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class APP_EXPORT QgsMapToolMoveLabel: public QgsMapToolLabel
4343

4444
bool canModifyCallout( const QgsCalloutPosition &position, bool isOrigin, int &xCol, int &yCol ) override;
4545

46-
QgsCalloutPosition mCurrentCallout;
4746
bool mCurrentCalloutMoveOrigin = false;
4847

4948
QgsRubberBand *mCalloutMoveRubberBand = nullptr;

0 commit comments

Comments
 (0)
Please sign in to comment.