Skip to content

Commit

Permalink
Made labeling working without QgsMapRenderer (using QgsMapSettings)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Nov 11, 2013
1 parent 2d12aca commit 35894f1
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 83 deletions.
12 changes: 2 additions & 10 deletions src/app/qgisapp.cpp
Expand Up @@ -715,8 +715,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
mMapCanvas->freeze( false );
mMapCanvas->clearExtentHistory(); // reset zoomnext/zoomlast
mLastComposerId = 0;
mLBL = new QgsPalLabeling();
mMapCanvas->mapRenderer()->setLabelingEngine( mLBL );

// Show a nice tip of the day
if ( settings.value( "/qgis/showTips", 1 ).toBool() )
Expand Down Expand Up @@ -2122,12 +2120,6 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

QgsPalLabeling *QgisApp::palLabeling()
{
Q_ASSERT( mLBL );
return mLBL;
}

QgsMessageBar* QgisApp::messageBar()
{
Q_ASSERT( mInfoBar );
Expand Down Expand Up @@ -3653,7 +3645,7 @@ bool QgisApp::addProject( QString projectFile )
}

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

emit projectRead(); // let plug-ins know that we've read in a new
// project so that they can check any project
Expand Down Expand Up @@ -4345,7 +4337,7 @@ void QgisApp::labeling()

QDialog *dlg = new QDialog( this );
dlg->setWindowTitle( tr( "Layer labeling settings" ) );
QgsLabelingGui *labelingGui = new QgsLabelingGui( mLBL, vlayer, mMapCanvas, dlg );
QgsLabelingGui *labelingGui = new QgsLabelingGui( mMapCanvas->labelingEngine(), 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 @@ -179,9 +179,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
/** Return the messageBar object which allows to display unobtrusive messages to the user.*/
QgsMessageBar* messageBar();

/** Get the mapcanvas object from the app */
QgsPalLabeling *palLabeling();

//! Set theme (icons)
void setTheme( QString themeName = "default" );

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()->palLabeling(), this );
QgsLabelEngineConfigDialog dlg( QgisApp::instance()->mapCanvas()->labelingEngine(), this );
dlg.exec();
}

