QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsscalerangewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalerangewidget.cpp
3  --------------------------------------
4  Date : 25.04.2014
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 "qgsscalerangewidget.h"
17 #include "qgsapplication.h"
18 #include "qgsproject.h"
19 #include "qgsscalewidget.h"
20 #include "qgsguiutils.h"
21 #include "qgsprojectviewsettings.h"
22 
24  : QWidget( parent )
25 
26 {
27  mLayout = new QGridLayout( this );
28  mLayout->setContentsMargins( 0, 0, 0, 0 );
29 
30  QLabel *minLbl = new QLabel( tr( "Minimum (exclusive)" ), this );
31  minLbl->setWordWrap( true );
32  minLbl->setAlignment( Qt::AlignTop );
33  minLbl->setToolTip( tr( "Minimum scale, i.e. most \"zoomed out\". "
34  "This limit is exclusive, that means the layer will not be displayed on this scale." ) );
35  QLabel *maxLbl = new QLabel( tr( "Maximum (inclusive)" ), this );
36  maxLbl->setWordWrap( true );
37  maxLbl->setAlignment( Qt::AlignTop );
38  maxLbl->setToolTip( tr( "Maximum scale, i.e. most \"zoomed in\". "
39  "This limit is inclusive, that means the layer will be displayed on this scale." ) );
40 
41  const int iconSize = QgsGuiUtils::scaleIconSize( 24 );
42  mMinimumScaleIconLabel = new QLabel( this );
43  mMinimumScaleIconLabel->setPixmap( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomOut.svg" ) ).pixmap( QSize( iconSize, iconSize ) ) );
44  mMaximumScaleIconLabel = new QLabel( this );
45  mMaximumScaleIconLabel->setPixmap( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomIn.svg" ) ).pixmap( QSize( iconSize, iconSize ) ) );
46 
47  mMinimumScaleWidget = new QgsScaleWidget( this );
48  mMaximumScaleWidget = new QgsScaleWidget( this );
49  connect( mMinimumScaleWidget, &QgsScaleWidget::scaleChanged, mMaximumScaleWidget, &QgsScaleWidget::setMinScale );
50  mMinimumScaleWidget->setShowCurrentScaleButton( true );
51  mMaximumScaleWidget->setShowCurrentScaleButton( true );
53  // add start, add comprehension of scales by settings fake ordered values
54  mMinimumScaleWidget->setScale( 100000 );
55  mMaximumScaleWidget->setScale( 1000 );
56 
57  mLayout->addWidget( minLbl, 0, 0, 1, 2 );
58  mLayout->addWidget( mMinimumScaleIconLabel, 1, 0 );
59  mLayout->addWidget( mMinimumScaleWidget, 1, 1 );
60  mLayout->addWidget( maxLbl, 0, 2, 1, 2 );
61  mLayout->addWidget( mMaximumScaleIconLabel, 1, 2 );
62  mLayout->addWidget( mMaximumScaleWidget, 1, 3 );
63 
64  mLayout->setColumnStretch( 0, 0 );
65  mLayout->setColumnStretch( 1, 3 );
66  mLayout->setColumnStretch( 2, 0 );
67  mLayout->setColumnStretch( 3, 3 );
68 
69  connect( mMinimumScaleWidget, &QgsScaleWidget::scaleChanged, this, &QgsScaleRangeWidget::emitRangeChanged );
70  connect( mMaximumScaleWidget, &QgsScaleWidget::scaleChanged, this, &QgsScaleRangeWidget::emitRangeChanged );
71 }
72 
74 {
75  const bool projectScales = QgsProject::instance()->viewSettings()->useProjectScales();
76  if ( projectScales )
77  {
78  QStringList scalesList;
79  const QVector< double >projectScales = QgsProject::instance()->viewSettings()->mapScales();
80  scalesList.reserve( projectScales.size() );
81  for ( const double scale : projectScales )
82  scalesList << QStringLiteral( "1:%1" ).arg( scale );
83  mMinimumScaleWidget->updateScales( scalesList );
84  mMaximumScaleWidget->updateScales( scalesList );
85  }
86 }
87 
89 {
90  mMinimumScaleWidget->setMapCanvas( mapCanvas );
91  mMaximumScaleWidget->setMapCanvas( mapCanvas );
92 }
93 
95 {
96  mMinimumScaleWidget->setScale( scale );
97 }
98 
100 {
101  return mMinimumScaleWidget->scale();
102 }
103 
105 {
106  mMaximumScaleWidget->setScale( scale );
107 }
108 
110 {
111  return mMaximumScaleWidget->scale();
112 }
113 
114 void QgsScaleRangeWidget::setScaleRange( double min, double max )
115 {
116  setMinimumScale( min );
117  setMaximumScale( max );
118 }
119 
120 void QgsScaleRangeWidget::emitRangeChanged()
121 {
123 }
124 
QgsScaleWidget::setShowCurrentScaleButton
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...
Definition: qgsscalewidget.cpp:54
QgsScaleWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Set the map canvas associated to the current button.
Definition: qgsscalewidget.cpp:60
QgsProject::viewSettings
const QgsProjectViewSettings * viewSettings() const
Returns the project's view settings, which contains settings and properties relating to how a QgsProj...
Definition: qgsproject.cpp:3491
qgsscalerangewidget.h
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsScaleRangeWidget::setScaleRange
void setScaleRange(double min, double max)
Sets the scale range, from min scale (i.e.
Definition: qgsscalerangewidget.cpp:114
QgsScaleRangeWidget::reloadProjectScales
void reloadProjectScales()
Call to reload the preset scales from the current project and apply them to the 2 scales combo boxes.
Definition: qgsscalerangewidget.cpp:73
QgsScaleRangeWidget::maximumScale
double maximumScale
Definition: qgsscalerangewidget.h:36
QgsScaleWidget::updateScales
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
Definition: qgsscalewidget.h:152
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsGuiUtils::iconSize
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
Definition: qgsguiutils.cpp:264
qgsscalewidget.h
qgsapplication.h
QgsScaleRangeWidget::setMaximumScale
void setMaximumScale(double scale)
Set the maximum scale (i.e.
Definition: qgsscalerangewidget.cpp:104
QgsScaleRangeWidget::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas which will be used for the current scale buttons.
Definition: qgsscalerangewidget.cpp:88
QgsScaleRangeWidget::QgsScaleRangeWidget
QgsScaleRangeWidget(QWidget *parent=nullptr)
Constructor for QgsScaleRangeWidget.
Definition: qgsscalerangewidget.cpp:23
QgsScaleWidget
A combobox which lets the user select map scale from predefined list and highlights nearest to curren...
Definition: qgsscalewidget.h:34
QgsScaleWidget::setMinScale
void setMinScale(double scale)
Set the minimum allowed scale.
Definition: qgsscalewidget.h:166
QgsScaleRangeWidget::rangeChanged
void rangeChanged(double min, double max)
Emitted when the scale range set in the widget is changed.
QgsScaleWidget::setScale
void setScale(double scale)
Set the selected scale from a double.
Definition: qgsscalewidget.cpp:94
qgsprojectviewsettings.h
QgsProjectViewSettings::mapScales
QVector< double > mapScales() const
Returns the list of custom project map scales.
Definition: qgsprojectviewsettings.cpp:115
QgsScaleWidget::scaleChanged
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
qgsguiutils.h
QgsGuiUtils::scaleIconSize
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
Definition: qgsguiutils.cpp:259
qgsproject.h
QgsScaleRangeWidget::setMinimumScale
void setMinimumScale(double scale)
Set the minimum scale (i.e.
Definition: qgsscalerangewidget.cpp:94
QgsScaleWidget::scale
bool scale
Definition: qgsscalewidget.h:38
QgsScaleRangeWidget::minimumScale
double minimumScale
Definition: qgsscalerangewidget.h:35
QgsProjectViewSettings::useProjectScales
bool useProjectScales() const
Returns true if project mapScales() are enabled.
Definition: qgsprojectviewsettings.cpp:129