Skip to content

Commit a433d4c

Browse files
committed
[FEATURE][composer] Add options for controlling which sides of
the map a grid frame is drawn (sponsored by NIWA, New Zealand)
1 parent dc27e13 commit a433d4c

File tree

5 files changed

+253
-59
lines changed

5 files changed

+253
-59
lines changed

src/app/composer/qgscomposermapwidget.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,10 @@ void QgsComposerMapWidget::toggleFrameControls( bool frameEnabled )
706706
mFrameWidthLabel->setEnabled( frameEnabled );
707707
mFramePenLabel->setEnabled( frameEnabled );
708708
mFrameFillLabel->setEnabled( frameEnabled );
709+
mCheckGridLeftSide->setEnabled( frameEnabled );
710+
mCheckGridRightSide->setEnabled( frameEnabled );
711+
mCheckGridTopSide->setEnabled( frameEnabled );
712+
mCheckGridBottomSide->setEnabled( frameEnabled );
709713
}
710714

711715
void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()
@@ -1109,6 +1113,10 @@ void QgsComposerMapWidget::blockGridItemsSignals( bool block )
11091113
mGridFrameFill1ColorButton->blockSignals( block );
11101114
mGridFrameFill2ColorButton->blockSignals( block );
11111115
mGridBlendComboBox->blockSignals( block );
1116+
mCheckGridLeftSide->blockSignals( block );
1117+
mCheckGridRightSide->blockSignals( block );
1118+
mCheckGridTopSide->blockSignals( block );
1119+
mCheckGridBottomSide->blockSignals( block );
11121120

11131121
//grid annotation
11141122
mDrawAnnotationGroupBox->blockSignals( block );
@@ -1212,6 +1220,11 @@ void QgsComposerMapWidget::setGridItems( const QgsComposerMapGrid* grid )
12121220
toggleFrameControls( false );
12131221
}
12141222

1223+
mCheckGridLeftSide->setChecked( grid->testGridFrameSideFlag( QgsComposerMapGrid::FrameLeft ) );
1224+
mCheckGridRightSide->setChecked( grid->testGridFrameSideFlag( QgsComposerMapGrid::FrameRight ) );
1225+
mCheckGridTopSide->setChecked( grid->testGridFrameSideFlag( QgsComposerMapGrid::FrameTop ) );
1226+
mCheckGridBottomSide->setChecked( grid->testGridFrameSideFlag( QgsComposerMapGrid::FrameBottom ) );
1227+
12151228
//line style
12161229
updateGridLineSymbolMarker( grid );
12171230
//marker style
@@ -1419,6 +1432,62 @@ void QgsComposerMapWidget::on_mFrameWidthSpinBox_valueChanged( double val )
14191432
mComposerMap->endCommand();
14201433
}
14211434

