23#include <QAbstractItemView>
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 );
42 QStringList myScalesList;
43 const QString oldScale = currentText();
45 if ( scales.isEmpty() )
49 if ( !myScales.isEmpty() )
51 myScalesList = myScales.split(
',' );
56 QStringList::const_iterator scaleIt = scales.constBegin();
57 for ( ; scaleIt != scales.constEnd(); ++scaleIt )
59 myScalesList.append( *scaleIt );
63 for (
int i = 0; i < myScalesList.size(); ++i )
65 const QStringList parts = myScalesList[ i ] .split(
':' );
66 if ( parts.size() < 2 )
70 const double denominator = QLocale().toDouble( parts[1], &ok );
73 myScalesList[ i ] =
toString( denominator );
79 addItems( myScalesList );
81 blockSignals(
false );
86 QComboBox::showPopup();
88 if ( !currentText().contains(
':' ) )
92 QStringList parts = currentText().split(
':' );
96 const long currScale = parts.at( 1 ).toLong( &ok );
97 long nextScale, delta;
98 for (
int i = 0; i < count(); i++ )
100 parts = itemText( i ).split(
':' );
101 nextScale = parts.at( 1 ).toLong( &ok );
102 delta = std::labs( currScale - nextScale );
110 blockSignals(
true );
111 view()->setCurrentIndex( model()->index( idx, 0 ) );
112 blockSignals(
false );
113 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
123 const double oldScale = mScale;
124 if ( mAllowNull &&
string.trimmed().isEmpty() )
126 mScale = std::numeric_limits< double >::quiet_NaN();
129 if ( !std::isnan( oldScale ) )
137 double newScale =
toDouble(
string, &ok );
138 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
140 newScale = mMinScale;
151 if ( mScale != oldScale )
166 return std::isnan( mScale );
174void QgsScaleComboBox::fixupScale()
176 if ( mAllowNull && currentText().trimmed().isEmpty() )
178 setScale( std::numeric_limits< double >::quiet_NaN() );
182 const QStringList txtList = currentText().split(
':' );
183 const bool userSetScale = txtList.size() != 2;
186 double newScale =
toDouble( currentText(), &ok );
192 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
194 newScale = 1 / newScale;
206 if ( std::isnan(
scale ) )
212 return QStringLiteral(
"0" );
214 else if (
scale <= 1 )
216 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString(
static_cast< int >( std::round( 1.0 /
scale ) ) ) );
220 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString(
static_cast< float >( std::round(
scale ) ),
'f', 0 ) );
240 QStringList txtList = scaleTxt.split(
':' );
241 if ( 2 == txtList.size() )
247 if ( okX && okY && x != 0 )
250 scale =
static_cast< double >( y ) /
static_cast< double >( x );
267 lineEdit()->setClearButtonEnabled(
allowNull );
279 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
288 setScale( std::numeric_limits< double >::quiet_NaN() );
static QString defaultProjectScales()
A string with default project scales.
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.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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...
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...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)