Skip to content

Commit fa0b021

Browse files
committedSep 24, 2015
GUI for rule-based labeling
This code has been funded by Tuscany Region (Italy) - SITA (CIG: 63526840AE) and commissioned to Gis3W s.a.s.
1 parent c30dd04 commit fa0b021

14 files changed

+892
-54
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5178,7 +5178,7 @@ void QgisApp::labeling()
51785178

51795179
QDialog *dlg = new QDialog( this );
51805180
dlg->setWindowTitle( tr( "Layer labeling settings" ) );
5181-
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, dlg );
5181+
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, 0, dlg );
51825182
labelingGui->init(); // load QgsPalLayerSettings for layer
51835183
labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
51845184
QVBoxLayout *layout = new QVBoxLayout( dlg );

‎src/app/qgslabelinggui.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgssymbollayerv2utils.h"
3434
#include "qgscharacterselectdialog.h"
3535
#include "qgssvgselectorwidget.h"
36+
#include "qgsvectorlayerlabeling.h"
3637

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

55-
QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
56+
QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsPalLayerSettings* settings, QWidget* parent )
5657
: QWidget( parent )
5758
, mLayer( layer )
5859
, mMapCanvas( mapCanvas )
60+
, mSettings( settings )
5961
, mMode( NoLabels )
6062
, mCharDlg( 0 )
6163
, mQuadrantBtnGrp( 0 )
@@ -308,7 +310,10 @@ void QgsLabelingGui::init()
308310
{
309311
// load labeling settings from layer
310312
QgsPalLayerSettings lyr;
311-
lyr.readFromLayer( mLayer );
313+
if ( mSettings )
314+
lyr = *mSettings;
315+
else
316+
lyr.readFromLayer( mLayer );
312317

313318
blockInitSignals( true );
314319

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

588593
void QgsLabelingGui::writeSettingsToLayer()
589594
{
595+
mLayer->setLabeling( new QgsVectorLayerSimpleLabeling );
596+
597+
// all configuration is still in layer's custom properties
590598
QgsPalLayerSettings settings = layerSettings();
591599
settings.writeToLayer( mLayer );
592600
}

‎src/app/qgslabelinggui.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
3333
Q_OBJECT
3434

3535
public:
36-
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
36+
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsPalLayerSettings* settings, QWidget* parent );
3737
~QgsLabelingGui();
3838

3939
QgsPalLayerSettings layerSettings();
4040
void writeSettingsToLayer();
4141

