QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsscalewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalewidget.cpp
3  --------------------------------------
4  Date : 08.01.2015
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15 
16 #include <QHBoxLayout>
17 
18 #include "qgsapplication.h"
19 #include "qgsscalewidget.h"
20 #include "qgsmapcanvas.h"
21 #include "qgsproject.h"
22 #include "qgslayoutmanager.h"
23 #include "qgslayoutitemmap.h"
24 #include "qgsprintlayout.h"
25 
26 #include <QMenu>
27 
29  : QWidget( parent )
30 {
31  QHBoxLayout *layout = new QHBoxLayout( this );
32  layout->setContentsMargins( 0, 0, 0, 0 );
33  layout->setSpacing( 6 );
34 
35  mScaleComboBox = new QgsScaleComboBox( this );
36  layout->addWidget( mScaleComboBox );
37 
38  mCurrentScaleButton = new QToolButton( this );
39  mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
40  mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );
41 
42  mMenu = new QMenu( this );
43  mCurrentScaleButton->setMenu( mMenu );
44  mCurrentScaleButton->setPopupMode( QToolButton::MenuButtonPopup );
45  connect( mMenu, &QMenu::aboutToShow, this, &QgsScaleWidget::menuAboutToShow );
46 
47  layout->addWidget( mCurrentScaleButton );
48  mCurrentScaleButton->hide();
49 
50  connect( mScaleComboBox, &QgsScaleComboBox::scaleChanged, this, &QgsScaleWidget::scaleChanged );
51  connect( mCurrentScaleButton, &QAbstractButton::clicked, this, &QgsScaleWidget::setScaleFromCanvas );
52 }
53 
54 void QgsScaleWidget::setShowCurrentScaleButton( bool showCurrentScaleButton )
55 {
56  mShowCurrentScaleButton = showCurrentScaleButton;
57  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
58 }
59 
61 {
62  mCanvas = canvas;
63  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
64 }
65 
67 {
68  return mScaleComboBox->isNull();
69 }
70 
71 void QgsScaleWidget::setAllowNull( bool allowNull )
72 {
73  mScaleComboBox->setAllowNull( allowNull );
74 }
75 
77 {
78  return mScaleComboBox->allowNull();
79 }
80 
82 {
83  if ( !mCanvas )
84  return;
85 
86  setScale( mCanvas->scale() );
87 }
88 
90 {
91  mScaleComboBox->setNull();
92 }
93 
94 void QgsScaleWidget::setScale( double scale )
95 {
96  mScaleComboBox->setScale( scale );
97 }
98 
99 void QgsScaleWidget::menuAboutToShow()
100 {
101  mMenu->clear();
102 
103  double scale = mCanvas->scale();
104  QAction *canvasScaleAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ),
105  tr( "Use Current Map Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
106  connect( canvasScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
107  mMenu->addAction( canvasScaleAction );
108 
109  bool first = true;
111  {
112  const QList<QgsPrintLayout *> layouts = manager->printLayouts();
113  for ( const QgsPrintLayout *layout : layouts )
114  {
115  QList< QgsLayoutItemMap * > maps;
116  layout->layoutItems( maps );
117  if ( maps.empty() )
118  continue;
119 
120  if ( first )
121  mMenu->addSeparator();
122 
123  first = false;
124 
125  QMenu *layoutMenu = new QMenu( layout->name(), mMenu );
126  for ( const QgsLayoutItemMap *map : std::as_const( maps ) )
127  {
128  scale = map->scale();
129  QAction *mapScaleAction = new QAction( tr( "%1 (1:%2)" ).arg( map->displayName(), qgsDoubleToString( scale, 0 ) ), mMenu );
130  connect( mapScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
131  layoutMenu->addAction( mapScaleAction );
132  }
133  mMenu->addMenu( layoutMenu );
134  }
135  }
136 }
QgsLayoutManager
Manages storage of a set of layouts.
Definition: qgslayoutmanager.h:44
QgsScaleWidget::setShowCurrentScaleButton
void setShowCurrentScaleButton(bool showCurrentScaleButton)
Sets whether to show a button to set the scale to the current scale of the map canvas next to the com...
Definition: qgsscalewidget.cpp:54
QgsScaleWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Set the map canvas associated to the current button.
Definition: qgsscalewidget.cpp:60
qgsmapcanvas.h
QgsScaleWidget::QgsScaleWidget
QgsScaleWidget(QWidget *parent=nullptr)
QgsScaleWidget creates a combobox which lets the user select map scale from predefined list and highl...
Definition: qgsscalewidget.cpp:28
QgsScaleComboBox
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...
Definition: qgsscalecombobox.h:30
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsScaleWidget::setScaleFromCanvas
void setScaleFromCanvas()
Assigns the current scale from the map canvas, if set.
Definition: qgsscalewidget.cpp:81
qgslayoutmanager.h
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsMapCanvas::scale
double scale() const
Returns the last reported scale of the canvas.
Definition: qgsmapcanvas.cpp:367
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
QgsProject::layoutManager
const QgsLayoutManager * layoutManager() const
Returns the project's layout manager, which manages print layouts, atlases and reports within the pro...
Definition: qgsproject.cpp:3461
qgsscalewidget.h
QgsScaleWidget::isNull
bool isNull() const
Returns true if the widget is currently set to a "null" value.
Definition: qgsscalewidget.cpp:66
qgsapplication.h
QgsPrintLayout
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Definition: qgsprintlayout.h:30
QgsScaleComboBox::setNull
void setNull()
Sets the combo box to the null value.
Definition: qgsscalecombobox.cpp:284
QgsScaleComboBox::allowNull
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
Definition: qgsscalecombobox.cpp:270
QgsScaleWidget::allowNull
bool allowNull() const
Returns true if the widget can be set to a NULL value.
Definition: qgsscalewidget.cpp:76
QgsScaleComboBox::setAllowNull
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
Definition: qgsscalecombobox.cpp:263
QgsScaleWidget::setScale
void setScale(double scale)
Set the selected scale from a double.
Definition: qgsscalewidget.cpp:94
QgsScaleComboBox::isNull
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
Definition: qgsscalecombobox.cpp:163
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
qgsprintlayout.h
QgsScaleWidget::scaleChanged
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
QgsScaleWidget::showCurrentScaleButton
bool showCurrentScaleButton
Definition: qgsscalewidget.h:37
QgsScaleWidget::setAllowNull
void setAllowNull(bool allowNull)
Sets whether the scale widget can be set to a NULL value.
Definition: qgsscalewidget.cpp:71
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsScaleWidget::setNull
void setNull()
Sets the widget to the null value.
Definition: qgsscalewidget.cpp:89
QgsScaleComboBox::setScale
void setScale(double scale)
Set the selected scale from a double.
Definition: qgsscalecombobox.cpp:168
QgsScaleComboBox::scaleChanged
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
qgsproject.h
QgsScaleWidget::scale
bool scale
Definition: qgsscalewidget.h:38
qgslayoutitemmap.h