QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
23  : QWidget( parent )
24 {
25  QHBoxLayout *layout = new QHBoxLayout( this );
26  layout->setContentsMargins( 0, 0, 0, 0 );
27  layout->setSpacing( 6 );
28 
29  mScaleComboBox = new QgsScaleComboBox( this );
30  layout->addWidget( mScaleComboBox );
31 
32  mCurrentScaleButton = new QToolButton( this );
33  mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
34  mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );
35  layout->addWidget( mCurrentScaleButton );
36  mCurrentScaleButton->hide();
37 
38  connect( mScaleComboBox, &QgsScaleComboBox::scaleChanged, this, &QgsScaleWidget::scaleChanged );
39  connect( mCurrentScaleButton, &QAbstractButton::clicked, this, &QgsScaleWidget::setScaleFromCanvas );
40 }
41 
43 {
44  mShowCurrentScaleButton = showCurrentScaleButton;
45  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
46 }
47 
49 {
50  mCanvas = canvas;
51  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
52 }
53 
55 {
56  return mScaleComboBox->isNull();
57 }
58 
60 {
61  mScaleComboBox->setAllowNull( allowNull );
62 }
63 
65 {
66  return mScaleComboBox->allowNull();
67 }
68 
70 {
71  if ( !mCanvas )
72  return;
73 
74  setScale( mCanvas->scale() );
75 }
76 
78 {
79  mScaleComboBox->setNull();
80 }
81 
83 {
84  mScaleComboBox->setScale( scale );
85 }
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
void setScaleFromCanvas()
Assigns the current scale from the map canvas, if set.
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
double scale() const
Returns the selected scale as a double.
bool showCurrentScaleButton()
Returns whether a button to set the scale from map canvas is shown or not.
void setScale(double scale)
Set the selected scale from a double.
void setScale(double scale)
Set the selected scale from a double.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
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 setAllowNull(bool allowNull)
Sets whether the scale widget can be set to a NULL value.
QgsScaleWidget(QWidget *parent=nullptr)
QgsScaleWidget creates a combobox which lets the user select map scale from predefined list and highl...
void setNull()
Sets the widget to the null value.
bool allowNull() const
Returns true if the widget can be set to a NULL value.
void setNull()
Sets the combo box to the null value.
double scale() const
Returns the last reported scale of the canvas.
bool isNull() const
Returns true if the widget is currently set to a "null" value.
void setMapCanvas(QgsMapCanvas *canvas)
Set the map canvas associated to the current button.
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...