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 scalesList;
43 const QString oldScale = currentText();
45 if ( scales.isEmpty() )
54 QStringList cleanedScalesList;
55 for (
const QString &
scale : std::as_const( scalesList ) )
57 const QStringList parts =
scale.split(
':' );
58 if ( parts.size() < 2 )
62 const double denominator = QLocale().toDouble( parts[1], &ok );
65 cleanedScalesList.push_back(
toString( denominator ) );
69 const double denominator = parts[1].toDouble( &ok );
72 cleanedScalesList.push_back(
toString( denominator ) );
79 addItems( cleanedScalesList );
81 blockSignals(
false );
86 if ( scales.isEmpty() )
92 const QString oldScale = currentText();
94 QStringList scalesStringList;
95 scalesStringList.reserve( scales.size() );
96 for (
double denominator : scales )
98 scalesStringList.push_back(
toString( denominator ) );
101 blockSignals(
true );
103 addItems( scalesStringList );
105 blockSignals(
false );
110 QComboBox::showPopup();
112 if ( !currentText().contains(
':' ) )
116 QStringList parts = currentText().split(
':' );
120 const long currScale = parts.at( 1 ).toLong( &ok );
121 long nextScale, delta;
122 for (
int i = 0; i < count(); i++ )
124 parts = itemText( i ).split(
':' );
125 nextScale = parts.at( 1 ).toLong( &ok );
126 delta = std::labs( currScale - nextScale );
134 blockSignals(
true );
135 view()->setCurrentIndex( model()->index( idx, 0 ) );
136 blockSignals(
false );
137 view()->setMinimumWidth( view()->sizeHintForColumn( 0 ) );
147 const double oldScale = mScale;
148 if ( mAllowNull &&
string.trimmed().isEmpty() )
150 mScale = std::numeric_limits< double >::quiet_NaN();
153 if ( !std::isnan( oldScale ) )
161 double newScale =
toDouble(
string, &ok );
162 if ( newScale > mMinScale && newScale != 0 && mMinScale != 0 )
164 newScale = mMinScale;
175 if ( mScale != oldScale )
190 return std::isnan( mScale );
198void QgsScaleComboBox::fixupScale()
200 if ( mAllowNull && currentText().trimmed().isEmpty() )
202 setScale( std::numeric_limits< double >::quiet_NaN() );
206 const QStringList txtList = currentText().split(
':' );
207 const bool userSetScale = txtList.size() != 2;
210 double newScale =
toDouble( currentText(), &ok );
216 if ( userSetScale && newScale < 1.0 && !
qgsDoubleNear( newScale, 0.0 ) )
218 newScale = 1 / newScale;
230 if ( std::isnan(
scale ) )
236 return QStringLiteral(
"0" );
238 else if (
scale <= 1 )
240 return QStringLiteral(
"%1:1" ).arg( QLocale().
toString(
static_cast< int >( std::round( 1.0 /
scale ) ) ) );
244 return QStringLiteral(
"1:%1" ).arg( QLocale().
toString(
static_cast< float >( std::round(
scale ) ),
'f', 0 ) );
264 QStringList txtList = scaleTxt.split(
':' );
265 if ( 2 == txtList.size() )
271 if ( okX && okY && x != 0 )
274 scale =
static_cast< double >( y ) /
static_cast< double >( x );
291 lineEdit()->setClearButtonEnabled(
allowNull );
303 if ( mScale > mMinScale && mScale != 0 && mMinScale != 0 )
312 setScale( std::numeric_limits< double >::quiet_NaN() );
void setPredefinedScales(const QVector< double > &scales)
Sets the list of predefined scales to show in the combobox.
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...
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)