Skip to content
Permalink
Browse files

Added sync between QgsMapCanvas settings and (legacy) QgsMapRenderer …

…(untested)
  • Loading branch information
wonder-sk committed Dec 5, 2013
1 parent 7194567 commit 412eaf4a07ef6263b41aa83eae50c10eebde3127
@@ -23,7 +23,6 @@
#include "qgspallabeling.h"
#include <QDialog>

class QgsMapRenderer;

/**A dialog to enter data defined label attributes*/
class APP_EXPORT QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialogBase
@@ -117,6 +117,8 @@ bool QgsMapRenderer::setExtent( const QgsRectangle& extent )
mExtent = extent;
if ( !extent.isEmpty() )
adjustExtentToSize();

emit extentsChanged();
return true;
}

@@ -164,129 +164,129 @@ class CORE_EXPORT QgsMapRenderer : public QObject

//! starts rendering
//! @ param forceWidthScale Force a specific scale factor for line widths and marker sizes. Automatically calculated from output device DPI if 0
Q_DECL_DEPRECATED void render( QPainter* painter, double* forceWidthScale = 0 );
void render( QPainter* painter, double* forceWidthScale = 0 );

//! sets extent and checks whether suitable (returns false if not)
Q_DECL_DEPRECATED bool setExtent( const QgsRectangle& extent );
bool setExtent( const QgsRectangle& extent );

//! returns current extent
Q_DECL_DEPRECATED QgsRectangle extent() const;
QgsRectangle extent() const;

Q_DECL_DEPRECATED const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }
const QgsMapToPixel* coordinateTransform() { return &( mRenderContext.mapToPixel() ); }

//! Scale denominator
Q_DECL_DEPRECATED double scale() const { return mScale; }
double scale() const { return mScale; }
/**Sets scale for scale based visibility. Normally, the scale is calculated automatically. This
function is only used to force a preview scale (e.g. for print composer)*/
Q_DECL_DEPRECATED void setScale( double scale ) {mScale = scale;}
Q_DECL_DEPRECATED double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }
void setScale( double scale ) {mScale = scale;}
double mapUnitsPerPixel() const { return mMapUnitsPerPixel; }

Q_DECL_DEPRECATED int width() const { return mSize.width(); }
Q_DECL_DEPRECATED int height() const { return mSize.height(); }
int width() const { return mSize.width(); }
int height() const { return mSize.height(); }

//! Recalculate the map scale
Q_DECL_DEPRECATED void updateScale();
void updateScale();

Q_DECL_DEPRECATED QGis::UnitType mapUnits() const;
Q_DECL_DEPRECATED void setMapUnits( QGis::UnitType u );
QGis::UnitType mapUnits() const;
void setMapUnits( QGis::UnitType u );

//! sets whether map image will be for overview
Q_DECL_DEPRECATED void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }
void enableOverviewMode( bool isOverview = true ) { mOverview = isOverview; }

Q_DECL_DEPRECATED void setOutputSize( QSize size, int dpi );
Q_DECL_DEPRECATED void setOutputSize( QSizeF size, double dpi );
void setOutputSize( QSize size, int dpi );
void setOutputSize( QSizeF size, double dpi );

//!accessor for output dpi
Q_DECL_DEPRECATED double outputDpi();
double outputDpi();
//!accessor for output size
Q_DECL_DEPRECATED QSize outputSize();
Q_DECL_DEPRECATED QSizeF outputSizeF();
QSize outputSize();
QSizeF outputSizeF();

/**
* @brief transform bounding box from layer's CRS to output CRS
* @see layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect ) if you want to transform a rectangle
* @return a bounding box (aligned rectangle) containing the transformed extent
*/
Q_DECL_DEPRECATED QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );
QgsRectangle layerExtentToOutputExtent( QgsMapLayer* theLayer, QgsRectangle extent );

/**
* @brief transform bounding box from output CRS to layer's CRS
* @see mapToLayerCoordinates( QgsMapLayer* theLayer,QgsRectangle rect ) if you want to transform a rectangle
* @return a bounding box (aligned rectangle) containing the transformed extent
*/
Q_DECL_DEPRECATED QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );
QgsRectangle outputExtentToLayerExtent( QgsMapLayer* theLayer, QgsRectangle extent );

/**
* @brief transform point coordinates from layer's CRS to output CRS
* @return the transformed point
*/
Q_DECL_DEPRECATED QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );
QgsPoint layerToMapCoordinates( QgsMapLayer* theLayer, QgsPoint point );

