Skip to content
Permalink
Browse files

Refactoring of QgsPalLabeling

The idea was not to expose the labeling engine instance used for rendering
to other parts of the application and keep it as an implementation
detail within renderer jobs.

Resulting placement is kept in a newly created class QgsLabelingResults
which is passed from the renderer job's labeling engine to map canvas
where it is available for map tools.

Global labeling settings are now kept only in QgsProject (there is
no instance of QgsPalLabeling which would stay alive all the time).
  • Loading branch information
wonder-sk committed Nov 14, 2013
1 parent cfc2a48 commit d49f7ac2ee762ca0a26c3654ac5d1aa3078a29f2
@@ -3643,9 +3643,6 @@ bool QgisApp::addProject( QString projectFile )
}
}

// load PAL engine settings
mMapCanvas->labelingEngine()->loadEngineSettings();

emit projectRead(); // let plug-ins know that we've read in a new
// project so that they can check any project
// specific plug-in state
@@ -4336,7 +4333,7 @@ void QgisApp::labeling()

QDialog *dlg = new QDialog( this );
dlg->setWindowTitle( tr( "Layer labeling settings" ) );
QgsLabelingGui *labelingGui = new QgsLabelingGui( mMapCanvas->labelingEngine(), vlayer, mMapCanvas, dlg );
QgsLabelingGui *labelingGui = new QgsLabelingGui( vlayer, mMapCanvas, dlg );
labelingGui->init(); // load QgsPalLayerSettings for layer
labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
QVBoxLayout *layout = new QVBoxLayout( dlg );
@@ -52,7 +52,6 @@ class QgsMapCanvas;
class QgsMapLayer;
class QgsMapTip;
class QgsMapTool;
class QgsPalLabeling;
class QgsPoint;
class QgsProviderRegistry;
class QgsPythonUtils;
@@ -1506,8 +1505,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QgsMessageLogViewer *mLogViewer;

QgsPalLabeling* mLBL;

//! project changed
void projectChanged( const QDomDocument & );

@@ -469,7 +469,7 @@ void QgsDiagramProperties::on_mDiagramAttributesTreeWidget_itemDoubleClicked( QT

void QgsDiagramProperties::on_mEngineSettingsButton_clicked()
{
QgsLabelEngineConfigDialog dlg( QgisApp::instance()->mapCanvas()->labelingEngine(), this );
QgsLabelEngineConfigDialog dlg( this );
dlg.exec();
}

@@ -19,48 +19,53 @@

#include <QPushButton>

QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWidget* parent )
: QDialog( parent ), mLBL( lbl )
QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QWidget* parent )
: QDialog( parent )
{
setupUi( this );

connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
connect( buttonBox->button( QDialogButtonBox::RestoreDefaults ), SIGNAL( clicked() ),
this, SLOT( setDefaults() ) );

QgsPalLabeling lbl;
lbl.loadEngineSettings();

// search method
cboSearchMethod->setCurrentIndex( mLBL->searchMethod() );
cboSearchMethod->setCurrentIndex( lbl.searchMethod() );

// candidate numbers
int candPoint, candLine, candPolygon;
mLBL->numCandidatePositions( candPoint, candLine, candPolygon );
lbl.numCandidatePositions( candPoint, candLine, candPolygon );
spinCandPoint->setValue( candPoint );
spinCandLine->setValue( candLine );
spinCandPolygon->setValue( candPolygon );

chkShowCandidates->setChecked( mLBL->isShowingCandidates() );
chkShowAllLabels->setChecked( mLBL->isShowingAllLabels() );
mShadowDebugRectChkBox->setChecked( mLBL->isShowingShadowRectangles() );
chkShowCandidates->setChecked( lbl.isShowingCandidates() );
chkShowAllLabels->setChecked( lbl.isShowingAllLabels() );
mShadowDebugRectChkBox->setChecked( lbl.isShowingShadowRectangles() );

chkShowPartialsLabels->setChecked( mLBL-> isShowingPartialsLabels() );
chkShowPartialsLabels->setChecked( lbl.isShowingPartialsLabels() );
}


void QgsLabelEngineConfigDialog::onOK()
{
QgsPalLabeling lbl;

// save
mLBL->setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );
lbl.setSearchMethod(( QgsPalLabeling::Search ) cboSearchMethod->currentIndex() );

