170 changes: 144 additions & 26 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
***************************************************************************/

#include "qgscomposermap.h"

#include "qgscoordinatetransform.h"
#include "qgslogger.h"
#include "qgsmaprenderer.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaptopixel.h"
#include "qgsproject.h"
Expand All @@ -42,7 +40,9 @@
QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ),
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mComposition = composition;
Expand Down Expand Up @@ -87,7 +87,9 @@ 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 ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mCrossLength( 3 ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mCrossLength( 3 ),
mMapCanvas( 0 ), mDrawCanvasItems( true )
{
//Offset
Expand Down Expand Up @@ -694,9 +696,15 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
//grid annotation
QDomElement annotationElem = doc.createElement( "Annotation" );
annotationElem.setAttribute( "show", mShowGridAnnotation );
annotationElem.setAttribute( "position", mGridAnnotationPosition );
annotationElem.setAttribute( "leftPosition", mLeftGridAnnotationPosition );
annotationElem.setAttribute( "rightPosition", mRightGridAnnotationPosition );
annotationElem.setAttribute( "topPosition", mTopGridAnnotationPosition );
annotationElem.setAttribute( "bottomPosition", mBottomGridAnnotationPosition );
annotationElem.setAttribute( "leftDirection", mLeftGridAnnotationDirection );
annotationElem.setAttribute( "rightDirection", mRightGridAnnotationDirection );
annotationElem.setAttribute( "topDirection", mTopGridAnnotationDirection );
annotationElem.setAttribute( "bottomDirection", mBottomGridAnnotationDirection );
annotationElem.setAttribute( "frameDistance", QString::number( mAnnotationFrameDistance ) );
annotationElem.setAttribute( "direction", mGridAnnotationDirection );
annotationElem.setAttribute( "font", mGridAnnotationFont.toString() );
annotationElem.setAttribute( "precision", mGridAnnotationPrecision );

Expand Down Expand Up @@ -811,9 +819,15 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
{
QDomElement annotationElem = annotationNodeList.at( 0 ).toElement();
mShowGridAnnotation = ( annotationElem.attribute( "show", "0" ) != "0" );
mGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "position", "0" ).toInt() );
mLeftGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "leftPosition", "0" ).toInt() );
mRightGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "rightPosition", "0" ).toInt() );
mTopGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "topPosition", "0" ).toInt() );
mBottomGridAnnotationPosition = QgsComposerMap::GridAnnotationPosition( annotationElem.attribute( "bottomPosition", "0" ).toInt() );
mLeftGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "leftDirection", "0" ).toInt() );
mRightGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "rightDirection", "0" ).toInt() );
mTopGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "topDirection", "0" ).toInt() );
mBottomGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "bottomDirection", "0" ).toInt() );
mAnnotationFrameDistance = annotationElem.attribute( "frameDistance", "0" ).toDouble();
mGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "direction", "0" ).toInt() );
mGridAnnotationFont.fromString( annotationElem.attribute( "font", "" ) );
mGridAnnotationPrecision = annotationElem.attribute( "precision", "3" ).toInt();
}
Expand Down Expand Up @@ -991,9 +1005,9 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
if ( frameBorder == Left )
{

if ( mGridAnnotationPosition == InsideMapFrame )
if ( mLeftGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
if ( mLeftGridAnnotationDirection == Vertical || mLeftGridAnnotationDirection == BoundaryDirection )
{
xpos += textHeight + mAnnotationFrameDistance;
ypos += textWidth / 2.0;
Expand All @@ -1005,9 +1019,9 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
ypos += textHeight / 2.0;
}
}
else //Outside map frame
else if ( mLeftGridAnnotationPosition == OutsideMapFrame ) //Outside map frame
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
if ( mLeftGridAnnotationDirection == Vertical || mLeftGridAnnotationDirection == BoundaryDirection )
{
xpos -= mAnnotationFrameDistance;
ypos += textWidth / 2.0;
Expand All @@ -1019,27 +1033,31 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
ypos += textHeight / 2.0;
}
}
else
{
return;
}

}
else if ( frameBorder == Right )
{
if ( mGridAnnotationPosition == InsideMapFrame )
if ( mRightGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
if ( mRightGridAnnotationDirection == Vertical || mRightGridAnnotationDirection == BoundaryDirection )
{
xpos -= mAnnotationFrameDistance;
ypos += textWidth / 2.0;
rotation = 270;
}
else //Horizontal
else
{
xpos -= textWidth + mAnnotationFrameDistance;
ypos += textHeight / 2.0;
}
}
else //OutsideMapFrame
else if ( mRightGridAnnotationPosition == OutsideMapFrame )//OutsideMapFrame
{
if ( mGridAnnotationDirection == Vertical || mGridAnnotationDirection == BoundaryDirection )
if ( mRightGridAnnotationDirection == Vertical || mRightGridAnnotationDirection == BoundaryDirection )
{
xpos += textHeight + mAnnotationFrameDistance;
ypos += textWidth / 2.0;
Expand All @@ -1051,12 +1069,16 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
ypos += textHeight / 2.0;
}
}
else
{
return;
}
}
else if ( frameBorder == Bottom )
{
if ( mGridAnnotationPosition == InsideMapFrame )
if ( mBottomGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
if ( mBottomGridAnnotationDirection == Horizontal || mBottomGridAnnotationDirection == BoundaryDirection )
{
ypos -= mAnnotationFrameDistance;
xpos -= textWidth / 2.0;
Expand All @@ -1068,9 +1090,9 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
rotation = 270;
}
}
else //OutsideMapFrame
else if ( mBottomGridAnnotationPosition == OutsideMapFrame ) //OutsideMapFrame
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
if ( mBottomGridAnnotationDirection == Horizontal || mBottomGridAnnotationDirection == BoundaryDirection )
{
ypos += mAnnotationFrameDistance + textHeight;
xpos -= textWidth / 2.0;
Expand All @@ -1082,12 +1104,16 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
rotation = 270;
}
}
else
{
return;
}
}
else //Top
{
if ( mGridAnnotationPosition == InsideMapFrame )
if ( mTopGridAnnotationPosition == InsideMapFrame )
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
if ( mTopGridAnnotationDirection == Horizontal || mTopGridAnnotationDirection == BoundaryDirection )
{
xpos -= textWidth / 2.0;
ypos += textHeight + mAnnotationFrameDistance;
Expand All @@ -1099,9 +1125,9 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
rotation = 270;
}
}
else //OutsideMapFrame
else if ( mTopGridAnnotationPosition == OutsideMapFrame ) //OutsideMapFrame
{
if ( mGridAnnotationDirection == Horizontal || mGridAnnotationDirection == BoundaryDirection )
if ( mTopGridAnnotationDirection == Horizontal || mTopGridAnnotationDirection == BoundaryDirection )
{
xpos -= textWidth / 2.0;
ypos -= mAnnotationFrameDistance;
Expand All @@ -1113,6 +1139,10 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
rotation = 270;
}
}
else
{
return;
}
}

