QGIS API Documentation  3.6.0-Noosa (5873452)
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  if ( !mCanvas )
57  return;
58 
59  setScale( mCanvas->scale() );
60 }
61 
63 {
64  mScaleComboBox->setScale( scale );
65 }
void setScaleFromCanvas()
Assigns the current scale from the map canvas, if set.
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...
QgsScaleWidget(QWidget *parent=nullptr)
QgsScaleWidget creates a combobox which lets the user select map scale from predefined list and highl...
double scale() const
Returns the last reported scale of the canvas.
void setMapCanvas(QgsMapCanvas *canvas)
Set the map canvas associated to the current button.
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...