Skip to content

Commit 2032b3b

Browse files
author
mhugent
committed
Grid annotation can be with coordinates or 1A, 1B, ...
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11811 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 34f0f6f commit 2032b3b

File tree

5 files changed

+165
-41
lines changed

5 files changed

+165
-41
lines changed

src/app/composer/qgscomposermapwidget.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
5454
mAnnotationDirectionComboBox->insertItem( 0, tr( "Horizontal" ) );
5555
mAnnotationDirectionComboBox->insertItem( 1, tr( "Vertical" ) );
5656
mAnnotationDirectionComboBox->insertItem( 2, tr( "Horizontal and Vertical" ) );
57+
58+
mAnnotationTypeComboBox->insertItem( 0, tr( "Coordinate" ) );
59+
mAnnotationTypeComboBox->insertItem( 1, tr( "Sector" ) );
5760
blockAllSignals( false );
5861

5962
if ( composerMap )
@@ -340,6 +343,16 @@ void QgsComposerMapWidget::updateGuiElements()
340343
mAnnotationDirectionComboBox->setCurrentIndex( mAnnotationDirectionComboBox->findText( tr( "Horizontal and Vertical" ) ) );
341344
}
342345

346+
QgsComposerMap::GridAnnotationType type = mComposerMap->gridAnnotationType();
347+
if ( type == QgsComposerMap::Sector )
348+
{
349+
mAnnotationTypeComboBox->setCurrentIndex( mAnnotationTypeComboBox->findText( tr( "Sector" ) ) );
350+
}
351+
else
352+
{
353+
mAnnotationTypeComboBox->setCurrentIndex( mAnnotationTypeComboBox->findText( tr( "Coordinate" ) ) );
354+
}
355+
343356

344357
QPen gridPen = mComposerMap->gridPen();
345358
mLineWidthSpinBox->setValue( gridPen.widthF() );
@@ -397,6 +410,7 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
397410
mAnnotationPositionComboBox->blockSignals( b );
398411
mDistanceToMapFrameSpinBox->blockSignals( b );
399412
mAnnotationDirectionComboBox->blockSignals( b );
413+
mAnnotationTypeComboBox->blockSignals( b );
400414
}
401415

402416
void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()
@@ -639,3 +653,21 @@ void QgsComposerMapWidget::on_mAnnotationDirectionComboBox_currentIndexChanged(
639653
mComposerMap->updateBoundingRect();
640654
mComposerMap->update();
641655
}
656+
657+
void QgsComposerMapWidget::on_mAnnotationTypeComboBox_currentIndexChanged( const QString& text )
658+
{
659+
if ( !mComposerMap )
660+
{
661+
return;
662+
}
663+
664+
if ( text == tr( "Sector" ) )
665+
{
666+
mComposerMap->setGridAnnotationType( QgsComposerMap::Sector );
667+
}
668+
else
669+
{
670+
mComposerMap->setGridAnnotationType( QgsComposerMap::Coordinate );
671+
}
672+
mComposerMap->update();
673+
}

src/app/composer/qgscomposermapwidget.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
6161
void on_mAnnotationPositionComboBox_currentIndexChanged( const QString& text );
6262
void on_mDrawAnnotationCheckBox_stateChanged( int state );
6363
void on_mAnnotationDirectionComboBox_currentIndexChanged( const QString& text );
64+
void on_mAnnotationTypeComboBox_currentIndexChanged( const QString& text );
6465

6566
/**Updates width and height without notify the composer map (to avoid infinite recursion)*/
6667
void updateSettingsNoSignals();

src/core/composer/qgscomposermap.cpp

