QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  , mCanvas( nullptr )
25  , mShowCurrentScaleButton( false )
26 {
27  QHBoxLayout* layout = new QHBoxLayout( this );
28  layout->setContentsMargins( 0, 0, 0, 0 );
29  layout->setSpacing( 2 );
30 
31  mScaleComboBox = new QgsScaleComboBox( this );
32  layout->addWidget( mScaleComboBox );
33 
34  mCurrentScaleButton = new QToolButton( this );
35  mCurrentScaleButton->setToolTip( tr( "Set to current canvas scale" ) );
36  mCurrentScaleButton->setIcon( QgsApplication::getThemeIcon( "/mActionMapIdentification.svg" ) );
37  layout->addWidget( mCurrentScaleButton );
38  mCurrentScaleButton->hide();
39 
40  connect( mScaleComboBox, SIGNAL( scaleChanged( double ) ), this, SIGNAL( scaleChanged( double ) ) );
41  connect( mCurrentScaleButton, SIGNAL( clicked() ), this, SLOT( setScaleFromCanvas() ) );
42 }
43 
44 
46 {
47 }
48 
50 {
51  mShowCurrentScaleButton = showCurrentScaleButton;
52  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
53 }
54 
56 {
57  mCanvas = canvas;
58  mCurrentScaleButton->setVisible( mShowCurrentScaleButton && mCanvas );
59 }
60 
62 {
63  if ( !mCanvas )
64  return;
65 
66  setScale( 1 / mCanvas->scale() );
67 }
68 
70 {
71  return mScaleComboBox->setScale( scale );
72 }
QLayout * layout() const
void setContentsMargins(int left, int top, int right, int bottom)
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void setScaleFromCanvas()
assign the current scale from the map canvas
virtual void setVisible(bool visible)
double scale() const
Function to read the selected scale as double.
bool showCurrentScaleButton()
void setScale(double scale)
Function to set the selected scale from double.
void setScale(double scale)
Function to set the selected scale from double.
void scaleChanged(double scale)
Signal is emitted when user has finished editing/selecting a new scale.
void setIcon(const QIcon &icon)
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
double scale()
Get the last reported scale of the canvas.
void setShowCurrentScaleButton(bool showCurrentScaleButton)
shows a button to set the scale to the current scale of the map canvas next to the combobox ...
void hide()
QgsScaleWidget(QWidget *parent=nullptr)
void setMapCanvas(QgsMapCanvas *canvas)
set the map canvas associated to the current button
virtual ~QgsScaleWidget()
void setToolTip(const QString &)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...