mLBL->setNumCandidatePositions( spinCandPoint->value(),
lbl.setNumCandidatePositions( spinCandPoint->value(),
spinCandLine->value(),
spinCandPolygon->value() );

mLBL->setShowingCandidates( chkShowCandidates->isChecked() );
mLBL->setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
mLBL->setShowingAllLabels( chkShowAllLabels->isChecked() );
mLBL->setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );
lbl.setShowingCandidates( chkShowCandidates->isChecked() );
lbl.setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
lbl.setShowingAllLabels( chkShowAllLabels->isChecked() );
lbl.setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );

mLBL->saveEngineSettings();
lbl.saveEngineSettings();

accept();
}
@@ -19,21 +19,19 @@

#include "ui_qgsengineconfigdialog.h"

class QgsPalLabeling;

class APP_EXPORT QgsLabelEngineConfigDialog : public QDialog, private Ui::QgsEngineConfigDialog
{
Q_OBJECT
public:
QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWidget* parent = NULL );
QgsLabelEngineConfigDialog( QWidget* parent = NULL );

public slots:
void onOK();
/** @note Added in QGIS 1.9 */
void setDefaults();

protected:
QgsPalLabeling* mLBL;
};

#endif // QGSLABELENGINECONFIGDIALOG_H
@@ -45,8 +45,8 @@
#include <QSettings>


QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
: QWidget( parent ), mLBL( lbl ), mLayer( layer ), mMapCanvas( mapCanvas )
QgsLabelingGui::QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent )
: QWidget( parent ), mLayer( layer ), mMapCanvas( mapCanvas )
{
if ( !layer )
return;
@@ -1171,7 +1171,7 @@ void QgsLabelingGui::setPreviewBackground( QColor color )

void QgsLabelingGui::showEngineConfigDialog()
{
QgsLabelEngineConfigDialog dlg( mLBL, this );
QgsLabelEngineConfigDialog dlg( this );
dlg.exec();
}

@@ -33,7 +33,7 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
Q_OBJECT

public:
QgsLabelingGui( QgsPalLabeling *lbl, QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
QgsLabelingGui( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QWidget* parent );
~QgsLabelingGui();

QgsPalLayerSettings layerSettings();
@@ -96,7 +96,6 @@ class APP_EXPORT QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
void updateFont( QFont font );

private:
QgsPalLabeling* mLBL;
QgsVectorLayer* mLayer;
QgsMapCanvas* mMapCanvas;
QFontDatabase mFontDB;
@@ -28,8 +28,8 @@
#include <QSettings>


QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QgsPalLabeling* labeling, QWidget * parent, Qt::WindowFlags f ):
QDialog( parent, f ), mLabeling( labeling ), mLabelFont( labelFont ), mCurLabelField( -1 )
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent, Qt::WindowFlags f ):
QDialog( parent, f ), mLabelFont( labelFont ), mCurLabelField( -1 )
{
setupUi( this );
fillHaliComboBox();
@@ -65,7 +65,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const

blockElementSignals( true );

QgsPalLayerSettings& layerSettings = mLabeling->layer( layerId );
QgsPalLayerSettings layerSettings = QgsPalLayerSettings::fromLayer( vlayer );

//get label field and fill line edit
if ( layerSettings.isExpression && !labelText.isNull() )
@@ -30,7 +30,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, QgsPalLabeling* labeling, QWidget * parent = 0, Qt::WindowFlags f = 0 );
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QWidget * parent = 0, Qt::WindowFlags f = 0 );
~QgsLabelPropertyDialog();

/**Returns properties changed by the user*/
@@ -86,8 +86,6 @@ class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPro
/**Insert changed value into mChangedProperties*/
void insertChangedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant value );

QgsPalLabeling* mLabeling;

QgsAttributeMap mChangedProperties;
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* > mDataDefinedProperties;
QFont mLabelFont;
@@ -61,7 +61,7 @@ void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QMouseEvent *e )
labeltext = mCurrentLabelPos.labelText;
}

QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, mCanvas->labelingEngine() );
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, 0 );
if ( d.exec() == QDialog::Accepted )
{
const QgsAttributeMap& changes = d.changedProperties();
@@ -26,6 +26,7 @@
#include <QMouseEvent>

QgsMapToolLabel::QgsMapToolLabel( QgsMapCanvas* canvas ): QgsMapTool( canvas ), mLabelRubberBand( 0 ), mFeatureRubberBand( 0 ), mFixPointRubberBand( 0 )
, mCurrentLayer( 0 )
{
}

@@ -39,10 +40,10 @@ QgsMapToolLabel::~QgsMapToolLabel()
bool QgsMapToolLabel::labelAtPosition( QMouseEvent* e, QgsLabelPosition& p )
{
QgsPoint pt = toMapCoordinates( e->pos() );
QgsLabelingEngineInterface* labelingEngine = mCanvas->labelingEngine();
if ( labelingEngine )
const QgsLabelingResults* labelingResults = mCanvas->labelingResults();
if ( labelingResults )
{
QList<QgsLabelPosition> labelPosList = labelingEngine->labelsAtPosition( pt );
QList<QgsLabelPosition> labelPosList = labelingResults->labelsAtPosition( pt );
QList<QgsLabelPosition>::const_iterator posIt = labelPosList.constBegin();
if ( posIt != labelPosList.constEnd() )
{
@@ -120,7 +121,7 @@ void QgsMapToolLabel::deleteRubberBands()

QgsVectorLayer* QgsMapToolLabel::currentLayer()
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ) );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( mCurrentLabelPos.layerID ) );
return vlayer;
}