+94-24
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int QgsComposerMap::mCurrentComposerId = 0;
4444
QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height )
4545
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
4646
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mShowGridAnnotation( false ), \
47-
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal )
47+
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mGridAnnotationType( Coordinate )
4848
{
4949
mComposition = composition;
5050
mMapRenderer = mComposition->mapRenderer();
@@ -74,7 +74,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
7474
QgsComposerMap::QgsComposerMap( QgsComposition *composition )
7575
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mGridEnabled( false ), mGridStyle( Solid ), \
7676
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mShowGridAnnotation( false ), \
77-
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal )
77+
mGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mGridAnnotationDirection( Horizontal ), mGridAnnotationType( Coordinate )
7878
{
7979
//Offset
8080
mXOffset = 0.0;
@@ -568,6 +568,7 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
568568
annotationElem.setAttribute( "frameDistance", mAnnotationFrameDistance );
569569
annotationElem.setAttribute( "direction", mGridAnnotationDirection );
570570
annotationElem.setAttribute( "font", mGridAnnotationFont.toString() );
571+
annotationElem.setAttribute( "type", mGridAnnotationType);
571572

572573
gridElem.appendChild( annotationElem );
573574
composerMapElem.appendChild( gridElem );
@@ -668,6 +669,7 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
668669
mAnnotationFrameDistance = annotationElem.attribute( "frameDistance", "0" ).toDouble();
669670
mGridAnnotationDirection = QgsComposerMap::GridAnnotationDirection( annotationElem.attribute( "direction", "0" ).toInt() );
670671
mGridAnnotationFont.fromString( annotationElem.attribute( "font", "" ) );
672+
mGridAnnotationType = QgsComposerMap::GridAnnotationType( annotationElem.attribute( "type", "0" ).toInt() );
671673
}
672674
}
673675

@@ -785,45 +787,81 @@ void QgsComposerMap::drawGridAnnotations( QPainter* p, const QList< QPair< doubl
785787
double currentFontHeight = fontAscentMillimeters( mGridAnnotationFont );
786788
QPointF currentAnnotationPos1, currentAnnotationPos2;
787789
double rotation = 0;
790+
double xpos1, xpos2, ypos1, ypos2;
788791

789792
//first draw annotations for vertical grid lines
790793
if ( mGridAnnotationDirection != Horizontal )
791794
{
792795
rotation = 270;
793796
}
797+
798+
794799
QList< QPair< double, QLineF > >::const_iterator vIt = vLines.constBegin();
800+
int loopCounter = 0;
795801
for ( ; vIt != vLines.constEnd(); ++vIt )
796802
{
797-
currentAnnotationString = QString::number( vIt->first );
803+
if ( mGridAnnotationType == Sector )
804+
{
805+
int letterNumber = loopCounter % 26 + 66;
806+
currentAnnotationString = QString( QChar( letterNumber ) );
807+
}
808+
else
809+
{
810+
currentAnnotationString = QString::number( vIt->first );
811+
}
812+
798813
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );
799814
if ( mGridAnnotationDirection == Horizontal )
800815
{
816+
xpos1 = vIt->second.x1() - currentFontWidth / 2.0;
817+
xpos2 = vIt->second.x2() - currentFontWidth / 2.0;
801818
if ( mGridAnnotationPosition == OutsideMapFrame )
802819
{
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 );
820+
ypos1 = vIt->second.y1() - mAnnotationFrameDistance;
821+
ypos2 = vIt->second.y2() + mAnnotationFrameDistance + currentFontHeight;
805822
}
806823
else
807824
{
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 );
825+
ypos1 = vIt->second.y1() + mAnnotationFrameDistance + currentFontHeight;
826+
ypos2 = vIt->second.y2() - mAnnotationFrameDistance;
810827
}
811828
}
812829
else //vertical annotation
813830
{
831+
xpos1 = vIt->second.x1() + currentFontHeight / 2.0;
832+
xpos2 = vIt->second.x2() + currentFontHeight / 2.0;
814833
if ( mGridAnnotationPosition == OutsideMapFrame )
815834
{
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 );
835+
ypos1 = vIt->second.y1() - mAnnotationFrameDistance;
836+
ypos2 = vIt->second.y2() + mAnnotationFrameDistance + currentFontWidth;
818837
}
819838
else
820839
{
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 );
840+
ypos1 = vIt->second.y1() + currentFontWidth + mAnnotationFrameDistance;
841+
ypos2 = vIt->second.y2() - mAnnotationFrameDistance;
823842
}
824843
}
825-
drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
826-
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
844+
845+
//shift positions in case of sector annotation
846+
if ( mGridAnnotationType == Sector && loopCounter < ( vLines.size() - 1 ) )
847+
{
848+
xpos1 += ( vLines.at( loopCounter + 1 ).second.x1() - vLines.at( loopCounter ).second.x1() ) / 2.0;
849+
xpos2 += ( vLines.at( loopCounter + 1 ).second.x2() - vLines.at( loopCounter ).second.x2() ) / 2.0;
850+
}
851+
else if ( mGridAnnotationType == Sector && loopCounter == ( vLines.size() - 1 ) )
852+
{
853+
xpos1 += ( rect().width() - vLines.at( loopCounter ).second.x1() ) / 2.0;
854+
xpos2 += ( rect().width() - vLines.at( loopCounter ).second.x2() ) / 2.0;
855+
}
856+
drawAnnotation( p, QPointF( xpos1, ypos1 ), rotation, currentAnnotationString );
857+
drawAnnotation( p, QPointF( xpos1, ypos2 ), rotation, currentAnnotationString );
858+
859+
if ( mGridAnnotationType == Sector && loopCounter == 0 )
860+
{
861+
drawAnnotation( p, QPointF( vLines.at( loopCounter ).second.x1() / 2.0, ypos1 ), rotation, "A" );
862+
drawAnnotation( p, QPointF( vLines.at( loopCounter ).second.x2() / 2.0, ypos2 ), rotation, "A" );
863+
}
864+
++loopCounter;
827865
}
828866

