Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Show a menu next to scale widget buttons, to allow setting
the widget directly to a scale from a print layout map
  • Loading branch information
nyalldawson committed Apr 2, 2020
1 parent a3f933a commit 3094534
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/gui/qgsscalewidget.cpp
Expand Up @@ -18,6 +18,12 @@
#include "qgsapplication.h"
#include "qgsscalewidget.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgslayoutmanager.h"
#include "qgslayoutitemmap.h"
#include "qgsprintlayout.h"

#include <QMenu>

QgsScaleWidget::QgsScaleWidget( QWidget *parent )
: QWidget( parent )
Expand All @@ -32,6 +38,12 @@ QgsScaleWidget::QgsScaleWidget( QWidget *parent )
mCurrentScaleButton = new QToolButton( this );
mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );

mMenu = new QMenu( this );
mCurrentScaleButton->setMenu( mMenu );
mCurrentScaleButton->setPopupMode( QToolButton::MenuButtonPopup );
connect( mMenu, &QMenu::aboutToShow, this, &QgsScaleWidget::menuAboutToShow );

layout->addWidget( mCurrentScaleButton );
mCurrentScaleButton->hide();

Expand Down Expand Up @@ -83,3 +95,42 @@ void QgsScaleWidget::setScale( double scale )
{
mScaleComboBox->setScale( scale );
}

void QgsScaleWidget::menuAboutToShow()
{
mMenu->clear();

double scale = mCanvas->scale();
QAction *canvasScaleAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ),
tr( "Current Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
connect( canvasScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
mMenu->addAction( canvasScaleAction );

bool first = true;
if ( QgsLayoutManager *manager = QgsProject::instance()->layoutManager() )
{
const QList<QgsPrintLayout *> layouts = manager->printLayouts();
for ( const QgsPrintLayout *layout : layouts )
{
QList< QgsLayoutItemMap * > maps;
layout->layoutItems( maps );
if ( maps.empty() )
continue;

if ( first )
mMenu->addSeparator();

first = false;

QMenu *layoutMenu = new QMenu( layout->name(), mMenu );
for ( const QgsLayoutItemMap *map : qgis::as_const( maps ) )
{
scale = map->scale();
QAction *mapScaleAction = new QAction( tr( "%1 (1:%2)" ).arg( map->displayName(), qgsDoubleToString( scale, 0 ) ), mMenu );
connect( mapScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
layoutMenu->addAction( mapScaleAction );
}
mMenu->addMenu( layoutMenu );
}
}
}
5 changes: 5 additions & 0 deletions src/gui/qgsscalewidget.h
Expand Up @@ -184,10 +184,15 @@ class GUI_EXPORT QgsScaleWidget : public QWidget
*/
void scaleChanged( double scale );

private slots:

void menuAboutToShow();

private:
QgsScaleComboBox *mScaleComboBox = nullptr;
QToolButton *mCurrentScaleButton = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QMenu *mMenu = nullptr;
bool mShowCurrentScaleButton = false;
};

Expand Down

0 comments on commit 3094534

Please sign in to comment.