Skip to content
Permalink
Browse files
Set alignment snap and tolerance for composition
  • Loading branch information
mhugent committed Jan 18, 2013
1 parent 4105eac commit b7dd4c4f39174b1d155d89820c102577ec28c954
@@ -56,6 +56,9 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
mPrintAsRasterCheckBox->setCheckState( Qt::Unchecked );
}

mAlignmentSnapCheckBox->setCheckState( mComposition->alignmentSnap() ? Qt::Checked : Qt::Unchecked );
mAlignmentToleranceSpinBox->setValue( mComposition->alignmentSnapTolerance() );

//snap grid
if ( mComposition->snapToGridEnabled() )
{
@@ -524,6 +527,22 @@ void QgsCompositionWidget::on_mSelectionToleranceSpinBox_valueChanged( double d
}
}

void QgsCompositionWidget::on_mAlignmentSnapCheckBox_stateChanged( int state )
{
if ( mComposition )
{
mComposition->setAlignmentSnap( state == Qt::Checked ? true : false );
}
}

void QgsCompositionWidget::on_mAlignmentToleranceSpinBox_valueChanged( double d )
{
if ( mComposition )
{
mComposition->setAlignmentSnapTolerance( d );
}
}

void QgsCompositionWidget::blockSignals( bool block )
{
mPaperSizeComboBox->blockSignals( block );
@@ -542,4 +561,6 @@ void QgsCompositionWidget::blockSignals( bool block )
mGridColorButton->blockSignals( block );
mGridStyleComboBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
mAlignmentSnapCheckBox->blockSignals( block );
mAlignmentToleranceSpinBox->blockSignals( block );
}
@@ -57,6 +57,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
void on_mPenWidthSpinBox_valueChanged( double d );
void on_mSelectionToleranceSpinBox_valueChanged( double d );
void on_mAlignmentSnapCheckBox_stateChanged( int state );
void on_mAlignmentToleranceSpinBox_valueChanged( double d );

/**Sets GUI elements to width/height from composition*/
void displayCompositionWidthHeight();
@@ -515,7 +515,7 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,
QPointF snappedPosition = mComposition->snapPointToGrid( currentPosition );

//snap to grid and align to other items
if ( mCurrentMouseMoveAction != QgsComposerItem::MoveItem )
if ( mComposition->alignmentSnap() && mCurrentMouseMoveAction != QgsComposerItem::MoveItem )
{
double alignX = 0;
double alignY = 0;
@@ -608,7 +608,7 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,
{
deleteAlignItems();
}
else //align item
else if ( mComposition->alignmentSnap() ) //align item
{
double alignX = 0;
double alignY = 0;
@@ -49,8 +49,8 @@

QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ) :
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPageWidth( 297 ), mPageHeight( 210 ), mSpaceBetweenPages( 10 ), mPrintAsRaster( false ), mSelectionTolerance( 0.0 ),
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ),
mAtlasComposition( this )
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mAlignmentSnap( true ), mAlignmentSnapTolerance( 2 ),
mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ), mAtlasComposition( this )
{
setBackgroundBrush( Qt::gray );
addPaperItem();
@@ -61,8 +61,8 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ) :

QgsComposition::QgsComposition():
QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPageWidth( 297 ), mPageHeight( 210 ), mSpaceBetweenPages( 10 ), mPrintAsRaster( false ),
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ),
mAtlasComposition( this )
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mAlignmentSnap( true ),
mAlignmentSnapTolerance( 2 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ), mAtlasComposition( this )
{
loadSettings();
}
@@ -303,6 +303,9 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "printResolution", mPrintResolution );
compositionElem.setAttribute( "printAsRaster", mPrintAsRaster );

compositionElem.setAttribute( "alignmentSnap", mAlignmentSnap ? 1 : 0 );
compositionElem.setAttribute( "alignmentSnapTolerance", mAlignmentSnapTolerance );

