QGIS API Documentation 3.99.0-Master (d270888f95f)
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 "qgsscalewidget.h"
17
18#include "qgsapplication.h"
19#include "qgslayoutitemmap.h"
20#include "qgslayoutmanager.h"
21#include "qgsmapcanvas.h"
22#include "qgsprintlayout.h"
23#include "qgsproject.h"
24
25#include <QHBoxLayout>
26#include <QMenu>
27#include <QString>
28
29#include "moc_qgsscalewidget.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QWidget( parent )
35{
36 QHBoxLayout *layout = new QHBoxLayout( this );
37 layout->setContentsMargins( 0, 0, 0, 0 );
38 layout->setSpacing( 6 );
39
40 mScaleComboBox = new QgsScaleComboBox( this );
41 layout->addWidget( mScaleComboBox );
42
43 mCurrentScaleButton = new QToolButton( this );
44 mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
45 mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( u"/mActionMapIdentification.svg"_s ) );
46
47 mMenu = new QMenu( this );
48 mCurrentScaleButton->setMenu( mMenu );
49 mCurrentScaleButton->setPopupMode( QToolButton::MenuButtonPopup );
50 connect( mMenu, &QMenu::aboutToShow, this, &QgsScaleWidget::menuAboutToShow );
51
52 layout->addWidget( mCurrentScaleButton );
53 mCurrentScaleButton->hide();
54
55 connect( mScaleComboBox, &QgsScaleComboBox::scaleChanged, this, &QgsScaleWidget::scaleChanged );
56 connect( mCurrentScaleButton, &QAbstractButton::clicked, this, &QgsScaleWidget::setScaleFromCanvas );
57}
58
60{
61 mShowCurrentScaleButton = showCurrentScaleButton;
62 mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
63}
64
66{
67 mCanvas = canvas;
68 mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
69}
70
72{
73 return mScaleComboBox->isNull();
74}
75
77{
78 mScaleComboBox->setAllowNull( allowNull );
79}
80
82{
83 return mScaleComboBox->allowNull();
84}
85
86void QgsScaleWidget::setPredefinedScales( const QVector<double> &scales )
87{
88 mScaleComboBox->setPredefinedScales( scales );
89}
90
92{
93 if ( !mCanvas )
94 return;
95
96 setScale( mCanvas->scale() );
97}
98
100{
101 mScaleComboBox->setNull();
102}
103
105{
106 mScaleComboBox->setScale( scale );
107}
108
109void QgsScaleWidget::menuAboutToShow()
110{
111 mMenu->clear();
112
113 double scale = mCanvas->scale();
114 QAction *canvasScaleAction = new QAction( QgsApplication::getThemeIcon( u"/mActionMapIdentification.svg"_s ), tr( "Use Current Map Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
115 connect( canvasScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
116 mMenu->addAction( canvasScaleAction );
117
118 bool first = true;
119 if ( QgsLayoutManager *manager = QgsProject::instance()->layoutManager() )
120 {
121 const QList<QgsPrintLayout *> layouts = manager->printLayouts();
122 for ( const QgsPrintLayout *layout : layouts )
123 {
124 QList<QgsLayoutItemMap *> maps;
125 layout->layoutItems( maps );
126 if ( maps.empty() )
127 continue;
128
129 if ( first )
130 mMenu->addSeparator();
131
132 first = false;
133
134 QMenu *layoutMenu = new QMenu( layout->name(), mMenu );
135 for ( const QgsLayoutItemMap *map : std::as_const( maps ) )
136 {
137 scale = map->scale();
138 QAction *mapScaleAction = new QAction( tr( "%1 (1:%2)" ).arg( map->displayName(), qgsDoubleToString( scale, 0 ) ), mMenu );
139 connect( mapScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
140 layoutMenu->addAction( mapScaleAction );
141 }
142 mMenu->addMenu( layoutMenu );
143 }
144 }
145}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...
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:6817