QGIS API Documentation  2.12.0-Lyon
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  blockSignals( false );
142 }
143 
145 {
146  if ( mUnitCombo->count() == 0 )
147  return QgsSymbolV2::Mixed;
148 
149  QVariant currentData = mUnitCombo->itemData( mUnitCombo->currentIndex() );
150  if ( currentData.isValid() )
151  {
152  return ( QgsSymbolV2::OutputUnit ) currentData.toInt();
153  }
154  //unknown
155  return QgsSymbolV2::Mixed;
156 }
157 
158 void QgsUnitSelectionWidget::setUnit( int unitIndex )
159 {
160  blockSignals( true );
161  mUnitCombo->setCurrentIndex( unitIndex );
162  blockSignals( false );
163 }
164 
166 {
167  int idx = mUnitCombo->findData( QVariant(( int ) unit ) );
168  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
169 }
170 
172 {
173  mUnitScaleDialog->setMapCanvas( canvas );
174 }
175 
176 void QgsUnitSelectionWidget::showDialog()
177 {
178  QgsMapUnitScale scale = mUnitScaleDialog->getMapUnitScale();
179  if ( mUnitScaleDialog->exec() != QDialog::Accepted )
180  {
181  mUnitScaleDialog->setMapUnitScale( scale );
182  }
183  else
184  {
185  QgsMapUnitScale newScale = mUnitScaleDialog->getMapUnitScale();
186  if ( scale != newScale )
187  {
188  emit changed();
189  }
190  }
191 }
192 
193 void QgsUnitSelectionWidget::toggleUnitRangeButton()
194 {
195  if ( unit() != QgsSymbolV2::Mixed )
196  {
197  mMapScaleButton->setVisible( unit() == QgsSymbolV2::MapUnit );
198  }
199  else
200  {
201  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
202  }
203 }
204 
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:55
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:60
int exec()
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:107
QgsMapUnitScaleDialog(QWidget *parent)
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:59
The output shall be in millimeters.
Definition: qgssymbolv2.h:57
void setEnabled(bool)
int toInt(bool *ok) const
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:58
bool blockSignals(bool block)
bool contains(const T &value) const
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)
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.
QgsUnitSelectionWidget(QWidget *parent=0)