Skip to content

Commit

Permalink
Merge branch 'composer_overview_map'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 11, 2012
2 parents c372772 + ea2f74a commit a18db24
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 18 deletions.
7 changes: 7 additions & 0 deletions python/core/qgscomposermap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@ class QgsComposerMap : QgsComposerItem

void updateItem();

/**Sets overview frame map. -1 disables the overview frame
@note: this function was added in version 1.9*/
void setOverviewFrameMap( int mapId );
/**Returns id of overview frame (or -1 if no overfiew frame)
@note: this function was added in version 1.9*/
int overviewFrameMapId() const;

public slots:

/**Called if map canvas has changed*/
Expand Down
103 changes: 103 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include "qgscomposermapwidget.h"
#include "qgscomposeritemwidget.h"
#include "qgsmaprenderer.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
//#include "qgssymbolv2propertiesdialog.h"
#include "qgssymbolv2selectordialog.h"
#include "qgssymbollayerv2utils.h"
#include <QColorDialog>
#include <QFontDialog>

Expand Down Expand Up @@ -64,6 +69,27 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
connect( composerMap, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
}

if ( mComposerMap )
{
//insert available maps into mMapComboBox
mOverviewFrameMapComboBox->addItem( tr( "None" ), -1 );
const QgsComposition* composition = mComposerMap->composition();
if ( composition )
{
QList<const QgsComposerMap*> availableMaps = composition->composerMapItems();
QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin();
for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt )
{
if (( *mapItemIt )->id() != mComposerMap->id() )
{
mOverviewFrameMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), ( *mapItemIt )->id() );
}
}
}
}

updateOverviewSymbolMarker();

updateGuiElements();
blockAllSignals( false );
}
Expand Down Expand Up @@ -322,6 +348,10 @@ void QgsComposerMapWidget::updateGuiElements()
mDrawCanvasItemsCheckBox->setCheckState( Qt::Unchecked );
}

//overview frame
int overviewMapFrameId = mComposerMap->overviewFrameMapId();
mOverviewFrameMapComboBox->setCurrentIndex( mOverviewFrameMapComboBox->findData( overviewMapFrameId ) );

//grid
if ( mComposerMap->gridEnabled() )
{
Expand Down Expand Up @@ -459,6 +489,8 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
mDrawCanvasItemsCheckBox->blockSignals( b );
mFrameStyleComboBox->blockSignals( b );
mFrameWidthSpinBox->blockSignals( b );
mOverviewFrameMapComboBox->blockSignals( b );
mOverviewFrameStyleButton->blockSignals( b );
}

void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()
Expand Down Expand Up @@ -519,6 +551,68 @@ void QgsComposerMapWidget::on_mDrawCanvasItemsCheckBox_stateChanged( int state )
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mOverviewFrameMapComboBox_currentIndexChanged( const QString& text )
{
if ( !mComposerMap )
{
return;
}

if ( text == tr( "None" ) )
{
mComposerMap->setOverviewFrameMap( -1 );
}

//get composition
const QgsComposition* composition = mComposerMap->composition();
if ( !composition )
{
return;
}

//extract id
int id;
bool conversionOk;
QStringList textSplit = text.split( " " );
if ( textSplit.size() < 1 )
{
return;
}

QString idString = textSplit.at( textSplit.size() - 1 );
id = idString.toInt( &conversionOk );

if ( !conversionOk )
{
return;
}

const QgsComposerMap* composerMap = composition->getComposerMapById( id );
if ( !composerMap )
{
return;
}

mComposerMap->setOverviewFrameMap( id );
mComposerMap->update();
}

void QgsComposerMapWidget::on_mOverviewFrameStyleButton_clicked()
{
if ( !mComposerMap )
{
return;
}

QgsSymbolV2SelectorDialog d( mComposerMap->overviewFrameMapSymbol(), QgsStyleV2::defaultStyle(), 0 );

//QgsSymbolV2PropertiesDialog d( mComposerMap->overviewFrameMapSymbol(), 0, this );
if ( d.exec() == QDialog::Accepted )
{
updateOverviewSymbolMarker();
}
}

