Skip to content

Commit

Permalink
GUI for rule-based labeling
Browse files Browse the repository at this point in the history
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 fa0b021
Show file tree
Hide file tree
Showing 14 changed files with 892 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5178,7 +5178,7 @@ void QgisApp::labeling()


QDialog *dlg = new QDialog( this ); QDialog *dlg = new QDialog( this );
dlg->setWindowTitle( tr( "Layer labeling settings" ) ); 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->init(); // load QgsPalLayerSettings for layer
labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 ); labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
QVBoxLayout *layout = new QVBoxLayout( dlg ); QVBoxLayout *layout = new QVBoxLayout( dlg );
Expand Down
12 changes: 10 additions & 2 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -33,6 +33,7 @@
#include "qgssymbollayerv2utils.h" #include "qgssymbollayerv2utils.h"
#include "qgscharacterselectdialog.h" #include "qgscharacterselectdialog.h"
#include "qgssvgselectorwidget.h" #include "qgssvgselectorwidget.h"
#include "qgsvectorlayerlabeling.h"


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


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


blockInitSignals( true ); blockInitSignals( true );


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


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

// all configuration is still in layer's custom properties
QgsPalLayerSettings settings = layerSettings(); QgsPalLayerSettings settings = layerSettings();
settings.writeToLayer( mLayer ); settings.writeToLayer( mLayer );
} }
Expand Down
6 changes: 4 additions & 2 deletions src/app/qgslabelinggui.h
Expand Up @@ -33,13 +33,14 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
Q_OBJECT Q_OBJECT


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


QgsPalLayerSettings layerSettings(); QgsPalLayerSettings layerSettings();
void writeSettingsToLayer(); void writeSettingsToLayer();


enum LabelMode { enum LabelMode
{
NoLabels, NoLabels,
Labels, Labels,
ObstaclesOnly, ObstaclesOnly,
Expand Down Expand Up @@ -105,6 +106,7 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
private: private:
QgsVectorLayer* mLayer; QgsVectorLayer* mLayer;
QgsMapCanvas* mMapCanvas; QgsMapCanvas* mMapCanvas;
const QgsPalLayerSettings* mSettings;
LabelMode mMode; LabelMode mMode;
QFontDatabase mFontDB; QFontDatabase mFontDB;
QgsCharacterSelectorDialog* mCharDlg; QgsCharacterSelectorDialog* mCharDlg;
Expand Down
65 changes: 39 additions & 26 deletions src/app/qgslabelingwidget.cpp
Expand Up @@ -9,25 +9,23 @@ QgsLabelingWidget::QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canva
: QWidget( parent ) : QWidget( parent )
, mLayer( layer ) , mLayer( layer )
, mCanvas( canvas ) , mCanvas( canvas )
, mWidget( 0 )
{ {
setupUi( this ); setupUi( this );


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


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


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


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

// load labeling settings from layer // load labeling settings from layer
QgsPalLayerSettings lyr; QgsPalLayerSettings lyr;
lyr.readFromLayer( mLayer ); lyr.readFromLayer( mLayer );
Expand All @@ -41,41 +39,56 @@ void QgsLabelingWidget::init()
{ {
mLabelModeComboBox->setCurrentIndex( lyr.drawLabels ? 1 : 2 ); 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() void QgsLabelingWidget::writeSettingsToLayer()
{ {
if ( mLabelModeComboBox->currentIndex() < 3 ) if ( mLabelModeComboBox->currentIndex() == 3 )
{ {
mWidgetSimple->writeSettingsToLayer(); qobject_cast<QgsRuleBasedLabelingWidget*>( mWidget )->writeSettingsToLayer();
} }
else else
{ {
mWidgetRules->writeSettingsToLayer(); qobject_cast<QgsLabelingGui*>( mWidget )->writeSettingsToLayer();
} }
} }




void QgsLabelingWidget::on_mLabelModeComboBox_currentIndexChanged( int index ) void QgsLabelingWidget::labelModeChanged( int index )
{ {
if ( index < 3 ) if ( index < 3 )
{ {
mStackedWidget->setCurrentIndex( 0 ); if ( QgsLabelingGui* widgetSimple = qobject_cast<QgsLabelingGui*>( mWidget ) )
mWidgetSimple->setLabelMode( ( QgsLabelingGui::LabelMode ) index ); {
// 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 else
{ {
// rule-based labeling QgsLabelingGui* w = new QgsLabelingGui( mLayer, mCanvas, 0, this );
mStackedWidget->setCurrentIndex( 1 ); w->setLabelMode(( QgsLabelingGui::LabelMode ) index );
w->init();
mWidget = w;
} }

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


void QgsLabelingWidget::showEngineConfigDialog() void QgsLabelingWidget::showEngineConfigDialog()
Expand Down
7 changes: 2 additions & 5 deletions src/app/qgslabelingwidget.h
Expand Up @@ -19,23 +19,20 @@ class QgsLabelingWidget : public QWidget, private Ui::QgsLabelingWidget
public: public:
QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent = 0 ); QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent = 0 );


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


signals: signals:


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


protected: protected:
QgsVectorLayer* mLayer; QgsVectorLayer* mLayer;
QgsMapCanvas* mCanvas; QgsMapCanvas* mCanvas;


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


#endif // QGSLABELINGWIDGET_H #endif // QGSLABELINGWIDGET_H

0 comments on commit fa0b021

Please sign in to comment.