QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsunitselectionwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsunitselectionwidget.h
3 -------------------
4 begin : Mar 24, 2014
5 copyright : (C) 2014 Sandro Mani
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
20#include "moc_qgsunitselectionwidget.cpp"
21#include "qgshelp.h"
22#include <QDialogButtonBox>
23
25 : QgsPanelWidget( parent )
26{
27 setupUi( this );
28 mComboBoxMinScale->setScale( 10000000.0 );
29 mComboBoxMaxScale->setScale( 1 );
30 mSpinBoxMinSize->setShowClearButton( false );
31 mSpinBoxMaxSize->setShowClearButton( false );
32 connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMinComboBox );
33 connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
34 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
35 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, mComboBoxMaxScale, &QgsScaleWidget::setMinScale );
36 connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMinComboBox );
37
38 connect( mCheckBoxMinSize, &QCheckBox::toggled, mSpinBoxMinSize, &QgsDoubleSpinBox::setEnabled );
39 connect( mCheckBoxMaxSize, &QCheckBox::toggled, mSpinBoxMaxSize, &QgsDoubleSpinBox::setEnabled );
40
41 // notification of setting changes
42 connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
43 connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
44 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
45 connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
46 connect( mCheckBoxMinSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
47 connect( mCheckBoxMaxSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
48 connect( mSpinBoxMinSize, static_cast<void ( QgsDoubleSpinBox::* )( double )>( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
49 connect( mSpinBoxMaxSize, static_cast<void ( QgsDoubleSpinBox::* )( double )>( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
50 mBlockSignals = false;
51}
52
54{
55 // can't block signals on the widgets themselves, some use them to update
56 // internal states
57 mBlockSignals = true;
58 mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 10000000 );
59 mCheckBoxMinScale->setChecked( scale.minScale != 0.0 );
60 mComboBoxMinScale->setEnabled( scale.minScale != 0.0 );
61 mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
62 mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
63 mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
64
65 mCheckBoxMinSize->setChecked( scale.minSizeMMEnabled );
66 mSpinBoxMinSize->setEnabled( scale.minSizeMMEnabled );
67 mSpinBoxMinSize->setValue( scale.minSizeMM );
68
69 mCheckBoxMaxSize->setChecked( scale.maxSizeMMEnabled );
70 mSpinBoxMaxSize->setEnabled( scale.maxSizeMMEnabled );
71 mSpinBoxMaxSize->setValue( scale.maxSizeMM );
72 mBlockSignals = false;
73
74 settingsChanged();
75}
76
78{
79 mComboBoxMinScale->setMapCanvas( canvas );
80 mComboBoxMinScale->setShowCurrentScaleButton( true );
81 mComboBoxMaxScale->setMapCanvas( canvas );
82 mComboBoxMaxScale->setShowCurrentScaleButton( true );
83}
84
85void QgsMapUnitScaleWidget::configureMinComboBox()
86{
87 mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
88 if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() < mComboBoxMaxScale->scale() )
89 {
90 mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
91 }
92}
93
94void QgsMapUnitScaleWidget::configureMaxComboBox()
95{
96 mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
97 if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() > mComboBoxMinScale->scale() )
98 {
99 mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
100 }
101}
102
103void QgsMapUnitScaleWidget::settingsChanged()
104{
105 if ( mBlockSignals )
106 return;
107
109}
110
112{
113 QgsMapUnitScale scale;
114 scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
115 scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
116 scale.minSizeMMEnabled = mCheckBoxMinSize->isChecked();
117 scale.minSizeMM = mSpinBoxMinSize->value();
118 scale.maxSizeMMEnabled = mCheckBoxMaxSize->isChecked();
119 scale.maxSizeMM = mSpinBoxMaxSize->value();
120 return scale;
121}
122
123
125 : QWidget( parent )
126
127{
128 mMapUnitIdx = -1;
129
130 setupUi( this );
131 mMapScaleButton->setVisible( false );
132 mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
133
134 setFocusPolicy( Qt::StrongFocus );
135 setFocusProxy( mUnitCombo );
136
137 connect( mUnitCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::toggleUnitRangeButton );
138 connect( mMapScaleButton, &QToolButton::clicked, this, &QgsUnitSelectionWidget::showDialog );
139 connect( mUnitCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::changed );
140}
141
142void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
143{
144 blockSignals( true );
145 mUnitCombo->addItems( units );
146 mMapUnitIdx = mapUnitIdx;
147 blockSignals( false );
148}
149
151{
152 blockSignals( true );
153 mUnitCombo->clear();
154
155 //instead of iterating over the units list, we specifically check for presence of unit types
156 //to ensure that the widget always keeps the same order for units, regardless of the
157 //order specified in the units list
158 mMapUnitIdx = -1;
159 if ( units.contains( Qgis::RenderUnit::Millimeters ) )
160 {
161 mUnitCombo->addItem( tr( "Millimeters" ), static_cast<int>( Qgis::RenderUnit::Millimeters ) );
162 }
163 if ( units.contains( Qgis::RenderUnit::Points ) )
164 {
165 mUnitCombo->addItem( tr( "Points" ), static_cast<int>( Qgis::RenderUnit::Points ) );
166 }
167 if ( units.contains( Qgis::RenderUnit::Pixels ) )
168 {
169 mUnitCombo->addItem( tr( "Pixels" ), static_cast<int>( Qgis::RenderUnit::Pixels ) );
170 }
171 if ( units.contains( Qgis::RenderUnit::MetersInMapUnits ) )
172 {
173 mUnitCombo->addItem( tr( "Meters at Scale" ), static_cast<int>( Qgis::RenderUnit::MetersInMapUnits ) );
174 }
175 if ( units.contains( Qgis::RenderUnit::MapUnits ) )
176 {
177 mUnitCombo->addItem( tr( "Map Units" ), static_cast<int>( Qgis::RenderUnit::MapUnits ) );
178 }
179 if ( units.contains( Qgis::RenderUnit::Percentage ) )
180 {
181 mUnitCombo->addItem( tr( "Percentage" ), static_cast<int>( Qgis::RenderUnit::Percentage ) );
182 }
183 if ( units.contains( Qgis::RenderUnit::Inches ) )
184 {
185 mUnitCombo->addItem( tr( "Inches" ), static_cast<int>( Qgis::RenderUnit::Inches ) );
186 }
187 blockSignals( false );
188}
189
191{
192 if ( mUnitCombo->count() == 0 )
194
195 const QVariant currentData = mUnitCombo->currentData();
196 if ( currentData.isValid() )
197 {
198 return static_cast<Qgis::RenderUnit>( currentData.toInt() );
199 }
200 //unknown
202}
203
205{
206 blockSignals( true );
207 mUnitCombo->setCurrentIndex( unitIndex );
208 blockSignals( false );
209}
210
212{
213 const int idx = mUnitCombo->findData( QVariant( static_cast<int>( unit ) ) );
214 mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
215}
216
218{
219 mCanvas = canvas;
220}
221
222void QgsUnitSelectionWidget::showDialog()
223{
225 if ( panel && panel->dockMode() )
226 {
227 QgsMapUnitScaleWidget *widget = new QgsMapUnitScaleWidget( panel );
228 widget->setPanelTitle( tr( "Adjust Scaling Range" ) );
229 widget->setMapCanvas( mCanvas );
230 widget->setMapUnitScale( mMapUnitScale );
231 connect( widget, &QgsMapUnitScaleWidget::mapUnitScaleChanged, this, &QgsUnitSelectionWidget::widgetChanged );
232 panel->openPanel( widget );
233 return;
234 }
235
236 QgsMapUnitScaleDialog dlg( this );
237 dlg.setMapUnitScale( mMapUnitScale );
238 dlg.setMapCanvas( mCanvas );
239 if ( dlg.exec() == QDialog::Accepted )
240 {
241 if ( mMapUnitScale != dlg.getMapUnitScale() )
242 {
243 mMapUnitScale = dlg.getMapUnitScale();
244 emit changed();
245 }
246 }
247}
248
249void QgsUnitSelectionWidget::toggleUnitRangeButton()
250{
252 {
253 mMapScaleButton->setVisible( mShowMapScaleButton && unit() == Qgis::RenderUnit::MapUnits );
254 }
255 else
256 {
257 mMapScaleButton->setVisible( mShowMapScaleButton && mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
258 }
259}
260
261void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale &scale )
262{
263 mMapUnitScale = scale;
264 emit changed();
265}
266
268{
269 return mShowMapScaleButton;
270}
271
273{
274 mShowMapScaleButton = show;
275 if ( !show )
276 mMapScaleButton->hide();
277}
278
279
281 : QDialog( parent )
282
283{
284 QVBoxLayout *vLayout = new QVBoxLayout();
285 mWidget = new QgsMapUnitScaleWidget();
286 vLayout->addWidget( mWidget );
287 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
288 connect( bbox, &QDialogButtonBox::accepted, this, &QgsMapUnitScaleDialog::accept );
289 connect( bbox, &QDialogButtonBox::rejected, this, &QgsMapUnitScaleDialog::reject );
290 connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsMapUnitScaleDialog::showHelp );
291 vLayout->addWidget( bbox );
292 setLayout( vLayout );
293 setWindowTitle( tr( "Adjust Scaling Range" ) );
294}
295
300
302{
303 mWidget->setMapUnitScale( scale );
304}
305
307{
308 mWidget->setMapCanvas( canvas );
309}
310
311void QgsMapUnitScaleDialog::showHelp()
312{
313 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#unit-selector" ) );
314}
RenderUnit
Rendering size units.
Definition qgis.h:4910
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size)
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ Unknown
Mixed or unknown units.
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Map canvas is a class for displaying all GIS data types on a canvas.
A dialog which allows the user to choose the minimum and maximum scale of an object in map units and ...
QgsMapUnitScale getMapUnitScale() const
Returns a QgsMapUnitScale representing the settings shown in the dialog.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
void setMapUnitScale(const QgsMapUnitScale &scale)
Updates the dialog to reflect the settings from the specified QgsMapUnitScale object.
QgsMapUnitScaleDialog(QWidget *parent=nullptr)
Constructor for QgsMapUnitScaleDialog.
A widget which allows the user to choose the minimum and maximum scale of an object in map units and ...
QgsMapUnitScaleWidget(QWidget *parent=nullptr)
Constructor for QgsMapUnitScaleWidget.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void mapUnitScaleChanged(const QgsMapUnitScale &scale)
Emitted when the settings in the widget are modified.
void setMapUnitScale(const QgsMapUnitScale &scale)
Updates the widget to reflect the settings from the specified QgsMapUnitScale object.
Struct for storing maximum and minimum scales for measurements in map units.
bool minSizeMMEnabled
Whether the minimum size in mm should be respected.
double maxScale
The maximum scale, or 0.0 if unset.
double minScale
The minimum scale, or 0.0 if unset.
double maxSizeMM
The maximum size in millimeters, or 0.0 if unset.
bool maxSizeMMEnabled
Whether the maximum size in mm should be respected.
double minSizeMM
The minimum size in millimeters, or 0.0 if unset.
Base class for any widget that can be shown as a inline panel.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool dockMode()
Returns the dock mode state.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
void setMinScale(double scale)
Set the minimum allowed scale.
void changed()
Emitted when the selected unit is changed, or the definition of the map unit scale is changed.
QgsUnitSelectionWidget(QWidget *parent=nullptr)
Constructor for QgsUnitSelectionWidget.
bool showMapScaleButton() const
Returns true if the widget can show the map scale button when the Map Units option is selected.
void setShowMapScaleButton(bool show)
Sets whether the widget can show the map scale button when the Map Units option is selected.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setUnits(const QStringList &units, int mapUnitIdx)
Sets the units which the user can choose from in the combobox.
void setUnit(int unitIndex)
Sets the selected unit index.
Qgis::RenderUnit unit() const
Returns the current predefined selected unit (if applicable).
QList< Qgis::RenderUnit > RenderUnitList
List of render units.