Skip to content
Permalink
Browse files

GUI for rule-based labeling

This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
  • Loading branch information
wonder-sk committed Sep 24, 2015
1 parent c30dd04 commit fa0b021f9fbff3fc5cb6bf90aeaf522dc0503b92
@@ -5178,7 +5178,7 @@ void QgisApp::labeling()

QDialog *dlg = new QDialog( this );
dlg->setWindowTitle( tr( "Layer labeling settings" ) );
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, dlg );
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, 0, dlg );
labelingGui->init(); // load QgsPalLayerSettings for layer
labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
QVBoxLayout *layout = new QVBoxLayout( dlg );
@@ -33,6 +33,7 @@
#include "qgssymbollayerv2utils.h"
#include "qgscharacterselectdialog.h"
#include "qgssvgselectorwidget.h"
#include "qgsvectorlayerlabeling.h"

#include <QCheckBox>
#include <QSettings>
@@ -52,10 +53,11 @@ static QgsExpressionContext _getExpressionContext( const void* context )
return expContext;
}

QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsPalLayerSettings* settings, QWidget* parent )
: QWidget( parent )
, mLayer( layer )
, mMapCanvas( mapCanvas )
, mSettings( settings )
, mMode( NoLabels )
, mCharDlg( 0 )
, mQuadrantBtnGrp( 0 )
@@ -308,7 +310,10 @@ void QgsLabelingGui::init()
{
// load labeling settings from layer
QgsPalLayerSettings lyr;
lyr.readFromLayer( mLayer );
if ( mSettings )
lyr = *mSettings;
else
lyr.readFromLayer( mLayer );

blockInitSignals( true );

@@ -587,6 +592,9 @@ void QgsLabelingGui::apply()

void QgsLabelingGui::writeSettingsToLayer()
{
mLayer->setLabeling( new QgsVectorLayerSimpleLabeling );

// all configuration is still in layer's custom properties
QgsPalLayerSettings settings = layerSettings();
settings.writeToLayer( mLayer );
}
@@ -33,13 +33,14 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
Q_OBJECT

public:
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsPalLayerSettings* settings, QWidget* parent );
~QgsLabelingGui();

QgsPalLayerSettings layerSettings();
void writeSettingsToLayer();

enum LabelMode {
enum LabelMode
{
NoLabels,
Labels,
ObstaclesOnly,
@@ -105,6 +106,7 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
private:
QgsVectorLayer* mLayer;
QgsMapCanvas* mMapCanvas;
const QgsPalLayerSettings* mSettings;
LabelMode mMode;
QFontDatabase mFontDB;
QgsCharacterSelectorDialog* mCharDlg;
@@ -9,25 +9,23 @@ QgsLabelingWidget::QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canva
: QWidget( parent )
, mLayer( layer )
, mCanvas( canvas )
, mWidget( 0 )
{
setupUi( this );

connect( mEngineSettingsButton, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );

mWidgetSimple = new QgsLabelingGui( layer, canvas, this );
mWidgetRules = new QgsRuleBasedLabelingWidget( layer, canvas, this );
mStackedWidget->addWidget( mWidgetSimple );
mStackedWidget->addWidget( mWidgetRules );
mLabelModeComboBox->setCurrentIndex( -1 );

mStackedWidget->setCurrentIndex( 0 );
}
connect( mLabelModeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( labelModeChanged( int ) ) );

void QgsLabelingWidget::init()
{
if ( !mLayer->labeling() || mLayer->labeling()->type() == "simple" )
// pick the right mode of the layer
if ( mLayer->labeling() && mLayer->labeling()->type() == "rule-based" )
{
mLabelModeComboBox->setCurrentIndex( 3 );
}
else
{
mStackedWidget->setCurrentIndex( 0 );

// load labeling settings from layer
QgsPalLayerSettings lyr;
lyr.readFromLayer( mLayer );
@@ -41,41 +39,56 @@ void QgsLabelingWidget::init()
{
mLabelModeComboBox->setCurrentIndex( lyr.drawLabels ? 1 : 2 );
}

mWidgetSimple->init();
}
else if ( mLayer->labeling() && mLayer->labeling()->type() == "rule-based" )
{
mStackedWidget->setCurrentIndex( 1 );
mWidgetRules->init();
}
}

void QgsLabelingWidget::writeSettingsToLayer()
{
if ( mLabelModeComboBox->currentIndex() < 3 )
if ( mLabelModeComboBox->currentIndex() == 3 )
{
mWidgetSimple->writeSettingsToLayer();
qobject_cast<QgsRuleBasedLabelingWidget*>( mWidget )->writeSettingsToLayer();
}
else
{
mWidgetRules->writeSettingsToLayer();
qobject_cast<QgsLabelingGui*>( mWidget )->writeSettingsToLayer();
}
}


void QgsLabelingWidget::on_mLabelModeComboBox_currentIndexChanged( int index )
void QgsLabelingWidget::labelModeChanged( int index )
{
if ( index < 3 )
{
mStackedWidget->setCurrentIndex( 0 );
mWidgetSimple->setLabelMode( ( QgsLabelingGui::LabelMode ) index );
if ( QgsLabelingGui* widgetSimple = qobject_cast<QgsLabelingGui*>( mWidget ) )
{
// lighter variant - just change the mode of existing widget
widgetSimple->setLabelMode(( QgsLabelingGui::LabelMode ) index );
return;
}
}

// in general case we need to recreate the widget

if ( mWidget )
mStackedWidget->removeWidget( mWidget );

delete mWidget;
mWidget = 0;

if ( index == 3 )
{
mWidget = new QgsRuleBasedLabelingWidget( mLayer, mCanvas, this );
}
else
{
// rule-based labeling
mStackedWidget->setCurrentIndex( 1 );
QgsLabelingGui* w = new QgsLabelingGui( mLayer, mCanvas, 0, this );
w->setLabelMode(( QgsLabelingGui::LabelMode ) index );
w->init();
mWidget = w;
}

mStackedWidget->addWidget( mWidget );
mStackedWidget->setCurrentWidget( mWidget );
}

void QgsLabelingWidget::showEngineConfigDialog()
@@ -19,23 +19,20 @@ class QgsLabelingWidget : public QWidget, private Ui::QgsLabelingWidget
public:
QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent = 0 );

//! load config from layer
void init();
//! save config to layer
void writeSettingsToLayer();

signals:

protected slots:
void on_mLabelModeComboBox_currentIndexChanged( int index );
void labelModeChanged( int index );
void showEngineConfigDialog();

protected:
QgsVectorLayer* mLayer;
QgsMapCanvas* mCanvas;

QgsLabelingGui* mWidgetSimple;
QgsRuleBasedLabelingWidget* mWidgetRules;
QWidget* mWidget;
};

#endif // QGSLABELINGWIDGET_H

0 comments on commit fa0b021

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