1435+
void QgsComposerMapWidget::on_mCheckGridLeftSide_toggled( bool checked )
1436+
{
1437+
QgsComposerMapGrid* grid = currentGrid();
1438+
if ( !grid )
1439+
{
1440+
return;
1441+
}
1442+
1443+
mComposerMap->beginCommand( tr( "Frame left side changed" ) );
1444+
grid->setGridFrameSideFlag( QgsComposerMapGrid::FrameLeft, checked );
1445+
mComposerMap->update();
1446+
mComposerMap->endCommand();
1447+
}
1448+
1449+
void QgsComposerMapWidget::on_mCheckGridRightSide_toggled( bool checked )
1450+
{
1451+
QgsComposerMapGrid* grid = currentGrid();
1452+
if ( !grid )
1453+
{
1454+
return;
1455+
}
1456+
1457+
mComposerMap->beginCommand( tr( "Frame right side changed" ) );
1458+
grid->setGridFrameSideFlag( QgsComposerMapGrid::FrameRight, checked );
1459+
mComposerMap->update();
1460+
mComposerMap->endCommand();
1461+
}
1462+
1463+
void QgsComposerMapWidget::on_mCheckGridTopSide_toggled( bool checked )
1464+
{
1465+
QgsComposerMapGrid* grid = currentGrid();
1466+
if ( !grid )
1467+
{
1468+
return;
1469+
}
1470+
1471+
mComposerMap->beginCommand( tr( "Frame top side changed" ) );
1472+
grid->setGridFrameSideFlag( QgsComposerMapGrid::FrameTop, checked );
1473+
mComposerMap->update();
1474+
mComposerMap->endCommand();
1475+
}
1476+
1477+
void QgsComposerMapWidget::on_mCheckGridBottomSide_toggled( bool checked )
1478+
{
1479+
QgsComposerMapGrid* grid = currentGrid();
1480+
if ( !grid )
1481+
{
1482+
return;
1483+
}
1484+
1485+
mComposerMap->beginCommand( tr( "Frame bottom side changed" ) );
1486+
grid->setGridFrameSideFlag( QgsComposerMapGrid::FrameBottom, checked );
1487+
mComposerMap->update();
1488+
mComposerMap->endCommand();
1489+
}
1490+
14221491
void QgsComposerMapWidget::on_mGridFramePenSizeSpinBox_valueChanged( double d )
14231492
{
14241493
QgsComposerMapGrid* grid = currentGrid();

src/app/composer/qgscomposermapwidget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class QgsComposerMapWidget: public QgsComposerItemBaseWidget, private Ui::QgsCom
9292
void on_mMapGridCRSButton_clicked();
9393
void on_mMapGridUnitComboBox_currentIndexChanged( const QString& text );
9494
void on_mGridBlendComboBox_currentIndexChanged( int index );
95+
void on_mCheckGridLeftSide_toggled( bool checked );
96+
void on_mCheckGridRightSide_toggled( bool checked );
97+
void on_mCheckGridTopSide_toggled( bool checked );
98+
void on_mCheckGridBottomSide_toggled( bool checked );
9599

96100
void on_mDrawAnnotationGroupBox_toggled( bool state );
97101
//annotation position

src/core/composer/qgscomposermapgrid.cpp

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ QgsComposerMapGrid::QgsComposerMapGrid( const QString& name, QgsComposerMap* map
5555
mBottomGridAnnotationDirection( QgsComposerMap::Horizontal ),
5656
mGridAnnotationFormat( QgsComposerMap::Decimal ),
5757
mGridFrameStyle( QgsComposerMap::NoGridFrame ),
58+
mGridFrameSides( QgsComposerMapGrid::FrameLeft | QgsComposerMapGrid::FrameRight |
59+
QgsComposerMapGrid::FrameTop | QgsComposerMapGrid::FrameBottom ),
5860
mGridFrameWidth( 2.0 ),
5961
mGridFramePenThickness( 0.5 ),
6062
mGridFramePenColor( QColor( 0, 0, 0 ) ),
@@ -176,6 +178,7 @@ bool QgsComposerMapGrid::writeXML( QDomElement& elem, QDomDocument& doc ) const
176178
mapGridElem.appendChild( markerStyleElem );
177179

178180
mapGridElem.setAttribute( "gridFrameStyle", mGridFrameStyle );
181+
mapGridElem.setAttribute( "gridFrameSideFlags", mGridFrameSides );
179182
mapGridElem.setAttribute( "gridFrameWidth", qgsDoubleToString( mGridFrameWidth ) );
180183
mapGridElem.setAttribute( "gridFramePenThickness", qgsDoubleToString( mGridFramePenThickness ) );
181184
mapGridElem.setAttribute( "gridFramePenColor", QgsSymbolLayerV2Utils::encodeColor( mGridFramePenColor ) );
@@ -227,6 +230,7 @@ bool QgsComposerMapGrid::readXML( const QDomElement& itemElem, const QDomDocumen
227230
mGridOffsetY = itemElem.attribute( "offsetY", "0" ).toDouble();
228231
mCrossLength = itemElem.attribute( "crossLength", "3" ).toDouble();
229232
mGridFrameStyle = ( QgsComposerMap::GridFrameStyle )itemElem.attribute( "gridFrameStyle", "0" ).toInt();
233+
mGridFrameSides = ( QgsComposerMapGrid::GridFrameSideFlags )itemElem.attribute( "gridFrameSideFlags", "15" ).toInt();
230234
mGridFrameWidth = itemElem.attribute( "gridFrameWidth", "2.0" ).toDouble();
231235
mGridFramePenThickness = itemElem.attribute( "gridFramePenThickness", "0.5" ).toDouble();
232236
mGridFramePenColor = QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "gridFramePenColor", "0,0,0" ) );
@@ -531,11 +535,22 @@ void QgsComposerMapGrid::drawGridFrame( QPainter* p, const QList< QPair< double,
531535

532536
sortGridLinesOnBorders( hLines, vLines, leftGridFrame, rightGridFrame, topGridFrame, bottomGridFrame );
533537

534-
drawGridFrameBorder( p, leftGridFrame, QgsComposerMap::Left );
535-
drawGridFrameBorder( p, rightGridFrame, QgsComposerMap::Right );
536-
drawGridFrameBorder( p, topGridFrame, QgsComposerMap::Top );
537-
drawGridFrameBorder( p, bottomGridFrame, QgsComposerMap::Bottom );
538-
538+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameLeft ) )
539+
{
540+
drawGridFrameBorder( p, leftGridFrame, QgsComposerMap::Left );
541+
}
542+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameRight ) )
543+
{
544+
drawGridFrameBorder( p, rightGridFrame, QgsComposerMap::Right );
545+
}
546+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameTop ) )
547+
{
548+
drawGridFrameBorder( p, topGridFrame, QgsComposerMap::Top );
549+
}
550+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameBottom ) )
551+
{
552+
drawGridFrameBorder( p, bottomGridFrame, QgsComposerMap::Bottom );
553+
}
539554
p->restore();
540555
}
541556