829867
//then annotations for horizontal grid lines
@@ -835,40 +873,72 @@ void QgsComposerMap::drawGridAnnotations( QPainter* p, const QList< QPair< doubl
835873
{
836874
rotation = 270;
837875
}
876+
877+
loopCounter = 0;
838878
QList< QPair< double, QLineF > >::const_iterator hIt = hLines.constBegin();
839879
for ( ; hIt != hLines.constEnd(); ++hIt )
840880
{
841-
currentAnnotationString = QString::number( hIt->first );
881+
if ( mGridAnnotationType == Sector )
882+
{
883+
currentAnnotationString = QString::number( hLines.size() - loopCounter - 1 );
884+
}
885+
else
886+
{
887+
currentAnnotationString = QString::number( hIt->first );
888+
}
889+
842890
currentFontWidth = textWidthMillimeters( mGridAnnotationFont, currentAnnotationString );
843891
if ( mGridAnnotationDirection == Vertical )
844892
{
893+
ypos1 = hIt->second.y1() + currentFontWidth / 2.0;
894+
ypos2 = hIt->second.y2() + currentFontWidth / 2.0;
845895
if ( mGridAnnotationPosition == OutsideMapFrame )
846896
{
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 );
897+
xpos1 = hIt->second.x1() - mAnnotationFrameDistance;
898+
xpos2 = hIt->second.x2() + mAnnotationFrameDistance + currentFontHeight;
849899
}
850900
else
851901
{
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 );
902+
xpos1 = hIt->second.x1() + mAnnotationFrameDistance + currentFontHeight;
903+
xpos2 = hIt->second.x2() - mAnnotationFrameDistance;
854904
}
855905
}
856906
else
857907
{
908+
ypos1 = hIt->second.y1() + currentFontHeight / 2.0;
909+
ypos2 = hIt->second.y2() + currentFontHeight / 2.0;
858910
if ( mGridAnnotationPosition == OutsideMapFrame )
859911
{
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 );
912+
xpos1 = hIt->second.x1() - ( mAnnotationFrameDistance + currentFontWidth );
913+
xpos2 = hIt->second.x2() + mAnnotationFrameDistance;
862914
}
863915
else
864916
{
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 );
917+
xpos1 = hIt->second.x1() + mAnnotationFrameDistance;
918+
xpos2 = hIt->second.x2() - ( mAnnotationFrameDistance + currentFontWidth );
867919
}
868920
}
869921

