Skip to content

Commit

Permalink
Refactoring of QgsPalLabeling
Browse files Browse the repository at this point in the history
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 d49f7ac
Show file tree
Hide file tree
Showing 26 changed files with 275 additions and 170 deletions.
5 changes: 1 addition & 4 deletions src/app/qgisapp.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 0 additions & 3 deletions src/app/qgisapp.h
Expand Up @@ -52,7 +52,6 @@ class QgsMapCanvas;
class QgsMapLayer;
class QgsMapTip;
class QgsMapTool;
class QgsPalLabeling;
class QgsPoint;
class QgsProviderRegistry;
class QgsPythonUtils;
Expand Down Expand Up @@ -1506,8 +1505,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QgsMessageLogViewer *mLogViewer;

QgsPalLabeling* mLBL;

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

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsdiagramproperties.cpp
Expand Up @@ -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();
}

Expand Down
35 changes: 20 additions & 15 deletions src/app/qgslabelengineconfigdialog.cpp
Expand Up @@ -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();
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/qgslabelengineconfigdialog.h
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -1171,7 +1171,7 @@ void QgsLabelingGui::setPreviewBackground( QColor color )

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

Expand Down
3 changes: 1 addition & 2 deletions src/app/qgslabelinggui.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgslabelpropertydialog.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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() )
Expand Down
4 changes: 1 addition & 3 deletions src/app/qgslabelpropertydialog.h
Expand Up @@ -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*/
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolchangelabelproperties.cpp
Expand Up @@ -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();
Expand Down
56 changes: 25 additions & 31 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QMouseEvent>

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

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

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

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

0 comments on commit d49f7ac

Please sign in to comment.