Skip to content
Permalink
Browse files

[FEATURE]: Zebra style for composer grid (black/white pattern at bord…

…ers)
  • Loading branch information
mhugent committed Jul 4, 2012
1 parent a8ae49a commit 4b3e8173dd9fc6cef10814dd403965a5e6fe2b37
@@ -56,6 +56,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
insertAnnotationDirectionEntries( mAnnotationDirectionComboBoxTop );
insertAnnotationDirectionEntries( mAnnotationDirectionComboBoxBottom );

mFrameStyleComboBox->insertItem( 0, tr( "No frame" ) );
mFrameStyleComboBox->insertItem( 1, tr( "Zebra" ) );

if ( composerMap )
{
connect( composerMap, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
@@ -442,6 +445,8 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
mAnnotationDirectionComboBoxBottom->blockSignals( b );
mCoordinatePrecisionSpinBox->blockSignals( b );
mDrawCanvasItemsCheckBox->blockSignals( b );
mFrameStyleComboBox->blockSignals( b );
mFrameWidthSpinBox->blockSignals( b );
}

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

void QgsComposerMapWidget::on_mFrameStyleComboBox_currentIndexChanged( const QString& text )
{
if ( !mComposerMap )
{
return;
}

mComposerMap->beginCommand( tr( "Changed grid frame style" ) );
if ( text == tr( "Zebra" ) )
{
mComposerMap->setGridFrameStyle( QgsComposerMap::Zebra );
}
else //no frame
{
mComposerMap->setGridFrameStyle( QgsComposerMap::NoGridFrame );
}
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mFrameWidthSpinBox_valueChanged( double d )
{
if ( mComposerMap )
{
mComposerMap->beginCommand( tr( "Changed grid frame width" ) );
mComposerMap->setGridFrameWidth( d );
mComposerMap->updateBoundingRect();
mComposerMap->update();
mComposerMap->endCommand();
}
}

void QgsComposerMapWidget::insertAnnotationPositionEntries( QComboBox* c )
{
c->insertItem( 0, tr( "Inside frame" ) );
@@ -76,6 +76,9 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void on_mDrawAnnotationCheckBox_stateChanged( int state );
void on_mCoordinatePrecisionSpinBox_valueChanged( int value );

void on_mFrameStyleComboBox_currentIndexChanged( const QString& text );
void on_mFrameWidthSpinBox_valueChanged( double d );

private slots:

/**Sets the GUI elements to the values of mPicture*/
@@ -42,7 +42,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mComposition = composition;
@@ -89,7 +89,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mCrossLength( 3 ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
mMapCanvas( 0 ), mDrawCanvasItems( true )
{
//Offset
@@ -960,12 +960,79 @@ void QgsComposerMap::drawGrid( QPainter* p )

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

if ( mGridFrameStyle != QgsComposerMap::NoGridFrame )
{
drawGridFrame( p, horizontalLines, verticalLines );
}

if ( mShowGridAnnotation )
{
drawCoordinateAnnotations( p, horizontalLines, verticalLines );
}
}

void QgsComposerMap::drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines )
{
//Sort the coordinate positions for each side
QMap< double, double > leftGridFrame;
QMap< double, double > rightGridFrame;
QMap< double, double > topGridFrame;
QMap< double, double > bottomGridFrame;

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

drawGridFrameBorder( p, leftGridFrame, QgsComposerMap::Left );
drawGridFrameBorder( p, rightGridFrame, QgsComposerMap::Right );
drawGridFrameBorder( p, topGridFrame, QgsComposerMap::Top );
drawGridFrameBorder( p, bottomGridFrame, QgsComposerMap::Bottom );
}

void QgsComposerMap::drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border )
{
double currentCoord = - mGridFrameWidth;
bool white = true;
double x = 0;
double y = 0;
double width = 0;
double height = 0;

QMap< double, double > pos = borderPos;
pos.insert( 0, 0 );
if ( border == Left || border == Right )
{
pos.insert( rect().height(), rect().height() );
pos.insert( rect().height() + mGridFrameWidth, rect().height() + mGridFrameWidth );
}
else //top or bottom
{
pos.insert( rect().width(), rect().width() );
pos.insert( rect().width() + mGridFrameWidth, rect().width() + mGridFrameWidth );
}

QMap< double, double >::const_iterator posIt = pos.constBegin();
for ( ; posIt != pos.constEnd(); ++posIt )
{
p->setBrush( QBrush( white ? Qt::white : Qt::black ) );
if ( border == Left || border == Right )
{
height = posIt.key() - currentCoord;
width = mGridFrameWidth;
x = ( border == Left ) ? -mGridFrameWidth : rect().width();
y = currentCoord;
}
else //top or bottom
{
height = mGridFrameWidth;
width = posIt.key() - currentCoord;
x = currentCoord;
y = ( border == Top ) ? -mGridFrameWidth : rect().height();
}
p->drawRect( QRectF( x, y, width, height ) );
currentCoord = posIt.key();
white = !white;
}
}

void QgsComposerMap::drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines )
{
if ( !p )
@@ -1002,6 +1069,8 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
double ypos = pos.y();
int rotation = 0;

double gridFrameDistance = ( mGridFrameStyle == NoGridFrame ) ? 0 : mGridFrameWidth;

if ( frameBorder == Left )
{

@@ -1023,13 +1092,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
{
if ( mLeftGridAnnotationDirection == Vertical || mLeftGridAnnotationDirection == BoundaryDirection )
{
xpos -= mAnnotationFrameDistance;
xpos -= ( mAnnotationFrameDistance + gridFrameDistance );
ypos += textWidth / 2.0;
rotation = 270;
}
else
{
xpos -= textWidth + mAnnotationFrameDistance;
xpos -= ( textWidth + mAnnotationFrameDistance + gridFrameDistance );
ypos += textHeight / 2.0;
}
}
@@ -1059,13 +1128,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
{
if ( mRightGridAnnotationDirection == Vertical || mRightGridAnnotationDirection == BoundaryDirection )
{
xpos += textHeight + mAnnotationFrameDistance;
xpos += ( textHeight + mAnnotationFrameDistance + gridFrameDistance );
ypos += textWidth / 2.0;
rotation = 270;
}
else //Horizontal
{
xpos += mAnnotationFrameDistance;
xpos += ( mAnnotationFrameDistance + gridFrameDistance );
ypos += textHeight / 2.0;
}
}
@@ -1094,13 +1163,13 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
{
if ( mBottomGridAnnotationDirection == Horizontal || mBottomGridAnnotationDirection == BoundaryDirection )
{
ypos += mAnnotationFrameDistance + textHeight;
ypos += ( mAnnotationFrameDistance + textHeight + gridFrameDistance );
xpos -= textWidth / 2.0;
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos += textWidth + mAnnotationFrameDistance;
ypos += ( textWidth + mAnnotationFrameDistance + gridFrameDistance );
rotation = 270;
}
}
@@ -1130,12 +1199,12 @@ void QgsComposerMap::drawCoordinateAnnotation( QPainter* p, const QPointF& pos,
if ( mTopGridAnnotationDirection == Horizontal || mTopGridAnnotationDirection == BoundaryDirection )
{
xpos -= textWidth / 2.0;
ypos -= mAnnotationFrameDistance;
ypos -= ( mAnnotationFrameDistance + gridFrameDistance );
}
else //Vertical
{
xpos += textHeight / 2.0;
ypos -= mAnnotationFrameDistance;
ypos -= ( mAnnotationFrameDistance + gridFrameDistance );
rotation = 270;
}
}
@@ -1389,7 +1458,9 @@ double QgsComposerMap::maxExtension() const
maxExtension = qMax( maxExtension, currentExtension );
}

return maxExtension + mAnnotationFrameDistance;
//grid frame
double gridFrameDist = ( mGridFrameStyle == NoGridFrame ) ? 0 : mGridFrameWidth;
return maxExtension + mAnnotationFrameDistance + gridFrameDist;
}

void QgsComposerMap::mapPolygon( QPolygonF& poly ) const
@@ -1729,3 +1800,45 @@ QgsComposerMap::GridAnnotationDirection QgsComposerMap::gridAnnotationDirection(
break;
}
}