870-
drawAnnotation( p, currentAnnotationPos1, rotation, currentAnnotationString );
871-
drawAnnotation( p, currentAnnotationPos2, rotation, currentAnnotationString );
922+
//shift y-Positions in case of sectoral annotations
923+
if ( mGridAnnotationType == Sector && loopCounter < ( hLines.size() - 1 ) )
924+
{
925+
ypos1 += ( hLines.at( loopCounter + 1 ).second.y1() - hLines.at( loopCounter ).second.y1() ) / 2.0;
926+
ypos2 += ( hLines.at( loopCounter + 1 ).second.y2() - hLines.at( loopCounter ).second.y2() ) / 2.0;
927+
}
928+
else if ( mGridAnnotationType == Sector && loopCounter == ( hLines.size() - 1 ) )
929+
{
930+
ypos1 -= hLines.at( loopCounter ).second.y1() / 2.0;
931+
ypos2 -= hLines.at( loopCounter ).second.y2() / 2.0;
932+
}
933+
934+
drawAnnotation( p, QPointF( xpos1, ypos1 ), rotation, currentAnnotationString );
935+
drawAnnotation( p, QPointF( xpos2, ypos2 ), rotation, currentAnnotationString );
936+
if ( mGridAnnotationType == Sector && loopCounter == 0 )
937+
{
938+
drawAnnotation( p, QPointF( xpos1, ( rect().height() + hLines.at( loopCounter ).second.y1() ) / 2.0 ), rotation, QString::number( hLines.size() ) );
939+
drawAnnotation( p, QPointF( xpos2, ( rect().height() + hLines.at( loopCounter ).second.y2() ) / 2.0 ), rotation, QString::number( hLines.size() ) );
940+
}
941+
++loopCounter;
872942
}
873943
}
874944

src/core/composer/qgscomposermap.h

+11
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
7373
HorizontalAndVertical
7474
};
7575

76+
enum GridAnnotationType
77+
{
78+
Coordinate = 0, //annotation at line, displays coordinates
79+
Sector //annotation at sector: 1, 2, 3 for horizontal lines and A, B, C for vertical ones
80+
};
81+
7682
/** \brief Draw to paint device
7783
@param extent map extent
7884
@param size size in scene coordinates
@@ -198,6 +204,9 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
198204
void setGridAnnotationDirection( GridAnnotationDirection d ) {mGridAnnotationDirection = d;}
199205
GridAnnotationDirection gridAnnotationDirection() const {return mGridAnnotationDirection;}
200206

207+
void setGridAnnotationType( GridAnnotationType t ) {mGridAnnotationType = t;}
208+
GridAnnotationType gridAnnotationType() const {return mGridAnnotationType; }
209+
201210
/**In case of annotations, the bounding rectangle can be larger than the map item rectangle*/
202211
QRectF boundingRect() const;
203212
/**Updates the bounding rect of this item. Call this function before doing any changes related to annotation out of the map rectangle*/
@@ -287,6 +296,8 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
287296
double mAnnotationFrameDistance;
288297
/**Annotation can be horizontal / vertical or different for axes*/
289298
GridAnnotationDirection mGridAnnotationDirection;
299+
/**Coordinate values (default) or sector (1A, 1B, ...)*/
300+
GridAnnotationType mGridAnnotationType;
290301
/**Current bounding rectangle. This is used to check if notification to the graphics scene is necessary*/
291302
QRectF mCurrentRectangle;
292303

0 commit comments

Comments
 (0)