drawAnnotation( p, QPointF( xpos, ypos ), rotation, annotationString );
Expand Down Expand Up @@ -1320,7 +1350,8 @@ QPolygonF QgsComposerMap::transformedMapPolygon() const

double QgsComposerMap::maxExtension() const
{
if ( !mGridEnabled || !mShowGridAnnotation || mGridAnnotationPosition != OutsideMapFrame )
if ( !mGridEnabled || !mShowGridAnnotation || ( mLeftGridAnnotationPosition != OutsideMapFrame && mRightGridAnnotationPosition != OutsideMapFrame
&& mTopGridAnnotationPosition != OutsideMapFrame && mBottomGridAnnotationPosition != OutsideMapFrame ) )
{
return 0;
}
Expand Down Expand Up @@ -1611,3 +1642,90 @@ QPointF QgsComposerMap::composerMapPosForItem( const QGraphicsItem* item ) const
double mapY = mapRendererExtent.yMaximum() - itemScenePos.y() / graphicsSceneRect.height() * mapRendererExtent.height();
return mapToItemCoords( QPointF( mapX, mapY ) );
}

void QgsComposerMap::setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border )
{
switch ( border )
{
case QgsComposerMap::Left:
mLeftGridAnnotationPosition = p;
break;
case QgsComposerMap::Right:
mRightGridAnnotationPosition = p;
break;
case QgsComposerMap::Top:
mTopGridAnnotationPosition = p;
break;
case QgsComposerMap::Bottom:
mBottomGridAnnotationPosition = p;
break;
default:
return;
}
updateBoundingRect();
update();
}

