QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
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 "moc_qgsscalewidget.cpp"
21#include "qgsmapcanvas.h"
22#include "qgsproject.h"
23#include "qgslayoutmanager.h"
24#include "qgslayoutitemmap.h"
25#include "qgsprintlayout.h"
26
27#include <QMenu>
28
30 : QWidget( parent )
31{
32 QHBoxLayout *layout = new QHBoxLayout( this );
33 layout->setContentsMargins( 0, 0, 0, 0 );
34 layout->setSpacing( 6 );
35
36 mScaleComboBox = new QgsScaleComboBox( this );
37 layout->addWidget( mScaleComboBox );
38
39 mCurrentScaleButton = new QToolButton( this );
40 mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
41 mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );
42
43 mMenu = new QMenu( this );
44 mCurrentScaleButton->setMenu( mMenu );
45 mCurrentScaleButton->setPopupMode( QToolButton::MenuButtonPopup );
46 connect( mMenu, &QMenu::aboutToShow, this, &QgsScaleWidget::menuAboutToShow );
47
48 layout->addWidget( mCurrentScaleButton );
49 mCurrentScaleButton->hide();
50
51 connect( mScaleComboBox, &QgsScaleComboBox::scaleChanged, this, &QgsScaleWidget::scaleChanged );
52 connect( mCurrentScaleButton, &QAbstractButton::clicked, this, &QgsScaleWidget::setScaleFromCanvas );
53}
54
55void QgsScaleWidget::setShowCurrentScaleButton( bool showCurrentScaleButton )
56{
57 mShowCurrentScaleButton = showCurrentScaleButton;
58 mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
59}
60
62{
63 mCanvas = canvas;
64 mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
65}
66
68{
69 return mScaleComboBox->isNull();
70}
71
72void QgsScaleWidget::setAllowNull( bool allowNull )
73{
74 mScaleComboBox->setAllowNull( allowNull );
75}
76
78{
79 return mScaleComboBox->allowNull();
80}
81
82void QgsScaleWidget::setPredefinedScales( const QVector<double> &scales )
83{
84 mScaleComboBox->setPredefinedScales( scales );
85}
86
88{
89 if ( !mCanvas )
90 return;
91
92 setScale( mCanvas->scale() );
93}
94
96{
97 mScaleComboBox->setNull();
98}
99
100void QgsScaleWidget::setScale( double scale )
101{
102 mScaleComboBox->setScale( scale );
103}
104
105void QgsScaleWidget::menuAboutToShow()
106{
107 mMenu->clear();
108
109 double scale = mCanvas->scale();
110 QAction *canvasScaleAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ), tr( "Use Current Map Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
111 connect( canvasScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
112 mMenu->addAction( canvasScaleAction );
113
114 bool first = true;
116 {
117 const QList<QgsPrintLayout *> layouts = manager->printLayouts();
118 for ( const QgsPrintLayout *layout : layouts )
119 {
120 QList<QgsLayoutItemMap *> maps;
121 layout->layoutItems( maps );
122 if ( maps.empty() )
123 continue;
124
125 if ( first )
126 mMenu->addSeparator();
127
128 first = false;
129
130 QMenu *layoutMenu = new QMenu( layout->name(), mMenu );
131 for ( const QgsLayoutItemMap *map : std::as_const( maps ) )
132 {
133 scale = map->scale();
134 QAction *mapScaleAction = new QAction( tr( "%1 (1:%2)" ).arg( map->displayName(), qgsDoubleToString( scale, 0 ) ), mMenu );
135 connect( mapScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
136 layoutMenu->addAction( mapScaleAction );
137 }
138 mMenu->addMenu( layoutMenu );
139 }
140 }
141}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Layout graphical items for displaying a map.
Manages storage of a set of layouts.
Map canvas is a class for displaying all GIS data types on a canvas.
double scale() const
Returns the last reported scale of the canvas.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
static QgsProject * instance()
Returns the QgsProject singleton instance.
const QgsLayoutManager * layoutManager() const
Returns the project's layout manager, which manages print layouts, atlases and reports within the pro...
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...
void setPredefinedScales(const QVector< double > &scales)
Sets the list of predefined scales to show in the combobox.
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
void setScale(double scale)
Set the selected scale from a double.
void setNull()
Sets the combo box to the null value.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
bool isNull() const
Returns true if the widget is currently set to a "null" value.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
void setScaleFromCanvas()
Assigns the current scale from the map canvas, if set.
bool allowNull() const
Returns true if the widget can be set to a NULL value.
void setMapCanvas(QgsMapCanvas *canvas)
Set the map canvas associated to the current button.
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...
void setNull()
Sets the widget to the null value.
void setPredefinedScales(const QVector< double > &scales)
Sets the list of predefined scales to show in the widget.
bool showCurrentScaleButton
void setScale(double scale)
Set the selected scale from a double.
QgsScaleWidget(QWidget *parent=nullptr)
QgsScaleWidget creates a combobox which lets the user select map scale from predefined list and highl...
void setAllowNull(bool allowNull)
Sets whether the scale widget can be set to a NULL value.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5983