Skip to content

Commit b05e60f

Browse files
author
mhugent
committedOct 12, 2009
Possibility to have annotations horizontally or vertically in the composer grid
git-svn-id: http://svn.osgeo.org/qgis/trunk@11796 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 856c6c8 commit b05e60f

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed
 

‎src/app/composer/qgscomposermapwidget.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidget(), mComposerMap( composerMap )
2626
{
2727
setupUi( this );
28-
mAnnotationDirectionComboBox->setEnabled( false ); //not yet supported
2928

3029
//add widget for general composer item properties
3130
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, composerMap );

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ double QgsComposerItem::textWidthMillimeters( const QFont& font, const QString&
717717
double QgsComposerItem::fontAscentMillimeters( const QFont& font ) const
718718
{
719719
QFont metricsFont = scaledFontPixelSize( font );
720-
QFontMetrics fontMetrics( metricsFont );
720+
QFontMetricsF fontMetrics( metricsFont );
721721
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
722722
}
723723

‎src/core/composer/qgscomposermap.cpp

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -783,44 +783,104 @@ void QgsComposerMap::drawGridAnnotations( QPainter* p, const QList< QPair< doubl
783783
QString currentAnnotationString;
784784
double currentFontWidth = 0;
785785
double currentFontHeight = fontAscentMillimeters( mGridAnnotationFont );
786+
QPointF currentAnnotationPos1, currentAnnotationPos2;
787+
double rotation = 0;
786788

789+
//first draw annotations for vertical grid lines
790+
if ( mGridAnnotationDirection != Horizontal )
791+
{
792+
rotation = 270;
793+
}
787794
QList< QPair< double, QLineF > >::const_iterator vIt = vLines.constBegin();
788795
for ( ; vIt != vLines.constEnd(); ++vIt )
789796
{
790797
currentAnnotationString = QString::number( vIt->first );
791798
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );
792-
793-
if ( mGridAnnotationPosition == OutsideMapFrame )
799+
if ( mGridAnnotationDirection == Horizontal )
794800
{
795-
drawText( p, vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() - mAnnotationFrameDistance, currentAnnotationString, mGridAnnotationFont );
796-
drawText( p, vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontHeight, currentAnnotationString, mGridAnnotationFont );
801+
if ( mGridAnnotationPosition == OutsideMapFrame )
802+
{
803+
currentAnnotationPos1 = QPointF( vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() - mAnnotationFrameDistance );
804+
currentAnnotationPos2 = QPointF( vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontHeight );
805+
}
806+
else
807+
{
808+
currentAnnotationPos1 = QPointF( vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() + mAnnotationFrameDistance + currentFontHeight );
809+
currentAnnotationPos2 = QPointF( vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() - mAnnotationFrameDistance );
810+
}
797811
}
798-
else
812+
else //vertical annotation
799813
{
800-
drawText( p, vIt->second.x1() - currentFontWidth / 2.0, vIt->second.y1() + mAnnotationFrameDistance + currentFontHeight, currentAnnotationString, mGridAnnotationFont );
801-
drawText( p, vIt->second.x2() - currentFontWidth / 2.0, vIt->second.y2() - mAnnotationFrameDistance, currentAnnotationString, mGridAnnotationFont );
814+
if ( mGridAnnotationPosition == OutsideMapFrame )
815+
{
816+
currentAnnotationPos1 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y1() - mAnnotationFrameDistance );
817+
currentAnnotationPos2 = QPointF( vIt->second.x2() + currentFontHeight / 2.0, vIt->second.y2() + mAnnotationFrameDistance + currentFontWidth );
818+
}
819+
else
820+
{
821+
currentAnnotationPos1 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y1() + currentFontWidth + mAnnotationFrameDistance );
822+
currentAnnotationPos2 = QPointF( vIt->second.x1() + currentFontHeight / 2.0, vIt->second.y2() - mAnnotationFrameDistance );
823+
}
802824
}
825+
drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
826+
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
803827
}
804828