void QgsComposerMapWidget::on_mGridCheckBox_toggled( bool state )
{
if ( !mComposerMap )
Expand Down Expand Up @@ -896,3 +990,12 @@ void QgsComposerMapWidget::initAnnotationDirectionBox( QComboBox* c, QgsComposer
c->setCurrentIndex( c->findText( tr( "Horizontal" ) ) );
}
}

void QgsComposerMapWidget::updateOverviewSymbolMarker()
{
if ( mComposerMap )
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mComposerMap->overviewFrameMapSymbol(), mOverviewFrameStyleButton->iconSize() );
mOverviewFrameStyleButton->setIcon( icon );
}
}
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposermapwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void on_mUpdatePreviewButton_clicked();
void on_mKeepLayerListCheckBox_stateChanged( int state );
void on_mDrawCanvasItemsCheckBox_stateChanged( int state );
void on_mOverviewFrameMapComboBox_currentIndexChanged( const QString& text );
void on_mOverviewFrameStyleButton_clicked();

void on_mXMinLineEdit_editingFinished();
void on_mXMaxLineEdit_editingFinished();
Expand Down Expand Up @@ -104,6 +106,8 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase

void initAnnotationPositionBox( QComboBox* c, QgsComposerMap::GridAnnotationPosition pos );
void initAnnotationDirectionBox( QComboBox* c, QgsComposerMap::GridAnnotationDirection dir );

void updateOverviewSymbolMarker();
};

#endif
115 changes: 113 additions & 2 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
#include <cmath>

QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ),
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mOverviewFrameMapId( -1 ), mGridEnabled( false ), mGridStyle( Solid ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mComposition = composition;
mOverviewFrameMapSymbol = 0;
createDefaultOverviewFrameSymbol();

//mId = mComposition->composerMapItems().size();
int maxId = -1;
Expand Down Expand Up @@ -85,13 +87,16 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
}

QgsComposerMap::QgsComposerMap( QgsComposition *composition )
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ),
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mOverviewFrameMapId( -1 ), mGridEnabled( false ), mGridStyle( Solid ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mOverviewFrameMapSymbol = 0;
createDefaultOverviewFrameSymbol();

//Offset
mXOffset = 0.0;
mYOffset = 0.0;
Expand All @@ -110,6 +115,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )

QgsComposerMap::~QgsComposerMap()
{
delete mOverviewFrameMapSymbol;
}

void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
Expand Down Expand Up @@ -364,6 +370,10 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
drawSelectionBoxes( painter );
}

if ( mOverviewFrameMapId != -1 )
{
drawOverviewMapExtent( painter );
}

painter->restore();
}
Expand Down Expand Up @@ -659,6 +669,13 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
composerMapElem.setAttribute( "drawCanvasItems", "false" );
}

//overview map frame
QDomElement overviewFrameElem = doc.createElement( "overviewFrame" );
overviewFrameElem.setAttribute( "overviewFrameMap", mOverviewFrameMapId );
QDomElement overviewFrameStyleElem = QgsSymbolLayerV2Utils::saveSymbol( QString(), mOverviewFrameMapSymbol, doc );
overviewFrameElem.appendChild( overviewFrameStyleElem );
composerMapElem.appendChild( overviewFrameElem );

//extent
QDomElement extentElem = doc.createElement( "Extent" );
extentElem.setAttribute( "xmin", QString::number( mExtent.xMinimum() ) );
Expand All @@ -679,6 +696,9 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
}
composerMapElem.appendChild( layerSetElem );

//overview map frame
composerMapElem.setAttribute( "overviewFrameMap", mOverviewFrameMapId );

//grid
QDomElement gridElem = doc.createElement( "Grid" );
gridElem.setAttribute( "show", mGridEnabled );
Expand Down Expand Up @@ -746,6 +766,18 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
mPreviewMode = Rectangle;
}

