Skip to content

Commit

Permalink
[FEATURE] Make labeling map tools work with rule-based labeling
Browse files Browse the repository at this point in the history
(until now they worked just with simple labeling)
  • Loading branch information
wonder-sk committed Apr 27, 2016
2 parents e9bc8ae + 572bd19 commit 1a3e648
Show file tree
Hide file tree
Showing 29 changed files with 442 additions and 372 deletions.
4 changes: 3 additions & 1 deletion python/core/qgsmaprenderer.sip
Expand Up @@ -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;
Expand All @@ -20,6 +20,8 @@ class QgsLabelPosition
bool upsideDown;
bool isDiagram;
bool isPinned;
//! @note added in 2.14
QString providerID;
};

/** Labeling engine interface. */
Expand Down
13 changes: 9 additions & 4 deletions src/app/qgslabelpropertydialog.cpp
Expand Up @@ -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() );
Expand All @@ -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 ) )
{
Expand All @@ -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() )
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgslabelpropertydialog.h
Expand Up @@ -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*/
Expand Down Expand Up @@ -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 );
Expand Down
27 changes: 15 additions & 12 deletions src/app/qgsmaptoolchangelabelproperties.cpp
Expand Up @@ -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;
}
Expand All @@ -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 )
Expand All @@ -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;

Expand All @@ -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();
Expand Down

0 comments on commit 1a3e648

Please sign in to comment.