Skip to content

Commit 4b3e817

Browse files
committed
[FEATURE]: Zebra style for composer grid (black/white pattern at borders)
1 parent a8ae49a commit 4b3e817

File tree

5 files changed

+250
-49
lines changed

5 files changed

+250
-49
lines changed

src/app/composer/qgscomposermapwidget.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
5656
insertAnnotationDirectionEntries( mAnnotationDirectionComboBoxTop );
5757
insertAnnotationDirectionEntries( mAnnotationDirectionComboBoxBottom );
5858

59+
mFrameStyleComboBox->insertItem( 0, tr( "No frame" ) );
60+
mFrameStyleComboBox->insertItem( 1, tr( "Zebra" ) );
61+
5962
if ( composerMap )
6063
{
6164
connect( composerMap, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
@@ -442,6 +445,8 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
442445
mAnnotationDirectionComboBoxBottom->blockSignals( b );
443446
mCoordinatePrecisionSpinBox->blockSignals( b );
444447
mDrawCanvasItemsCheckBox->blockSignals( b );
448+
mFrameStyleComboBox->blockSignals( b );
449+
mFrameWidthSpinBox->blockSignals( b );
445450
}
446451

447452
void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()
@@ -749,6 +754,39 @@ void QgsComposerMapWidget::on_mCoordinatePrecisionSpinBox_valueChanged( int valu
749754
mComposerMap->endCommand();
750755
}
751756

757+
void QgsComposerMapWidget::on_mFrameStyleComboBox_currentIndexChanged( const QString& text )
758+
{
759+
if ( !mComposerMap )
760+
{
761+
return;
762+
}
763+
764+
mComposerMap->beginCommand( tr( "Changed grid frame style" ) );
765+
if ( text == tr( "Zebra" ) )
766+
{
767+
mComposerMap->setGridFrameStyle( QgsComposerMap::Zebra );
768+
}
769+
else //no frame
770+
{
771+
mComposerMap->setGridFrameStyle( QgsComposerMap::NoGridFrame );
772+
}
773+
mComposerMap->updateBoundingRect();
774+
mComposerMap->update();
775+
mComposerMap->endCommand();
776+
}
777+
778+
void QgsComposerMapWidget::on_mFrameWidthSpinBox_valueChanged( double d )
779+
{
780+
if ( mComposerMap )
781+
{
782+
mComposerMap->beginCommand( tr( "Changed grid frame width" ) );
783+
mComposerMap->setGridFrameWidth( d );
784+
mComposerMap->updateBoundingRect();
785+
mComposerMap->update();
786+
mComposerMap->endCommand();
787+
}
788+
}
789+
752790
void QgsComposerMapWidget::insertAnnotationPositionEntries( QComboBox* c )
753791
{
754792
c->insertItem( 0, tr( "Inside frame" ) );

src/app/composer/qgscomposermapwidget.h

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
7676
void on_mDrawAnnotationCheckBox_stateChanged( int state );
7777
void on_mCoordinatePrecisionSpinBox_valueChanged( int value );
7878

79+
void on_mFrameStyleComboBox_currentIndexChanged( const QString& text );
80+
void on_mFrameWidthSpinBox_valueChanged( double d );
81+
7982
private slots:
8083

8184
/**Sets the GUI elements to the values of mPicture*/

src/core/composer/qgscomposermap.cpp

+124-11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
4242
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
4343
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
4444
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
45-
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ),
45+
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
4646
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
4747
{
4848
mComposition = composition;
@@ -89,7 +89,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
8989
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
9090
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
9191
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
92-
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mCrossLength( 3 ),
92+
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
9393
mMapCanvas( 0 ), mDrawCanvasItems( true )
9494
{
9595
//Offset
@@ -960,12 +960,79 @@ void QgsComposerMap::drawGrid( QPainter* p )
960960

961961
p->setClipRect( thisPaintRect , Qt::NoClip );
962962

963+
if ( mGridFrameStyle != QgsComposerMap::NoGridFrame )
964+
{
965+
drawGridFrame( p, horizontalLines, verticalLines );
966+
}
967+
963968
if ( mShowGridAnnotation )
964969
{
965970
drawCoordinateAnnotations( p, horizontalLines, verticalLines );
966971
}
967972
}
968973

974+
void QgsComposerMap::drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines )
975+
{
976+
//Sort the coordinate positions for each side
977+
QMap< double, double > leftGridFrame;
978+
QMap< double, double > rightGridFrame;
979+
QMap< double, double > topGridFrame;
980+
QMap< double, double > bottomGridFrame;
981+
982+
sortGridLinesOnBorders( hLines, vLines, leftGridFrame, rightGridFrame, topGridFrame, bottomGridFrame );
983+
984+
drawGridFrameBorder( p, leftGridFrame, QgsComposerMap::Left );
985+
drawGridFrameBorder( p, rightGridFrame, QgsComposerMap::Right );
986+
drawGridFrameBorder( p, topGridFrame, QgsComposerMap::Top );
987+
drawGridFrameBorder( p, bottomGridFrame, QgsComposerMap::Bottom );
988+
}
989+
990+
void QgsComposerMap::drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border )
991+
{
992+
double currentCoord = - mGridFrameWidth;
993+
bool white = true;
994+
double x = 0;
995+
double y = 0;
996+
double width = 0;
997+
double height = 0;
998+
999+
QMap< double, double > pos = borderPos;
1000+
pos.insert( 0, 0 );
1001+
if ( border == Left || border == Right )
1002+
{
1003+
pos.insert( rect().height(), rect().height() );
1004+
pos.insert( rect().height() + mGridFrameWidth, rect().height() + mGridFrameWidth );
1005+
}
1006+
else //top or bottom
1007+
{
1008+
pos.insert( rect().width(), rect().width() );
1009+
pos.insert( rect().width() + mGridFrameWidth, rect().width() + mGridFrameWidth );
1010+
}
1011+
1012+
QMap< double, double >::const_iterator posIt = pos.constBegin();
1013+
for ( ; posIt != pos.constEnd(); ++posIt )
1014+
{
1015+
p->setBrush( QBrush( white ? Qt::white : Qt::black ) );
1016+
if ( border == Left || border == Right )
1017+
{
1018+
height = posIt.key() - currentCoord;
1019+
width = mGridFrameWidth;
1020+
x = ( border == Left ) ? -mGridFrameWidth : rect().width();
1021+
y = currentCoord;
1022+
}
1023+
else //top or bottom
1024+
{
1025+
height = mGridFrameWidth;
1026+
width = posIt.key() - currentCoord;
1027+
x = currentCoord;
1028+
y = ( border == Top ) ? -mGridFrameWidth : rect().height();
1029+
}
1030+
p->drawRect( QRectF( x, y, width, height ) );
1031+
currentCoord = posIt.key();
1032+
white = !white;
1033+
}
1034+
}
1035+
9691036
void QgsComposerMap::drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines )
9701037
{
9711038
if ( !p )
@@ -1002,6 +1069,8 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
10021069
double ypos = pos.y();
10031070
int rotation = 0;
10041071

1072+
double gridFrameDistance = ( mGridFrameStyle == NoGridFrame ) ? 0 : mGridFrameWidth;
1073+
10051074
if ( frameBorder == Left )
10061075
{
10071076

@@ -1023,13 +1092,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
10231092
{
10241093
if ( mLeftGridAnnotationDirection == Vertical || mLeftGridAnnotationDirection == BoundaryDirection )
10251094
{
1026-
xpos -= mAnnotationFrameDistance;
1095+
xpos -= ( mAnnotationFrameDistance + gridFrameDistance );
10271096
ypos += textWidth / 2.0;
10281097
rotation = 270;
10291098
}
10301099
else
10311100
{
1032-
xpos -= textWidth + mAnnotationFrameDistance;
1101+
xpos -= ( textWidth + mAnnotationFrameDistance + gridFrameDistance );
10331102
ypos += textHeight / 2.0;
10341103
}
10351104
}
@@ -1059,13 +1128,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
10591128
{
10601129
if ( mRightGridAnnotationDirection == Vertical || mRightGridAnnotationDirection == BoundaryDirection )
10611130
{
1062-
xpos += textHeight + mAnnotationFrameDistance;
1131+
xpos += ( textHeight + mAnnotationFrameDistance + gridFrameDistance );
10631132
ypos += textWidth / 2.0;
10641133
rotation = 270;
10651134
}
10661135
else //Horizontal
10671136
{
1068-
xpos += mAnnotationFrameDistance;
1137+
xpos += ( mAnnotationFrameDistance + gridFrameDistance );
10691138
ypos += textHeight / 2.0;
10701139
}
10711140
}
@@ -1094,13 +1163,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
10941163
{
10951164
if ( mBottomGridAnnotationDirection == Horizontal || mBottomGridAnnotationDirection == BoundaryDirection )
10961165
{
1097-
ypos += mAnnotationFrameDistance + textHeight;
1166+
ypos += ( mAnnotationFrameDistance + textHeight + gridFrameDistance );
10981167
xpos -= textWidth / 2.0;
10991168
}
11001169
else //Vertical
11011170
{
11021171
xpos += textHeight / 2.0;
1103-
ypos += textWidth + mAnnotationFrameDistance;
1172+
ypos += ( textWidth + mAnnotationFrameDistance + gridFrameDistance );
11041173
rotation = 270;
11051174
}
11061175
}
@@ -1130,12 +1199,12 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
11301199
if ( mTopGridAnnotationDirection == Horizontal || mTopGridAnnotationDirection == BoundaryDirection )
11311200
{
11321201
xpos -= textWidth / 2.0;
1133-
ypos -= mAnnotationFrameDistance;
1202+
ypos -= ( mAnnotationFrameDistance + gridFrameDistance );
11341203
}
11351204
else //Vertical
11361205
{
11371206
xpos += textHeight / 2.0;
1138-
ypos -= mAnnotationFrameDistance;
1207+
ypos -= ( mAnnotationFrameDistance + gridFrameDistance );
11391208
rotation = 270;
11401209
}
11411210
}
@@ -1389,7 +1458,9 @@ double QgsComposerMap::maxExtension() const
13891458
maxExtension = qMax( maxExtension, currentExtension );
13901459
}
13911460

