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