Skip to content

Commit

Permalink
[FEATURE] [needs-docs] Add the grid frame MARGIN In the "Print Layout…
Browse files Browse the repository at this point in the history
…" map grid
  • Loading branch information
konst authored and nyalldawson committed Dec 6, 2018
1 parent e6ec1ec commit d794bdb
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 37 deletions.
16 changes: 16 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemmapgrid.sip.in
Expand Up @@ -748,6 +748,22 @@ The size of the line outlines drawn in the frame can be retrieved via the
framePenSize method.

.. seealso:: :py:func:`setFrameWidth`
%End

void setGridFrameMargin( const double margin );
%Docstring
Sets the grid frame margin (in layout units).
This property controls distance between the map frame and the grid frame.

.. seealso:: GridFrameMargin
%End

double GridFrameMargin() const;
%Docstring
Sets the grid frame Margin (in layout units).
This property controls distance between the map frame and the grid frame.

.. seealso:: :py:func:`setGridFrameMargin`
%End

void setFramePenSize( const double width );
Expand Down
20 changes: 20 additions & 0 deletions src/app/layout/qgslayoutmapgridwidget.cpp
Expand Up @@ -40,6 +40,7 @@ QgsLayoutMapGridWidget::QgsLayoutMapGridWidget( QgsLayoutItemMapGrid *mapGrid, Q
connect( mOffsetYSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMapGridWidget::mOffsetYSpinBox_valueChanged );
connect( mCrossWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMapGridWidget::mCrossWidthSpinBox_valueChanged );
connect( mFrameWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMapGridWidget::mFrameWidthSpinBox_valueChanged );
connect( mGridFrameMarginSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMapGridWidget::mGridFrameMarginSpinBox_valueChanged );
connect( mFrameStyleComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutMapGridWidget::mFrameStyleComboBox_currentIndexChanged );
connect( mGridFramePenSizeSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMapGridWidget::mGridFramePenSizeSpinBox_valueChanged );
connect( mGridFramePenColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutMapGridWidget::mGridFramePenColorButton_colorChanged );
Expand Down Expand Up @@ -197,6 +198,7 @@ void QgsLayoutMapGridWidget::blockAllSignals( bool block )
mCrossWidthSpinBox->blockSignals( block );
mFrameStyleComboBox->blockSignals( block );
mFrameWidthSpinBox->blockSignals( block );
mGridFrameMarginSpinBox->blockSignals( block );
mGridLineStyleButton->blockSignals( block );
mMapGridUnitComboBox->blockSignals( block );
mGridFramePenSizeSpinBox->blockSignals( block );
Expand Down Expand Up @@ -282,11 +284,13 @@ void QgsLayoutMapGridWidget::toggleFrameControls( bool frameEnabled, bool frameF
{
//set status of frame controls
mFrameWidthSpinBox->setEnabled( frameSizeEnabled );
mGridFrameMarginSpinBox->setEnabled( frameEnabled );
mGridFramePenSizeSpinBox->setEnabled( frameEnabled );
mGridFramePenColorButton->setEnabled( frameEnabled );
mGridFrameFill1ColorButton->setEnabled( frameFillEnabled );
mGridFrameFill2ColorButton->setEnabled( frameFillEnabled );
mFrameWidthLabel->setEnabled( frameSizeEnabled );
mFrameMarginLabel->setEnabled( frameEnabled );
mFramePenLabel->setEnabled( frameEnabled );
mFrameFillLabel->setEnabled( frameFillEnabled );
mCheckGridLeftSide->setEnabled( frameEnabled );
Expand Down Expand Up @@ -459,6 +463,7 @@ void QgsLayoutMapGridWidget::setGridItems()
mOffsetYSpinBox->setValue( mMapGrid->offsetY() );
mCrossWidthSpinBox->setValue( mMapGrid->crossLength() );
mFrameWidthSpinBox->setValue( mMapGrid->frameWidth() );
mGridFrameMarginSpinBox->setValue( mMapGrid->GridFrameMargin() );
mGridFramePenSizeSpinBox->setValue( mMapGrid->framePenSize() );
mGridFramePenColorButton->setColor( mMapGrid->framePenColor() );
mGridFrameFill1ColorButton->setColor( mMapGrid->frameFillColor1() );
Expand Down Expand Up @@ -515,6 +520,7 @@ void QgsLayoutMapGridWidget::setGridItems()

//grid frame
mFrameWidthSpinBox->setValue( mMapGrid->frameWidth() );
mGridFrameMarginSpinBox->setValue( mMapGrid->GridFrameMargin() );
QgsLayoutItemMapGrid::FrameStyle gridFrameStyle = mMapGrid->frameStyle();
switch ( gridFrameStyle )
{
Expand Down Expand Up @@ -689,6 +695,20 @@ void QgsLayoutMapGridWidget::mFrameWidthSpinBox_valueChanged( double val )
mMap->endCommand();
}

void QgsLayoutMapGridWidget::mGridFrameMarginSpinBox_valueChanged( double val )
{
if ( !mMapGrid || !mMap )
{
return;
}

mMap->beginCommand( tr( "Change Grid Frame Margin" ) );
mMapGrid->setGridFrameMargin( val );
mMap->updateBoundingRect();
mMap->update();
mMap->endCommand();
}

void QgsLayoutMapGridWidget::mCheckGridLeftSide_toggled( bool checked )
{
if ( !mMapGrid || !mMap )
Expand Down
1 change: 1 addition & 0 deletions src/app/layout/qgslayoutmapgridwidget.h
Expand Up @@ -42,6 +42,7 @@ class QgsLayoutMapGridWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLay
void mOffsetYSpinBox_valueChanged( double value );
void mCrossWidthSpinBox_valueChanged( double val );
void mFrameWidthSpinBox_valueChanged( double val );
void mGridFrameMarginSpinBox_valueChanged( double val );
void mFrameStyleComboBox_currentIndexChanged( const QString &text );
void mGridFramePenSizeSpinBox_valueChanged( double d );
void mGridFramePenColorButton_colorChanged( const QColor &newColor );
Expand Down
85 changes: 63 additions & 22 deletions src/core/layout/qgslayoutitemmapgrid.cpp
Expand Up @@ -234,6 +234,7 @@ bool QgsLayoutItemMapGrid::writeXml( QDomElement &elem, QDomDocument &doc, const
mapGridElem.setAttribute( QStringLiteral( "gridFrameStyle" ), mGridFrameStyle );
mapGridElem.setAttribute( QStringLiteral( "gridFrameSideFlags" ), mGridFrameSides );
mapGridElem.setAttribute( QStringLiteral( "gridFrameWidth" ), qgsDoubleToString( mGridFrameWidth ) );
mapGridElem.setAttribute( QStringLiteral( "gridFrameMargin" ), qgsDoubleToString( mGridFrameMargin ) );
mapGridElem.setAttribute( QStringLiteral( "gridFramePenThickness" ), qgsDoubleToString( mGridFramePenThickness ) );
mapGridElem.setAttribute( QStringLiteral( "gridFramePenColor" ), QgsSymbolLayerUtils::encodeColor( mGridFramePenColor ) );
mapGridElem.setAttribute( QStringLiteral( "frameFillColor1" ), QgsSymbolLayerUtils::encodeColor( mGridFrameFillColor1 ) );
Expand Down Expand Up @@ -294,6 +295,7 @@ bool QgsLayoutItemMapGrid::readXml( const QDomElement &itemElem, const QDomDocum
mGridFrameStyle = static_cast< QgsLayoutItemMapGrid::FrameStyle >( itemElem.attribute( QStringLiteral( "gridFrameStyle" ), QStringLiteral( "0" ) ).toInt() );
mGridFrameSides = static_cast< QgsLayoutItemMapGrid::FrameSideFlags >( itemElem.attribute( QStringLiteral( "gridFrameSideFlags" ), QStringLiteral( "15" ) ).toInt() );
mGridFrameWidth = itemElem.attribute( QStringLiteral( "gridFrameWidth" ), QStringLiteral( "2.0" ) ).toDouble();
mGridFrameMargin = itemElem.attribute( QStringLiteral( "gridFrameMargin" ), QStringLiteral( "0.0" ) ).toDouble();
mGridFramePenThickness = itemElem.attribute( QStringLiteral( "gridFramePenThickness" ), QStringLiteral( "0.3" ) ).toDouble();
mGridFramePenColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "gridFramePenColor" ), QStringLiteral( "0,0,0" ) ) );
mGridFrameFillColor1 = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "frameFillColor1" ), QStringLiteral( "255,255,255,255" ) ) );
Expand Down Expand Up @@ -800,21 +802,23 @@ void QgsLayoutItemMapGrid::drawGridFrameZebraBorder( QPainter *p, const QMap< do

if ( extension )
{
*extension = mGridFrameWidth + mGridFramePenThickness / 2.0;
*extension = mGridFrameMargin + mGridFrameWidth + mGridFramePenThickness / 2.0;
return;
}

QMap< double, double > pos = borderPos;

double currentCoord = 0;
double currentCoord = 0.0;
if ( ( border == QgsLayoutItemMapGrid::Left || border == QgsLayoutItemMapGrid::Right ) && testFrameSideFlag( QgsLayoutItemMapGrid::FrameTop ) )
{
currentCoord = - mGridFrameWidth;
// currentCoord = - (mGridFrameWidth + mGridFrameMargin);
currentCoord = -mGridFramePenThickness / 2.0;
pos.insert( 0, 0 );
}
else if ( ( border == QgsLayoutItemMapGrid::Top || border == QgsLayoutItemMapGrid::Bottom ) && testFrameSideFlag( QgsLayoutItemMapGrid::FrameLeft ) )
{
currentCoord = - mGridFrameWidth;
// currentCoord = - (mGridFrameWidth + mGridFrameMargin);
currentCoord = -mGridFramePenThickness / 2.0;
pos.insert( 0, 0 );
}
bool color1 = true;
Expand All @@ -828,15 +832,17 @@ void QgsLayoutItemMapGrid::drawGridFrameZebraBorder( QPainter *p, const QMap< do
pos.insert( mMap->rect().height(), mMap->rect().height() );
if ( testFrameSideFlag( QgsLayoutItemMapGrid::FrameBottom ) )
{
pos.insert( mMap->rect().height() + mGridFrameWidth, mMap->rect().height() + mGridFrameWidth );
// pos.insert( mMap->rect().height() + mGridFrameWidth, mMap->rect().height() + mGridFrameWidth );
pos.insert( mMap->rect().height(), mMap->rect().height() );
}
}
else if ( border == QgsLayoutItemMapGrid::Top || border == QgsLayoutItemMapGrid::Bottom )
{
pos.insert( mMap->rect().width(), mMap->rect().width() );
if ( testFrameSideFlag( QgsLayoutItemMapGrid::FrameRight ) )
{
pos.insert( mMap->rect().width() + mGridFrameWidth, mMap->rect().width() + mGridFrameWidth );
// pos.insert( mMap->rect().width() + mGridFrameWidth, mMap->rect().width() + mGridFrameWidth );
pos.insert( mMap->rect().width(), mMap->rect().width() );
}
}

Expand All @@ -854,20 +860,27 @@ void QgsLayoutItemMapGrid::drawGridFrameZebraBorder( QPainter *p, const QMap< do
{
height = posIt.key() - currentCoord;
width = mGridFrameWidth;
x = ( border == QgsLayoutItemMapGrid::Left ) ? -mGridFrameWidth : mMap->rect().width();
x = ( border == QgsLayoutItemMapGrid::Left ) ? -( mGridFrameWidth + mGridFrameMargin ) : mMap->rect().width() + mGridFrameMargin;
y = currentCoord;
}
else //top or bottom
{
height = mGridFrameWidth;
width = posIt.key() - currentCoord;
x = currentCoord;
y = ( border == QgsLayoutItemMapGrid::Top ) ? -mGridFrameWidth : mMap->rect().height();
y = ( border == QgsLayoutItemMapGrid::Top ) ? -( mGridFrameWidth + mGridFrameMargin ) : mMap->rect().height() + mGridFrameMargin;
}
p->drawRect( QRectF( x, y, width, height ) );
currentCoord = posIt.key();
color1 = !color1;
}
//draw corners
width = height = ( mGridFrameWidth + mGridFrameMargin ) ;
p->setBrush( QBrush( mGridFrameFillColor1 ) );
p->drawRect( QRectF( -( mGridFrameWidth + mGridFrameMargin ), -( mGridFrameWidth + mGridFrameMargin ), width, height ) );
p->drawRect( QRectF( mMap->rect().width(), -( mGridFrameWidth + mGridFrameMargin ), width, height ) );
p->drawRect( QRectF( -( mGridFrameWidth + mGridFrameMargin ), mMap->rect().height(), width, height ) );
p->drawRect( QRectF( mMap->rect().width(), mMap->rect().height(), width, height ) );
}

