Skip to content

Commit 1a3e648

Browse files
committedApr 27, 2016
[FEATURE] Make labeling map tools work with rule-based labeling
(until now they worked just with simple labeling)
2 parents e9bc8ae + 572bd19 commit 1a3e648

29 files changed

+442
-372
lines changed
 

‎python/core/qgsmaprenderer.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class QgsLabelPosition
66
#include <qgsmaprenderer.h>
77
%End
88
public:
9-
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false );
9+
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false, const QString& providerId = QString() );
1010
QgsLabelPosition();
1111
int featureId;
1212
double rotation;
@@ -20,6 +20,8 @@ class QgsLabelPosition
2020
bool upsideDown;
2121
bool isDiagram;
2222
bool isPinned;
23+
//! @note added in 2.14
24+
QString providerID;
2325
};
2426

2527
/** Labeling engine interface. */

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@
2424
#include "qgsvectorlayer.h"
2525
#include "qgisapp.h"
2626
#include "qgsmapcanvas.h"
27+
#include "qgsvectorlayerlabeling.h"
2728

2829
#include <QColorDialog>
2930
#include <QFontDatabase>
3031
#include <QSettings>
3132
#include <QDialogButtonBox>
3233

3334

34-
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
35+
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, const QString& providerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
3536
QDialog( parent, f ), mLabelFont( labelFont ), mCurLabelField( -1 )
3637
{
3738
setupUi( this );
3839
fillHaliComboBox();
3940
fillValiComboBox();
4041

41-
init( layerId, featureId, labelText );
42+
init( layerId, providerId, featureId, labelText );
4243

4344
QSettings settings;
4445
restoreGeometry( settings.value( QString( "/Windows/ChangeLabelProps/geometry" ) ).toByteArray() );
@@ -60,14 +61,18 @@ void QgsLabelPropertyDialog::on_buttonBox_clicked( QAbstractButton *button )
6061
}
6162
}
6263

63-
void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const QString& labelText )
64+
void QgsLabelPropertyDialog::init( const QString& layerId, const QString& providerId, int featureId, const QString& labelText )
6465
{
6566
//get feature attributes
6667
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
6768
if ( !vlayer )
6869
{
6970
return;
7071
}
72+
if ( !vlayer->labeling() )
73+
{
74+
return;
75+
}
7176

7277
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( mCurLabelFeat ) )
7378
{
@@ -79,7 +84,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
7984

8085
blockElementSignals( true );
8186

82-
QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( vlayer );
87+
QgsPalLayerSettings layerSettings = vlayer->labeling()->settings( vlayer, providerId );
8388

8489
//get label field and fill line edit
8590
if ( layerSettings.isExpression && !labelText.isNull() )

‎src/app/qgslabelpropertydialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
2929
{
3030
Q_OBJECT
3131
public:
32-
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = nullptr, Qt::WindowFlags f = nullptr );
32+
QgsLabelPropertyDialog( const QString& layerId, const QString& providerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = nullptr, Qt::WindowFlags f = nullptr );
3333
~QgsLabelPropertyDialog();
3434

3535
/** Returns properties changed by the user*/
@@ -68,7 +68,7 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
6868

6969
private:
7070
/** Sets activation / values to the gui elements depending on the label settings and feature values*/
71-
void init( const QString& layerId, int featureId, const QString& labelText );
71+
void init( const QString& layerId, const QString& providerId, int featureId, const QString& labelText );
7272
void disableGuiElements();
7373
/** Block / unblock all input element signals*/
7474
void blockElementSignals( bool block );

‎src/app/qgsmaptoolchangelabelproperties.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ void QgsMapToolChangeLabelProperties::canvasPressEvent( QgsMapMouseEvent* e )
3333
{
3434
deleteRubberBands();
3535

36-
if ( !labelAtPosition( e, mCurrentLabelPos ) || mCurrentLabelPos.isDiagram )
36+
QgsLabelPosition labelPos;
37+
if ( !labelAtPosition( e, labelPos ) || labelPos.isDiagram )
3738
{
39+
mCurrentLabel = LabelDetails();
3840
return;
3941
}
4042

41-
QgsVectorLayer* vlayer = currentLayer();
42-
if ( !vlayer || !vlayer->isEditable() )
43+
mCurrentLabel = LabelDetails( labelPos );
44+
if ( !mCurrentLabel.valid || !mCurrentLabel.layer || !mCurrentLabel.layer->isEditable() )
4345
{
4446
return;
4547
}
@@ -50,18 +52,19 @@ void QgsMapToolChangeLabelProperties::canvasPressEvent( QgsMapMouseEvent* e )
5052
void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QgsMapMouseEvent* e )
5153
{
5254
Q_UNUSED( e );
53-
QgsVectorLayer* vlayer = currentLayer();
54-
if ( mLabelRubberBand && mCanvas && vlayer )
55+
if ( mLabelRubberBand && mCurrentLabel.valid )
5556
{
5657
QString labeltext = QString(); // NULL QString signifies no expression
57-
bool settingsOk;
58-
QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk );
59-
if ( settingsOk && labelSettings.isExpression )
58+
if ( mCurrentLabel.settings.isExpression )
6059
{
61-
labeltext = mCurrentLabelPos.labelText;
60+
labeltext = mCurrentLabel.pos.labelText;
6261
}
6362

64-
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, nullptr );
63+
QgsLabelPropertyDialog d( mCurrentLabel.pos.layerID,
64+
mCurrentLabel.pos.providerID,
65+
mCurrentLabel.pos.featureId,
66+
mCurrentLabel.pos.labelFont,
67+
labeltext, nullptr );
6568

6669
connect( &d, SIGNAL( applied() ), this, SLOT( dialogPropertiesApplied() ) );
6770
if ( d.exec() == QDialog::Accepted )
@@ -75,7 +78,7 @@ void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QgsMapMouseEvent* e )
7578

7679
void QgsMapToolChangeLabelProperties::applyChanges( const QgsAttributeMap& changes )
7780
{
78-
QgsVectorLayer* vlayer = currentLayer();
81+
QgsVectorLayer* vlayer = mCurrentLabel.layer;
7982
if ( !vlayer )
8083
return;
8184

@@ -86,7 +89,7 @@ void QgsMapToolChangeLabelProperties::applyChanges( const QgsAttributeMap& chang
8689
QgsAttributeMap::const_iterator changeIt = changes.constBegin();
8790
for ( ; changeIt != changes.constEnd(); ++changeIt )
8891
{
89-
vlayer->changeAttributeValue( mCurrentLabelPos.featureId, changeIt.key(), changeIt.value() );
92+
vlayer->changeAttributeValue( mCurrentLabel.pos.featureId, changeIt.key(), changeIt.value() );
9093
}
9194

9295
vlayer->endEditCommand();

0 commit comments

Comments
 (0)
Please sign in to comment.