Expand Down
16 changes: 3 additions & 13 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, QgsMapRenderer* renderer, QWidget * parent, Qt::WindowFlags f ):
QDialog( parent, f ), mMapRenderer( renderer ), mLabelFont( labelFont ), mCurLabelField( -1 )
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 )
{
setupUi( this );
fillHaliComboBox();
Expand All @@ -48,11 +48,6 @@ QgsLabelPropertyDialog::~QgsLabelPropertyDialog()

void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const QString& labelText )
{
if ( !mMapRenderer )
{
return;
}

//get feature attributes
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( !vlayer )
Expand All @@ -67,15 +62,10 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
const QgsAttributes& attributeValues = mCurLabelFeat.attributes();

//get layerproperties. Problem: only for pallabeling...
QgsPalLabeling* lbl = dynamic_cast<QgsPalLabeling*>( mMapRenderer->labelingEngine() );
if ( !lbl )
{
return;
}

blockElementSignals( true );

QgsPalLayerSettings& layerSettings = lbl->layer( layerId );
QgsPalLayerSettings& layerSettings = mLabeling->layer( layerId );

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

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

QgsMapRenderer* mMapRenderer;
QgsPalLabeling* mLabeling;

QgsAttributeMap mChangedProperties;
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* > mDataDefinedProperties;
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->mapRenderer() );
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, mCanvas->labelingEngine() );
if ( d.exec() == QDialog::Accepted )
{
const QgsAttributeMap& changes = d.changedProperties();
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsmaptoollabel.cpp
Expand Up @@ -39,7 +39,7 @@ QgsMapToolLabel::~QgsMapToolLabel()
bool QgsMapToolLabel::labelAtPosition( QMouseEvent* e, QgsLabelPosition& p )
{
QgsPoint pt = toMapCoordinates( e->pos() );
QgsLabelingEngineInterface* labelingEngine = mCanvas->mapRenderer()->labelingEngine();
QgsLabelingEngineInterface* labelingEngine = mCanvas->labelingEngine();
if ( labelingEngine )
{
QList<QgsLabelPosition> labelPosList = labelingEngine->labelsAtPosition( pt );
Expand Down Expand Up @@ -131,7 +131,7 @@ QgsPalLayerSettings& QgsMapToolLabel::currentLabelSettings( bool* ok )
if ( vlayer )
{
//QgsDebugMsg( "has vlayer" );
QgsPalLabeling* labelEngine = dynamic_cast<QgsPalLabeling*>( mCanvas->mapRenderer()->labelingEngine() );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( labelEngine )
{
//QgsDebugMsg( "has labelEngine" );
Expand Down Expand Up @@ -433,7 +433,7 @@ bool QgsMapToolLabel::rotationPoint( QgsPoint& pos, bool ignoreUpsideDown, bool
int QgsMapToolLabel::dataDefinedColumnIndex( QgsPalLayerSettings::DataDefinedProperties p, const QgsVectorLayer* vlayer ) const
{

QgsPalLabeling* labelEngine = dynamic_cast<QgsPalLabeling*>( mCanvas->mapRenderer()->labelingEngine() );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( !labelEngine )
{
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolpinlabels.cpp
Expand Up @@ -181,7 +181,7 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
QgsDebugMsg( QString( "Highlighting pinned labels" ) );

// get list of all drawn labels from all layers within given extent
QgsPalLabeling* labelEngine = dynamic_cast<QgsPalLabeling*>( mCanvas->mapRenderer()->labelingEngine() );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( !labelEngine )
{
QgsDebugMsg( QString( "No labeling engine" ) );
Expand Down Expand Up @@ -252,7 +252,7 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle& ext, QMouseEvent *
// get list of all drawn labels from all layers within, or touching, chosen extent
bool labelChanged = false;

QgsPalLabeling* labelEngine = dynamic_cast<QgsPalLabeling*>( mCanvas->mapRenderer()->labelingEngine() );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( !labelEngine )
{
QgsDebugMsg( QString( "No labeling engine" ) );
Expand Down
14 changes: 3 additions & 11 deletions src/app/qgsmaptoolshowhidelabels.cpp
Expand Up @@ -30,7 +30,6 @@

QgsMapToolShowHideLabels::QgsMapToolShowHideLabels( QgsMapCanvas* canvas ): QgsMapToolLabel( canvas )
{
mRender = 0;
mRubberBand = 0;
}

Expand Down Expand Up @@ -110,13 +109,6 @@ void QgsMapToolShowHideLabels::showHideLabels( QMouseEvent * e )
return;
}

mRender = mCanvas->mapRenderer();
if ( !mRender )
{
QgsDebugMsg( "Failed to acquire map renderer" );
return;
}

QgsMapLayer* layer = mCanvas->currentLayer();

QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( layer );
Expand Down Expand Up @@ -198,11 +190,11 @@ bool QgsMapToolShowHideLabels::selectedFeatures( QgsVectorLayer* vlayer,
// and then click somewhere off the globe, an exception will be thrown.
QgsGeometry selectGeomTrans( *selectGeometry );

if ( mRender->hasCrsTransformEnabled() )
if ( mCanvas->hasCrsTransformEnabled() )
{
try
{
QgsCoordinateTransform ct( mRender->destinationCrs(), vlayer->crs() );
QgsCoordinateTransform ct( mCanvas->mapSettings().destinationCrs(), vlayer->crs() );
selectGeomTrans.transform( ct );
}
catch ( QgsCsException &cse )
Expand Down Expand Up @@ -242,7 +234,7 @@ bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer* vlayer,
{
// get list of all drawn labels from current layer that intersect rubberband

QgsPalLabeling* labelEngine = dynamic_cast<QgsPalLabeling*>( mRender->labelingEngine() );
QgsPalLabeling* labelEngine = mCanvas->labelingEngine();
if ( !labelEngine )
{
QgsDebugMsg( "No labeling engine" );
Expand Down
3 changes: 0 additions & 3 deletions src/app/qgsmaptoolshowhidelabels.h
Expand Up @@ -53,9 +53,6 @@ class APP_EXPORT QgsMapToolShowHideLabels : public QgsMapToolLabel

private:

//! Pointer to map renderer
QgsMapRenderer* mRender;

//! Select valid labels to pin or unpin
void showHideLabels( QMouseEvent * e );

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -106,7 +106,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
// Create the Labeling dialog tab
layout = new QVBoxLayout( labelingFrame );
layout->setMargin( 0 );
labelingDialog = new QgsLabelingGui( QgisApp::instance()->palLabeling(), layer, QgisApp::instance()->mapCanvas(), labelingFrame );
labelingDialog = new QgsLabelingGui( QgisApp::instance()->mapCanvas()->labelingEngine(), layer, QgisApp::instance()->mapCanvas(), labelingFrame );
labelingDialog->layout()->setContentsMargins( -1, 0, -1, 0 );
layout->addWidget( labelingDialog );
labelingFrame->setLayout( layout );
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsmaprenderer.h
Expand Up @@ -73,7 +73,10 @@ class CORE_EXPORT QgsLabelingEngineInterface
virtual ~QgsLabelingEngineInterface() {}

//! called when we're going to start with rendering
virtual void init( QgsMapRenderer* mp ) = 0;
//! @deprecated since 2.1 - use override with QgsMapSettings
Q_DECL_DEPRECATED virtual void init( QgsMapRenderer* mp ) = 0;
//! called when we're going to start with rendering
virtual void init( const QgsMapSettings& mapSettings ) = 0;
//! called to find out whether the layer is used for labeling
virtual bool willUseLayer( QgsVectorLayer* layer ) = 0;
//! clears all PAL layer settings for registered layers
Expand Down
40 changes: 26 additions & 14 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -8,6 +8,7 @@
#include "qgsrendercontext.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgspallabeling.h"


QgsMapRendererQImageJob::QgsMapRendererQImageJob(QgsMapRendererJob::Type type, const QgsMapSettings& settings)
Expand All @@ -21,6 +22,14 @@ QgsMapRendererSequentialJob::QgsMapRendererSequentialJob(const QgsMapSettings& s
, mInternalJob(0)
{
qDebug("SEQUENTIAL construct");

mImage = QImage(mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied);

mPainter = new QPainter(&mImage);

mInternalJob = new QgsMapRendererCustomPainterJob(mSettings, mPainter);

connect(mInternalJob, SIGNAL(finished()), SLOT(internalFinished()));
}

QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob()
Expand All @@ -36,16 +45,7 @@ void QgsMapRendererSequentialJob::start()
qDebug("SEQUENTIAL START");
qDebug("%d,%d", mSettings.outputSize().width(), mSettings.outputSize().height());

mImage = QImage(mSettings.outputSize(), QImage::Format_ARGB32_Premultiplied);

// 1. create an image where we will output all rendering
mPainter = new QPainter(&mImage);

// 2. start rendering the layers in a thread (using custom painter)
mInternalJob = new QgsMapRendererCustomPainterJob(mSettings, mPainter);
mInternalJob->start();

connect(mInternalJob, SIGNAL(finished()), SLOT(internalFinished()));
}


Expand All @@ -59,6 +59,11 @@ void QgsMapRendererSequentialJob::cancel()
}
}

void QgsMapRendererSequentialJob::setLabelingEngine( QgsPalLabeling *labeling )
{
mInternalJob->setLabelingEngine( labeling );
}


QImage QgsMapRendererSequentialJob::renderedImage()
{
Expand Down Expand Up @@ -88,6 +93,7 @@ void QgsMapRendererSequentialJob::internalFinished()
QgsMapRendererCustomPainterJob::QgsMapRendererCustomPainterJob(const QgsMapSettings& settings, QPainter* painter)
: QgsMapRendererJob(CustomPainterJob, settings)
, mPainter(painter)
, mLabelingEngine( 0 )
{
qDebug("QPAINTER construct");
}
Expand Down Expand Up @@ -134,6 +140,12 @@ void QgsMapRendererCustomPainterJob::cancel()
}


void QgsMapRendererCustomPainterJob::setLabelingEngine( QgsPalLabeling *labeling )
{
mLabelingEngine = labeling;
}


void QgsMapRendererCustomPainterJob::futureFinished()
{
qDebug("QPAINTER futureFinished");
Expand Down Expand Up @@ -199,9 +211,9 @@ void QgsMapRendererCustomPainterJob::startRender()
mRenderContext.setScaleFactor( scaleFactor );
mRenderContext.setRendererScale( mSettings.scale() );

/*mRenderContext.setLabelingEngine( mLabelingEngine );
mRenderContext.setLabelingEngine( mLabelingEngine );
if ( mLabelingEngine )
mLabelingEngine->init( this );*/
mLabelingEngine->init( mSettings );

// render all layers in the stack, starting at the base
QListIterator<QString> li( mSettings.layers() );
Expand Down Expand Up @@ -513,15 +525,15 @@ void QgsMapRendererCustomPainterJob::startRender()
}
} // if (!mOverview)*/

/*if ( mLabelingEngine )
if ( mLabelingEngine )
{
// set correct extent
mRenderContext.setExtent( mExtent );
mRenderContext.setExtent( mSettings.visibleExtent() );
mRenderContext.setCoordinateTransform( NULL );

mLabelingEngine->drawLabeling( mRenderContext );
mLabelingEngine->exit();
}*/
}

QgsDebugMsg( "Rendering completed in (seconds): " + QString( "%1" ).arg( renderTime.elapsed() / 1000.0 ) );

Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsmaprendererjob.h
Expand Up @@ -8,6 +8,9 @@

#include "qgsmapsettings.h"

class QgsPalLabeling;


/** abstract base class renderer jobs that asynchronously start map rendering */
class QgsMapRendererJob : public QObject
{
Expand Down Expand Up @@ -36,6 +39,10 @@ class QgsMapRendererJob : public QObject

// TODO: isActive() ?

//! Assign an existing labeling engine with the rendering job
//! TODO: handle concurrency - one labeling instance cannot be shared by multiple active rendering jobs!
virtual void setLabelingEngine( QgsPalLabeling* labeling ) = 0;

signals:

//! emitted when asynchronous rendering is finished (or canceled).
Expand Down Expand Up @@ -83,6 +90,8 @@ class QgsMapRendererSequentialJob : public QgsMapRendererQImageJob
virtual void start();
virtual void cancel();

virtual void setLabelingEngine( QgsPalLabeling* labeling );

// from QgsMapRendererJobWithPreview
virtual QImage renderedImage();

Expand Down Expand Up @@ -121,6 +130,8 @@ class QgsMapRendererCustomPainterJob : public QgsMapRendererJob
virtual void start();
virtual void cancel();

virtual void setLabelingEngine( QgsPalLabeling* labeling );

protected slots:
void futureFinished();

Expand All @@ -134,6 +145,7 @@ protected slots:
QFuture<void> mFuture;
QFutureWatcher<void> mFutureWatcher;
QgsRenderContext mRenderContext;
QgsPalLabeling* mLabelingEngine;
};


Expand Down

0 comments on commit 35894f1

Please sign in to comment.