QGIS API Documentation  2.99.0-Master (ae4d26a)
qgsscalecombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalecombobox.h
3  ------------------------
4  begin : January 7, 2012
5  copyright : (C) 2012 by Alexander Bruy
6  email : alexander dot bruy at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgis.h"
19 #include "qgslogger.h"
20 #include "qgsscalecombobox.h"
21 #include "qgssettings.h"
22 
23 #include <QAbstractItemView>
24 #include <QLocale>
25 #include <QLineEdit>
26 
28  : QComboBox( parent )
29 {
30  updateScales();
31 
32  setEditable( true );
33  setInsertPolicy( QComboBox::NoInsert );
34  setCompleter( nullptr );
35  connect( this, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::activated ), this, &QgsScaleComboBox::fixupScale );
36  connect( lineEdit(), &QLineEdit::editingFinished, this, &QgsScaleComboBox::fixupScale );
37  fixupScale();
38 }
39 
40 void QgsScaleComboBox::updateScales( const QStringList &scales )
41 {
42  QStringList myScalesList;
43  QString oldScale = currentText();
44 
45  if ( scales.isEmpty() )
46  {
47  QgsSettings settings;
48  QString myScales = settings.value( QStringLiteral( "Map/scales" ), PROJECT_SCALES ).toString();
49  if ( !myScales.isEmpty() )
50  {
51  myScalesList = myScales.split( ',' );
52  }
53  }
54  else
55  {
56  QStringList::const_iterator scaleIt = scales.constBegin();
57  for ( ; scaleIt != scales.constEnd(); ++scaleIt )
58  {
59  myScalesList.append( *scaleIt );
60  }
61  }
62 
63  QStringList parts;
64  double denominator;
65  bool ok;
66  for ( int i = 0; i < myScalesList.size(); ++i )
67  {
68  parts = myScalesList[ i ] .split( ':' );
69  denominator = QLocale::system().toDouble( parts[1], &ok );
70  if ( ok )
71  {
72  myScalesList[ i ] = toString( denominator );
73  }
74  }
75 
76  blockSignals( true );
77  clear();
78  addItems( myScalesList );
79  setScaleString( oldScale );
80  blockSignals( false );
81 }
82 
84 {
85  QComboBox::showPopup();
86 
87  if ( !currentText().contains( ':' ) )
88  {
89  return;
90  }
91  QStringList parts = currentText().split( ':' );
92  bool ok;
93  int idx = 0;
94  int min = 999999;
95  long currScale = parts.at( 1 ).toLong( &ok );
96  long nextScale, delta;
97  for ( int i = 0; i < count(); i++ )
98  {
99  parts = itemText( i ).split( ':' );
100  nextScale = parts.at( 1 ).toLong( &ok );
101  delta = std::labs( currScale - nextScale );
102  if ( delta < min )
103  {
104  min = delta;
105  idx = i;
106  }
107  }
108 
109  blockSignals( true );
110  view()->setCurrentIndex( model()->index( idx, 0 ) );
111  blockSignals( false );
112  view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
113 }
114 
116 {
117  return toString( mScale );
118 }
119 
120 bool QgsScaleComboBox::setScaleString( const QString &string )
121 {
122  bool ok;
123  double newScale = toDouble( string, &ok );
124  double oldScale = mScale;
125  if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
126  {
127  newScale = mMinScale;
128  }
129  if ( ! ok )
130  {
131  return false;
132  }
133  else
134  {
135  mScale = newScale;
136  setEditText( toString( mScale ) );
137  clearFocus();
138  if ( mScale != oldScale )
139  {
140  emit scaleChanged( mScale );
141  }
142  return true;
143  }
144 }
145 
146 double QgsScaleComboBox::scale() const
147 {
148  return mScale;
149 }
150 
152 {
153  setScaleString( toString( scale ) );
154 }
155 
156 void QgsScaleComboBox::fixupScale()
157 {
158  QStringList txtList = currentText().split( ':' );
159  bool userSetScale = txtList.size() != 2;
160 
161  bool ok;
162  double newScale = toDouble( currentText(), &ok );
163 
164  // Valid string representation
165  if ( ok )
166  {
167  // if a user types scale = 2345, we transform to 1:2345
168  if ( userSetScale && newScale < 1.0 && !qgsDoubleNear( newScale, 0.0 ) )
169  {
170  newScale = 1 / newScale;
171  }
172  setScale( newScale );
173  }
174  else
175  {
176  setScale( mScale );
177  }
178 }
179 
181 {
182  if ( scale == 0 )
183  {
184  return QStringLiteral( "0" );
185  }
186  else if ( scale <= 1 )
187  {
188  return QStringLiteral( "%1:1" ).arg( QLocale::system().toString( static_cast< int >( std::round( 1.0 / scale ) ) ) );
189  }
190  else
191  {
192  return QStringLiteral( "1:%1" ).arg( QLocale::system().toString( static_cast< int >( std::round( scale ) ) ) );
193  }
194 }
195 
196 double QgsScaleComboBox::toDouble( const QString &scaleString, bool *returnOk )
197 {
198  bool ok = false;
199  QString scaleTxt( scaleString );
200 
201  double denominator = qgsPermissiveToDouble( scaleTxt, ok );
202  double scale = !qgsDoubleNear( denominator, 0.0 ) ? 1.0 / denominator : 0.0;
203  if ( ok )
204  {
205  // Create a text version and set that text and rescan
206  // Idea is to get the same rounding.
207  scaleTxt = toString( scale );
208  }
209  else
210  {
211  // It is now either X:Y or not valid
212  QStringList txtList = scaleTxt.split( ':' );
213  if ( 2 == txtList.size() )
214  {
215  bool okX = false;
216  bool okY = false;
217  int x = qgsPermissiveToInt( txtList[ 0 ], okX );
218  int y = qgsPermissiveToInt( txtList[ 1 ], okY );
219  if ( okX && okY && x != 0 )
220  {
221  // Scale is fraction of x and y
222  scale = static_cast< double >( y ) / static_cast< double >( x );
223  ok = true;
224  }
225  }
226  }
227 
228  // Set up optional return flag
229  if ( returnOk )
230  {
231  *returnOk = ok;
232  }
233  return scale;
234 }
235 
237 {
238  mMinScale = scale;
239  if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
240  {
241  setScale( mMinScale );
242  }
243 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:55
QString scaleString() const
Returns the selected scale as a string, e.g.
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition: qgis.cpp:93
void setScale(double scale)
Set the selected scale from a double.
static double toDouble(const QString &string, bool *ok=nullptr)
Helper function to convert a scale string to double.
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
Definition: qgis.cpp:100
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:227
void showPopup() override
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
double scale() const
Returns the selected scale as a double.
void setMinScale(double scale)
Set the minimum allowed scale.
bool setScaleString(const QString &string)
Set the selected scale from a string, e.g.
static QString toString(double scale)
Helper function to convert a scale double to scale string.
const QString PROJECT_SCALES
Definition: qgis.cpp:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
QgsScaleComboBox(QWidget *parent=nullptr)
Constructor for QgsScaleComboBox.