QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  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 
96 void QgsUnitSelectionWidget::setUnit( int unitIndex )
97 {
98  blockSignals( true );
99  mUnitCombo->setCurrentIndex( unitIndex );
100  blockSignals( false );
101 }
102 
103 void QgsUnitSelectionWidget::showDialog()
104 {
105  QgsMapUnitScale scale = mUnitScaleDialog->getMapUnitScale();
106  if ( mUnitScaleDialog->exec() != QDialog::Accepted )
107  {
108  mUnitScaleDialog->setMapUnitScale( scale );
109  }
110  else
111  {
112  QgsMapUnitScale newScale = mUnitScaleDialog->getMapUnitScale();
113  if ( scale.minScale != newScale.minScale || scale.maxScale != newScale.maxScale )
114  {
115  emit changed();
116  }
117  }
118 }
119 
120 void QgsUnitSelectionWidget::toggleUnitRangeButton()
121 {
122  mMapScaleButton->setVisible( mMapUnitIdx != -1 && mUnitCombo->currentIndex() == mMapUnitIdx );
123 }
124 
125 
126