Skip to content
Permalink
Browse files

Made labeling working without QgsMapRenderer (using QgsMapSettings)

  • Loading branch information
wonder-sk committed Nov 11, 2013
1 parent 2d12aca commit 35894f1ecc160d83a907a40305d45c24928ea0be
@@ -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() )
@@ -2122,12 +2120,6 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

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

QgsMessageBar* QgisApp::messageBar()
{
Q_ASSERT( mInfoBar );
@@ -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
@@ -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 );
@@ -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" );

@@ -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();
}

@@ -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();
@@ -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 )
@@ -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() )
@@ -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*/
@@ -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;
@@ -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();
@@ -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 );
@@ -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" );
@@ -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;
@@ -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" ) );
@@ -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" ) );
@@ -30,7 +30,6 @@

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

@@ -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 );
@@ -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 )
@@ -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" );
@@ -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 );

@@ -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 );
@@ -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
@@ -8,6 +8,7 @@
#include "qgsrendercontext.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgspallabeling.h"


QgsMapRendererQImageJob::QgsMapRendererQImageJob(QgsMapRendererJob::Type type, const QgsMapSettings& settings)
@@ -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()
@@ -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()));
}


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

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


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


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


void QgsMapRendererCustomPainterJob::futureFinished()
{
qDebug("QPAINTER futureFinished");
@@ -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() );
@@ -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 ) );

@@ -8,6 +8,9 @@

#include "qgsmapsettings.h"

class QgsPalLabeling;


/** abstract base class renderer jobs that asynchronously start map rendering */
class QgsMapRendererJob : public QObject
{
@@ -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).
@@ -83,6 +90,8 @@ class QgsMapRendererSequentialJob : public QgsMapRendererQImageJob
virtual void start();
virtual void cancel();

virtual void setLabelingEngine( QgsPalLabeling* labeling );

// from QgsMapRendererJobWithPreview
virtual QImage renderedImage();

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

virtual void setLabelingEngine( QgsPalLabeling* labeling );

protected slots:
void futureFinished();

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


0 comments on commit 35894f1

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