829+
//then annotations for horizontal grid lines
830+
if ( mGridAnnotationDirection != Vertical )
831+
{
832+
rotation = 0;
833+
}
834+
else
835+
{
836+
rotation = 270;
837+
}
805838
QList< QPair< double, QLineF > >::const_iterator hIt = hLines.constBegin();
806839
for ( ; hIt != hLines.constEnd(); ++hIt )
807840
{
808841
currentAnnotationString = QString::number( hIt->first );
809842
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );
810-
811-
if ( mGridAnnotationPosition == OutsideMapFrame )
843+
if ( mGridAnnotationDirection == Vertical )
812844
{
813-
drawText( p, hIt->second.x1() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y1() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
814-
drawText( p, hIt->second.x2() + mAnnotationFrameDistance, hIt->second.y2() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
845+
if ( mGridAnnotationPosition == OutsideMapFrame )
846+
{
847+
currentAnnotationPos1 = QPointF( hIt->second.x1() - mAnnotationFrameDistance, hIt->second.y1() + currentFontWidth / 2.0 );
848+
currentAnnotationPos2 = QPointF( hIt->second.x2() + mAnnotationFrameDistance + currentFontHeight, hIt->second.y2() + currentFontWidth / 2.0 );
849+
}
850+
else
851+
{
852+
currentAnnotationPos1 = QPointF( hIt->second.x1() + mAnnotationFrameDistance + currentFontHeight, hIt->second.y1() + currentFontWidth / 2.0 );
853+
currentAnnotationPos2 = QPointF( hIt->second.x2() - mAnnotationFrameDistance, hIt->second.y1() + currentFontWidth / 2.0 );
854+
}
815855
}
816856
else
817857
{
818-
drawText( p, hIt->second.x1() + mAnnotationFrameDistance, hIt->second.y1() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
819-
drawText( p, hIt->second.x2() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y2() + currentFontHeight / 2.0, currentAnnotationString, mGridAnnotationFont );
858+
if ( mGridAnnotationPosition == OutsideMapFrame )
859+
{
860+
currentAnnotationPos1 = QPointF( hIt->second.x1() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y1() + currentFontHeight / 2.0 );
861+
currentAnnotationPos2 = QPointF( hIt->second.x2() + mAnnotationFrameDistance, hIt->second.y2() + currentFontHeight / 2.0 );
862+
}
863+
else
864+
{
865+
currentAnnotationPos1 = QPointF( hIt->second.x1() + mAnnotationFrameDistance, hIt->second.y1() + currentFontHeight / 2.0 );
866+
currentAnnotationPos2 = QPointF( hIt->second.x2() - ( mAnnotationFrameDistance + currentFontWidth ), hIt->second.y2() + currentFontHeight / 2.0 );
867+
}
820868
}
869+
870+
drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
871+
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
821872
}
822873
}
823874

875+
void QgsComposerMap::drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText )
876+
{
877+
p->save();
878+
p->translate( pos );
879+
p->rotate( rotation );
880+
drawText( p, 0, 0, annotationText, mGridAnnotationFont );
881+
p->restore();
882+
}
883+
824884
int QgsComposerMap::verticalGridLines( QList< QPair< double, QLineF > >& lines ) const
825885
{
826886
lines.clear();

‎src/core/composer/qgscomposermap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
294294
void drawGrid( QPainter* p );
295295
/**Annotations for composer grid*/
296296
void drawGridAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
297+
/**Draws a single annotation
298+
@param p drawing painter
299+
@param pos item coordinates where to draw
300+
@param rotation text rotation
301+
@param the text to draw*/
302+
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText );
297303
/**Calculates the horizontal grid lines
298304
@lines list containing the map coordinates and the lines in item coordinates
299305
@return 0 in case of success*/

0 commit comments

Comments
 (0)
Please sign in to comment.