Skip to content

Commit 75b7d14

Browse files
committed
[FEATURE][layouts] Add actions in map item properties to set map item
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.
1 parent cdd23e6 commit 75b7d14

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/app/layout/qgslayoutmapwidget.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ QgsLayoutMapWidget::QgsLayoutMapWidget( QgsLayoutItemMap *item )
4444
connect( mScaleLineEdit, &QLineEdit::editingFinished, this, &QgsLayoutMapWidget::mScaleLineEdit_editingFinished );
4545
connect( mActionSetToCanvasExtent, &QAction::triggered, this, &QgsLayoutMapWidget::setToMapCanvasExtent );
4646
connect( mActionViewExtentInCanvas, &QAction::triggered, this, &QgsLayoutMapWidget::viewExtentInCanvas );
47+
connect( mActionSetToCanvasScale, &QAction::triggered, this, &QgsLayoutMapWidget::setToMapCanvasScale );
48+
connect( mActionViewScaleInCanvas, &QAction::triggered, this, &QgsLayoutMapWidget::viewScaleInCanvas );
4749
connect( mActionUpdatePreview, &QAction::triggered, this, &QgsLayoutMapWidget::updatePreview );
4850
connect( mFollowVisibilityPresetCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMapWidget::mFollowVisibilityPresetCheckBox_stateChanged );
4951
connect( mKeepLayerListCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMapWidget::mKeepLayerListCheckBox_stateChanged );
@@ -558,6 +560,20 @@ void QgsLayoutMapWidget::setToMapCanvasExtent()
558560
mMapItem->layout()->undoStack()->endCommand();
559561
}
560562

563+
void QgsLayoutMapWidget::setToMapCanvasScale()
564+
{
565+
if ( !mMapItem )
566+
{
567+
return;
568+
}
569+
570+
const double newScale = QgisApp::instance()->mapCanvas()->scale();
571+
572+
mMapItem->layout()->undoStack()->beginCommand( mMapItem, tr( "Change Map Scale" ) );
573+
mMapItem->setScale( newScale );
574+
mMapItem->layout()->undoStack()->endCommand();
575+
}
576+
561577
void QgsLayoutMapWidget::viewExtentInCanvas()
562578
{
563579
if ( !mMapItem )
@@ -591,6 +607,17 @@ void QgsLayoutMapWidget::viewExtentInCanvas()
591607
}
592608
}
593609

610+
void QgsLayoutMapWidget::viewScaleInCanvas()
611+
{
612+
if ( !mMapItem )
613+
{
614+
return;
615+
}
616+
617+
const double currentScale = mMapItem->scale();
618+
QgisApp::instance()->mapCanvas()->zoomScale( currentScale );
619+
}
620+
594621
void QgsLayoutMapWidget::mXMinLineEdit_editingFinished()
595622
{
596623
updateComposerExtentFromGui();

src/app/layout/qgslayoutmapwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class QgsLayoutMapWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLayoutM
4444
public slots:
4545
void mScaleLineEdit_editingFinished();
4646
void setToMapCanvasExtent();
47+
void setToMapCanvasScale();
4748
void viewExtentInCanvas();
49+
void viewScaleInCanvas();
4850
void updatePreview();
4951
void mFollowVisibilityPresetCheckBox_stateChanged( int state );
5052
void mKeepLayerListCheckBox_stateChanged( int state );

src/ui/layout/qgslayoutmapwidgetbase.ui

+27
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<addaction name="mActionSetToCanvasExtent"/>
6868
<addaction name="mActionViewExtentInCanvas"/>
6969
<addaction name="separator"/>
70+
<addaction name="mActionSetToCanvasScale"/>
71+
<addaction name="mActionViewScaleInCanvas"/>
72+
<addaction name="separator"/>
7073
<addaction name="mActionMoveContent"/>
7174
</widget>
7275
</item>
@@ -850,6 +853,30 @@
850853
<string>Interactively Edit Map Extent</string>
851854
</property>
852855
</action>
856+
<action name="mActionViewScaleInCanvas">
857+
<property name="icon">
858+
<iconset resource="../../../images/images.qrc">
859+
<normaloff>:/images/themes/default/mActionScaleHighlightFeature.svg</normaloff>:/images/themes/default/mActionScaleHighlightFeature.svg</iconset>
860+
</property>
861+
<property name="text">
862+
<string>View Scale in Map Canvas</string>
863+
</property>
864+
<property name="toolTip">
865+
<string>Set Main Canvas to Match Current Map Scale</string>
866+
</property>
867+
</action>
868+
<action name="mActionSetToCanvasScale">
869+
<property name="icon">
870+
<iconset resource="../../../images/images.qrc">
871+
<normaloff>:/images/themes/default/mActionZoomToBookmark.svg</normaloff>:/images/themes/default/mActionZoomToBookmark.svg</iconset>
872+
</property>
873+
<property name="text">
874+
<string>Set to Map Canvas Scale</string>
875+
</property>
876+
<property name="toolTip">
877+
<string>Set Map Scale to Match Main Canvas Scale</string>
878+
</property>
879+
</action>
853880
</widget>
854881
<layoutdefault spacing="6" margin="11"/>
855882
<customwidgets>

0 commit comments

Comments
 (0)