Skip to content
Permalink
Browse files

[FEATURE] Move option to snap to guides to composer view menu, add in…

…depenant toggles for showing guides and for enabling smart guides
  • Loading branch information
nyalldawson authored and mhugent committed Oct 29, 2013
1 parent 71b762b commit 3dfb9ff42ed5f878ab5110fbbf0a6f25ecf75b3b
@@ -172,6 +172,9 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )

mActionShowGrid->setCheckable( true );
mActionSnapGrid->setCheckable( true );
mActionShowGuides->setCheckable( true );
mActionSnapGuides->setCheckable( true );
mActionSmartGuides->setCheckable( true );

#ifdef Q_WS_MAC
mActionQuit->setText( tr( "Close" ) );
@@ -258,6 +261,10 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
viewMenu->addSeparator();
viewMenu->addAction( mActionShowGrid );
viewMenu->addAction( mActionSnapGrid );
viewMenu->addSeparator();
viewMenu->addAction( mActionShowGuides );
viewMenu->addAction( mActionSnapGuides );
viewMenu->addAction( mActionSmartGuides );

// Panel and toolbar submenus
mPanelMenu = new QMenu( tr( "Panels" ), this );
@@ -348,9 +355,9 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
connect( mComposition->undoStack(), SIGNAL( canRedoChanged( bool ) ), mActionRedo, SLOT( setEnabled( bool ) ) );
}

restoreGridSettings();
connectSlots();


mComposition->setParent( mView );
mView->setComposition( mComposition );

@@ -677,7 +684,7 @@ void QgsComposer::on_mActionRefreshView_triggered()

void QgsComposer::on_mActionShowGrid_triggered( bool checked )
{
//enable or disable snap items to grid
//show or hide grid
if ( mComposition )
{
mComposition->setGridVisible( checked );
@@ -693,6 +700,33 @@ void QgsComposer::on_mActionSnapGrid_triggered( bool checked )
}
}

void QgsComposer::on_mActionShowGuides_triggered( bool checked )
{
//show or hide guide lines
if ( mComposition )
{
mComposition->setSnapLinesVisible( checked );
}
}

void QgsComposer::on_mActionSnapGuides_triggered( bool checked )
{
//enable or disable snap items to guides
if ( mComposition )
{
mComposition->setAlignmentSnap( checked );
}
}

void QgsComposer::on_mActionSmartGuides_triggered( bool checked )
{
//enable or disable smart snapping guides
if ( mComposition )
{
mComposition->setSmartGuidesEnabled( checked );
}
}

void QgsComposer::on_mActionExportAsPDF_triggered()
{
if ( !mComposition || !mView )
@@ -2102,8 +2136,7 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
}

//restore grid settings
mActionSnapGrid->setChecked( mComposition->snapToGridEnabled() );
mActionShowGrid->setChecked( mComposition->gridVisible() );
restoreGridSettings();

// look for world file composer map, if needed
// Note: this must be done after maps have been added by addItemsFromXML
@@ -2152,6 +2185,17 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
setSelectionTool();
}

void QgsComposer::restoreGridSettings()
{
//restore grid settings
mActionSnapGrid->setChecked( mComposition->snapToGridEnabled() );
mActionShowGrid->setChecked( mComposition->gridVisible() );
//restore guide settings
mActionShowGuides->setChecked( mComposition->snapLinesVisible() );
mActionSnapGuides->setChecked( mComposition->alignmentSnap() );
mActionSmartGuides->setChecked( mComposition->smartGuidesEnabled() );
}

void QgsComposer::deleteItemWidgets()
{
//delete all the items
@@ -291,6 +291,15 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//!Enable or disable snap items to grid
void on_mActionSnapGrid_triggered( bool checked );

//!Show/hide guides
void on_mActionShowGuides_triggered( bool checked );

//!Enable or disable snap items to guides
void on_mActionSnapGuides_triggered( bool checked );

//!Enable or disable smart guides
void on_mActionSmartGuides_triggered( bool checked );

//! Save window state
void saveWindowState();

@@ -382,6 +391,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Write a world file
void writeWorldFile( QString fileName, double a, double b, double c, double d, double e, double f ) const;

//! Updates the grid/guide action status based on compositions grid/guide settings
void restoreGridSettings();

/**Composer title*/
QString mTitle;

@@ -73,8 +73,6 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
connect( mComposition, SIGNAL( composerMapAdded( QgsComposerMap* ) ), this, SLOT( onComposerMapAdded( QgsComposerMap* ) ) );
connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( onItemRemoved( QgsComposerItem* ) ) );