QDomElement overviewFrameElem = itemElem.firstChildElement( "overviewFrame" );
if ( !overviewFrameElem.isNull() )
{
setOverviewFrameMap( overviewFrameElem.attribute( "overviewFrameMap", "-1" ).toInt() );
QDomElement overviewFrameSymbolElem = overviewFrameElem.firstChildElement( "symbol" );
if ( !overviewFrameSymbolElem.isNull() )
{
delete mOverviewFrameMapSymbol;
mOverviewFrameMapSymbol = dynamic_cast<QgsFillSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( overviewFrameSymbolElem ) );
}
}

//extent
QDomNodeList extentNodeList = itemElem.elementsByTagName( "Extent" );
if ( extentNodeList.size() > 0 )
Expand Down Expand Up @@ -1536,6 +1568,34 @@ double QgsComposerMap::mapUnitsToMM() const
return rect().width() / extentWidth;
}

void QgsComposerMap::setOverviewFrameMap( int mapId )
{
if ( mOverviewFrameMapId != -1 )
{
const QgsComposerMap* map = mComposition->getComposerMapById( mapId );
if ( map )
{
QObject::disconnect( map, SIGNAL( extentChanged() ), this, SLOT( repaint() ) );
}
}
mOverviewFrameMapId = mapId;
if ( mOverviewFrameMapId != -1 )
{
const QgsComposerMap* map = mComposition->getComposerMapById( mapId );
if ( map )
{
QObject::connect( map, SIGNAL( extentChanged() ), this, SLOT( repaint() ) );
}
}
update();
}

void QgsComposerMap::setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol )
{
delete mOverviewFrameMapSymbol;
mOverviewFrameMapSymbol = symbol;
}

void QgsComposerMap::transformShift( double& xShift, double& yShift ) const
{
double mmToMapUnits = 1.0 / mapUnitsToMM();
Expand Down Expand Up @@ -1845,4 +1905,55 @@ void QgsComposerMap::sortGridLinesOnBorders( const QList< QPair< double, QLineF
}
}

void QgsComposerMap::drawOverviewMapExtent( QPainter* p )
{
if ( mOverviewFrameMapId == -1 || !mComposition )
{
return;
}

const QgsComposerMap* overviewFrameMap = mComposition->getComposerMapById( mOverviewFrameMapId );
if ( !overviewFrameMap )
{
return;
}

QgsRectangle otherExtent = overviewFrameMap->extent();
QgsRectangle thisExtent = extent();
QgsRectangle intersectRect = thisExtent.intersect( &otherExtent );

QgsRenderContext context;
context.setPainter( p );
if ( mPreviewMode == Rectangle )
{
return;
}
else
{
context.setScaleFactor( 1.0 );
context.setRasterScaleFactor( mComposition->printResolution() / 25.4 );
}

QPolygonF polygon;
double x = ( intersectRect.xMinimum() - thisExtent.xMinimum() ) / thisExtent.width() * rect().width();
double y = ( thisExtent.yMaximum() - intersectRect.yMaximum() ) / thisExtent.height() * rect().height();
double width = intersectRect.width() / thisExtent.width() * rect().width();
double height = intersectRect.height() / thisExtent.height() * rect().height();
polygon << QPointF( x, y ) << QPointF( x + width, y ) << QPointF( x + width, y + height ) << QPointF( x, y + height ) << QPointF( x, y );

QList<QPolygonF> rings; //empty list
mOverviewFrameMapSymbol->startRender( context );
mOverviewFrameMapSymbol->renderPolygon( polygon, &rings, 0, context );
mOverviewFrameMapSymbol->stopRender( context );
}

void QgsComposerMap::createDefaultOverviewFrameSymbol()
{
delete mOverviewFrameMapSymbol;
QgsStringMap properties;
properties.insert( "color", "255,0,0,125" );
properties.insert( "style", "solid" );
properties.insert( "style_border", "no" );
mOverviewFrameMapSymbol = QgsFillSymbolV2::createSimple( properties );
mOverviewFrameMapSymbol->setAlpha( 0.3 );
}

0 comments on commit a18db24

Please sign in to comment.