QGIS API Documentation  2.99.0-Master (e077efd)
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, SIGNAL( toggled( bool ) ), this, SLOT( configureMinComboBox() ) );
32  connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMaxComboBox() ) );
33  connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMaxComboBox() ) );
34  connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), mComboBoxMaxScale, SLOT( setMinScale( double ) ) );
35  connect( mComboBoxMaxScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMinComboBox() ) );
36 
37  connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), mSpinBoxMinSize, SLOT( setEnabled( bool ) ) );
38  connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), mSpinBoxMaxSize, SLOT( setEnabled( bool ) ) );
39 
40  // notification of setting changes
41  connect( mCheckBoxMinScale, SIGNAL( toggled( bool ) ), this, SLOT( settingsChanged() ) );
42  connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( settingsChanged() ) );
43  connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), this, SLOT( settingsChanged() ) );
44  connect( mComboBoxMaxScale, SIGNAL( scaleChanged( double ) ), this, SLOT( settingsChanged() ) );
45  connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), this, SLOT( settingsChanged() ) );
46  connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), this, SLOT( settingsChanged() ) );
47  connect( mSpinBoxMinSize, SIGNAL( valueChanged( double ) ), this, SLOT( settingsChanged() ) );
48  connect( mSpinBoxMaxSize, SIGNAL( valueChanged( double ) ), this, SLOT( 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, SIGNAL( currentIndexChanged( int ) ), this, SLOT( toggleUnitRangeButton() ) );
140  connect( mMapScaleButton, SIGNAL( clicked() ), this, SLOT( showDialog() ) );
141  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( 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  blockSignals( false );
182 }
183 
185 {
186  if ( mUnitCombo->count() == 0 )
188 
189  QVariant currentData = mUnitCombo->currentData();
190  if ( currentData.isValid() )
191  {
192  return ( QgsUnitTypes::RenderUnit ) currentData.toInt();
193  }
194  //unknown
196 }
197 
198 void QgsUnitSelectionWidget::setUnit( int unitIndex )
199 {
200  blockSignals( true );
201  mUnitCombo->setCurrentIndex( unitIndex );
202  blockSignals( false );
203 }
204 
206 {
207  int idx = mUnitCombo->findData( QVariant(( int ) unit ) );
208  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
209 }
210 
212 {
213  mCanvas = canvas;
214 }
215 
216 void QgsUnitSelectionWidget::showDialog()
217 {
219  if ( panel && panel->dockMode() )
220  {
221  QgsMapUnitScaleWidget* widget = new QgsMapUnitScaleWidget( panel );
222  widget->setPanelTitle( tr( "Adjust scaling range" ) );
223  widget->setMapCanvas( mCanvas );
224  widget->setMapUnitScale( mMapUnitScale );
225  connect( widget, SIGNAL( mapUnitScaleChanged( QgsMapUnitScale ) ), this, SLOT( widgetChanged( QgsMapUnitScale ) ) );
226  panel->openPanel( widget );
227  return;
228  }
229 
230  QgsMapUnitScaleDialog dlg( this );
231  dlg.setMapUnitScale( mMapUnitScale );
232  dlg.setMapCanvas( mCanvas );
233  if ( dlg.exec() == QDialog::Accepted )
234  {
235  if ( mMapUnitScale != dlg.getMapUnitScale() )
236  {
237  mMapUnitScale = dlg.getMapUnitScale();
238  emit changed();
239  }
240  }
241 }
242 
243 void QgsUnitSelectionWidget::toggleUnitRangeButton()
244 {
246  {
247  mMapScaleButton->setVisible( unit() == QgsUnitTypes::RenderMapUnits );
248  }
249  else
250  {
251  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
252  }
253 }
254 
255 void QgsUnitSelectionWidget::widgetChanged( const QgsMapUnitScale& scale )
256 {
257  mMapUnitScale = scale;
258  emit changed();
259 }
260 
261 
263  : QDialog( parent )
264  , mWidget( nullptr )
265 {
266  QVBoxLayout* vLayout = new QVBoxLayout();
267  mWidget = new QgsMapUnitScaleWidget();
268  vLayout->addWidget( mWidget );
269  QDialogButtonBox* bbox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
270  connect( bbox, SIGNAL( accepted() ), this, SLOT( accept() ) );
271  connect( bbox, SIGNAL( rejected() ), this, SLOT( reject() ) );
272  vLayout->addWidget( bbox );
273  setLayout( vLayout );
274 }
275 
277 {
278  return mWidget->mapUnitScale();
279 }
280 
282 {
283  mWidget->setMapUnitScale( scale );
284 }
285 
287 {
288  mWidget->setMapCanvas( canvas );
289 }
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.
points (eg for font sizes)
Definition: qgsunittypes.h:98
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 (eg canvas size, feature size)
Definition: qgsunittypes.h:96
QList< RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:102
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:106
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 ...
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:91