mAlignmentSnapGroupCheckBox->setChecked( mComposition->alignmentSnap() );
mAlignmentToleranceSpinBox->setValue( mComposition->alignmentSnapTolerance() );

//snap grid
@@ -570,14 +568,6 @@ void QgsCompositionWidget::on_mSelectionToleranceSpinBox_valueChanged( double d
}
}

void QgsCompositionWidget::on_mAlignmentSnapGroupCheckBox_toggled( bool state )
{
if ( mComposition )
{
mComposition->setAlignmentSnap( state );
}
}

void QgsCompositionWidget::on_mAlignmentToleranceSpinBox_valueChanged( double d )
{
if ( mComposition )
@@ -603,6 +593,5 @@ void QgsCompositionWidget::blockSignals( bool block )
mGridStyleComboBox->blockSignals( block );
mGridToleranceSpinBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
mAlignmentSnapGroupCheckBox->blockSignals( block );
mAlignmentToleranceSpinBox->blockSignals( block );
}
@@ -60,7 +60,6 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
void on_mGridToleranceSpinBox_valueChanged( double d );
void on_mSelectionToleranceSpinBox_valueChanged( double d );
void on_mAlignmentSnapGroupCheckBox_toggled( bool state );
void on_mAlignmentToleranceSpinBox_valueChanged( double d );

/**Sets GUI elements to width/height from composition*/
@@ -805,7 +805,7 @@ QPointF QgsComposerMouseHandles::snapPoint( const QPointF& point, QgsComposerMou
}