void QgsLayoutItemMapGrid::drawGridFrameTicks( QPainter *p, const QMap< double, double > &borderPos, QgsLayoutItemMapGrid::BorderSide border, double *extension ) const
Expand All @@ -880,7 +893,7 @@ void QgsLayoutItemMapGrid::drawGridFrameTicks( QPainter *p, const QMap< double,
if ( extension )
{
if ( mGridFrameStyle != QgsLayoutItemMapGrid::InteriorTicks )
*extension = mGridFrameWidth;
*extension = mGridFrameMargin + mGridFrameWidth;
return;
}

Expand All @@ -906,17 +919,17 @@ void QgsLayoutItemMapGrid::drawGridFrameTicks( QPainter *p, const QMap< double,
if ( mGridFrameStyle == QgsLayoutItemMapGrid::InteriorTicks )
{
width = mGridFrameWidth;
x = ( border == QgsLayoutItemMapGrid::Left ) ? 0 : mMap->rect().width() - mGridFrameWidth;
x = ( border == QgsLayoutItemMapGrid::Left ) ? 0 - mGridFrameMargin : mMap->rect().width() - mGridFrameWidth + mGridFrameMargin;
}
else if ( mGridFrameStyle == QgsLayoutItemMapGrid::ExteriorTicks )
{
width = mGridFrameWidth;
x = ( border == QgsLayoutItemMapGrid::Left ) ? - mGridFrameWidth : mMap->rect().width();
x = ( border == QgsLayoutItemMapGrid::Left ) ? - mGridFrameWidth - mGridFrameMargin : mMap->rect().width() + mGridFrameMargin;
}
else if ( mGridFrameStyle == QgsLayoutItemMapGrid::InteriorExteriorTicks )
{
width = mGridFrameWidth * 2;
x = ( border == QgsLayoutItemMapGrid::Left ) ? - mGridFrameWidth : mMap->rect().width() - mGridFrameWidth;
x = ( border == QgsLayoutItemMapGrid::Left ) ? - mGridFrameWidth - mGridFrameMargin : mMap->rect().width() - mGridFrameWidth + mGridFrameMargin;
}
}
else //top or bottom
Expand All @@ -926,17 +939,17 @@ void QgsLayoutItemMapGrid::drawGridFrameTicks( QPainter *p, const QMap< double,
if ( mGridFrameStyle == QgsLayoutItemMapGrid::InteriorTicks )
{
height = mGridFrameWidth;
y = ( border == QgsLayoutItemMapGrid::Top ) ? 0 : mMap->rect().height() - mGridFrameWidth;
y = ( border == QgsLayoutItemMapGrid::Top ) ? 0 - mGridFrameMargin : mMap->rect().height() - mGridFrameWidth + mGridFrameMargin;
}
else if ( mGridFrameStyle == QgsLayoutItemMapGrid::ExteriorTicks )
{
height = mGridFrameWidth;
y = ( border == QgsLayoutItemMapGrid::Top ) ? -mGridFrameWidth : mMap->rect().height();
y = ( border == QgsLayoutItemMapGrid::Top ) ? -mGridFrameWidth - mGridFrameMargin : mMap->rect().height() + mGridFrameMargin;
}
else if ( mGridFrameStyle == QgsLayoutItemMapGrid::InteriorExteriorTicks )
{
height = mGridFrameWidth * 2;
y = ( border == QgsLayoutItemMapGrid::Top ) ? -mGridFrameWidth : mMap->rect().height() - mGridFrameWidth;
y = ( border == QgsLayoutItemMapGrid::Top ) ? -mGridFrameWidth - mGridFrameMargin : mMap->rect().height() - mGridFrameWidth + mGridFrameMargin;
}
}
p->drawLine( QLineF( x, y, x + width, y + height ) );
Expand All @@ -952,7 +965,7 @@ void QgsLayoutItemMapGrid::drawGridFrameLineBorder( QPainter *p, QgsLayoutItemMa

if ( extension )
{
*extension = mGridFramePenThickness / 2.0;
*extension = mGridFrameMargin + mGridFramePenThickness / 2.0;
return;
}

Expand All @@ -966,16 +979,44 @@ void QgsLayoutItemMapGrid::drawGridFrameLineBorder( QPainter *p, QgsLayoutItemMa
switch ( border )
{
case QgsLayoutItemMapGrid::Left:
p->drawLine( QLineF( 0, 0, 0, mMap->rect().height() ) );
p->drawLine( QLineF( 0 - mGridFrameMargin, 0 - mGridFrameMargin, 0 - mGridFrameMargin, mMap->rect().height() + mGridFrameMargin ) );
//corner left-top
if ( mGridFrameMargin != 0 )
{
const double X1 = 0 - mGridFrameMargin + mGridFramePenThickness / 2.0;
const double Y1 = 0 - mGridFrameMargin + mGridFramePenThickness / 2.0;
p->drawLine( QLineF( 0, 0, X1, Y1 ) );
}
break;
case QgsLayoutItemMapGrid::Right:
p->drawLine( QLineF( mMap->rect().width(), 0, mMap->rect().width(), mMap->rect().height() ) );
p->drawLine( QLineF( mMap->rect().width() + mGridFrameMargin, 0 - mGridFrameMargin, mMap->rect().width() + mGridFrameMargin, mMap->rect().height() + mGridFrameMargin ) );
//corner right-bottom
if ( mGridFrameMargin != 0 )
{
const double X1 = mMap->rect().width() + mGridFrameMargin - mGridFramePenThickness / 2.0 ;
const double Y1 = mMap->rect().height() + mGridFrameMargin - mGridFramePenThickness / 2.0 ;
p->drawLine( QLineF( mMap->rect().width(), mMap->rect().height(), X1, Y1 ) );
}
break;
case QgsLayoutItemMapGrid::Top:
p->drawLine( QLineF( 0, 0, mMap->rect().width(), 0 ) );
p->drawLine( QLineF( 0 - mGridFrameMargin, 0 - mGridFrameMargin, mMap->rect().width() + mGridFrameMargin, 0 - mGridFrameMargin ) );
//corner right-top
if ( mGridFrameMargin != 0 )
{
const double X1 = mMap->rect().width() + mGridFrameMargin - mGridFramePenThickness / 2.0 ;
const double Y1 = 0 - mGridFrameMargin + mGridFramePenThickness / 2.0 ;
p->drawLine( QLineF( mMap->rect().width(), 0, X1, Y1 ) );
}
break;
case QgsLayoutItemMapGrid::Bottom:
p->drawLine( QLineF( 0, mMap->rect().height(), mMap->rect().width(), mMap->rect().height() ) );
p->drawLine( QLineF( 0 - mGridFrameMargin, mMap->rect().height() + mGridFrameMargin, mMap->rect().width() + mGridFrameMargin, mMap->rect().height() + mGridFrameMargin ) );
//corner left-bottom
if ( mGridFrameMargin != 0 )
{
const double X1 = 0 - mGridFrameMargin + mGridFramePenThickness / 2.0 ;
const double Y1 = mMap->rect().height() + mGridFrameMargin - mGridFramePenThickness / 2.0 ;
p->drawLine( QLineF( 0, mMap->rect().height(), X1, Y1 ) );
}
break;
}
}
Expand Down Expand Up @@ -1684,7 +1725,7 @@ int QgsLayoutItemMapGrid::xGridLinesCrsTransform( const QgsRectangle &bbox, cons
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
}

currentX += step;
Expand Down Expand Up @@ -1760,7 +1801,7 @@ int QgsLayoutItemMapGrid::yGridLinesCrsTransform( const QgsRectangle &bbox, cons
catch ( QgsCsException &cse )
{
Q_UNUSED( cse );
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
}

currentY += step;
Expand Down
15 changes: 15 additions & 0 deletions src/core/layout/qgslayoutitemmapgrid.h
Expand Up @@ -724,6 +724,20 @@ class CORE_EXPORT QgsLayoutItemMapGrid : public QgsLayoutItemMapItem
*/
double frameWidth() const { return mGridFrameWidth; }

/**
* Sets the grid frame margin (in layout units).
* This property controls distance between the map frame and the grid frame.
* \see GridFrameMargin()
*/
void setGridFrameMargin( const double margin ) { mGridFrameMargin = margin; }

/**
* Sets the grid frame Margin (in layout units).
* This property controls distance between the map frame and the grid frame.
* \see setGridFrameMargin()
*/
double GridFrameMargin() const { return mGridFrameMargin; }

/**
* Sets the \a width of the stroke drawn in the grid frame.
* \see framePenSize()
Expand Down Expand Up @@ -867,6 +881,7 @@ class CORE_EXPORT QgsLayoutItemMapGrid : public QgsLayoutItemMapItem
QColor mGridFrameFillColor1 = Qt::white;
QColor mGridFrameFillColor2 = Qt::black;
double mCrossLength = 3.0;
double mGridFrameMargin = 0.0;

//! Divisions for frame on left map side
DisplayMode mLeftFrameDivisions = QgsLayoutItemMapGrid::ShowAll;
Expand Down

0 comments on commit d794bdb

Please sign in to comment.