QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
124
125
126
128 : QWidget( parent )
129
130{
131 mMapUnitIdx = -1;
132
133 setupUi( this );
134 mMapScaleButton->setVisible( false );
135 mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
136
137 setFocusPolicy( Qt::StrongFocus );
138 setFocusProxy( mUnitCombo );
139
140 connect( mUnitCombo, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::toggleUnitRangeButton );
141 connect( mMapScaleButton, &QToolButton::clicked, this, &QgsUnitSelectionWidget::showDialog );
142 connect( mUnitCombo, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::changed );
143}
144
145void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
146{
147 blockSignals( true );
148 mUnitCombo->addItems( units );
149 mMapUnitIdx = mapUnitIdx;
150 blockSignals( false );
151}
152
154{
155 blockSignals( true );
156 mUnitCombo->clear();
157
158 //instead of iterating over the units list, we specifically check for presence of unit types
159 //to ensure that the widget always keeps the same order for units, regardless of the
160 //order specified in the units list
161 mMapUnitIdx = -1;
162 if ( units.contains( Qgis::RenderUnit::Millimeters ) )
163 {
164 mUnitCombo->addItem( tr( "Millimeters" ), static_cast< int >( Qgis::RenderUnit::Millimeters ) );
165 }
166 if ( units.contains( Qgis::RenderUnit::Points ) )
167 {
168 mUnitCombo->addItem( tr( "Points" ), static_cast< int >( Qgis::RenderUnit::Points ) );
169 }
170 if ( units.contains( Qgis::RenderUnit::Pixels ) )
171 {
172 mUnitCombo->addItem( tr( "Pixels" ), static_cast< int >( Qgis::RenderUnit::Pixels ) );
173 }
174 if ( units.contains( Qgis::RenderUnit::MetersInMapUnits ) )
175 {
176 mUnitCombo->addItem( tr( "Meters at Scale" ), static_cast< int >( Qgis::RenderUnit::MetersInMapUnits ) );
177 }
178 if ( units.contains( Qgis::RenderUnit::MapUnits ) )
179 {
180 mUnitCombo->addItem( tr( "Map Units" ), static_cast< int >( Qgis::RenderUnit::MapUnits ) );
181 }
182 if ( units.contains( Qgis::RenderUnit::Percentage ) )
183 {
184 mUnitCombo->addItem( tr( "Percentage" ), static_cast< int >( Qgis::RenderUnit::Percentage ) );
185 }
186 if ( units.contains( Qgis::RenderUnit::Inches ) )
187 {
188 mUnitCombo->addItem( tr( "Inches" ), static_cast< int >( Qgis::RenderUnit::Inches ) );
189 }
190 blockSignals( false );
191}
192
194{
195 if ( mUnitCombo->count() == 0 )
197
198 const QVariant currentData = mUnitCombo->currentData();
199 if ( currentData.isValid() )
200 {
201 return static_cast< Qgis::RenderUnit >( currentData.toInt() );
202 }
203 //unknown
205}
206
208{
209 blockSignals( true );
210 mUnitCombo->setCurrentIndex( unitIndex );
211 blockSignals( false );
212}
213
215{
216 const int idx = mUnitCombo->findData( QVariant( static_cast< int >( unit ) ) );
217 mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
218}
219
221{
222 mCanvas = canvas;
223}
224
225void QgsUnitSelectionWidget::showDialog()
226{
228 if ( panel && panel->dockMode() )
229 {
230 QgsMapUnitScaleWidget *widget = new QgsMapUnitScaleWidget( panel );
231 widget->setPanelTitle( tr( "Adjust Scaling Range" ) );
232 widget->setMapCanvas( mCanvas );
233 widget->setMapUnitScale( mMapUnitScale );
234 connect( widget, &QgsMapUnitScaleWidget::mapUnitScaleChanged, this, &QgsUnitSelectionWidget::widgetChanged );
235 panel->openPanel( widget );
236 return;
237 }
238
239 QgsMapUnitScaleDialog dlg( this );
240 dlg.setMapUnitScale( mMapUnitScale );
241 dlg.setMapCanvas( mCanvas );
242 if ( dlg.exec() == QDialog::Accepted )
243 {
244 if ( mMapUnitScale != dlg.getMapUnitScale() )
245 {
246 mMapUnitScale = dlg.getMapUnitScale();
247 emit changed();
248 }
249 }
250}
251
252void QgsUnitSelectionWidget::toggleUnitRangeButton()
253{
255 {
256 mMapScaleButton->setVisible( mShowMapScaleButton && unit() == Qgis::RenderUnit::MapUnits );
257 }
258 else
259 {
260 mMapScaleButton->setVisible( mShowMapScaleButton && mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
261 }
262}
263
264void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale &scale )
265{
266 mMapUnitScale = scale;
267 emit changed();
268}
269
271{
272 return mShowMapScaleButton;
273}
274
276{
277 mShowMapScaleButton = show;
278 if ( !show )
279 mMapScaleButton->hide();
280}
281
282
284 : QDialog( parent )
285
286{
287 QVBoxLayout *vLayout = new QVBoxLayout();
288 mWidget = new QgsMapUnitScaleWidget();
289 vLayout->addWidget( mWidget );
290 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
291 connect( bbox, &QDialogButtonBox::accepted, this, &QgsMapUnitScaleDialog::accept );
292 connect( bbox, &QDialogButtonBox::rejected, this, &QgsMapUnitScaleDialog::reject );
293 connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsMapUnitScaleDialog::showHelp );
294 vLayout->addWidget( bbox );
295 setLayout( vLayout );
296 setWindowTitle( tr( "Adjust Scaling Range" ) );
297}
298
303
305{
306 mWidget->setMapUnitScale( scale );
307}
308
310{
311 mWidget->setMapCanvas( canvas );
312}
313
314void QgsMapUnitScaleDialog::showHelp()
315{
316 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#unit-selector" ) );
317}
RenderUnit
Rendering size units.
Definition qgis.h:4847
@ 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.