QgsComposerMap::GridAnnotationPosition QgsComposerMap::gridAnnotationPosition( QgsComposerMap::Border border ) const
{
switch ( border )
{
case QgsComposerMap::Left:
return mLeftGridAnnotationPosition;
break;
case QgsComposerMap::Right:
return mRightGridAnnotationPosition;
break;
case QgsComposerMap::Top:
return mTopGridAnnotationPosition;
break;
case QgsComposerMap::Bottom:
default:
return mBottomGridAnnotationPosition;
break;
}
}

void QgsComposerMap::setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border )
{
switch ( border )
{
case QgsComposerMap::Left:
mLeftGridAnnotationDirection = d;
break;
case QgsComposerMap::Right:
mRightGridAnnotationDirection = d;
break;
case QgsComposerMap::Top:
mTopGridAnnotationDirection = d;
break;
case QgsComposerMap::Bottom:
mBottomGridAnnotationDirection = d;
break;
default:
return;
break;
}
updateBoundingRect();
update();
}

QgsComposerMap::GridAnnotationDirection QgsComposerMap::gridAnnotationDirection( QgsComposerMap::Border border ) const
{
switch ( border )
{
case QgsComposerMap::Left:
return mLeftGridAnnotationDirection;
break;
case QgsComposerMap::Right:
return mRightGridAnnotationDirection;
break;
case QgsComposerMap::Top:
return mTopGridAnnotationDirection;
break;
case QgsComposerMap::Bottom:
default:
return mBottomGridAnnotationDirection;
break;
}
}
57 changes: 35 additions & 22 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
enum GridAnnotationPosition
{
InsideMapFrame = 0,
OutsideMapFrame
OutsideMapFrame,
Disabled
};

enum GridAnnotationDirection
Expand All @@ -77,6 +78,15 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
BoundaryDirection
};

/**Enum for different frame borders*/
enum Border
{
Left,
Right,
Bottom,
Top
};

/**This function is deprecated*/
void draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi );

Expand Down Expand Up @@ -227,20 +237,16 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setShowGridAnnotation( bool show ) {mShowGridAnnotation = show;}
bool showGridAnnotation() const {return mShowGridAnnotation;}

/**Sets position of grid annotations. Possibilities are inside or outside of the map frame
@note this function was added in version 1.4*/
void setGridAnnotationPosition( GridAnnotationPosition p ) {mGridAnnotationPosition = p;}
GridAnnotationPosition gridAnnotationPosition() const {return mGridAnnotationPosition;}
void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;

/**Sets distance between map frame and annotations
@note this function was added in version 1.4*/
void setAnnotationFrameDistance( double d ) {mAnnotationFrameDistance = d;}
double annotationFrameDistance() const {return mAnnotationFrameDistance;}

/**Sets grid annotation direction. Can be horizontal, vertical, direction of axis and horizontal and vertical
@note this function was added in version 1.4*/
void setGridAnnotationDirection( GridAnnotationDirection d ) {mGridAnnotationDirection = d;}
GridAnnotationDirection gridAnnotationDirection() const {return mGridAnnotationDirection;}
void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;

/**In case of annotations, the bounding rectangle can be larger than the map item rectangle
@note this function was added in version 1.4*/
Expand Down Expand Up @@ -279,15 +285,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem

private:

/**Enum for different frame borders*/
enum Border
{
Left,
Right,
Bottom,
Top
};

// Pointer to map renderer of the QGIS main map. Note that QgsComposerMap uses a different map renderer,
//it just copies some properties from the main map renderer.
QgsMapRenderer *mMapRenderer;
Expand Down Expand Up @@ -352,12 +349,28 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
int mGridAnnotationPrecision;
/**True if coordinate values should be drawn*/
bool mShowGridAnnotation;
/**Annotation position inside or outside of map frame*/
GridAnnotationPosition mGridAnnotationPosition;

