QGIS API Documentation  2.15.0-Master (af20121)
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( double ) ), this, SLOT( configureMaxComboBox() ) );
32  connect( mComboBoxMinScale, SIGNAL( scaleChanged( double ) ), mComboBoxMaxScale, SLOT( setMinScale( double ) ) );
33  connect( mComboBoxMaxScale, SIGNAL( scaleChanged( double ) ), this, SLOT( configureMinComboBox() ) );
34 
35  connect( mCheckBoxMinSize, SIGNAL( toggled( bool ) ), mSpinBoxMinSize, SLOT( setEnabled( bool ) ) );
36  connect( mCheckBoxMaxSize, SIGNAL( toggled( bool ) ), mSpinBoxMaxSize, SLOT( setEnabled( bool ) ) );
37 }
38 
40 {
41  mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 0.0000001 );
42  mCheckBoxMinScale->setChecked( scale.minScale > 0.0 );
43  mComboBoxMinScale->setEnabled( scale.minScale > 0.0 );
44  mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
45  mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
46  mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
47 
48  mCheckBoxMinSize->setChecked( scale.minSizeMMEnabled );
49  mSpinBoxMinSize->setEnabled( scale.minSizeMMEnabled );
50  mSpinBoxMinSize->setValue( scale.minSizeMM );
51 
52  mCheckBoxMaxSize->setChecked( scale.maxSizeMMEnabled );
53  mSpinBoxMaxSize->setEnabled( scale.maxSizeMMEnabled );
54  mSpinBoxMaxSize->setValue( scale.maxSizeMM );
55 }
56 
58 {
59  mComboBoxMinScale->setMapCanvas( canvas );
60  mComboBoxMinScale->setShowCurrentScaleButton( true );
61  mComboBoxMaxScale->setMapCanvas( canvas );
62  mComboBoxMaxScale->setShowCurrentScaleButton( true );
63 }
64 
65 void QgsMapUnitScaleDialog::configureMinComboBox()
66 {
67  mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
68  if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() > mComboBoxMaxScale->scale() )
69  {
70  mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
71  }
72 }
73 
74 void QgsMapUnitScaleDialog::configureMaxComboBox()
75 {
76  mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
77  if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() < mComboBoxMinScale->scale() )
78  {
79  mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
80  }
81 }
82 
84 {
85  QgsMapUnitScale scale;
86  scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
87  scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
88  scale.minSizeMMEnabled = mCheckBoxMinSize->isChecked();
89  scale.minSizeMM = mSpinBoxMinSize->value();
90  scale.maxSizeMMEnabled = mCheckBoxMaxSize->isChecked();
91  scale.maxSizeMM = mSpinBoxMaxSize->value();
92  return scale;
93 }
94 
96  : QWidget( parent )
97 {
98  mMapUnitIdx = -1;
99  mUnitScaleDialog = new QgsMapUnitScaleDialog( this );
100 
101  setupUi( this );
102  mMapScaleButton->setVisible( false );
103  mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
104 
105  setFocusPolicy( Qt::StrongFocus );
106  setFocusProxy( mUnitCombo );
107 
108  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( toggleUnitRangeButton() ) );
109  connect( mMapScaleButton, SIGNAL( clicked() ), this, SLOT( showDialog() ) );
110  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
111 }
112 
113 void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
114 {
115  blockSignals( true );
116  mUnitCombo->addItems( units );
117  mMapUnitIdx = mapUnitIdx;
118  blockSignals( false );
119 }
120 
122 {
123  blockSignals( true );
124  mUnitCombo->clear();
125 
126  //instead of iterating over the units list, we specifically check for presence of unit types
127  //to ensure that the widget always keeps the same order for units, regardless of the
128  //order specified in the units list
129  mMapUnitIdx = -1;
130  if ( units.contains( QgsSymbolV2::MM ) )
131  {
132  mUnitCombo->addItem( tr( "Millimeter" ), QgsSymbolV2::MM );
133  }
134  if ( units.contains( QgsSymbolV2::Pixel ) )
135  {
136  mUnitCombo->addItem( tr( "Pixels" ), QgsSymbolV2::Pixel );
137  }
138  if ( units.contains( QgsSymbolV2::MapUnit ) )
139  {
140  mUnitCombo->addItem( tr( "Map unit" ), QgsSymbolV2::MapUnit );
141  }
142  if ( units.contains( QgsSymbolV2::Percentage ) )
143  {
144  mUnitCombo->addItem( tr( "Percentage" ), QgsSymbolV2::Percentage );
145  }
146  blockSignals( false );
147 }
148 
150 {
151  if ( mUnitCombo->count() == 0 )
152  return QgsSymbolV2::Mixed;
153 
154  QVariant currentData = mUnitCombo->itemData( mUnitCombo->currentIndex() );
155  if ( currentData.isValid() )
156  {
157  return ( QgsSymbolV2::OutputUnit ) currentData.toInt();
158  }
159  //unknown
160  return QgsSymbolV2::Mixed;
161 }
162 
163 void QgsUnitSelectionWidget::setUnit( int unitIndex )
164 {
165  blockSignals( true );
166  mUnitCombo->setCurrentIndex( unitIndex );
167  blockSignals( false );
168 }
169 
171 {
172  int idx = mUnitCombo->findData( QVariant(( int ) unit ) );
173  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
174 }
175 
177 {
178  mUnitScaleDialog->setMapCanvas( canvas );
179 }
180 
181 void QgsUnitSelectionWidget::showDialog()
182 {
183  QgsMapUnitScale scale = mUnitScaleDialog->getMapUnitScale();
184  if ( mUnitScaleDialog->exec() != QDialog::Accepted )
185  {
186  mUnitScaleDialog->setMapUnitScale( scale );
187  }
188  else
189  {
190  QgsMapUnitScale newScale = mUnitScaleDialog->getMapUnitScale();
191  if ( scale != newScale )
192  {
193  emit changed();
194  }
195  }
196 }
197 
198 void QgsUnitSelectionWidget::toggleUnitRangeButton()
199 {
200  if ( unit() != QgsSymbolV2::Mixed )
201  {
202  mMapScaleButton->setVisible( unit() == QgsSymbolV2::MapUnit );
203  }
204  else
205  {
206  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
207  }
208 }
209 
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:108
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.