1392-
return maxExtension + mAnnotationFrameDistance;
1461+
//grid frame
1462+
double gridFrameDist = ( mGridFrameStyle == NoGridFrame ) ? 0 : mGridFrameWidth;
1463+
return maxExtension + mAnnotationFrameDistance + gridFrameDist;
13931464
}
13941465

13951466
void QgsComposerMap::mapPolygon( QPolygonF& poly ) const
@@ -1729,3 +1800,45 @@ QgsComposerMap::GridAnnotationDirection QgsComposerMap::gridAnnotationDirection(
17291800
break;
17301801
}
17311802
}
1803+
1804+
void QgsComposerMap::sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
1805+
QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const
1806+
{
1807+
QList< QPair< double, QPointF > > borderPositions;
1808+
QList< QPair< double, QLineF > >::const_iterator it = hLines.constBegin();
1809+
for ( ; it != hLines.constEnd(); ++it )
1810+
{
1811+
borderPositions << qMakePair( it->first, it->second.p1() );
1812+
borderPositions << qMakePair( it->first, it->second.p2() );
1813+
}
1814+
it = vLines.constBegin();
1815+
for ( ; it != vLines.constEnd(); ++it )
1816+
{
1817+
borderPositions << qMakePair( it->first, it->second.p1() );
1818+
borderPositions << qMakePair( it->first, it->second.p2() );
1819+
}
1820+
1821+
QList< QPair< double, QPointF > >::const_iterator bIt = borderPositions.constBegin();
1822+
for ( ; bIt != borderPositions.constEnd(); ++bIt )
1823+
{
1824+
Border frameBorder = borderForLineCoord( bIt->second );
1825+
if ( frameBorder == QgsComposerMap::Left )
1826+
{
1827+
leftFrameEntries.insert( bIt->second.y(), bIt->first );
1828+
}
1829+
else if ( frameBorder == QgsComposerMap::Right )
1830+
{
1831+
rightFrameEntries.insert( bIt->second.y(), bIt->first );
1832+
}
1833+
else if ( frameBorder == QgsComposerMap::Top )
1834+
{
1835+
topFrameEntries.insert( bIt->second.x(), bIt->first );
1836+
}
1837+
else //Bottom
1838+
{
1839+
bottomFrameEntries.insert( bIt->second.x(), bIt->first );
1840+
}
1841+
}
1842+
}
1843+
1844+