void QgsComposerMap::sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const
{
QList< QPair< double, QPointF > > borderPositions;
QList< QPair< double, QLineF > >::const_iterator it = hLines.constBegin();
for ( ; it != hLines.constEnd(); ++it )
{
borderPositions << qMakePair( it->first, it->second.p1() );
borderPositions << qMakePair( it->first, it->second.p2() );
}
it = vLines.constBegin();
for ( ; it != vLines.constEnd(); ++it )
{
borderPositions << qMakePair( it->first, it->second.p1() );
borderPositions << qMakePair( it->first, it->second.p2() );
}

QList< QPair< double, QPointF > >::const_iterator bIt = borderPositions.constBegin();
for ( ; bIt != borderPositions.constEnd(); ++bIt )
{
Border frameBorder = borderForLineCoord( bIt->second );
if ( frameBorder == QgsComposerMap::Left )
{
leftFrameEntries.insert( bIt->second.y(), bIt->first );
}
else if ( frameBorder == QgsComposerMap::Right )
{
rightFrameEntries.insert( bIt->second.y(), bIt->first );
}
else if ( frameBorder == QgsComposerMap::Top )
{
topFrameEntries.insert( bIt->second.x(), bIt->first );
}
else //Bottom
{
bottomFrameEntries.insert( bIt->second.x(), bIt->first );
}
}
}


@@ -78,6 +78,12 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
BoundaryDirection
};

enum GridFrameStyle
{
NoGridFrame = 0,
Zebra //black / white pattern
};

/**Enum for different frame borders*/
enum Border
{
@@ -248,6 +254,16 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;

/**Set grid frame style (NoGridFrame or Zebra)
@note: this function was added in version 1.9*/
void setGridFrameStyle( GridFrameStyle style ) { mGridFrameStyle = style; }
GridFrameStyle gridFrameStyle() const { return mGridFrameStyle; }

/**Set grid frame width
@note: this function was added in version 1.9*/
void setGridFrameWidth( double w ) { mGridFrameWidth = w; }
double gridFrameWidth() const { return mGridFrameWidth; }

/**In case of annotations, the bounding rectangle can be larger than the map item rectangle
@note this function was added in version 1.4*/
QRectF boundingRect() const;
@@ -371,6 +387,9 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
/**Annotation direction on bottom side ( horizontal or vertical )*/
GridAnnotationDirection mBottomGridAnnotationDirection;

GridFrameStyle mGridFrameStyle;
double mGridFrameWidth;

/**Current bounding rectangle. This is used to check if notification to the graphics scene is necessary*/
QRectF mCurrentRectangle;
/**The length of the cross sides for mGridStyle Cross*/
@@ -381,6 +400,7 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem

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

#endif

0 comments on commit 4b3e817

Please sign in to comment.
You can’t perform that action at this time.