Skip to content

Commit

Permalink
[FEATURE] Move option to snap to guides to composer view menu, add in…
Browse files Browse the repository at this point in the history
…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 3dfb9ff
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 50 deletions.
52 changes: 48 additions & 4 deletions src/app/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
Expand All @@ -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 )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/app/composer/qgscomposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand Down
11 changes: 0 additions & 11 deletions src/app/composer/qgscompositionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -603,6 +593,5 @@ void QgsCompositionWidget::blockSignals( bool block )
mGridStyleComboBox->blockSignals( block );
mGridToleranceSpinBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
mAlignmentSnapGroupCheckBox->blockSignals( block );
mAlignmentToleranceSpinBox->blockSignals( block );
}
1 change: 0 additions & 1 deletion src/app/composer/qgscompositionwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down
55 changes: 30 additions & 25 deletions src/core/composer/qgscomposermousehandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ QPointF QgsComposerMouseHandles::snapPoint( const QPointF& point, QgsComposerMou
}

//align item
if ( !mComposition->alignmentSnap() )
if ( !mComposition->alignmentSnap() && !mComposition->smartGuidesEnabled() )
{
return point;
}
Expand Down Expand Up @@ -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 );
}
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -105,6 +107,8 @@ QgsComposition::QgsComposition()
mSnapGridOffsetX( 0.0 ),
mSnapGridOffsetY( 0.0 ),
mAlignmentSnap( true ),
mGuidesVisible( true ),
mSmartGuides( true ),
mAlignmentSnapTolerance( 2 ),
mSelectionHandles( 0 ),
mActiveItemCommand( 0 ),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 )
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand All @@ -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 )
{
Expand Down
17 changes: 12 additions & 5 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand All @@ -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; }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)*/
Expand Down
33 changes: 33 additions & 0 deletions src/ui/qgscomposerbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Loading

0 comments on commit 3dfb9ff

Please sign in to comment.