/**Annotation position for left map side (inside / outside / not shown)*/
GridAnnotationPosition mLeftGridAnnotationPosition;
/**Annotation position for right map side (inside / outside / not shown)*/
GridAnnotationPosition mRightGridAnnotationPosition;
/**Annotation position for top map side (inside / outside / not shown)*/
GridAnnotationPosition mTopGridAnnotationPosition;
/**Annotation position for bottom map side (inside / outside / not shown)*/
GridAnnotationPosition mBottomGridAnnotationPosition;

/**Distance between map frame and annotation*/
double mAnnotationFrameDistance;
/**Annotation can be horizontal / vertical or different for axes*/
GridAnnotationDirection mGridAnnotationDirection;

/**Annotation direction on left side ( horizontal or vertical )*/
GridAnnotationDirection mLeftGridAnnotationDirection;
/**Annotation direction on right side ( horizontal or vertical )*/
GridAnnotationDirection mRightGridAnnotationDirection;
/**Annotation direction on top side ( horizontal or vertical )*/
GridAnnotationDirection mTopGridAnnotationDirection;
/**Annotation direction on bottom side ( horizontal or vertical )*/
GridAnnotationDirection mBottomGridAnnotationDirection;

/**Current bounding rectangle. This is used to check if notification to the graphics scene is necessary*/
QRectF mCurrentRectangle;
/**The length of the cross sides for mGridStyle Cross*/
Expand Down
56 changes: 56 additions & 0 deletions src/core/qgsprojectfiletransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,62 @@ void QgsProjectFileTransform::transform1800to1900()
rasterLayer.readXML( layerNode );
convertRasterProperties( mDom, layerNode, rasterPropertiesElem, &rasterLayer );
}

//composer: replace mGridAnnotationPosition with mLeftGridAnnotationPosition & co.
// and mGridAnnotationDirection with mLeftGridAnnotationDirection & co.
QDomNodeList composerMapList = mDom.elementsByTagName( "ComposerMap" );
for ( int i = 0; i < composerMapList.size(); ++i )
{
QDomNodeList gridList = composerMapList.at( i ).toElement().elementsByTagName( "Grid" );
for ( int j = 0; j < gridList.size(); ++j )
{
QDomNodeList annotationList = gridList.at( j ).toElement().elementsByTagName( "Annotation" );
for ( int k = 0; k < annotationList.size(); ++k )
{
QDomElement annotationElem = annotationList.at( k ).toElement();

//position
if ( annotationElem.hasAttribute( "position" ) )
{
int pos = annotationElem.attribute( "position" ).toInt();
annotationElem.setAttribute( "leftPosition", pos );
annotationElem.setAttribute( "rightPosition", pos );
annotationElem.setAttribute( "topPosition", pos );
annotationElem.setAttribute( "bottomPosition", pos );
annotationElem.removeAttribute( "position" );
}

//direction
if ( annotationElem.hasAttribute( "direction" ) )
{
int dir = annotationElem.attribute( "direction" ).toInt();
if ( dir == 2 )
{
annotationElem.setAttribute( "leftDirection", 0 );
annotationElem.setAttribute( "rightDirection", 0 );
annotationElem.setAttribute( "topDirection", 1 );
annotationElem.setAttribute( "bottomDirection", 1 );
}
else if ( dir == 3 )
{
annotationElem.setAttribute( "leftDirection", 1 );
annotationElem.setAttribute( "rightDirection", 1 );
annotationElem.setAttribute( "topDirection", 0 );
annotationElem.setAttribute( "bottomDirection", 0 );
}
else
{
annotationElem.setAttribute( "leftDirection", dir );
annotationElem.setAttribute( "rightDirection", dir );
annotationElem.setAttribute( "topDirection", dir );
annotationElem.setAttribute( "bottomDirection", dir );
}
annotationElem.removeAttribute( "direction" );
}
}
}
}

QgsDebugMsg( mDom.toString() );
}

Expand Down
482 changes: 284 additions & 198 deletions src/ui/qgscomposermapwidgetbase.ui

Large diffs are not rendered by default.