QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QDialogButtonBox>
21 
23  : QgsPanelWidget( parent )
24 {
25  setupUi( this );
26  mComboBoxMinScale->setScale( 10000000.0 );
27  mComboBoxMaxScale->setScale( 1 );
28  mSpinBoxMinSize->setShowClearButton( false );
29  mSpinBoxMaxSize->setShowClearButton( false );
30  connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMinComboBox );
31  connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
32  connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMaxComboBox );
33  connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, mComboBoxMaxScale, &QgsScaleWidget::setMinScale );
34  connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::configureMinComboBox );
35 
36  connect( mCheckBoxMinSize, &QCheckBox::toggled, mSpinBoxMinSize, &QgsDoubleSpinBox::setEnabled );
37  connect( mCheckBoxMaxSize, &QCheckBox::toggled, mSpinBoxMaxSize, &QgsDoubleSpinBox::setEnabled );
38 
39  // notification of setting changes
40  connect( mCheckBoxMinScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
41  connect( mCheckBoxMaxScale, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
42  connect( mComboBoxMinScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
43  connect( mComboBoxMaxScale, &QgsScaleWidget::scaleChanged, this, &QgsMapUnitScaleWidget::settingsChanged );
44  connect( mCheckBoxMinSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
45  connect( mCheckBoxMaxSize, &QCheckBox::toggled, this, &QgsMapUnitScaleWidget::settingsChanged );
46  connect( mSpinBoxMinSize, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
47  connect( mSpinBoxMaxSize, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsMapUnitScaleWidget::settingsChanged );
48  mBlockSignals = false;
49 }
50 
52 {
53  // can't block signals on the widgets themselves, some use them to update
54  // internal states
55  mBlockSignals = true;
56  mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 10000000 );
57  mCheckBoxMinScale->setChecked( scale.minScale != 0.0 );
58  mComboBoxMinScale->setEnabled( scale.minScale != 0.0 );
59  mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
60  mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
61  mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
62 
63  mCheckBoxMinSize->setChecked( scale.minSizeMMEnabled );
64  mSpinBoxMinSize->setEnabled( scale.minSizeMMEnabled );
65  mSpinBoxMinSize->setValue( scale.minSizeMM );
66 
67  mCheckBoxMaxSize->setChecked( scale.maxSizeMMEnabled );
68  mSpinBoxMaxSize->setEnabled( scale.maxSizeMMEnabled );
69  mSpinBoxMaxSize->setValue( scale.maxSizeMM );
70  mBlockSignals = false;
71 
72  settingsChanged();
73 }
74 
76 {
77  mComboBoxMinScale->setMapCanvas( canvas );
78  mComboBoxMinScale->setShowCurrentScaleButton( true );
79  mComboBoxMaxScale->setMapCanvas( canvas );
80  mComboBoxMaxScale->setShowCurrentScaleButton( true );
81 }
82 
83 void QgsMapUnitScaleWidget::configureMinComboBox()
84 {
85  mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
86  if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() < mComboBoxMaxScale->scale() )
87  {
88  mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
89  }
90 }
91 
92 void QgsMapUnitScaleWidget::configureMaxComboBox()
93 {
94  mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
95  if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() > mComboBoxMinScale->scale() )
96  {
97  mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
98  }
99 }
100 
101 void QgsMapUnitScaleWidget::settingsChanged()
102 {
103  if ( mBlockSignals )
104  return;
105 
107 }
108 
110 {
111  QgsMapUnitScale scale;
112  scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
113  scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
114  scale.minSizeMMEnabled = mCheckBoxMinSize->isChecked();
115  scale.minSizeMM = mSpinBoxMinSize->value();
116  scale.maxSizeMMEnabled = mCheckBoxMaxSize->isChecked();
117  scale.maxSizeMM = mSpinBoxMaxSize->value();
118  return scale;
119 }
120 
121 
122 
123 
124 
126  : QWidget( parent )
127 
128 {
129  mMapUnitIdx = -1;
130 
131  setupUi( this );
132  mMapScaleButton->setVisible( false );
133  mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
134 
135  setFocusPolicy( Qt::StrongFocus );
136  setFocusProxy( mUnitCombo );
137 
138  connect( mUnitCombo, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::toggleUnitRangeButton );
139  connect( mMapScaleButton, &QToolButton::clicked, this, &QgsUnitSelectionWidget::showDialog );
140  connect( mUnitCombo, static_cast < void ( QComboBox::* )( int ) > ( &QComboBox::currentIndexChanged ), this, &QgsUnitSelectionWidget::changed );
141 }
142 
143 void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
144 {
145  blockSignals( true );
146  mUnitCombo->addItems( units );
147  mMapUnitIdx = mapUnitIdx;
148  blockSignals( false );
149 }
150 
152 {
153  blockSignals( true );
154  mUnitCombo->clear();
155 
156  //instead of iterating over the units list, we specifically check for presence of unit types
157  //to ensure that the widget always keeps the same order for units, regardless of the
158  //order specified in the units list
159  mMapUnitIdx = -1;
160  if ( units.contains( QgsUnitTypes::RenderMillimeters ) )
161  {
162  mUnitCombo->addItem( tr( "Millimeter" ), QgsUnitTypes::RenderMillimeters );
163  }
164  if ( units.contains( QgsUnitTypes::RenderPoints ) )
165  {
166  mUnitCombo->addItem( tr( "Points" ), QgsUnitTypes::RenderPoints );
167  }
168  if ( units.contains( QgsUnitTypes::RenderPixels ) )
169  {
170  mUnitCombo->addItem( tr( "Pixels" ), QgsUnitTypes::RenderPixels );
171  }
172  if ( units.contains( QgsUnitTypes::RenderMetersInMapUnits ) )
173  {
174  mUnitCombo->addItem( tr( "Meters at Scale" ), QgsUnitTypes::RenderMetersInMapUnits );
175  }
176  if ( units.contains( QgsUnitTypes::RenderMapUnits ) )
177  {
178  mUnitCombo->addItem( tr( "Map Units" ), QgsUnitTypes::RenderMapUnits );
179  }
180  if ( units.contains( QgsUnitTypes::RenderPercentage ) )
181  {
182  mUnitCombo->addItem( tr( "Percentage" ), QgsUnitTypes::RenderPercentage );
183  }
184  if ( units.contains( QgsUnitTypes::RenderInches ) )
185  {
186  mUnitCombo->addItem( tr( "Inches" ), QgsUnitTypes::RenderInches );
187  }
188  blockSignals( false );
189 }
190 
192 {
193  if ( mUnitCombo->count() == 0 )
195 
196  QVariant currentData = mUnitCombo->currentData();
197  if ( currentData.isValid() )
198  {
199  return static_cast< QgsUnitTypes::RenderUnit >( currentData.toInt() );
200  }
201  //unknown
203 }
204 
205 void QgsUnitSelectionWidget::setUnit( int unitIndex )
206 {
207  blockSignals( true );
208  mUnitCombo->setCurrentIndex( unitIndex );
209  blockSignals( false );
210 }
211 
213 {
214  int idx = mUnitCombo->findData( QVariant( static_cast< int >( unit ) ) );
215  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
216 }
217 
219 {
220  mCanvas = canvas;
221 }
222 
223 void QgsUnitSelectionWidget::showDialog()
224 {
226  if ( panel && panel->dockMode() )
227  {
228  QgsMapUnitScaleWidget *widget = new QgsMapUnitScaleWidget( panel );
229  widget->setPanelTitle( tr( "Adjust Scaling Range" ) );
230  widget->setMapCanvas( mCanvas );
231  widget->setMapUnitScale( mMapUnitScale );
232  connect( widget, &QgsMapUnitScaleWidget::mapUnitScaleChanged, this, &QgsUnitSelectionWidget::widgetChanged );
233  panel->openPanel( widget );
234  return;
235  }
236 
237  QgsMapUnitScaleDialog dlg( this );
238  dlg.setMapUnitScale( mMapUnitScale );
239  dlg.setMapCanvas( mCanvas );
240  if ( dlg.exec() == QDialog::Accepted )
241  {
242  if ( mMapUnitScale != dlg.getMapUnitScale() )
243  {
244  mMapUnitScale = dlg.getMapUnitScale();
245  emit changed();
246  }
247  }
248 }
249 
250 void QgsUnitSelectionWidget::toggleUnitRangeButton()
251 {
253  {
254  mMapScaleButton->setVisible( unit() == QgsUnitTypes::RenderMapUnits );
255  }
256  else
257  {
258  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
259  }
260 }
261 
262 void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale &scale )
263 {
264  mMapUnitScale = scale;
265  emit changed();
266 }
267 
268 
270  : QDialog( parent )
271 
272 {
273  QVBoxLayout *vLayout = new QVBoxLayout();
274  mWidget = new QgsMapUnitScaleWidget();
275  vLayout->addWidget( mWidget );
276  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
277  connect( bbox, &QDialogButtonBox::accepted, this, &QgsMapUnitScaleDialog::accept );
278  connect( bbox, &QDialogButtonBox::rejected, this, &QgsMapUnitScaleDialog::reject );
279  vLayout->addWidget( bbox );
280  setLayout( vLayout );
281  setWindowTitle( tr( "Adjust Scaling Range" ) );
282 }
283 
285 {
286  return mWidget->mapUnitScale();
287 }
288 
290 {
291  mWidget->setMapUnitScale( scale );
292 }
293 
295 {
296  mWidget->setMapCanvas( canvas );
297 }
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
void mapUnitScaleChanged(const QgsMapUnitScale &scale)
Emitted when the settings in the widget are modified.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
Meters value as Map units.
Definition: qgsunittypes.h:120
double minSizeMM
The minimum size in millimeters, or 0.0 if unset.
A widget which allows the user to choose the minimum and maximum scale of an object in map units and ...
bool dockMode()
Returns the dock mode state.
void setUnit(int unitIndex)
Sets the selected unit index.
void setUnits(const QStringList &units, int mapUnitIdx)
Sets the units which the user can choose from in the combobox.
QgsMapUnitScaleWidget(QWidget *parent=nullptr)
Constructor for QgsMapUnitScaleWidget.
QgsMapUnitScaleDialog(QWidget *parent=nullptr)
Constructor for QgsMapUnitScaleDialog.
Base class for any widget that can be shown as a inline panel.
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:116
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:184
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
double maxScale
The maximum scale, or 0.0 if unset.
bool minSizeMMEnabled
Whether the minimum size in mm should be respected.
QgsMapUnitScale mapUnitScale() const
Returns a QgsMapUnitScale representing the settings shown in the widget.
QgsMapUnitScale getMapUnitScale() const
Returns a QgsMapUnitScale representing the settings shown in the dialog.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget...
void setMapUnitScale(const QgsMapUnitScale &scale)
Updates the dialog to reflect the settings from the specified QgsMapUnitScale object.
A dialog which allows the user to choose the minimum and maximum scale of an object in map units and ...
points (e.g., for font sizes)
Definition: qgsunittypes.h:118
void setMinScale(double scale)
Set the minimum allowed scale.
QgsUnitTypes::RenderUnit unit() const
Returns the current predefined selected unit (if applicable).
QgsUnitSelectionWidget(QWidget *parent=nullptr)
Constructor for QgsUnitSelectionWidget.
double maxSizeMM
The maximum size in millimeters, or 0.0 if unset.
Struct for storing maximum and minimum scales for measurements in map units.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMapUnitScale(const QgsMapUnitScale &scale)
Updates the widget to reflect the settings from the specified QgsMapUnitScale object.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
double minScale
The minimum scale, or 0.0 if unset.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
bool maxSizeMMEnabled
Whether the maximum size in mm should be respected.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:111