Skip to content
Permalink
Browse files

Merge pull request #916 from nyalldawson/composer_grid2

Composer grid fixes
  • Loading branch information
mhugent committed Oct 17, 2013
2 parents 087b52b + d7db4a5 commit cca580529894f809059a063c09ce07028e810509
@@ -84,9 +84,6 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
mOffsetYSpinBox->setValue( mComposition->snapGridOffsetY() );


//grid pen width
mPenWidthSpinBox->setValue( mComposition->gridPen().widthF() );

//grid pen color
mGridColorButton->setColor( mComposition->gridPen().color() );
mGridColorButton->setColorDialogTitle( tr( "Select grid color" ) );
@@ -111,6 +108,7 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
}

mSelectionToleranceSpinBox->setValue( mComposition->selectionTolerance() );
mGridToleranceSpinBox->setValue( mComposition->snapGridTolerance() );
}
blockSignals( false );
}
@@ -566,13 +564,11 @@ void QgsCompositionWidget::on_mGridStyleComboBox_currentIndexChanged( const QStr
}
}

void QgsCompositionWidget::on_mPenWidthSpinBox_valueChanged( double d )
void QgsCompositionWidget::on_mGridToleranceSpinBox_valueChanged( double d )
{
if ( mComposition )
{
QPen pen = mComposition->gridPen();
pen.setWidthF( d );
mComposition->setGridPen( pen );
mComposition->setSnapGridTolerance( d );
}
}

@@ -614,9 +610,9 @@ void QgsCompositionWidget::blockSignals( bool block )
mGridResolutionSpinBox->blockSignals( block );
mOffsetXSpinBox->blockSignals( block );
mOffsetYSpinBox->blockSignals( block );
mPenWidthSpinBox->blockSignals( block );
mGridColorButton->blockSignals( block );
mGridStyleComboBox->blockSignals( block );
mGridToleranceSpinBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
mAlignmentSnapGroupCheckBox->blockSignals( block );
mAlignmentToleranceSpinBox->blockSignals( block );
@@ -59,7 +59,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mOffsetYSpinBox_valueChanged( double d );
void on_mGridColorButton_colorChanged( const QColor &newColor );
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
void on_mPenWidthSpinBox_valueChanged( double d );
void on_mGridToleranceSpinBox_valueChanged( double d );
void on_mSelectionToleranceSpinBox_valueChanged( double d );
void on_mAlignmentSnapGroupCheckBox_toggled( bool state );
void on_mAlignmentToleranceSpinBox_valueChanged( double d );
@@ -61,6 +61,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mSelectionTolerance( 0.0 )
, mSnapToGrid( false )
, mSnapGridResolution( 10.0 )
, mSnapGridTolerance( 2 )
, mSnapGridOffsetX( 0.0 )
, mSnapGridOffsetY( 0.0 )
, mAlignmentSnap( true )
@@ -97,6 +98,7 @@ QgsComposition::QgsComposition()
mSelectionTolerance( 0.0 ),
mSnapToGrid( false ),
mSnapGridResolution( 10.0 ),
mSnapGridTolerance( 2 ),
mSnapGridOffsetX( 0.0 ),
mSnapGridOffsetY( 0.0 ),
mAlignmentSnap( true ),
@@ -434,6 +436,7 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "snapping", "0" );
}
compositionElem.setAttribute( "snapGridResolution", QString::number( mSnapGridResolution ) );
compositionElem.setAttribute( "snapGridTolerance", QString::number( mSnapGridTolerance ) );
compositionElem.setAttribute( "snapGridOffsetX", QString::number( mSnapGridOffsetX ) );
compositionElem.setAttribute( "snapGridOffsetY", QString::number( mSnapGridOffsetY ) );