//save items except paper items and frame items (they are saved with the corresponding multiframe)
QList<QGraphicsItem*> itemList = items();
QList<QGraphicsItem*>::const_iterator itemIt = itemList.constBegin();
@@ -365,8 +368,11 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mSnapGridResolution = compositionElem.attribute( "snapGridResolution" ).toDouble();
mSnapGridOffsetX = compositionElem.attribute( "snapGridOffsetX" ).toDouble();
mSnapGridOffsetY = compositionElem.attribute( "snapGridOffsetY" ).toDouble();
mPrintAsRaster = compositionElem.attribute( "printAsRaster" ).toInt();

mAlignmentSnap = compositionElem.attribute( "alignmentSnap", "1" ).toInt() == 0 ? false : true;
mAlignmentSnapTolerance = compositionElem.attribute( "alignmentSnapTolerance", "2.0" ).toDouble();

mPrintAsRaster = compositionElem.attribute( "printAsRaster" ).toInt();
mPrintResolution = compositionElem.attribute( "printResolution", "300" ).toInt();

updatePaperItems();
@@ -1036,7 +1042,7 @@ QPointF QgsComposition::alignPos( const QPointF& pos, const QgsComposerItem* exc
}

QPointF result( pos.x(), pos.y() );
if ( abs( nearestX - pos.x() ) < 5 )
if ( abs( nearestX - pos.x() ) < mAlignmentSnapTolerance )
{
result.setX( nearestX );
alignX = nearestX;
@@ -1046,7 +1052,7 @@ QPointF QgsComposition::alignPos( const QPointF& pos, const QgsComposerItem* exc
alignX = -1;
}

if ( abs( nearestY - pos.y() ) < 5 )
if ( abs( nearestY - pos.y() ) < mAlignmentSnapTolerance )
{
result.setY( nearestY );
alignY = nearestY;
@@ -1744,7 +1750,7 @@ void QgsComposition::collectAlignCoordinates( QMap< double, const QgsComposerIte
}

void QgsComposition::checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff,
double itemCoordOffset, double& itemCoord, double& alignCoord )
double itemCoordOffset, double& itemCoord, double& alignCoord ) const
{
double currentCoord = 0;
if ( !nearestItem( alignCoords, checkCoord, currentCoord ) )
@@ -1753,7 +1759,7 @@ void QgsComposition::checkNearestItem( double checkCoord, const QMap< double, co
}

double currentDiff = abs( checkCoord - currentCoord );
if ( currentDiff < smallestDiff )
if ( currentDiff < mAlignmentSnapTolerance )
{
itemCoord = currentCoord + itemCoordOffset;
alignCoord = currentCoord;
@@ -115,6 +115,12 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
void setGridStyle( GridStyle s );
GridStyle gridStyle() const {return mGridStyle;}

void setAlignmentSnap( bool s ) { mAlignmentSnap = s; }
bool alignmentSnap() const { return mAlignmentSnap; }

void setAlignmentSnapTolerance( double t ) { mAlignmentSnapTolerance = t; }
double alignmentSnapTolerance() const { return mAlignmentSnapTolerance; }

/**Returns pointer to undo/redo command storage*/
QUndoStack* undoStack() { return &mUndoStack; }

@@ -358,6 +364,10 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
QPen mGridPen;
GridStyle mGridStyle;

/**Parameters for alignment snap*/
bool mAlignmentSnap;
double mAlignmentSnapTolerance;

QUndoStack mUndoStack;

QgsComposerItemCommand* mActiveItemCommand;
@@ -391,8 +401,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
void collectAlignCoordinates( QMap< double, const QgsComposerItem* >& alignCoordsX,
QMap< double, const QgsComposerItem* >& alignCoordsY, const QgsComposerItem* excludeItem );

static void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff,
double itemCoordOffset, double& itemCoord, double& alignCoord );
void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff,
double itemCoordOffset, double& itemCoord, double& alignCoord ) const;

/**Find nearest item in coordinate map to a double.
@return true if item found, false if coords is empty*/

0 comments on commit b7dd4c4

Please sign in to comment.