@@ -577,24 +592,40 @@ void QgsComposerMapGrid::drawGridFrameBorder( QPainter* p, const QMap< double, d
577592
return;
578593
}
579594

580-
double currentCoord = - mGridFrameWidth;
595+
QMap< double, double > pos = borderPos;
596+
597+
double currentCoord = 0;
598+
if (( border == QgsComposerMap::Left || border == QgsComposerMap::Right ) && testGridFrameSideFlag( QgsComposerMapGrid::FrameTop ) )
599+
{
600+
currentCoord = - mGridFrameWidth;
601+
pos.insert( 0, 0 );
602+
}
603+
else if (( border == QgsComposerMap::Top || border == QgsComposerMap::Bottom ) && testGridFrameSideFlag( QgsComposerMapGrid::FrameLeft ) )
604+
{
605+
currentCoord = - mGridFrameWidth;
606+
pos.insert( 0, 0 );
607+
}
581608
bool color1 = true;
582609
double x = 0;
583610
double y = 0;
584611
double width = 0;
585612
double height = 0;
586613

587-
QMap< double, double > pos = borderPos;
588-
pos.insert( 0, 0 );
589614
if ( border == QgsComposerMap::Left || border == QgsComposerMap::Right )
590615
{
591616
pos.insert( mComposerMap->rect().height(), mComposerMap->rect().height() );
592-
pos.insert( mComposerMap->rect().height() + mGridFrameWidth, mComposerMap->rect().height() + mGridFrameWidth );
617+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameBottom ) )
618+
{
619+
pos.insert( mComposerMap->rect().height() + mGridFrameWidth, mComposerMap->rect().height() + mGridFrameWidth );
620+
}
593621
}
594-
else //top or bottom
622+
else if ( border == QgsComposerMap::Top || border == QgsComposerMap::Bottom )
595623
{
596624
pos.insert( mComposerMap->rect().width(), mComposerMap->rect().width() );
597-
pos.insert( mComposerMap->rect().width() + mGridFrameWidth, mComposerMap->rect().width() + mGridFrameWidth );
625+
if ( testGridFrameSideFlag( QgsComposerMapGrid::FrameRight ) )
626+
{
627+
pos.insert( mComposerMap->rect().width() + mGridFrameWidth, mComposerMap->rect().width() + mGridFrameWidth );
628+
}
598629
}
599630

600631
//set pen to current frame pen
@@ -667,10 +698,14 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
667698
double ypos = pos.y();
668699
int rotation = 0;
669700

670-
double gridFrameDistance = ( mGridFrameStyle == QgsComposerMap::NoGridFrame ) ? 0 : mGridFrameWidth;
701+
double gridFrameDistance = ( mGridFrameStyle == QgsComposerMap::NoGridFrame ) ? 0 : mGridFrameWidth + ( mGridFramePenThickness / 2.0 );
671702

