QGIS API Documentation  2.99.0-Master (40f86b2)
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  , mBlockSignals( true )
25 {
26  setupUi( this );
27  mComboBoxMinScale->setScale( 0.0000001 );
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 : 0.0000001 );
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  , mCanvas( nullptr )
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, &QPushButton::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( "Millimeter" ), 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::RenderMapUnits ) )
174  {
175  mUnitCombo->addItem( tr( "Map unit" ), QgsUnitTypes::RenderMapUnits );
176  }
177  if ( units.contains( QgsUnitTypes::RenderPercentage ) )
178  {
179  mUnitCombo->addItem( tr( "Percentage" ), QgsUnitTypes::RenderPercentage );
180  }
181  if ( units.contains( QgsUnitTypes::RenderInches ) )
182  {
183  mUnitCombo->addItem( tr( "Inches" ), QgsUnitTypes::RenderInches );
184  }
185  blockSignals( false );
186 }
187 
189 {
190  if ( mUnitCombo->count() == 0 )
192 
193  QVariant currentData = mUnitCombo->currentData();
194  if ( currentData.isValid() )
195  {
196  return ( QgsUnitTypes::RenderUnit ) currentData.toInt();
197  }
198  //unknown
200 }
201 
202 void QgsUnitSelectionWidget::setUnit( int unitIndex )
203 {
204  blockSignals( true );
205  mUnitCombo->setCurrentIndex( unitIndex );
206  blockSignals( false );
207 }
208 
210 {
211  int idx = mUnitCombo->findData( QVariant( ( int ) unit ) );
212  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
213 }
214 
216 {
217  mCanvas = canvas;
218 }
219 
220 void QgsUnitSelectionWidget::showDialog()
221 {
223  if ( panel && panel->dockMode() )
224  {
225  QgsMapUnitScaleWidget *widget = new QgsMapUnitScaleWidget( panel );
226  widget->setPanelTitle( tr( "Adjust scaling range" ) );
227  widget->setMapCanvas( mCanvas );
228  widget->setMapUnitScale( mMapUnitScale );
229  connect( widget, &QgsMapUnitScaleWidget::mapUnitScaleChanged, this, &QgsUnitSelectionWidget::widgetChanged );
230  panel->openPanel( widget );
231  return;
232  }
233 
234  QgsMapUnitScaleDialog dlg( this );
235  dlg.setMapUnitScale( mMapUnitScale );
236  dlg.setMapCanvas( mCanvas );
237  if ( dlg.exec() == QDialog::Accepted )
238  {
239  if ( mMapUnitScale != dlg.getMapUnitScale() )
240  {
241  mMapUnitScale = dlg.getMapUnitScale();
242  emit changed();
243  }
244  }
245 }
246 
247 void QgsUnitSelectionWidget::toggleUnitRangeButton()
248 {
250  {
251  mMapScaleButton->setVisible( unit() == QgsUnitTypes::RenderMapUnits );
252  }
253  else
254  {
255  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
256  }
257 }
258 
259 void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale &scale )
260 {
261  mMapUnitScale = scale;
262  emit changed();
263 }
264 
265 
267  : QDialog( parent )
268  , mWidget( nullptr )
269 {
270  QVBoxLayout *vLayout = new QVBoxLayout();
271  mWidget = new QgsMapUnitScaleWidget();
272  vLayout->addWidget( mWidget );
273  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
274  connect( bbox, &QDialogButtonBox::accepted, this, &QgsMapUnitScaleDialog::accept );
275  connect( bbox, &QDialogButtonBox::rejected, this, &QgsMapUnitScaleDialog::reject );
276  vLayout->addWidget( bbox );
277  setLayout( vLayout );
278 }
279 
281 {
282  return mWidget->mapUnitScale();
283 }
284 
286 {
287  mWidget->setMapUnitScale( scale );
288 }
289 
291 {
292  mWidget->setMapCanvas( canvas );
293 }
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 ...
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()
Return 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:100
void scaleChanged(double scale)
Signal is emitted when user has finished editing/selecting a new scale.
QList< RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:145
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:72
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:102
void setMinScale(double scale)
Function to set the min 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:95