Skip to content

Commit

Permalink
[FEATURE][layouts] Add actions in map item properties to set map item
Browse files Browse the repository at this point in the history
scale to match main canvas scale, and to set main canvas scale to
match map's scale

These mirror the existing settings for setting the extent from
the canvas and pushing the extent TO the canvas.
  • Loading branch information
nyalldawson committed Dec 18, 2018
1 parent cdd23e6 commit 75b7d14
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/app/layout/qgslayoutmapwidget.cpp
Expand Up @@ -44,6 +44,8 @@ QgsLayoutMapWidget::QgsLayoutMapWidget( QgsLayoutItemMap *item )
connect( mScaleLineEdit, &QLineEdit::editingFinished, this, &QgsLayoutMapWidget::mScaleLineEdit_editingFinished );
connect( mActionSetToCanvasExtent, &QAction::triggered, this, &QgsLayoutMapWidget::setToMapCanvasExtent );
connect( mActionViewExtentInCanvas, &QAction::triggered, this, &QgsLayoutMapWidget::viewExtentInCanvas );
connect( mActionSetToCanvasScale, &QAction::triggered, this, &QgsLayoutMapWidget::setToMapCanvasScale );
connect( mActionViewScaleInCanvas, &QAction::triggered, this, &QgsLayoutMapWidget::viewScaleInCanvas );
connect( mActionUpdatePreview, &QAction::triggered, this, &QgsLayoutMapWidget::updatePreview );
connect( mFollowVisibilityPresetCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMapWidget::mFollowVisibilityPresetCheckBox_stateChanged );
connect( mKeepLayerListCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMapWidget::mKeepLayerListCheckBox_stateChanged );
Expand Down Expand Up @@ -558,6 +560,20 @@ void QgsLayoutMapWidget::setToMapCanvasExtent()
mMapItem->layout()->undoStack()->endCommand();
}

void QgsLayoutMapWidget::setToMapCanvasScale()
{
if ( !mMapItem )
{
return;
}

const double newScale = QgisApp::instance()->mapCanvas()->scale();

mMapItem->layout()->undoStack()->beginCommand( mMapItem, tr( "Change Map Scale" ) );
mMapItem->setScale( newScale );
mMapItem->layout()->undoStack()->endCommand();
}

void QgsLayoutMapWidget::viewExtentInCanvas()
{
if ( !mMapItem )
Expand Down Expand Up @@ -591,6 +607,17 @@ void QgsLayoutMapWidget::viewExtentInCanvas()
}
}

void QgsLayoutMapWidget::viewScaleInCanvas()
{
if ( !mMapItem )
{
return;
}

const double currentScale = mMapItem->scale();
QgisApp::instance()->mapCanvas()->zoomScale( currentScale );
}

void QgsLayoutMapWidget::mXMinLineEdit_editingFinished()
{
updateComposerExtentFromGui();
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout/qgslayoutmapwidget.h
Expand Up @@ -44,7 +44,9 @@ class QgsLayoutMapWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLayoutM
public slots:
void mScaleLineEdit_editingFinished();
void setToMapCanvasExtent();
void setToMapCanvasScale();
void viewExtentInCanvas();
void viewScaleInCanvas();
void updatePreview();
void mFollowVisibilityPresetCheckBox_stateChanged( int state );
void mKeepLayerListCheckBox_stateChanged( int state );
Expand Down
27 changes: 27 additions & 0 deletions src/ui/layout/qgslayoutmapwidgetbase.ui
Expand Up @@ -67,6 +67,9 @@
<addaction name="mActionSetToCanvasExtent"/>
<addaction name="mActionViewExtentInCanvas"/>
<addaction name="separator"/>
<addaction name="mActionSetToCanvasScale"/>
<addaction name="mActionViewScaleInCanvas"/>
<addaction name="separator"/>
<addaction name="mActionMoveContent"/>
</widget>
</item>
Expand Down Expand Up @@ -850,6 +853,30 @@
<string>Interactively Edit Map Extent</string>
</property>
</action>
<action name="mActionViewScaleInCanvas">
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionScaleHighlightFeature.svg</normaloff>:/images/themes/default/mActionScaleHighlightFeature.svg</iconset>
</property>
<property name="text">
<string>View Scale in Map Canvas</string>
</property>
<property name="toolTip">
<string>Set Main Canvas to Match Current Map Scale</string>
</property>
</action>
<action name="mActionSetToCanvasScale">
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionZoomToBookmark.svg</normaloff>:/images/themes/default/mActionZoomToBookmark.svg</iconset>
</property>
<property name="text">
<string>Set to Map Canvas Scale</string>
</property>
<property name="toolTip">
<string>Set Map Scale to Match Main Canvas Scale</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
Expand Down

2 comments on commit 75b7d14

@lajunek
Copy link

@lajunek lajunek commented on 75b7d14 Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyalldawson for documentation 3.4: is this feature only available in 3.6? when I lock the legend item this have the effect that it doesn't the main canvas scale doesn't match the map canvas scale anymore?

@lajunek
Copy link

@lajunek lajunek commented on 75b7d14 Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nyalldawson for documentation 3.4: is this feature only available in 3.6? when I lock the legend item, does this have the effect that the main canvas scale doesn't match the map canvas scale anymore?

Please sign in to comment.