672703
if ( frameBorder == QgsComposerMap::Left )
673704
{
705+
if ( !testGridFrameSideFlag( QgsComposerMapGrid::FrameLeft ) )
706+
{
707+
gridFrameDistance = 0;
708+
}
674709

675710
if ( mLeftGridAnnotationPosition == QgsComposerMap::InsideMapFrame )
676711
{
@@ -708,6 +743,11 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
708743
}
709744
else if ( frameBorder == QgsComposerMap::Right )
710745
{
746+
if ( !testGridFrameSideFlag( QgsComposerMapGrid::FrameRight ) )
747+
{
748+
gridFrameDistance = 0;
749+
}
750+
711751
if ( mRightGridAnnotationPosition == QgsComposerMap::InsideMapFrame )
712752
{
713753
if ( mRightGridAnnotationDirection == QgsComposerMap::Vertical || mRightGridAnnotationDirection == QgsComposerMap::BoundaryDirection )
@@ -743,6 +783,11 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
743783
}
744784
else if ( frameBorder == QgsComposerMap::Bottom )
745785
{
786+
if ( !testGridFrameSideFlag( QgsComposerMapGrid::FrameBottom ) )
787+
{
788+
gridFrameDistance = 0;
789+
}
790+
746791
if ( mBottomGridAnnotationPosition == QgsComposerMap::InsideMapFrame )
747792
{
748793
if ( mBottomGridAnnotationDirection == QgsComposerMap::Horizontal || mBottomGridAnnotationDirection == QgsComposerMap::BoundaryDirection )
@@ -778,6 +823,11 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
778823
}
779824
else //Top
780825
{
826+
if ( !testGridFrameSideFlag( QgsComposerMapGrid::FrameTop ) )
827+
{
828+
gridFrameDistance = 0;
829+
}
830+
781831
if ( mTopGridAnnotationPosition == QgsComposerMap::InsideMapFrame )
782832
{
783833
if ( mTopGridAnnotationDirection == QgsComposerMap::Horizontal || mTopGridAnnotationDirection == QgsComposerMap::BoundaryDirection )
@@ -1317,6 +1367,29 @@ QgsComposerMap::GridAnnotationDirection QgsComposerMapGrid::gridAnnotationDirect
13171367
return mLeftGridAnnotationDirection;
13181368
}
13191369

1370+
void QgsComposerMapGrid::setGridFrameSideFlags( GridFrameSideFlags flags )
1371+
{
1372+
mGridFrameSides = flags;
1373+
}
1374+
1375+
void QgsComposerMapGrid::setGridFrameSideFlag( QgsComposerMapGrid::GridFrameSideFlag flag, bool on )
1376+
{
1377+
if ( on )
1378+
mGridFrameSides |= flag;
1379+
else
1380+
mGridFrameSides &= ~flag;
1381+
}
1382+
1383+
QgsComposerMapGrid::GridFrameSideFlags QgsComposerMapGrid::gridFrameSideFlags() const
1384+
{
1385+
return mGridFrameSides;
1386+
}
1387+
1388+
bool QgsComposerMapGrid::testGridFrameSideFlag( QgsComposerMapGrid::GridFrameSideFlag flag ) const
1389+
{
1390+
return mGridFrameSides.testFlag( flag );
1391+
}
1392+
13201393
void QgsComposerMapGrid::setGridAnnotationDirection( QgsComposerMap::GridAnnotationDirection d )
13211394
{
13221395
mLeftGridAnnotationDirection = d;

src/core/composer/qgscomposermapgrid.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ class CORE_EXPORT QgsComposerMapGrid
152152
void setGridFrameStyle( QgsComposerMap::GridFrameStyle style ) { mGridFrameStyle = style; }
153153
QgsComposerMap::GridFrameStyle gridFrameStyle() const { return mGridFrameStyle; }
154154

155+
//! Enumeration of flags that adjust which side of the map the frame is drawn on
156+
enum GridFrameSideFlag
157+
{
158+
FrameLeft = 0x01,
159+
FrameRight = 0x02,
160+
FrameTop = 0x04,
161+
FrameBottom = 0x08
162+
};
163+
Q_DECLARE_FLAGS( GridFrameSideFlags, GridFrameSideFlag )
164+
165+
void setGridFrameSideFlags( GridFrameSideFlags flags );
166+
void setGridFrameSideFlag( GridFrameSideFlag flag, bool on = true );
167+
GridFrameSideFlags gridFrameSideFlags() const;
168+
bool testGridFrameSideFlag( GridFrameSideFlag flag ) const;
169+
155170
/**Set grid frame width
156171
@note: this function was added in version 1.9*/
157172
void setGridFrameWidth( double w ) { mGridFrameWidth = w; }
@@ -257,6 +272,7 @@ class CORE_EXPORT QgsComposerMapGrid
257272
QgsComposerMap::GridAnnotationDirection mBottomGridAnnotationDirection;
258273
QgsComposerMap::GridAnnotationFormat mGridAnnotationFormat;
259274
QgsComposerMap::GridFrameStyle mGridFrameStyle;
275+
GridFrameSideFlags mGridFrameSides;
260276
double mGridFrameWidth;
261277
double mGridFramePenThickness;
262278
QColor mGridFramePenColor;

0 commit comments

Comments
 (0)