Skip to content

Commit

Permalink
Possibility to have annotations horizontally or vertically in the com…
Browse files Browse the repository at this point in the history
…poser grid

git-svn-id: http://svn.osgeo.org/qgis/trunk@11796 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 12, 2009
1 parent 856c6c8 commit b05e60f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -25,7 +25,6 @@
QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidget(), mComposerMap( composerMap )
{
setupUi( this );
mAnnotationDirectionComboBox->setEnabled( false ); //not yet supported

//add widget for general composer item properties
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, composerMap );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposeritem.cpp
Expand Up @@ -717,7 +717,7 @@ double QgsComposerItem::textWidthMillimeters( const QFont& font, const QString&
double QgsComposerItem::fontAscentMillimeters( const QFont& font ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetrics fontMetrics( metricsFont );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
}

Expand Down
86 changes: 73 additions & 13 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -783,44 +783,104 @@ void QgsComposerMap::drawGridAnnotations( QPainter* p, const QList< QPair< doubl
QString currentAnnotationString;
double currentFontWidth = 0;
double currentFontHeight = fontAscentMillimeters( mGridAnnotationFont );
QPointF currentAnnotationPos1, currentAnnotationPos2;
double rotation = 0;

//first draw annotations for vertical grid lines
if ( mGridAnnotationDirection != Horizontal )
{
rotation = 270;
}
QList< QPair< double, QLineF > >::const_iterator vIt = vLines.constBegin();
for ( ; vIt != vLines.constEnd(); ++vIt )
{
currentAnnotationString = QString::number( vIt->first );
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );

if ( mGridAnnotationPosition == OutsideMapFrame )
if ( mGridAnnotationDirection == Horizontal )
{
drawText( p, vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() - mAnnotationFrameDistance, currentAnnotationString, mGridAnnotationFont );
drawText( p, vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontHeight, currentAnnotationString, mGridAnnotationFont );
if ( mGridAnnotationPosition == OutsideMapFrame )
{
currentAnnotationPos1 = QPointF( vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() - mAnnotationFrameDistance );
currentAnnotationPos2 = QPointF( vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontHeight );
}
else
{
currentAnnotationPos1 = QPointF( vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() + mAnnotationFrameDistance + currentFontHeight );
currentAnnotationPos2 = QPointF( vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() - mAnnotationFrameDistance );
}
}
else
else //vertical annotation
{
drawText( p, vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() + mAnnotationFrameDistance + currentFontHeight, currentAnnotationString, mGridAnnotationFont );
drawText( p, vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() - mAnnotationFrameDistance, currentAnnotationString, mGridAnnotationFont );
if ( mGridAnnotationPosition == OutsideMapFrame )
{
currentAnnotationPos1 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y1() - mAnnotationFrameDistance );
currentAnnotationPos2 = QPointF( vIt->second.x2() + currentFontHeight / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontWidth );
}
else
{
currentAnnotationPos1 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y1() + currentFontWidth + mAnnotationFrameDistance );
currentAnnotationPos2 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y2() - mAnnotationFrameDistance );
}
}
drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
}

//then annotations for horizontal grid lines
if ( mGridAnnotationDirection != Vertical )
{
rotation = 0;
}
else
{
rotation = 270;
}
QList< QPair< double, QLineF > >::const_iterator hIt = hLines.constBegin();
for ( ; hIt != hLines.constEnd(); ++hIt )
{
currentAnnotationString = QString::number( hIt->first );
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );

if ( mGridAnnotationPosition == OutsideMapFrame )
if ( mGridAnnotationDirection == Vertical )
{
drawText( p, hIt->second.x1() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y1() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
drawText( p, hIt->second.x2() + mAnnotationFrameDistance, hIt->second.y2() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
if ( mGridAnnotationPosition == OutsideMapFrame )
{
currentAnnotationPos1 = QPointF( hIt->second.x1() - mAnnotationFrameDistance, hIt->second.y1() + currentFontWidth / 2.0 );
currentAnnotationPos2 = QPointF( hIt->second.x2() + mAnnotationFrameDistance + currentFontHeight, hIt->second.y2() + currentFontWidth / 2.0 );
}
else
{
currentAnnotationPos1 = QPointF( hIt->second.x1() + mAnnotationFrameDistance + currentFontHeight, hIt->second.y1() + currentFontWidth / 2.0 );
currentAnnotationPos2 = QPointF( hIt->second.x2() - mAnnotationFrameDistance, hIt->second.y1() + currentFontWidth / 2.0 );
}
}
else
{
drawText( p, hIt->second.x1() + mAnnotationFrameDistance, hIt->second.y1() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
drawText( p, hIt->second.x2() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y2() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
if ( mGridAnnotationPosition == OutsideMapFrame )
{
currentAnnotationPos1 = QPointF( hIt->second.x1() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y1() + currentFontHeight / 2.0 );
currentAnnotationPos2 = QPointF( hIt->second.x2() + mAnnotationFrameDistance, hIt->second.y2() + currentFontHeight / 2.0 );
}
else
{
currentAnnotationPos1 = QPointF( hIt->second.x1() + mAnnotationFrameDistance, hIt->second.y1() + currentFontHeight / 2.0 );
currentAnnotationPos2 = QPointF( hIt->second.x2() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y2() + currentFontHeight / 2.0 );
}
}

drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
}
}

void QgsComposerMap::drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText )
{
p->save();
p->translate( pos );
p->rotate( rotation );
drawText( p, 0, 0, annotationText, mGridAnnotationFont );
p->restore();
}

int QgsComposerMap::verticalGridLines( QList< QPair< double, QLineF > >& lines ) const
{
lines.clear();
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -294,6 +294,12 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
void drawGrid( QPainter* p );
/**Annotations for composer grid*/
void drawGridAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
/**Draws a single annotation
@param p drawing painter
@param pos item coordinates where to draw
@param rotation text rotation
@param the text to draw*/
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText );
/**Calculates the horizontal grid lines
@lines list containing the map coordinates and the lines in item coordinates
@return 0 in case of success*/
Expand Down

0 comments on commit b05e60f

Please sign in to comment.