QGIS API Documentation  2.11.0-Master
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 : smani@sourcepole.ch
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  connect( mCheckBoxMinScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMinComboBox() ) );
28  connect( mCheckBoxMaxScale, SIGNAL( toggled( bool ) ), this, SLOT( configureMaxComboBox() ) );
29  connect( mComboBoxMinScale, SIGNAL( scaleChanged() ), this, SLOT( configureMaxComboBox() ) );
30  connect( mComboBoxMaxScale, SIGNAL( scaleChanged() ), this, SLOT( configureMinComboBox() ) );
31 }
32 
34 {
35  mComboBoxMinScale->setScale( scale.minScale > 0.0 ? scale.minScale : 0.0000001 );
36  mCheckBoxMinScale->setChecked( scale.minScale > 0.0 );
37  mComboBoxMinScale->setEnabled( scale.minScale > 0.0 );
38  mComboBoxMaxScale->setScale( scale.maxScale > 0.0 ? scale.maxScale : 1.0 );
39  mCheckBoxMaxScale->setChecked( scale.maxScale > 0.0 );
40  mComboBoxMaxScale->setEnabled( scale.maxScale > 0.0 );
41 }
42 
43 void QgsMapUnitScaleDialog::configureMinComboBox()
44 {
45  mComboBoxMinScale->setEnabled( mCheckBoxMinScale->isChecked() );
46  if ( mCheckBoxMinScale->isChecked() && mComboBoxMinScale->scale() > mComboBoxMaxScale->scale() )
47  {
48  mComboBoxMinScale->setScale( mComboBoxMaxScale->scale() );
49  }
50 }
51 
52 void QgsMapUnitScaleDialog::configureMaxComboBox()
53 {
54  mComboBoxMaxScale->setEnabled( mCheckBoxMaxScale->isChecked() );
55  if ( mCheckBoxMaxScale->isChecked() && mComboBoxMaxScale->scale() < mComboBoxMinScale->scale() )
56  {
57  mComboBoxMaxScale->setScale( mComboBoxMinScale->scale() );
58  }
59 }
60 
62 {
63  QgsMapUnitScale scale;
64  scale.minScale = mCheckBoxMinScale->isChecked() ? mComboBoxMinScale->scale() : 0;
65  scale.maxScale = mCheckBoxMaxScale->isChecked() ? mComboBoxMaxScale->scale() : 0;
66  return scale;
67 }
68 
69 
71  : QWidget( parent )
72 {
73  mMapUnitIdx = -1;
74  mUnitScaleDialog = new QgsMapUnitScaleDialog( this );
75 
76  setupUi( this );
77  mMapScaleButton->setVisible( false );
78  mMapScaleButton->setToolTip( tr( "Adjust scaling range" ) );
79 
80  setFocusPolicy( Qt::StrongFocus );
81  setFocusProxy( mUnitCombo );
82 
83  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( toggleUnitRangeButton() ) );
84  connect( mMapScaleButton, SIGNAL( clicked() ), this, SLOT( showDialog() ) );
85  connect( mUnitCombo, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
86 }
87 
88 void QgsUnitSelectionWidget::setUnits( const QStringList &units, int mapUnitIdx )
89 {
90  blockSignals( true );
91  mUnitCombo->addItems( units );
92  mMapUnitIdx = mapUnitIdx;
93  blockSignals( false );
94 }
95 
97 {
98  blockSignals( true );
99  mUnitCombo->clear();
100 
101  //instead of iterating over the units list, we specifically check for presence of unit types
102  //to ensure that the widget always keeps the same order for units, regardless of the
103  //order specified in the units list
104  mMapUnitIdx = -1;
105  if ( units.contains( QgsSymbolV2::MM ) )
106  {
107  mUnitCombo->addItem( tr( "Millimeter" ), QgsSymbolV2::MM );
108  }
109  if ( units.contains( QgsSymbolV2::Pixel ) )
110  {
111  mUnitCombo->addItem( tr( "Pixels" ), QgsSymbolV2::Pixel );
112  }
113  if ( units.contains( QgsSymbolV2::MapUnit ) )
114  {
115  mUnitCombo->addItem( tr( "Map unit" ), QgsSymbolV2::MapUnit );
116  }
117  blockSignals( false );
118 }
119 
121 {
122  if ( mUnitCombo->count() == 0 )
123  return QgsSymbolV2::Mixed;
124 
125  QVariant currentData = mUnitCombo->itemData( mUnitCombo->currentIndex() );
126  if ( currentData.isValid() )
127  {
128  return ( QgsSymbolV2::OutputUnit ) currentData.toInt();
129  }
130  //unknown
131  return QgsSymbolV2::Mixed;
132 }
133 
134 void QgsUnitSelectionWidget::setUnit( int unitIndex )
135 {
136  blockSignals( true );
137  mUnitCombo->setCurrentIndex( unitIndex );
138  blockSignals( false );
139 }
140 
142 {
143  int idx = mUnitCombo->findData( QVariant(( int ) unit ) );
144  mUnitCombo->setCurrentIndex( idx == -1 ? 0 : idx );
145 }
146 
147 void QgsUnitSelectionWidget::showDialog()
148 {
149  QgsMapUnitScale scale = mUnitScaleDialog->getMapUnitScale();
150  if ( mUnitScaleDialog->exec() != QDialog::Accepted )
151  {
152  mUnitScaleDialog->setMapUnitScale( scale );
153  }
154  else
155  {
156  QgsMapUnitScale newScale = mUnitScaleDialog->getMapUnitScale();
157  if ( scale.minScale != newScale.minScale || scale.maxScale != newScale.maxScale )
158  {
159  emit changed();
160  }
161  }
162 }
163 
164 void QgsUnitSelectionWidget::toggleUnitRangeButton()
165 {
166  if ( unit() != QgsSymbolV2::Mixed )
167  {
168  mMapScaleButton->setVisible( unit() == QgsSymbolV2::MapUnit );
169  }
170  else
171  {
172  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
173  }
174 }
175 
void setupUi(QWidget *widget)
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.
int exec()
QString tr(const char *sourceText, const char *disambiguation, int n)
QgsMapUnitScaleDialog(QWidget *parent)
double maxScale
The maximum scale, or 0.0 if unset.
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...
bool blockSignals(bool block)
bool contains(const T &value) const
QgsSymbolV2::OutputUnit unit() const
Returns the current predefined selected unit (if applicable).
bool isValid() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
double minScale
The minimum scale, or 0.0 if unset.
QgsUnitSelectionWidget(QWidget *parent=0)