QGIS API Documentation 3.99.0-Master (d270888f95f)
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
21#include "qgshelp.h"
22
23#include <QDialogButtonBox>
24#include <QString>
25
26#include "moc_qgsunitselectionwidget.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QgsPanelWidget( parent )
32{
33 setupUi( this );
34 mComboBoxMinScale->setScale( 10000000.0 );
35 mComboBoxMaxScale->setScale( 1 );
36 mSpinBoxMinSize->setShowClearButton( false );
37 mSpinBoxMaxSize->setShowClearButton( false );
38 connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMinComboBox );
39 connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
40 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
41 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, mComboBoxMaxScale, &QgsScaleWidget::setMinScale );
42 connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMinComboBox );
43
44 connect( mCheckBoxMinSize, &QCheckBox::toggled, mSpinBoxMinSize, &QgsDoubleSpinBox::setEnabled );
45 connect( mCheckBoxMaxSize, &QCheckBox::toggled, mSpinBoxMaxSize, &QgsDoubleSpinBox::setEnabled );
46
47 // notification of setting changes
48 connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
49 connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
50 connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
51 connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
52 connect( mCheckBoxMinSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
53 connect( mCheckBoxMaxSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
54 connect( mSpinBoxMinSize, static_cast<void ( QgsDoubleSpinBox::* )( double )>( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
55 connect( mSpinBoxMaxSize, static_cast<void ( QgsDoubleSpinBox::* )( double )>( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
56 mBlockSignals = false;
57}
58
60{
61 // can't block signals on the widgets themselves, some use them to update
62 // internal states
63 mBlockSignals = true;
64 mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 10000000 );
65 mCheckBoxMinScale->setChecked( scale.minScale != 0.0 );
66 mComboBoxMinScale->setEnabled( scale.minScale != 0.0 );
67 mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
68 mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
69 mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
70
71 mCheckBoxMinSize->setChecked( scale.minSizeMMEnabled );
72 mSpinBoxMinSize->setEnabled( scale.minSizeMMEnabled );
73 mSpinBoxMinSize->setValue( scale.minSizeMM );
74
75 mCheckBoxMaxSize->setChecked( scale.maxSizeMMEnabled );
76 mSpinBoxMaxSize->setEnabled( scale.maxSizeMMEnabled );
77 mSpinBoxMaxSize->setValue( scale.maxSizeMM );
78 mBlockSignals = false;
79
80 settingsChanged();
81}
82
84{
85 mComboBoxMinScale->setMapCanvas( canvas );
86 mComboBoxMinScale->setShowCurrentScaleButton( true );
87 mComboBoxMaxScale->setMapCanvas( canvas );
88 mComboBoxMaxScale->setShowCurrentScaleButton( true );
89}
90
91void QgsMapUnitScaleWidget::configureMinComboBox()
92{
93 mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
94 if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() < mComboBoxMaxScale->scale() )
95 {
96 mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
97 }
98}
99
100void QgsMapUnitScaleWidget::configureMaxComboBox()
101{
102 mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
103 if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() > mComboBoxMinScale->scale() )
104 {
105 mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
106 }
107}
108
109void QgsMapUnitScaleWidget::settingsChanged()
110{
111 if ( mBlockSignals )
112 return;
113
115}
116
118{
119 QgsMapUnitScale scale;
120 scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
121 scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
122 scale.minSizeMMEnabled = mCheckBoxMinSize->isChecked();
123 scale.minSizeMM = mSpinBoxMinSize->value();
124 scale.maxSizeMMEnabled = mCheckBoxMaxSize->isChecked();
125 scale.maxSizeMM = mSpinBoxMaxSize->value();
126 return scale;
127}
128
129
131 : QWidget( parent )
132
133{
134 mMapUnitIdx = -1;
135
136 setupUi( this );
137 mMapScaleButton->setVisible( false );
138 mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
139
140 setFocusPolicy( Qt::StrongFocus );
141 setFocusProxy( mUnitCombo );
142
143 connect( mUnitCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::toggleUnitRangeButton );
144 connect( mMapScaleButton, &QToolButton::clicked, this, &QgsUnitSelectionWidget::showDialog );
145 connect( mUnitCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::changed );
146}
147
148void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
149{
150 blockSignals( true );
151 mUnitCombo->addItems( units );
152 mMapUnitIdx = mapUnitIdx;
153 blockSignals( false );
154}
155
157{
158 blockSignals( true );
159 mUnitCombo->clear();
160
161 //instead of iterating over the units list, we specifically check for presence of unit types
162 //to ensure that the widget always keeps the same order for units, regardless of the
163 //order specified in the units list
164 mMapUnitIdx = -1;
165 if ( units.contains( Qgis::RenderUnit::Millimeters ) )
166 {
167 mUnitCombo->addItem( tr( "Millimeters" ), static_cast<int>( Qgis::RenderUnit::Millimeters ) );
168 }
169 if ( units.contains( Qgis::RenderUnit::Points ) )
170 {
171 mUnitCombo->addItem( tr( "Points" ), static_cast<int>( Qgis::RenderUnit::Points ) );
172 }
173 if ( units.contains( Qgis::RenderUnit::Pixels ) )
174 {
175 mUnitCombo->addItem( tr( "Pixels" ), static_cast<int>( Qgis::RenderUnit::Pixels ) );
176 }
177 if ( units.contains( Qgis::RenderUnit::MetersInMapUnits ) )
178 {
179 mUnitCombo->addItem( tr( "Meters at Scale" ), static_cast<int>( Qgis::RenderUnit::MetersInMapUnits ) );
180 }
181 if ( units.contains( Qgis::RenderUnit::MapUnits ) )
182 {
183 mUnitCombo->addItem( tr( "Map Units" ), static_cast<int>( Qgis::RenderUnit::MapUnits ) );
184 }
185 if ( units.contains( Qgis::RenderUnit::Percentage ) )
186 {
187 mUnitCombo->addItem( tr( "Percentage" ), static_cast<int>( Qgis::RenderUnit::Percentage ) );
188 }
189 if ( units.contains( Qgis::RenderUnit::Inches ) )
190 {
191 mUnitCombo->addItem( tr( "Inches" ), static_cast<int>( Qgis::RenderUnit::Inches ) );
192 }
193 blockSignals( false );
194}
195
197{
198 if ( mUnitCombo->count() == 0 )
200
201 const QVariant currentData = mUnitCombo->currentData();
202 if ( currentData.isValid() )
203 {
204 return static_cast<Qgis::RenderUnit>( currentData.toInt() );
205 }
206 //unknown
208}
209
211{
212 blockSignals( true );
213 mUnitCombo->setCurrentIndex( unitIndex );
214 blockSignals( false );
215}
216
218{
219 const int idx = mUnitCombo->findData( QVariant( static_cast<int>( unit ) ) );
220 mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
221}
222
224{
225 mCanvas = canvas;
226}
227
228void QgsUnitSelectionWidget::showDialog()
229{
231 if ( panel && panel->dockMode() )
232 {
233 QgsMapUnitScaleWidget *widget = new QgsMapUnitScaleWidget( panel );
234 widget->setPanelTitle( tr( "Adjust Scaling Range" ) );
235 widget->setMapCanvas( mCanvas );
236 widget->setMapUnitScale( mMapUnitScale );
237 connect( widget, &QgsMapUnitScaleWidget::mapUnitScaleChanged, this, &QgsUnitSelectionWidget::widgetChanged );
238 panel->openPanel( widget );
239 return;
240 }
241
242 QgsMapUnitScaleDialog dlg( this );
243 dlg.setMapUnitScale( mMapUnitScale );
244 dlg.setMapCanvas( mCanvas );
245 if ( dlg.exec() == QDialog::Accepted )
246 {
247 if ( mMapUnitScale != dlg.getMapUnitScale() )
248 {
249 mMapUnitScale = dlg.getMapUnitScale();
250 emit changed();
251 }
252 }
253}
254
255void QgsUnitSelectionWidget::toggleUnitRangeButton()
256{
258 {
259 mMapScaleButton->setVisible( mShowMapScaleButton && unit() == Qgis::RenderUnit::MapUnits );
260 }
261 else
262 {
263 mMapScaleButton->setVisible( mShowMapScaleButton && mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
264 }
265}
266
267void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale &scale )
268{
269 mMapUnitScale = scale;
270 emit changed();
271}
272
274{
275 return mShowMapScaleButton;
276}
277
279{
280 mShowMapScaleButton = show;
281 if ( !show )
282 mMapScaleButton->hide();
283}
284
285
287 : QDialog( parent )
288
289{
290 QVBoxLayout *vLayout = new QVBoxLayout();
291 mWidget = new QgsMapUnitScaleWidget();
292 vLayout->addWidget( mWidget );
293 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
294 connect( bbox, &QDialogButtonBox::accepted, this, &QgsMapUnitScaleDialog::accept );
295 connect( bbox, &QDialogButtonBox::rejected, this, &QgsMapUnitScaleDialog::reject );
296 connect( bbox, &QDialogButtonBox::helpRequested, this, &QgsMapUnitScaleDialog::showHelp );
297 vLayout->addWidget( bbox );
298 setLayout( vLayout );
299 setWindowTitle( tr( "Adjust Scaling Range" ) );
300}
301
303{
304 return mWidget->mapUnitScale();
305}
306
308{
309 mWidget->setMapUnitScale( scale );
310}
311
313{
314 mWidget->setMapCanvas( canvas );
315}
316
317void QgsMapUnitScaleDialog::showHelp()
318{
319 QgsHelp::openHelp( u"introduction/general_tools.html#unit-selector"_s );
320}
RenderUnit
Rendering size units.
Definition qgis.h:5255
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5259
@ Millimeters
Millimeters.
Definition qgis.h:5256
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5260
@ Unknown
Mixed or unknown units.
Definition qgis.h:5262
@ MapUnits
Map units.
Definition qgis.h:5257
@ Pixels
Pixels.
Definition qgis.h:5258
@ Inches
Inches.
Definition qgis.h:5261
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5263
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:41
Map canvas is a class for displaying all GIS data types on a canvas.
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 an 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 ...
bool dockMode() const
Returns the dock mode state.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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.
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.