src/core/composer/qgscomposermap.h

+23
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
7878
BoundaryDirection
7979
};
8080

81+
enum GridFrameStyle
82+
{
83+
NoGridFrame = 0,
84+
Zebra //black / white pattern
85+
};
86+
8187
/**Enum for different frame borders*/
8288
enum Border
8389
{
@@ -248,6 +254,16 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
248254
void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
249255
GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
250256

257+
/**Set grid frame style (NoGridFrame or Zebra)
258+
@note: this function was added in version 1.9*/
259+
void setGridFrameStyle( GridFrameStyle style ) { mGridFrameStyle = style; }
260+
GridFrameStyle gridFrameStyle() const { return mGridFrameStyle; }
261+
262+
/**Set grid frame width
263+
@note: this function was added in version 1.9*/
264+
void setGridFrameWidth( double w ) { mGridFrameWidth = w; }
265+
double gridFrameWidth() const { return mGridFrameWidth; }
266+
251267
/**In case of annotations, the bounding rectangle can be larger than the map item rectangle
252268
@note this function was added in version 1.4*/
253269
QRectF boundingRect() const;
@@ -371,6 +387,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
371387
/**Annotation direction on bottom side ( horizontal or vertical )*/
372388
GridAnnotationDirection mBottomGridAnnotationDirection;
373389

390+
GridFrameStyle mGridFrameStyle;
391+
double mGridFrameWidth;
392+
374393
/**Current bounding rectangle. This is used to check if notification to the graphics scene is necessary*/
375394
QRectF mCurrentRectangle;
376395
/**The length of the cross sides for mGridStyle Cross*/
@@ -381,6 +400,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
381400

382401
/**Draws the map grid*/
383402
void drawGrid( QPainter* p );
403+
void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
384404
/**Draw coordinates for mGridAnnotationType Coordinate
385405
@param p drawing painter
386406
@param hLines horizontal coordinate lines in item coordinates
@@ -422,6 +442,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
422442
void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
423443
void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
424444
QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
445+
void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
446+
QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
447+
void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border );
425448
};
426449

427450
#endif

0 commit comments

Comments
 (0)