//align item
if ( !mComposition->alignmentSnap() )
if ( !mComposition->alignmentSnap() && !mComposition->smartGuidesEnabled() )
{
return point;
}
@@ -985,38 +985,43 @@ void QgsComposerMouseHandles::collectAlignCoordinates( QMap< double, const QgsCo
alignCoordsX.clear();
alignCoordsY.clear();

QList<QGraphicsItem *> itemList = mComposition->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
if ( mComposition->smartGuidesEnabled() )
{
const QgsComposerItem* currentItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
//don't snap to selected items, since they're the ones that will be snapping to something else
if ( !currentItem || currentItem->selected() )
QList<QGraphicsItem *> itemList = mComposition->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
continue;
const QgsComposerItem* currentItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
//don't snap to selected items, since they're the ones that will be snapping to something else
if ( !currentItem || currentItem->selected() )
{
continue;
}
alignCoordsX.insert( currentItem->transform().dx(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().width(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().center().x(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().top(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().center().y(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().bottom(), currentItem );
}
alignCoordsX.insert( currentItem->transform().dx(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().width(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().center().x(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().top(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().center().y(), currentItem );
alignCoordsY.insert( currentItem->transform().dy() + currentItem->rect().bottom(), currentItem );

}

//arbitrary snap lines
QList< QGraphicsLineItem* >::const_iterator sIt = mComposition->snapLines()->constBegin();
for ( ; sIt != mComposition->snapLines()->constEnd(); ++sIt )
if ( mComposition->alignmentSnap() )
{
double x = ( *sIt )->line().x1();
double y = ( *sIt )->line().y1();
if ( qgsDoubleNear( y, 0.0 ) )
{
alignCoordsX.insert( x, 0 );
}
else
QList< QGraphicsLineItem* >::const_iterator sIt = mComposition->snapLines()->constBegin();
for ( ; sIt != mComposition->snapLines()->constEnd(); ++sIt )
{
alignCoordsY.insert( y, 0 );
double x = ( *sIt )->line().x1();
double y = ( *sIt )->line().y1();
if ( qgsDoubleNear( y, 0.0 ) )
{
alignCoordsX.insert( x, 0 );
}
else
{
alignCoordsY.insert( y, 0 );
}
}
}
}
@@ -66,6 +66,8 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mSnapGridOffsetX( 0.0 )
, mSnapGridOffsetY( 0.0 )
, mAlignmentSnap( true )
, mGuidesVisible( true )
, mSmartGuides( true )
, mAlignmentSnapTolerance( 2 )
, mSelectionHandles( 0 )
, mActiveItemCommand( 0 )
@@ -105,6 +107,8 @@ QgsComposition::QgsComposition()
mSnapGridOffsetX( 0.0 ),
mSnapGridOffsetY( 0.0 ),
mAlignmentSnap( true ),
mGuidesVisible( true ),
mSmartGuides( true ),
mAlignmentSnapTolerance( 2 ),
mSelectionHandles( 0 ),
mActiveItemCommand( 0 ),
@@ -475,6 +479,8 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
}

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

//save items except paper items and frame items (they are saved with the corresponding multiframe)
@@ -549,6 +555,11 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mSnapGridOffsetX = compositionElem.attribute( "snapGridOffsetX" ).toDouble();
mSnapGridOffsetY = compositionElem.attribute( "snapGridOffsetY" ).toDouble();

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

//custom snap lines
QDomNodeList snapLineNodes = compositionElem.elementsByTagName( "SnapLine" );
for ( int i = 0; i < snapLineNodes.size(); ++i )
@@ -562,9 +573,6 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
snapItem->setLine( x1, y1, x2, y2 );
}

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();

@@ -1407,6 +1415,7 @@ QGraphicsLineItem* QgsComposition::addSnapLine()
linePen.setWidthF( 0 );
item->setPen( linePen );
item->setZValue( 100 );
item->setVisible( mGuidesVisible );
addItem( item );
mSnapLines.push_back( item );
return item;
@@ -1421,6 +1430,7 @@ void QgsComposition::removeSnapLine( QGraphicsLineItem* line )

void QgsComposition::setSnapLinesVisible( bool visible )
{
mGuidesVisible = visible;
QList< QGraphicsLineItem* >::iterator it = mSnapLines.begin();
for ( ; it != mSnapLines.end(); ++it )
{
@@ -114,6 +114,16 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
void setGridVisible( bool b );
bool gridVisible() const {return mGridVisible;}

/**Hides / shows custom snap lines*/
void setSnapLinesVisible( bool visible );
bool snapLinesVisible() const {return mGuidesVisible;}

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

void setSmartGuidesEnabled( bool b ) { mSmartGuides = b; };
bool smartGuidesEnabled() const {return mSmartGuides;}

void setSnapGridResolution( double r );
double snapGridResolution() const {return mSnapGridResolution;}

@@ -132,9 +142,6 @@ 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; }

@@ -310,8 +317,6 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
* @note not available in python bindings
*/
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance, QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode > >& snappedItems );
/**Hides / shows custom snap lines*/
void setSnapLinesVisible( bool visible );

/**Allocates new item command and saves initial state in it
@param item target item
@@ -438,6 +443,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene

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

/**Arbitraty snap lines (horizontal and vertical)*/
@@ -307,6 +307,39 @@
<string>Ctrl+&quot;</string>
</property>
</action>
<action name="mActionShowGuides">
<property name="text">
<string>Show Guides</string>
</property>
<property name="toolTip">
<string>Show guides</string>
</property>
<property name="shortcut">
<string>Ctrl+;</string>
</property>
</action>
<action name="mActionSnapGuides">
<property name="text">
<string>Snap to Guides</string>
</property>
<property name="toolTip">
<string>Snap to guides</string>
</property>
<property name="shortcut">
<string>Ctrl+:</string>
</property>
</action>
<action name="mActionSmartGuides">
<property name="text">
<string>Smart Guides</string>
</property>
<property name="toolTip">
<string>Smart guides</string>
</property>
<property name="shortcut">
<string>Ctrl+Alt+;</string>
</property>
</action>
<action name="mActionAddImage">
<property name="icon">
<iconset resource="../../images/images.qrc">

0 comments on commit 3dfb9ff

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