QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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"
20#include "qgsscalecombobox.h"
22
23#include <QAbstractItemView>
24#include <QLocale>
25#include <QLineEdit>
26
28 : QComboBox( parent )
29{
31
32 setEditable( true );
33 setInsertPolicy( QComboBox::NoInsert );
34 setCompleter( nullptr );
35 connect( this, qOverload< int >( &QComboBox::activated ), this, &QgsScaleComboBox::fixupScale );
36 connect( lineEdit(), &QLineEdit::editingFinished, this, &QgsScaleComboBox::fixupScale );
37 fixupScale();
38}
39
40void QgsScaleComboBox::updateScales( const QStringList &scales )
41{
42 QStringList myScalesList;
43 const QString oldScale = currentText();
44
45 if ( scales.isEmpty() )
46 {
47 const QStringList scales = QgsSettingsRegistryCore::settingsMapScales->value();
48 }
49 else
50 {
51 QStringList::const_iterator scaleIt = scales.constBegin();
52 for ( ; scaleIt != scales.constEnd(); ++scaleIt )
53 {
54 myScalesList.append( *scaleIt );
55 }
56 }
57
58 QStringList parts;
59 double denominator;
60 bool ok;
61 for ( int i = 0; i < myScalesList.size(); ++i )
62 {
63 parts = myScalesList[ i ] .split( ':' );
64 denominator = QLocale().toDouble( parts[1], &ok );
65 if ( ok )
66 {
67 myScalesList[ i ] = toString( denominator );
68 }
69 }
70
71 blockSignals( true );
72 clear();
73 addItems( myScalesList );
74 setScaleString( oldScale );
75 blockSignals( false );
76}
77
79{
80 QComboBox::showPopup();
81
82 if ( !currentText().contains( ':' ) )
83 {
84 return;
85 }
86 QStringList parts = currentText().split( ':' );
87 bool ok;
88 int idx = 0;
89 int min = 999999;
90 const long currScale = parts.at( 1 ).toLong( &ok );
91 long nextScale, delta;
92 for ( int i = 0; i < count(); i++ )
93 {
94 parts = itemText( i ).split( ':' );
95 nextScale = parts.at( 1 ).toLong( &ok );
96 delta = std::labs( currScale - nextScale );
97 if ( delta < min )
98 {
99 min = delta;
100 idx = i;
101 }
102 }
103
104 blockSignals( true );
105 view()->setCurrentIndex( model()->index( idx, 0 ) );
106 blockSignals( false );
107 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
108}
109
111{
112 return toString( mScale );
113}
114
115bool QgsScaleComboBox::setScaleString( const QString &string )
116{
117 const double oldScale = mScale;
118 if ( mAllowNull && string.trimmed().isEmpty() )
119 {
120 mScale = std::numeric_limits< double >::quiet_NaN();
121 setEditText( toString( mScale ) );
122 clearFocus();
123 if ( !std::isnan( oldScale ) )
124 {
125 emit scaleChanged( mScale );
126 }
127 return true;
128 }
129
130 bool ok;
131 double newScale = toDouble( string, &ok );
132 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
133 {
134 newScale = mMinScale;
135 }
136 if ( ! ok )
137 {
138 return false;
139 }
140 else
141 {
142 mScale = newScale;
143 setEditText( toString( mScale ) );
144 clearFocus();
145 if ( mScale != oldScale )
146 {
147 emit scaleChanged( mScale );
148 }
149 return true;
150 }
151}
152
154{
155 return mScale;
156}
157
159{
160 return std::isnan( mScale );
161}
162
163void QgsScaleComboBox::setScale( double scale )
164{
166}
167
168void QgsScaleComboBox::fixupScale()
169{
170 if ( mAllowNull && currentText().trimmed().isEmpty() )
171 {
172 setScale( std::numeric_limits< double >::quiet_NaN() );
173 return;
174 }
175
176 const QStringList txtList = currentText().split( ':' );
177 const bool userSetScale = txtList.size() != 2;
178
179 bool ok;
180 double newScale = toDouble( currentText(), &ok );
181
182 // Valid string representation
183 if ( ok )
184 {
185 // if a user types scale = 2345, we transform to 1:2345
186 if ( userSetScale && newScale < 1.0 && !qgsDoubleNear( newScale, 0.0 ) )
187 {
188 newScale = 1 / newScale;
189 }
190 setScale( newScale );
191 }
192 else
193 {
194 setScale( mScale );
195 }
196}
197
198QString QgsScaleComboBox::toString( double scale )
199{
200 if ( std::isnan( scale ) )
201 {
202 return QString();
203 }
204 if ( scale == 0 )
205 {
206 return QStringLiteral( "0" );
207 }
208 else if ( scale <= 1 )
209 {
210 return QStringLiteral( "%1:1" ).arg( QLocale().toString( static_cast< int >( std::round( 1.0 / scale ) ) ) );
211 }
212 else
213 {
214 return QStringLiteral( "1:%1" ).arg( QLocale().toString( static_cast< float >( std::round( scale ) ), 'f', 0 ) );
215 }
216}
217
218double QgsScaleComboBox::toDouble( const QString &scaleString, bool *returnOk )
219{
220 bool ok = false;
221 QString scaleTxt( scaleString );
222
223 const double denominator = qgsPermissiveToDouble( scaleTxt, ok );
224 double scale = !qgsDoubleNear( denominator, 0.0 ) ? 1.0 / denominator : 0.0;
225 if ( ok )
226 {
227 // Create a text version and set that text and rescan
228 // Idea is to get the same rounding.
229 scaleTxt = toString( scale );
230 }
231 else
232 {
233 // It is now either X:Y or not valid
234 QStringList txtList = scaleTxt.split( ':' );
235 if ( 2 == txtList.size() )
236 {
237 bool okX = false;
238 bool okY = false;
239 const int x = qgsPermissiveToInt( txtList[ 0 ], okX );
240 const int y = qgsPermissiveToInt( txtList[ 1 ], okY );
241 if ( okX && okY && x != 0 )
242 {
243 // Scale is fraction of x and y
244 scale = static_cast< double >( y ) / static_cast< double >( x );
245 ok = true;
246 }
247 }
248 }
249
250 // Set up optional return flag
251 if ( returnOk )
252 {
253 *returnOk = ok;
254 }
255 return scale;
256}
257
258void QgsScaleComboBox::setAllowNull( bool allowNull )
259{
260 mAllowNull = allowNull;
261 lineEdit()->setClearButtonEnabled( allowNull );
262 updateScales();
263}
264
266{
267 return mAllowNull;
268}
269
271{
272 mMinScale = scale;
273 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
274 {
275 setScale( mMinScale );
276 }
277}
278
280{
281 if ( allowNull() )
282 setScale( std::numeric_limits< double >::quiet_NaN() );
283}
static QString toString(double scale)
Helper function to convert a scale double to scale string.
void updateScales(const QStringList &scales=QStringList())
Sets the list of predefined scales to show in the combobox.
QString scaleString() const
Returns the selected scale as a string, e.g.
bool setScaleString(const QString &string)
Set the selected scale from a string, e.g.
void setAllowNull(bool allowNull)
Sets whether the scale combobox can be set to a NULL value.
QgsScaleComboBox(QWidget *parent=nullptr)
Constructor for QgsScaleComboBox.
bool isNull() const
Returns true if the combo box is currently set to a "null" value.
bool allowNull() const
Returns true if the combobox can be set to a NULL value.
static double toDouble(const QString &string, bool *ok=nullptr)
Helper function to convert a scale string to double.
void setScale(double scale)
Set the selected scale from a double.
void showPopup() override
void setNull()
Sets the combo box to the null value.
void setMinScale(double scale)
Set the minimum allowed scale.
void scaleChanged(double scale)
Emitted when user has finished editing/selecting a new scale.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
static const QgsSettingsEntryStringList * settingsMapScales
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:71
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:78
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:3509