QGIS API Documentation  2.14.0-Essen
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 
22  : QDialog( parent )
23 {
24  setupUi( this );
25  mComboBoxMinScale->setScale( 0.0000001 );
26  mComboBoxMaxScale->setScale( 1 );
27  mSpinBoxMinSize->setShowClearButton( false );
28  mSpinBoxMaxSize->setShowClearButton( false );
29  connect( mCheckBoxMinScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMinComboBox() ) );
30  connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMaxComboBox() ) );
31  connect( mComboBoxMinScale, SIGNAL( scaleChanged() ), this, SLOT( configureMaxComboBox() ) );
32  connect( mComboBoxMaxScale, SIGNAL( scaleChanged() ), this, SLOT( configureMinComboBox() ) );
33 
34  connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), mSpinBoxMinSize, SLOT( setEnabled( bool ) ) );
35  connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), mSpinBoxMaxSize, SLOT( setEnabled( bool ) ) );
36 }
37 
39 {
40  mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 0.0000001 );
41  mCheckBoxMinScale->setChecked( scale.minScale > 0.0 );
42  mComboBoxMinScale->setEnabled( scale.minScale > 0.0 );
43  mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
44  mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
45  mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
46 
47  mCheckBoxMinSize->setChecked( scale.minSizeMMEnabled );
48  mSpinBoxMinSize->setEnabled( scale.minSizeMMEnabled );
49  mSpinBoxMinSize->setValue( scale.minSizeMM );
50 
51  mCheckBoxMaxSize->setChecked( scale.maxSizeMMEnabled );
52  mSpinBoxMaxSize->setEnabled( scale.maxSizeMMEnabled );
53  mSpinBoxMaxSize->setValue( scale.maxSizeMM );
54 }
55 
57 {
58  mComboBoxMinScale->setMapCanvas( canvas );
59  mComboBoxMinScale->setShowCurrentScaleButton( true );
60  mComboBoxMaxScale->setMapCanvas( canvas );
61  mComboBoxMaxScale->setShowCurrentScaleButton( true );
62 }
63 
64 void QgsMapUnitScaleDialog::configureMinComboBox()
65 {
66  mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
67  if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() > mComboBoxMaxScale->scale() )
68  {
69  mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
70  }
71 }
72 
73 void QgsMapUnitScaleDialog::configureMaxComboBox()
74 {
75  mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
76  if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() < mComboBoxMinScale->scale() )
77  {
78  mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
79  }
80 }
81 
83 {
84  QgsMapUnitScale scale;
85  scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
86  scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
87  scale.minSizeMMEnabled = mCheckBoxMinSize->isChecked();
88  scale.minSizeMM = mSpinBoxMinSize->value();
89  scale.maxSizeMMEnabled = mCheckBoxMaxSize->isChecked();
90  scale.maxSizeMM = mSpinBoxMaxSize->value();
91  return scale;
92 }
93 
95  : QWidget( parent )
96 {
97  mMapUnitIdx = -1;
98  mUnitScaleDialog = new QgsMapUnitScaleDialog( this );
99 
100  setupUi( this );
101  mMapScaleButton->setVisible( false );
102  mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
103 
104  setFocusPolicy( Qt::StrongFocus );
105  setFocusProxy( mUnitCombo );
106 
107  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( toggleUnitRangeButton() ) );
108  connect( mMapScaleButton, SIGNAL( clicked() ), this, SLOT( showDialog() ) );
109  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
110 }
111 
112 void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
113 {
114  blockSignals( true );
115  mUnitCombo->addItems( units );
116  mMapUnitIdx = mapUnitIdx;
117  blockSignals( false );
118 }
119 
121 {
122  blockSignals( true );
123  mUnitCombo->clear();
124 
125  //instead of iterating over the units list, we specifically check for presence of unit types
126  //to ensure that the widget always keeps the same order for units, regardless of the
127  //order specified in the units list
128  mMapUnitIdx = -1;
129  if ( units.contains( QgsSymbolV2::MM ) )
130  {
131  mUnitCombo->addItem( tr( "Millimeter" ), QgsSymbolV2::MM );
132  }
133  if ( units.contains( QgsSymbolV2::Pixel ) )
134  {
135  mUnitCombo->addItem( tr( "Pixels" ), QgsSymbolV2::Pixel );
136  }
137  if ( units.contains( QgsSymbolV2::MapUnit ) )
138  {
139  mUnitCombo->addItem( tr( "Map unit" ), QgsSymbolV2::MapUnit );
140  }
141  if ( units.contains( QgsSymbolV2::Percentage ) )
142  {
143  mUnitCombo->addItem( tr( "Percentage" ), QgsSymbolV2::Percentage );
144  }
145  blockSignals( false );
146 }
147 
149 {
150  if ( mUnitCombo->count() == 0 )
151  return QgsSymbolV2::Mixed;
152 
153  QVariant currentData = mUnitCombo->itemData( mUnitCombo->currentIndex() );
154  if ( currentData.isValid() )
155  {
156  return ( QgsSymbolV2::OutputUnit ) currentData.toInt();
157  }
158  //unknown
159  return QgsSymbolV2::Mixed;
160 }
161 
162 void QgsUnitSelectionWidget::setUnit( int unitIndex )
163 {
164  blockSignals( true );
165  mUnitCombo->setCurrentIndex( unitIndex );
166  blockSignals( false );
167 }
168 
170 {
171  int idx = mUnitCombo->findData( QVariant(( int ) unit ) );
172  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
173 }
174 
176 {
177  mUnitScaleDialog->setMapCanvas( canvas );
178 }
179 
180 void QgsUnitSelectionWidget::showDialog()
181 {
182  QgsMapUnitScale scale = mUnitScaleDialog->getMapUnitScale();
183  if ( mUnitScaleDialog->exec() != QDialog::Accepted )
184  {
185  mUnitScaleDialog->setMapUnitScale( scale );
186  }
187  else
188  {
189  QgsMapUnitScale newScale = mUnitScaleDialog->getMapUnitScale();
190  if ( scale != newScale )
191  {
192  emit changed();
193  }
194  }
195 }
196 
197 void QgsUnitSelectionWidget::toggleUnitRangeButton()
198 {
199  if ( unit() != QgsSymbolV2::Mixed )
200  {
201  mMapScaleButton->setVisible( unit() == QgsSymbolV2::MapUnit );
202  }
203  else
204  {
205  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
206  }
207 }
208 
double minSizeMM
The minimum size in millimeters, or 0.0 if unset.
void setupUi(QWidget *widget)
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
QgsMapUnitScale getMapUnitScale() const
Returns the map unit scale.
void setFocusPolicy(Qt::FocusPolicy policy)
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.
The output shall be in pixels.
Definition: qgssymbolv2.h:67
int exec()
QgsMapUnitScaleDialog(QWidget *parent=nullptr)
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
double maxScale
The maximum scale, or 0.0 if unset.
bool minSizeMMEnabled
Whether the minimum size in mm should be respected.
Mixed units in symbol layers.
Definition: qgssymbolv2.h:66
The output shall be in millimeters.
Definition: qgssymbolv2.h:64
void setEnabled(bool)
int toInt(bool *ok) const
The ouput shall be a percentage of another measurement (eg canvas size, feature size) ...
Definition: qgssymbolv2.h:68
void setFocusProxy(QWidget *w)
void setMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale.
Dialog allowing the user to choose the minimum and maximum scale of an object in map units...
The output shall be in map unitx.
Definition: qgssymbolv2.h:65
bool blockSignals(bool block)
bool contains(const T &value) const
QgsUnitSelectionWidget(QWidget *parent=nullptr)
QgsSymbolV2::OutputUnit unit() const
Returns the current predefined selected unit (if applicable).
double maxSizeMM
The maximum size in millimeters, or 0.0 if unset.
Struct for storing maximum and minimum scales for measurements in map units.
bool isValid() const
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
double minScale
The minimum scale, or 0.0 if unset.
bool maxSizeMMEnabled
Whether the maximum size in mm should be respected.