/**
* @brief transform rectangle from layer's CRS to output CRS
* @see layerExtentToOutputExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
Q_DECL_DEPRECATED QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
QgsRectangle layerToMapCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

/**
* @brief transform point coordinates from output CRS to layer's CRS
* @return the transformed point
*/
Q_DECL_DEPRECATED QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );
QgsPoint mapToLayerCoordinates( QgsMapLayer* theLayer, QgsPoint point );

/**
* @brief transform rectangle from output CRS to layer's CRS
* @see outputExtentToLayerExtent() if you want to transform a bounding box
* @return the transformed rectangle
*/
Q_DECL_DEPRECATED QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );
QgsRectangle mapToLayerCoordinates( QgsMapLayer* theLayer, QgsRectangle rect );

//! sets whether to use projections for this layer set
Q_DECL_DEPRECATED void setProjectionsEnabled( bool enabled );
void setProjectionsEnabled( bool enabled );

//! returns true if projections are enabled for this layer set
Q_DECL_DEPRECATED bool hasCrsTransformEnabled() const;
bool hasCrsTransformEnabled() const;

//! sets destination coordinate reference system
Q_DECL_DEPRECATED void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );

//! returns CRS of destination coordinate reference system
Q_DECL_DEPRECATED const QgsCoordinateReferenceSystem& destinationCrs() const;
const QgsCoordinateReferenceSystem& destinationCrs() const;

Q_DECL_DEPRECATED void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}
void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}

Q_DECL_DEPRECATED OutputUnits outputUnits() const {return mOutputUnits;}
OutputUnits outputUnits() const {return mOutputUnits;}

//! returns current extent of layer set
Q_DECL_DEPRECATED QgsRectangle fullExtent();
QgsRectangle fullExtent();

//! returns current layer set
Q_DECL_DEPRECATED QStringList& layerSet();
QStringList& layerSet();

//! change current layer set
Q_DECL_DEPRECATED void setLayerSet( const QStringList& layers );
void setLayerSet( const QStringList& layers );

//! updates extent of the layer set
Q_DECL_DEPRECATED void updateFullExtent();
void updateFullExtent();

//! read settings
Q_DECL_DEPRECATED bool readXML( QDomNode & theNode );
bool readXML( QDomNode & theNode );

//! write settings
Q_DECL_DEPRECATED bool writeXML( QDomNode & theNode, QDomDocument & theDoc );
bool writeXML( QDomNode & theNode, QDomDocument & theDoc );

//! Accessor for render context
Q_DECL_DEPRECATED QgsRenderContext* rendererContext() {return &mRenderContext;}
QgsRenderContext* rendererContext() {return &mRenderContext;}

//! Labeling engine (NULL if there's no custom engine)
//! \note Added in QGIS v1.4
Q_DECL_DEPRECATED QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }
QgsLabelingEngineInterface* labelingEngine() { return mLabelingEngine; }

//! Set labeling engine. Previous engine (if any) is deleted.
//! Takes ownership of the engine.
//! Added in QGIS v1.4
Q_DECL_DEPRECATED void setLabelingEngine( QgsLabelingEngineInterface* iface );
void setLabelingEngine( QgsLabelingEngineInterface* iface );

//! Returns a QPainter::CompositionMode corresponding to a BlendMode
//! Added in 1.9
@@ -315,6 +315,10 @@ class CORE_EXPORT QgsMapRenderer : public QObject
//! emitted when layer's draw() returned false
void drawError( QgsMapLayer* );

//! emitted when the current extent gets changed
//! @note added in 2.1
void extentsChanged();

public slots:

//! @deprecated in 2.1 - does nothing
@@ -81,6 +81,83 @@ class QgsMapCanvas::CanvasProperties



QgsMapCanvasRendererSync::QgsMapCanvasRendererSync( QgsMapCanvas* canvas, QgsMapRenderer* renderer )
: QObject( canvas )
, mCanvas( canvas )
, mRenderer( renderer )
{
connect( mCanvas, SIGNAL(extentsChanged()), this, SLOT(onExtentC2R()) );
connect( mRenderer, SIGNAL(extentsChanged()), this, SLOT(onExtentR2C()) );

connect( mCanvas, SIGNAL(mapUnitsChanged()), this, SLOT(onMapUnitsC2R()) );
connect( mRenderer, SIGNAL(mapUnitsChanged()), this, SLOT(onMapUnitsR2C()) );

connect( mCanvas, SIGNAL(hasCrsTransformEnabled(bool)), this, SLOT(onCrsTransformC2R()) );
connect( mRenderer, SIGNAL(hasCrsTransformEnabled(bool)), this, SLOT(onCrsTransformR2C()) );

connect( mCanvas, SIGNAL(destinationSrsChanged()), this, SLOT(onDestCrsC2R()) );
connect( mRenderer, SIGNAL(destinationSrsChanged()), this, SLOT(onDestCrsR2C()) );

connect( mCanvas, SIGNAL(layersChanged()), this, SLOT(onLayersC2R()) );
// TODO: layers R2C ? (should not happen!)

}