42-
enum LabelMode {
42+
enum LabelMode
43+
{
4344
NoLabels,
4445
Labels,
4546
ObstaclesOnly,
@@ -105,6 +106,7 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
105106
private:
106107
QgsVectorLayer* mLayer;
107108
QgsMapCanvas* mMapCanvas;
109+
const QgsPalLayerSettings* mSettings;
108110
LabelMode mMode;
109111
QFontDatabase mFontDB;
110112
QgsCharacterSelectorDialog* mCharDlg;

‎src/app/qgslabelingwidget.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ QgsLabelingWidget::QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canva
99
: QWidget( parent )
1010
, mLayer( layer )
1111
, mCanvas( canvas )
12+
, mWidget( 0 )
1213
{
1314
setupUi( this );
1415

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

17-
mWidgetSimple = new QgsLabelingGui( layer, canvas, this );
18-
mWidgetRules = new QgsRuleBasedLabelingWidget( layer, canvas, this );
19-
mStackedWidget->addWidget( mWidgetSimple );
20-
mStackedWidget->addWidget( mWidgetRules );
18+
mLabelModeComboBox->setCurrentIndex( -1 );
2119

22-
mStackedWidget->setCurrentIndex( 0 );
23-
}
20+
connect( mLabelModeComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( labelModeChanged( int ) ) );
2421

25-
void QgsLabelingWidget::init()
26-
{
27-
if ( !mLayer->labeling() || mLayer->labeling()->type() == "simple" )
22+
// pick the right mode of the layer
23+
if ( mLayer->labeling() && mLayer->labeling()->type() == "rule-based" )
24+
{
25+
mLabelModeComboBox->setCurrentIndex( 3 );
26+
}
27+
else
2828
{
29-
mStackedWidget->setCurrentIndex( 0 );
30-
3129
// load labeling settings from layer
3230
QgsPalLayerSettings lyr;
3331
lyr.readFromLayer( mLayer );
@@ -41,41 +39,56 @@ void QgsLabelingWidget::init()
4139
{
4240
mLabelModeComboBox->setCurrentIndex( lyr.drawLabels ? 1 : 2 );
4341
}
44-
45-
mWidgetSimple->init();
46-
}
47-
else if ( mLayer->labeling() && mLayer->labeling()->type() == "rule-based" )
48-
{
49-
mStackedWidget->setCurrentIndex( 1 );
50-
mWidgetRules->init();
5142
}
5243
}
5344

5445
void QgsLabelingWidget::writeSettingsToLayer()
5546
{
56-
if ( mLabelModeComboBox->currentIndex() < 3 )
47+
if ( mLabelModeComboBox->currentIndex() == 3 )
5748
{
58-
mWidgetSimple->writeSettingsToLayer();
49+
qobject_cast<QgsRuleBasedLabelingWidget*>( mWidget )->writeSettingsToLayer();
5950
}
6051
else
6152
{
62-
mWidgetRules->writeSettingsToLayer();
53+
qobject_cast<QgsLabelingGui*>( mWidget )->writeSettingsToLayer();
6354
}
6455
}
6556

6657

67-
void QgsLabelingWidget::on_mLabelModeComboBox_currentIndexChanged( int index )
58+
void QgsLabelingWidget::labelModeChanged( int index )
6859
{
6960
if ( index < 3 )
7061
{
71-
mStackedWidget->setCurrentIndex( 0 );
72-
mWidgetSimple->setLabelMode( ( QgsLabelingGui::LabelMode ) index );
62+
if ( QgsLabelingGui* widgetSimple = qobject_cast<QgsLabelingGui*>( mWidget ) )
63+
{
64+
// lighter variant - just change the mode of existing widget
65+
widgetSimple->setLabelMode(( QgsLabelingGui::LabelMode ) index );
66+
return;
67+
}
68+
}
69+
70+
// in general case we need to recreate the widget
71+
72+
if ( mWidget )
73+
mStackedWidget->removeWidget( mWidget );
74+
75+
delete mWidget;
76+
mWidget = 0;
77+
78+
if ( index == 3 )
79+
{
80+
mWidget = new QgsRuleBasedLabelingWidget( mLayer, mCanvas, this );
7381
}
7482
else
7583
{
76-
// rule-based labeling
77-
mStackedWidget->setCurrentIndex( 1 );
84+
QgsLabelingGui* w = new QgsLabelingGui( mLayer, mCanvas, 0, this );
85+
w->setLabelMode(( QgsLabelingGui::LabelMode ) index );
86+
w->init();
87+
mWidget = w;
7888
}
89+
90+
mStackedWidget->addWidget( mWidget );
91+
mStackedWidget->setCurrentWidget( mWidget );
7992
}
8093

8194
void QgsLabelingWidget::showEngineConfigDialog()

‎src/app/qgslabelingwidget.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ class QgsLabelingWidget : public QWidget, private Ui::QgsLabelingWidget
1919
public:
2020
QgsLabelingWidget( QgsVectorLayer* layer, QgsMapCanvas* canvas, QWidget* parent = 0 );
2121

22-
//! load config from layer
23-
void init();
2422
//! save config to layer
2523
void writeSettingsToLayer();
2624

2725
signals:
2826

2927
protected slots:
30-
void on_mLabelModeComboBox_currentIndexChanged( int index );
28+
void labelModeChanged( int index );
3129
void showEngineConfigDialog();
3230

3331
protected:
3432
QgsVectorLayer* mLayer;
3533
QgsMapCanvas* mCanvas;
3634

37-
QgsLabelingGui* mWidgetSimple;
38-
QgsRuleBasedLabelingWidget* mWidgetRules;
35+
QWidget* mWidget;
3936
};
4037

4138
#endif // QGSLABELINGWIDGET_H

0 commit comments

Comments
 (0)
Please sign in to comment.