@@ -130,17 +131,16 @@ QgsPalLayerSettings& QgsMapToolLabel::currentLabelSettings( bool* ok )
QgsVectorLayer* vlayer = currentLayer();
if ( vlayer )
{
//QgsDebugMsg( "has vlayer" );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( labelEngine )
{
//QgsDebugMsg( "has labelEngine" );
if ( ok )
{
if ( vlayer == mCurrentLayer )
return mCurrentSettings;

mCurrentLayer = vlayer;
mCurrentSettings = QgsPalLayerSettings::fromLayer( vlayer );

if ( ok )
*ok = true;
}
return labelEngine->layer( mCurrentLabelPos.layerID );
}

return mCurrentSettings;
}

if ( ok )
@@ -430,16 +430,10 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool
return true;
}

int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsVectorLayer* vlayer ) const
int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, QgsVectorLayer* vlayer ) const
{

QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( !labelEngine )
{
return -1;
}
QgsDebugMsg( QString( "dataDefinedProperties layer id:%1" ).arg( vlayer->id() ) );
QgsPalLayerSettings& labelSettings = labelEngine->layer( vlayer->id() );
QgsPalLayerSettings labelSettings( QgsPalLayerSettings::fromLayer( vlayer ) );

QgsDebugMsg( QString( "dataDefinedProperties count:%1" ).arg( labelSettings.dataDefinedProperties.size() ) );

@@ -498,9 +492,9 @@ bool QgsMapToolLabel::dataDefinedPosition( QgsVectorLayer* vlayer, int featureId
return true;
}

bool QgsMapToolLabel::layerIsRotatable( const QgsMapLayer* layer, int& rotationCol ) const
bool QgsMapToolLabel::layerIsRotatable( QgsMapLayer* layer, int& rotationCol ) const
{
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( layer );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( layer );
if ( !vlayer || !vlayer->isEditable() )
{
return false;
@@ -575,9 +569,9 @@ bool QgsMapToolLabel::dataDefinedShowHide( QgsVectorLayer* vlayer, int featureId
return true;
}

bool QgsMapToolLabel::diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
bool QgsMapToolLabel::diagramMoveable( QgsMapLayer* ml, int& xCol, int& yCol ) const
{
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
if ( vlayer && vlayer->diagramRenderer() )
{
const QgsDiagramLayerSettings *dls = vlayer->diagramLayerSettings();
@@ -591,9 +585,9 @@ bool QgsMapToolLabel::diagramMoveable( const QgsMapLayer* ml, int& xCol, int& yC
return false;
}

bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol ) const
bool QgsMapToolLabel::labelMoveable( QgsMapLayer *ml, int& xCol, int& yCol ) const
{
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
if ( !vlayer || !vlayer->isEditable() )
{
return false;
@@ -624,17 +618,17 @@ bool QgsMapToolLabel::labelMoveable( const QgsMapLayer* ml, int& xCol, int& yCol
return false;
}

bool QgsMapToolLabel::layerCanPin( const QgsMapLayer* ml, int& xCol, int& yCol ) const
bool QgsMapToolLabel::layerCanPin( QgsMapLayer* ml, int& xCol, int& yCol ) const
{
// currently same as QgsMapToolLabel::labelMoveable, but may change
bool canPin = labelMoveable( ml, xCol, yCol );
return canPin;
}

bool QgsMapToolLabel::layerCanShowHide( const QgsMapLayer* ml, int& showCol ) const
bool QgsMapToolLabel::layerCanShowHide( QgsMapLayer* ml, int& showCol ) const
{
//QgsDebugMsg( "entered" );
const QgsVectorLayer* vlayer = dynamic_cast<const QgsVectorLayer*>( ml );
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( ml );
if ( !vlayer || !vlayer->isEditable() )
{
return false;

0 comments on commit d49f7ac

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