Skip to content
Permalink
Browse files

[FEATURE] Make labeling map tools work with rule-based labeling

(until now they worked just with simple labeling)
  • Loading branch information
wonder-sk committed Apr 27, 2016
2 parents e9bc8ae + 572bd19 commit 1a3e648e17c8e10a018bd97c91c31825705d693d
@@ -6,7 +6,7 @@ class QgsLabelPosition
#include <qgsmaprenderer.h>
%End
public:
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 );
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() );
QgsLabelPosition();
int featureId;
double rotation;
@@ -20,6 +20,8 @@ class QgsLabelPosition
bool upsideDown;
bool isDiagram;
bool isPinned;
//! @note added in 2.14
QString providerID;
};

/** Labeling engine interface. */
@@ -24,21 +24,22 @@
#include "qgsvectorlayer.h"
#include "qgisapp.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayerlabeling.h"

#include <QColorDialog>
#include <QFontDatabase>
#include <QSettings>
#include <QDialogButtonBox>


QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, const QString& providerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
QDialog( parent, f ), mLabelFont( labelFont ), mCurLabelField( -1 )
{
setupUi( this );
fillHaliComboBox();
fillValiComboBox();

init( layerId, featureId, labelText );
init( layerId, providerId, featureId, labelText );

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

void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const QString& labelText )
void QgsLabelPropertyDialog::init( const QString& layerId, const QString& providerId, int featureId, const QString& labelText )
{
//get feature attributes
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( !vlayer )
{
return;
}
if ( !vlayer->labeling() )
{
return;
}

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

blockElementSignals( true );

QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( vlayer );
QgsPalLayerSettings layerSettings = vlayer->labeling()->settings( vlayer, providerId );

//get label field and fill line edit
if ( layerSettings.isExpression && !labelText.isNull() )
@@ -29,7 +29,7 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
{
Q_OBJECT
public:
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = nullptr, Qt::WindowFlags f = nullptr );
QgsLabelPropertyDialog( const QString& layerId, const QString& providerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = nullptr, Qt::WindowFlags f = nullptr );
~QgsLabelPropertyDialog();

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

private:
/** Sets activation / values to the gui elements depending on the label settings and feature values*/
void init( const QString& layerId, int featureId, const QString& labelText );
void init( const QString& layerId, const QString& providerId, int featureId, const QString& labelText );
void disableGuiElements();
/** Block / unblock all input element signals*/
void blockElementSignals( bool block );
@@ -33,13 +33,15 @@ void QgsMapToolChangeLabelProperties::canvasPressEvent( QgsMapMouseEvent* e )
{
deleteRubberBands();

if ( !labelAtPosition( e, mCurrentLabelPos ) || mCurrentLabelPos.isDiagram )
QgsLabelPosition labelPos;
if ( !labelAtPosition( e, labelPos ) || labelPos.isDiagram )
{
mCurrentLabel = LabelDetails();
return;
}

QgsVectorLayer* vlayer = currentLayer();
if ( !vlayer || !vlayer->isEditable() )
mCurrentLabel = LabelDetails( labelPos );
if ( !mCurrentLabel.valid || !mCurrentLabel.layer || !mCurrentLabel.layer->isEditable() )
{
return;
}
@@ -50,18 +52,19 @@ void QgsMapToolChangeLabelProperties::canvasPressEvent( QgsMapMouseEvent* e )
void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QgsMapMouseEvent* e )
{
Q_UNUSED( e );
QgsVectorLayer* vlayer = currentLayer();
if ( mLabelRubberBand && mCanvas && vlayer )
if ( mLabelRubberBand && mCurrentLabel.valid )
{
QString labeltext = QString(); // NULL QString signifies no expression
bool settingsOk;
QgsPalLayerSettings& labelSettings = currentLabelSettings( &settingsOk );
if ( settingsOk && labelSettings.isExpression )
if ( mCurrentLabel.settings.isExpression )
{
labeltext = mCurrentLabelPos.labelText;
labeltext = mCurrentLabel.pos.labelText;
}

QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, nullptr );
QgsLabelPropertyDialog d( mCurrentLabel.pos.layerID,
mCurrentLabel.pos.providerID,
mCurrentLabel.pos.featureId,
mCurrentLabel.pos.labelFont,
labeltext, nullptr );

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

void QgsMapToolChangeLabelProperties::applyChanges( const QgsAttributeMap& changes )
{
QgsVectorLayer* vlayer = currentLayer();
QgsVectorLayer* vlayer = mCurrentLabel.layer;
if ( !vlayer )
return;

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

vlayer->endEditCommand();

0 comments on commit 1a3e648

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