void QgsMapCanvasRendererSync::onExtentC2R()
{
mRenderer->blockSignals( true );
mRenderer->setExtent( mCanvas->mapSettings().extent() );
mRenderer->blockSignals( false );
}

void QgsMapCanvasRendererSync::onExtentR2C()
{
mCanvas->setExtent( mRenderer->extent() );
}

void QgsMapCanvasRendererSync::onMapUnitsC2R()
{
mRenderer->blockSignals( true );
mRenderer->setMapUnits( mCanvas->mapSettings().mapUnits() );
mRenderer->blockSignals( false );
}

void QgsMapCanvasRendererSync::onMapUnitsR2C()
{
mCanvas->setMapUnits( mRenderer->mapUnits() );
}

void QgsMapCanvasRendererSync::onCrsTransformC2R()
{
mRenderer->blockSignals( true );
mRenderer->setProjectionsEnabled( mCanvas->mapSettings().hasCrsTransformEnabled() );
mRenderer->blockSignals( false );
}

void QgsMapCanvasRendererSync::onCrsTransformR2C()
{
mCanvas->setCrsTransformEnabled( mRenderer->hasCrsTransformEnabled() );
}

void QgsMapCanvasRendererSync::onDestCrsC2R()
{
mRenderer->blockSignals( true );
mRenderer->setDestinationCrs( mCanvas->mapSettings().destinationCrs() );
mRenderer->blockSignals( false );
}

void QgsMapCanvasRendererSync::onDestCrsR2C()
{
mCanvas->setDestinationCrs( mRenderer->destinationCrs() );
}

void QgsMapCanvasRendererSync::onLayersC2R()
{
mRenderer->setLayerSet( mCanvas->mapSettings().layers() );
}



QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )
: QGraphicsView( parent )
, mCanvasProperties( new CanvasProperties )
@@ -128,7 +205,12 @@ QgsMapCanvas::QgsMapCanvas( QWidget * parent, const char *name )

mSettings.setFlag( QgsMapSettings::DrawEditingInfo );

// class that will sync most of the changes between canvas and (legacy) map renderer
// it is parented to map canvas, will be deleted automatically
new QgsMapCanvasRendererSync( this, mMapRenderer );

mSettings.setOutputSize( size() );
mMapRenderer->setOutputSize( size(), mSettings.outputDpi() );
setSceneRect( 0, 0, size().width(), size().height() );
mScene->setSceneRect( QRectF( 0, 0, size().width(), size().height() ) );

@@ -776,24 +858,6 @@ bool QgsMapCanvas::hasCrsTransformEnabled()
return mapSettings().hasCrsTransformEnabled();
}

void QgsMapCanvas::mapUnitsChanged()
{
// We assume that if the map units have changed, the changed value
// will be accessible from QgsMapRenderer

// And then force a redraw of the scale number in the status bar
updateScale();

// And then redraw the map to force the scale bar to update
// itself. This is less than ideal as the entire map gets redrawn
// just to get the scale bar to redraw itself. If we ask the scale
// bar to redraw itself without redrawing the map, the existing
// scale bar is not removed, and we end up with two scale bars in
// the same location. This can perhaps be fixed when/if the scale
// bar is done as a transparent layer on top of the map canvas.
refresh();
}

void QgsMapCanvas::zoomToSelected( QgsVectorLayer* layer )
{
if ( layer == NULL )
@@ -1076,6 +1140,7 @@ void QgsMapCanvas::resizeEvent( QResizeEvent * e )
QSize lastSize = size();

mSettings.setOutputSize( lastSize );
mMapRenderer->setOutputSize( lastSize, mSettings.outputDpi() );

mScene->setSceneRect( QRectF( 0, 0, lastSize.width(), lastSize.height() ) );

@@ -1364,6 +1429,12 @@ void QgsMapCanvas::setMapUnits( QGis::UnitType u )
{
QgsDebugMsg( "Setting map units to " + QString::number( static_cast<int>( u ) ) );
mSettings.setMapUnits( u );

updateScale();

refresh(); // this will force the scale bar to be updated

emit mapUnitsChanged();
}


0 comments on commit 412eaf4

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