@@ -522,6 +525,7 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mSnapToGrid = true;
}
mSnapGridResolution = compositionElem.attribute( "snapGridResolution" ).toDouble();
mSnapGridTolerance = compositionElem.attribute( "snapGridTolerance", "2.0" ).toDouble();
mSnapGridOffsetX = compositionElem.attribute( "snapGridOffsetX" ).toDouble();
mSnapGridOffsetY = compositionElem.attribute( "snapGridOffsetY" ).toDouble();

@@ -1356,7 +1360,21 @@ QPointF QgsComposition::snapPointToGrid( const QPointF& scenePoint ) const
int xRatio = ( int )(( scenePoint.x() - mSnapGridOffsetX ) / mSnapGridResolution + 0.5 );
int yRatio = ( int )(( yPage - mSnapGridOffsetY ) / mSnapGridResolution + 0.5 );

return QPointF( xRatio * mSnapGridResolution + mSnapGridOffsetX, yRatio * mSnapGridResolution + mSnapGridOffsetY + yOffset );
double xSnapped = xRatio * mSnapGridResolution + mSnapGridOffsetX;
double ySnapped = yRatio * mSnapGridResolution + mSnapGridOffsetY + yOffset;

if ( abs( xSnapped - scenePoint.x() ) > mSnapGridTolerance )
{
//snap distance is outside of tolerance
xSnapped = scenePoint.x();
}
if ( abs( ySnapped - scenePoint.y() ) > mSnapGridTolerance )
{
//snap distance is outside of tolerance
ySnapped = scenePoint.y();
}

return QPointF( xSnapped, ySnapped );
}

QGraphicsLineItem* QgsComposition::addSnapLine()
@@ -1535,6 +1553,12 @@ void QgsComposition::setSnapGridResolution( double r )
saveSettings();
}

void QgsComposition::setSnapGridTolerance( double tolerance )
{
mSnapGridTolerance = tolerance;
saveSettings();
}

void QgsComposition::setSnapGridOffsetX( double offset )
{
mSnapGridOffsetX = offset;
@@ -1552,6 +1576,8 @@ void QgsComposition::setSnapGridOffsetY( double offset )
void QgsComposition::setGridPen( const QPen& p )
{
mGridPen = p;
//make sure grid is drawn using a zero-width cosmetic pen
mGridPen.setWidthF( 0 );
updatePaperItems();
saveSettings();
}
@@ -1576,16 +1602,14 @@ void QgsComposition::loadSettings()

QString gridStyleString;
int red, green, blue;
double penWidth;

gridStyleString = s.value( "/qgis/composerGridStyle", "Dots" ).toString();
penWidth = s.value( "/qgis/composerGridWidth", 0.5 ).toDouble();
red = s.value( "/qgis/composerGridRed", 0 ).toInt();
green = s.value( "/qgis/composerGridGreen", 0 ).toInt();
blue = s.value( "/qgis/composerGridBlue", 0 ).toInt();

mGridPen.setColor( QColor( red, green, blue ) );
mGridPen.setWidthF( penWidth );
mGridPen.setWidthF( 0 );

if ( gridStyleString == "Dots" )
{
@@ -1607,7 +1631,6 @@ void QgsComposition::saveSettings()
{
//store grid appearance settings
QSettings s;
s.setValue( "/qgis/composerGridWidth", mGridPen.widthF() );
s.setValue( "/qgis/composerGridRed", mGridPen.color().red() );
s.setValue( "/qgis/composerGridGreen", mGridPen.color().green() );
s.setValue( "/qgis/composerGridBlue", mGridPen.color().blue() );
@@ -114,6 +114,9 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
void setSnapGridResolution( double r );
double snapGridResolution() const {return mSnapGridResolution;}

void setSnapGridTolerance( double tolerance );
double snapGridTolerance() const {return mSnapGridTolerance;}

void setSnapGridOffsetX( double offset );
double snapGridOffsetX() const {return mSnapGridOffsetX;}

@@ -419,6 +422,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
/**Parameters for snap to grid function*/
bool mSnapToGrid;
double mSnapGridResolution;
double mSnapGridTolerance;
double mSnapGridOffsetX;
double mSnapGridOffsetY;
QPen mGridPen;
@@ -51,7 +51,6 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
drawBackground( painter );

//draw grid

if ( mComposition )
{
if ( mComposition->snapToGridEnabled() && mComposition->plotStyle() == QgsComposition::Preview
@@ -63,13 +62,15 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
double currentYCoord;
double minYCoord = mComposition->snapGridOffsetY() - gridMultiplyY * mComposition->snapGridResolution();

painter->save();
//turn of antialiasing so grid is nice and sharp
painter->setRenderHint( QPainter::Antialiasing, false );

if ( mComposition->gridStyle() == QgsComposition::Solid )
{
painter->setPen( mComposition->gridPen() );

//draw vertical lines


for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
{
painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
@@ -87,26 +88,41 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
QPen gridPen = mComposition->gridPen();
painter->setPen( gridPen );
painter->setBrush( QBrush( gridPen.color() ) );
double halfCrossLength = mComposition->snapGridResolution() / 6;
double halfCrossLength = 1;
if ( mComposition->gridStyle() == QgsComposition::Dots )
{
//dots are actually drawn as tiny crosses a few pixels across
//check QGraphicsView to get current transform
if ( scene() )
{
QList<QGraphicsView*> viewList = scene()->views();
if ( viewList.size() > 0 )
{
QGraphicsView* currentView = viewList.at( 0 );
if ( currentView->isVisible() )
{
//set halfCrossLength to equivalent of 1 pixel
halfCrossLength = 1 / currentView->transform().m11();
}
}
}
}
else if ( mComposition->gridStyle() == QgsComposition::Crosses )
{
halfCrossLength = mComposition->snapGridResolution() / 6;
}

for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
{
currentYCoord = minYCoord;
for ( ; currentYCoord <= rect().height(); currentYCoord += mComposition->snapGridResolution() )
{
if ( mComposition->gridStyle() == QgsComposition::Dots )
{
QRectF pieRect( currentXCoord - gridPen.widthF() / 2, currentYCoord - gridPen.widthF() / 2, gridPen.widthF(), gridPen.widthF() );
painter->drawChord( pieRect, 0, 5760 );
}
else if ( mComposition->gridStyle() == QgsComposition::Crosses )
{
painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );
}
painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );
}
}
}
painter->restore();
}
}
}
@@ -23,7 +23,16 @@
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
@@ -276,6 +285,38 @@
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="generalSettingsGroupBox">
<property name="title">
<string>General settings</string>
</property>
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<property name="labelAlignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Selection tolerance</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="mSelectionToleranceSpinBox">
<property name="suffix">
<string> mm</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="mSnapToGridGroupCheckBox">
<property name="title">
@@ -361,26 +402,6 @@
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Pen width</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="mPenWidthSpinBox">
<property name="prefix">
<string/>
</property>
<property name="suffix">
<string> mm</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mGridStyleLabel">
<property name="text">
<string>Grid style</string>
@@ -393,33 +414,36 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="2" column="1">
<widget class="QComboBox" name="mGridStyleComboBox"/>
</item>
<item row="5" column="0">
<item row="3" column="1">
<widget class="QgsColorButton" name="mGridColorButton">
<property name="text">
<string>Color...</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Selection tolerance</string>
<string>Tolerance</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QDoubleSpinBox" name="mSelectionToleranceSpinBox">
<item row="4" column="1">
<widget class="QDoubleSpinBox" name="mGridToleranceSpinBox">
<property name="prefix">
<string/>
</property>
<property name="suffix">
<string> mm</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QgsColorButton" name="mGridColorButton">
<property name="text">
<string>Color...</string>
<property name="value">
<double>2.000000000000000</double>
</property>
</widget>
</item>

0 comments on commit cca5805

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